Cacheable Downloader : Download « Network « Android






Cacheable Downloader

  
//package com.jeffcai.joyreader.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLConnection;
import java.util.HashMap;

public class CacheableDownloader {
  
  private CacheableDownloader() {
    cache = new HashMap<String, byte[]>();
    HttpURLConnection.setFollowRedirects(true);
  }
  
  public String getContentAsString(String url) throws IOException {

    byte[] content = null;

    if (cache.containsKey(url)) {
      content = cache.get(url);
    } else {
      content = downloadContent(url);
      cache.put(url, content);
    }

    try {
      // TODO: Always UTF8 Now
      return new String(content, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return "";
  }
  
  public byte[] getContentAsByteArray(String url) throws IOException {

    byte[] content = null;

    if (cache.containsKey(url)) {
      content = cache.get(url);
    } else {
      content = downloadContent(url);
      cache.put(url, content);
    }

    return content;
  }

  public static CacheableDownloader getInstacne() {
    if (instance == null) {
      instance = new CacheableDownloader();
    }
    
    return instance;
  }
  
  private byte [] downloadContent(String url) throws IOException {    
    URI u = null;
    
    try {
      u = new URI(url);
    } catch (URISyntaxException e) {
    }
    
    //Proxy proxy = new Proxy(Type.SOCKS, new InetSocketAddress("137.117.15.37",8080));    
    URLConnection conn = u.toURL().openConnection();
    //conn.connect();
    
    InputStream is = (InputStream) conn.getContent();
    
    byte[] buf = new byte[1024];
    
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    
    int cnt = 0;
    while ((cnt = is.read(buf)) != -1) {
      os.write(buf,0,cnt);
    }
    
    return os.toByteArray();
  }
  
  private static CacheableDownloader instance;
  
  private HashMap<String, byte []> cache; 
}

   
    
  








Related examples in the same category

1.Android download file tools
2.Download Manager Demo
3.Using AsyncTask to download a big file
4.Using DownloadManager
5.Download Task
6.File Download Service
7.Download Manager
8.Book Downloader
9.download File