Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
Flesh out new put and get methods. Still needs work
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Banes committed Dec 16, 2012
1 parent a5c4475 commit 683b32d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
77 changes: 74 additions & 3 deletions library/src/uk/co/senab/bitmapcache/BitmapLruCache.java
@@ -1,9 +1,17 @@
package uk.co.senab.bitmapcache;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.jakewharton.DiskLruCache;
import com.jakewharton.DiskLruCache.Editor;

public class BitmapLruCache {

Expand All @@ -26,10 +34,63 @@ public CacheableBitmapWrapper get(String url) {
return null;
}

public void put(CacheableBitmapWrapper value) {
public void put(String url, InputStream inputStream) {
// First we need to save the stream contents to a temporary file, so it
// can be read multiple times
File tmpFile = null;
try {
tmpFile = File.createTempFile("bitmapcache_", null);

// Pipe InputStream to file
// TODO Add Buffered Layer
Util.pipe(inputStream, new FileOutputStream(tmpFile));

// Close the original InputStream
try {
inputStream.close();
} finally {
}
} catch (IOException e) {
e.printStackTrace();
}

if (null != tmpFile) {
if (null != mDiskCache) {
try {
Editor editor = mDiskCache.edit(url);
if (null != editor) {
Util.pipe(inputStream, editor.newOutputStream(0));
editor.commit();
}
} catch (IOException e) {
e.printStackTrace();
}
}

if (null != mMemoryCache) {
putIntoMemoryCache(url, BitmapFactory.decodeFile(tmpFile.getAbsolutePath()));
}

// Finally, delete the temporary file
tmpFile.delete();
}
}

public void put(String url, Bitmap bitmap) {
if (null != mDiskCache) {
try {
Editor editor = mDiskCache.edit(url);
if (null != editor) {
Util.saveBitmap(bitmap, editor.newOutputStream(0));
editor.commit();
}
} catch (IOException e) {
e.printStackTrace();
}
}

if (null != mMemoryCache) {
value.setCached(true);
mMemoryCache.put(value.getUrl(), value);
putIntoMemoryCache(url, bitmap);
}
}

Expand All @@ -45,6 +106,16 @@ public void trimMemory() {
}
}

private void putIntoMemoryCache(String url, Bitmap bitmap) {
if (null != bitmap) {
CacheableBitmapWrapper wrapper = new CacheableBitmapWrapper(url, bitmap);
wrapper.setCached(true);
mMemoryCache.put(url, wrapper);
} else {
// TODO Add log here
}
}

public static class Builder {

static final float DEFAULT_CACHE_SIZE = 1f / 8f;
Expand Down
33 changes: 33 additions & 0 deletions library/src/uk/co/senab/bitmapcache/Util.java
@@ -0,0 +1,33 @@
package uk.co.senab.bitmapcache;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

public class Util {

/**
* Pipe an InputStream to the given OutputStream
*
* @param in
* @param out
* @throws IOException
*/
// TODO Can probably be optimized
public static void pipe(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
}
}

public static void saveBitmap(Bitmap bitmap, OutputStream out) {
bitmap.compress(CompressFormat.PNG, 100, out);
}

}

0 comments on commit 683b32d

Please sign in to comment.