    001// License: GPL. For details, see LICENSE file.
    002package org.openstreetmap.josm.plugins.streetside.io.export;
    003
    004import java.awt.image.BufferedImage;
    005import java.io.ByteArrayInputStream;
    006import java.io.IOException;
    007import java.util.concurrent.ArrayBlockingQueue;
    008
    009import javax.imageio.ImageIO;
    010
    011import org.openstreetmap.josm.data.cache.CacheEntry;
    012import org.openstreetmap.josm.data.cache.CacheEntryAttributes;
    013import org.openstreetmap.josm.data.cache.ICachedLoaderListener;
    014import org.openstreetmap.josm.plugins.streetside.StreetsideAbstractImage;
    015import org.openstreetmap.josm.plugins.streetside.StreetsideImage;
    016import org.openstreetmap.josm.plugins.streetside.cache.CacheUtils;
    017import org.openstreetmap.josm.plugins.streetside.cache.StreetsideCache;
    018import org.openstreetmap.josm.tools.Logging;
    019
    020/**
    021 * This is the thread that downloads one of the images that are going to be
    022 * exported and writes them in a {@link ArrayBlockingQueue}.
    023 *
    024 * @author nokutu
    025 * @see StreetsideExportManager
    026 * @see StreetsideExportWriterThread
    027 */
    028public class StreetsideExportDownloadThread extends Thread implements
    029    ICachedLoaderListener {
    030
    031  private final ArrayBlockingQueue<BufferedImage> queue;
    032  private final ArrayBlockingQueue<StreetsideAbstractImage> queueImages;
    033
    034  private final StreetsideImage image;
    035
    036  /**
    037   * Main constructor.
    038   *
    039   * @param image
    040   *          Image to be downloaded.
    041   * @param queue
    042   *          Queue of {@link BufferedImage} objects for the
    043   *          {@link StreetsideExportWriterThread}.
    044   * @param queueImages
    045   *          Queue of {@link StreetsideAbstractImage} objects for the
    046   *          {@link StreetsideExportWriterThread}.
    047   */
    048  public StreetsideExportDownloadThread(StreetsideImage image,
    049      ArrayBlockingQueue<BufferedImage> queue,
    050      ArrayBlockingQueue<StreetsideAbstractImage> queueImages) {
    051    this.queue = queue;
    052    this.image = image;
    053    this.queueImages = queueImages;
    054  }
    055
    056  @Override
    057  public void run() {
    058    CacheUtils.submit(this.image.getId(), StreetsideCache.Type.FULL_IMAGE, this);
    059  }
    060
    061  @Override
    062  public synchronized void loadingFinished(CacheEntry data,
    063      CacheEntryAttributes attributes, LoadResult result) {
    064    try {
    065      synchronized (StreetsideExportDownloadThread.class) {
    066        this.queue
    067            .put(ImageIO.read(new ByteArrayInputStream(data.getContent())));
    068        this.queueImages.put(this.image);
    069      }
    070    } catch (InterruptedException | IOException e) {
    071      Logging.error(e);
    072    }
    073  }
    074}