Android Open Source - SIC Image Downloader






From Project

Back to project page SIC.

License

The source code is released under:

MIT License

If you think the Android project SIC listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.sun.imageloader.downloader.impl;
//from www.  ja v  a  2 s  .  c  o m
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;


import android.net.Uri;

import com.sun.imageloader.downloader.api.ImageRetriever;

public class ImageDownloader implements ImageRetriever {

  private final int _maxRedirectCount;
  private final int _maxTimeOut;
  private final int _maxReadTimeOut;
  private final static int BUFFER_SIZE = 16 * 1024;

  
  public ImageDownloader(int maxRedirectCount_, int maxTimeOut_, int maxReadTimeOut_){
    _maxRedirectCount = maxRedirectCount_;
      _maxTimeOut =  maxTimeOut_ ;
      _maxReadTimeOut = maxReadTimeOut_;
  }

  
  @Override
  public InputStream getStream(URI imageUr_) throws URISyntaxException, IOException {
  
    return getImageFromNetwork(imageUr_);
  }

  private InputStream getImageFromNetwork(URI imageUri_) throws URISyntaxException, IOException {
    HttpURLConnection imageUrlConn = null;
    try {
      imageUrlConn = openConnection(imageUri_);
      int redirectionCount= 0;
      while(redirectionCount < _maxRedirectCount && imageUrlConn.getResponseCode() / 100 == 3 ){
        imageUrlConn = openConnection(new URI (imageUrlConn.getHeaderField("Location")));
              redirectionCount++;
      }  
      imageUrlConn.connect();

    } catch (IOException e) {
      if (imageUrlConn != null) {
        consumeErrorStream(imageUrlConn);
      }
    }  
    return new BufferedInputStream(imageUrlConn.getInputStream(), BUFFER_SIZE);

  }
  
  /**
   * Opens the connection to the {@link URI} specified 
   * 
   * @param imageUri_
   *       the {@link Uri} of the image to load
   * @return
   *       {@link HttpURLConnection} instance created after openin the conection
   * @throws IOException
   * @throws URISyntaxException
   */
  protected HttpURLConnection openConnection(URI imageUri_) throws IOException, URISyntaxException{
    
    HttpURLConnection imageUrlConn = (HttpURLConnection) imageUri_.toURL().openConnection();
    imageUrlConn.setConnectTimeout(_maxTimeOut);
    imageUrlConn.setReadTimeout(_maxReadTimeOut);
    return imageUrlConn;
        
  }
  
  private void consumeErrorStream(HttpURLConnection connection_) throws IOException{
    
    try {
      InputStream errorStream = new BufferedInputStream(connection_.getInputStream(), BUFFER_SIZE);
      errorStream.close();
    } catch (IOException ex) {
      throw new IOException(ex);
    }
  }
  

}




Java Source Code List

com.sun.imageloader.cache.api.MemoryCache.java
com.sun.imageloader.cache.impl.DiskCache.java
com.sun.imageloader.cache.impl.ImageFileFilter.java
com.sun.imageloader.cache.impl.LRUCache.java
com.sun.imageloader.cache.impl.SoftCache.java
com.sun.imageloader.computable.impl.ComputableImage.java
com.sun.imageloader.computable.impl.Computable.java
com.sun.imageloader.concurrent.ComputableCallable.java
com.sun.imageloader.concurrent.DisplayImageTask.java
com.sun.imageloader.concurrent.ImageLoaderTask.java
com.sun.imageloader.core.FlingLock.java
com.sun.imageloader.core.ImageKey.java
com.sun.imageloader.core.ImagePreferences.java
com.sun.imageloader.core.ImageSettings.java
com.sun.imageloader.core.ImageWriter.java
com.sun.imageloader.core.SimpleImageListenerImpl.java
com.sun.imageloader.core.UrlImageLoaderConfiguration.java
com.sun.imageloader.core.UrlImageLoader.java
com.sun.imageloader.core.UrlImageTaskExecutor.java
com.sun.imageloader.core.api.FailedTaskReason.java
com.sun.imageloader.core.api.ImageFailListenter.java
com.sun.imageloader.core.api.ImageTaskListener.java
com.sun.imageloader.core.api.Settings.java
com.sun.imageloader.downloader.api.ImageRetriever.java
com.sun.imageloader.downloader.impl.ImageDownloader.java
com.sun.imageloader.downloader.impl.ImageRetrieverFactory.java
com.sun.imageloader.downloader.impl.Scheme.java
com.sun.imageloader.imagedecoder.api.ImageDecoder.java
com.sun.imageloader.imagedecoder.impl.SimpleImageDecoder.java
com.sun.imageloader.memorizer.api.AMemorizer.java
com.sun.imageloader.memorizer.api.BitmapMemorizer.java
com.sun.imageloader.memorizer.api.IMemorizer.java
com.sun.imageloader.memorizer.api.InterruptedImageLoadException.java
com.sun.imageloader.utils.KeyUtils.java
com.sun.imageloader.utils.L.java
com.sun.imageloader.utils.ViewUtils.java