Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing some trivial issues. #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/github/droidfu/cachefu/AbstractCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public abstract class AbstractCache<KeyT, ValT> implements Map<KeyT, ValT> {

public static final int DISK_CACHE_INTERNAL = 0;
public static final int DISK_CACHE_SDCARD = 1;


private static final String LOG_TAG = "Droid-Fu[CacheFu]";

Expand All @@ -69,6 +70,8 @@ public abstract class AbstractCache<KeyT, ValT> implements Map<KeyT, ValT> {
private String name;

private long expirationInMinutes;

public static final int DEFAULT_BUFFER_SIZE = 8192;

/**
* Creates a new cache instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getFileNameForKey(String url) {

@Override
protected ResponseData readValueFromDisk(File file) throws IOException {
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file),AbstractCache.DEFAULT_BUFFER_SIZE);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
Expand All @@ -55,7 +55,7 @@ protected ResponseData readValueFromDisk(File file) throws IOException {

@Override
protected void writeValueToDisk(File file, ResponseData data) throws IOException {
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file),AbstractCache.DEFAULT_BUFFER_SIZE);

ostream.write(data.getStatusCode());
ostream.write(data.getResponseBody());
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/github/droidfu/cachefu/ImageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
*/
public class ImageCache extends AbstractCache<String, byte[]> {


public ImageCache(int initialCapacity, long expirationInMinutes, int maxConcurrentThreads) {
super("ImageCache", initialCapacity, expirationInMinutes, maxConcurrentThreads);
Expand All @@ -49,7 +50,7 @@ public String getFileNameForKey(String imageUrl) {

@Override
protected byte[] readValueFromDisk(File file) throws IOException {
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file));
BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file),AbstractCache.DEFAULT_BUFFER_SIZE);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes");
Expand All @@ -74,7 +75,7 @@ public synchronized Bitmap getBitmap(Object elementKey) {

@Override
protected void writeValueToDisk(File file, byte[] imageData) throws IOException {
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));
BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file),AbstractCache.DEFAULT_BUFFER_SIZE);

ostream.write(imageData);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/droidfu/cachefu/ModelCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected CachedModel readValueFromDisk(File file) throws IOException {

// Read file into byte array
byte[] dataWritten = new byte[(int) file.length()];
BufferedInputStream bistream = new BufferedInputStream(istream);
BufferedInputStream bistream = new BufferedInputStream(istream,AbstractCache.DEFAULT_BUFFER_SIZE);
bistream.read(dataWritten);
bistream.close();

Expand Down Expand Up @@ -105,7 +105,7 @@ protected void writeValueToDisk(File file, CachedModel data) throws IOException

// Write byte data to file
FileOutputStream ostream = new FileOutputStream(file);
BufferedOutputStream bistream = new BufferedOutputStream(ostream);
BufferedOutputStream bistream = new BufferedOutputStream(ostream,AbstractCache.DEFAULT_BUFFER_SIZE);
bistream.write(parcelOut.marshall());
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/github/droidfu/imageloader/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public static synchronized void initialize(Context context) {
imageCache.enableDiskCache(context, ImageCache.DISK_CACHE_SDCARD);
}
}

public static synchronized void initialize(Context context, boolean enableSDCardCache) {
if (executor == null) {
executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(DEFAULT_POOL_SIZE);
}
if (imageCache == null) {
imageCache = new ImageCache(25, expirationInMinutes, DEFAULT_POOL_SIZE);
imageCache.enableDiskCache(context, (enableSDCardCache) ? ImageCache.DISK_CACHE_SDCARD:ImageCache.DISK_CACHE_INTERNAL);
}
}

public static synchronized void initialize(Context context, long expirationInMinutes) {
ImageLoader.expirationInMinutes = expirationInMinutes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public Class<?> loadFinalizer() {
ClassLoader finalizerLoader = newLoader(getBaseUrl());
return finalizerLoader.loadClass(FINALIZER_CLASS_NAME);
} catch (Exception e) {
logger.log(Level.WARNING, LOADING_ERROR, e);
//logger.log(Level.WARNING, LOADING_ERROR, e);
return null;
}
}
Expand Down