Mega Code Archive

 
Categories / Android / Network
 

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;  }