Skip to content

Instantly share code, notes, and snippets.

@emanonwzy
Created December 23, 2014 10:12
Show Gist options
  • Save emanonwzy/8868a960c4be279d47c0 to your computer and use it in GitHub Desktop.
Save emanonwzy/8868a960c4be279d47c0 to your computer and use it in GitHub Desktop.
public class WebImageRequestHandler extends RequestHandler {
private static final String TAG = "WebImageRequestHandler";
private static final String SCHEME_HTTP = "http";
private static final String SCHEME_HTTPS = "https";
private Downloader downloader;
public WebImageRequestHandler(Downloader downloader) {
this.downloader = downloader;
}
@Override
public boolean canHandleRequest(Request data) {
String scheme = data.uri.getScheme();
Log.d(TAG, "scheme:" + scheme);
return (SCHEME_HTTP.equals(scheme) || SCHEME_HTTPS.equals(scheme));
}
@Override
public Result load(Request data) throws IOException {
Downloader.Response response = downloader.load(data.uri, false);
if (response == null) {
return null;
}
InputStream is = response.getInputStream();
if (is == null) {
return null;
}
// Sometimes response content length is zero when requests are being replayed. Haven't found
// root cause to this but retrying the request seems safe to do so.
if (response.getContentLength() == 0) {
closeQuietly(is);
throw new IOException("Received response with 0 content-length header.");
}
try {
saveToWebCaches(is, data.uri);
return null;
} finally {
closeQuietly(is);
}
}
private void saveToWebCaches(InputStream inputStream, Uri uri) {
BufferedOutputStream outputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
//File file = ImageCacheUtils.getFile(MainApplication.getApp(), uri.toString());
File file = getFile(MainApplication.getApp(), uri);
if (BuildConfig.DEBUG) {
Log.d(TAG, "save input stream to caches, uri:" + uri + ", path " + file.getAbsolutePath());
}
byte[] buffer = new byte[4096];
outputStream = new BufferedOutputStream(new FileOutputStream(file));
bufferedInputStream = new BufferedInputStream(inputStream);
int count;
while ((count = bufferedInputStream.read(buffer, 0, 4096)) != -1) {
outputStream.write(buffer, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "exception:" + e);
} finally {
closeQuietly(bufferedInputStream);
closeQuietly(outputStream);
}
}
private void closeQuietly(Closeable is) {
if (is == null) return;
try {
is.close();
} catch (IOException ignored) {
Log.e(TAG, "close exception, " + ignored);
}
}
private File getFile(Context context, Uri uri) {
File cacheDir;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
cacheDir = new File(context.getExternalCacheDir(), ".web_images");
} else {
cacheDir = new File(context.getCacheDir(), ".web_images");
}
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
final String fileName = getFileName(uri);
return new File(cacheDir, fileName);
}
private String getFileName(Uri uri) {
return uri.getLastPathSegment();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment