Skip to content

Commit

Permalink
Updated DiskLruCache to jw/1.2.0, closes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
DHuckaby committed Oct 2, 2012
1 parent 7eeb3bd commit 1d36c22
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 113 deletions.
22 changes: 22 additions & 0 deletions library/src/com/handlerexploit/common/utils/Arrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.handlerexploit.common.utils;

import java.lang.reflect.Array;

/* From java.util.Arrays */
class Arrays {
@SuppressWarnings("unchecked")
public static <T> T[] copyOfRange(T[] original, int start, int end) {
int originalLength = original.length; // For exception priority compatibility.
if (start > end) {
throw new IllegalArgumentException();
}
if (start < 0 || start > originalLength) {
throw new ArrayIndexOutOfBoundsException();
}
int resultLength = end - start;
int copyLength = Math.min(resultLength, originalLength - start);
T[] result = (T[]) Array.newInstance(original.getClass().getComponentType(), resultLength);
System.arraycopy(original, start, result, 0, copyLength);
return result;
}
}
9 changes: 9 additions & 0 deletions library/src/com/handlerexploit/common/utils/Charsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.handlerexploit.common.utils;

import java.nio.charset.Charset;

/** From java.nio.charset.Charsets */
class Charsets {
static final Charset US_ASCII = Charset.forName("US-ASCII");
static final Charset UTF_8 = Charset.forName("UTF-8");
}
104 changes: 24 additions & 80 deletions library/src/com/handlerexploit/common/utils/DiskLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -92,6 +87,7 @@
* responding appropriately.
*
* @hide
* @version jw/1.2.0
*/
public final class DiskLruCache implements Closeable {
static final String JOURNAL_FILE = "journal";
Expand All @@ -105,69 +101,6 @@ public final class DiskLruCache implements Closeable {
private static final String REMOVE = "REMOVE";
private static final String READ = "READ";

/* XXX From java.util.Arrays */
@SuppressWarnings("unchecked")
private static <T> T[] copyOfRange(T[] original, int start, int end) {
int originalLength = original.length; // For exception priority compatibility.
if (start > end) {
throw new IllegalArgumentException();
}
if (start < 0 || start > originalLength) {
throw new ArrayIndexOutOfBoundsException();
}
int resultLength = end - start;
int copyLength = Math.min(resultLength, originalLength - start);
T[] result = (T[]) Array.newInstance(original.getClass().getComponentType(), resultLength);
System.arraycopy(original, start, result, 0, copyLength);
return result;
}

/* XXX From java.nio.charset.Charsets */
private static final Charset UTF_8 = Charset.forName("UTF-8");

/* XXX From libcore.io.IoUtils */
private static void deleteContents(File dir) throws IOException {
File[] files = dir.listFiles();
if (files == null) {
throw new IllegalArgumentException("not a directory: " + dir);
}
for (File file : files) {
if (file.isDirectory()) {
deleteContents(file);
}
if (!file.delete()) {
throw new IOException("failed to delete file: " + file);
}
}
}

/* XXX From libcore.io.IoUtils */
private static void closeQuietly(/*Auto*/Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}

/* XXX From libcore.io.Streams */
private static String readFully(Reader reader) throws IOException {
try {
StringWriter writer = new StringWriter();
char[] buffer = new char[1024];
int count;
while ((count = reader.read(buffer)) != -1) {
writer.write(buffer, 0, count);
}
return writer.toString();
} finally {
reader.close();
}
}

/*
* This cache uses a journal file named "journal". A typical journal file
* looks like this:
Expand Down Expand Up @@ -297,7 +230,8 @@ public static DiskLruCache open(File directory, int appVersion, int valueCount,
}

private void readJournal() throws IOException {
StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile), StrictLineReader.US_ASCII);
StrictLineReader reader = new StrictLineReader(new FileInputStream(journalFile),
Charsets.US_ASCII);
try {
String magic = reader.readLine();
String version = reader.readLine();
Expand All @@ -321,7 +255,7 @@ private void readJournal() throws IOException {
}
}
} finally {
/*IoUtils.*/closeQuietly(reader);
IoUtils.closeQuietly(reader);
}
}

Expand All @@ -346,7 +280,7 @@ private void readJournalLine(String line) throws IOException {
if (parts[0].equals(CLEAN) && parts.length == 2 + valueCount) {
entry.readable = true;
entry.currentEditor = null;
entry.setLengths(/*Arrays.*/copyOfRange(parts, 2, parts.length));
entry.setLengths(Arrays.copyOfRange(parts, 2, parts.length));
} else if (parts[0].equals(DIRTY) && parts.length == 2) {
entry.currentEditor = new Editor(entry);
} else if (parts[0].equals(READ) && parts.length == 2) {
Expand Down Expand Up @@ -682,7 +616,7 @@ private void trimToSize() throws IOException {
*/
public void delete() throws IOException {
close();
/*IoUtils.*/deleteContents(directory);
IoUtils.deleteContents(directory);
}

private void validateKey(String key) {
Expand All @@ -694,7 +628,7 @@ private void validateKey(String key) {
}

private static String inputStreamToString(InputStream in) throws IOException {
return /*Streams.*/readFully(new InputStreamReader(in, /*Charsets.*/UTF_8));
return Streams.readFully(new InputStreamReader(in, Charsets.UTF_8));
}

/**
Expand Down Expand Up @@ -736,7 +670,7 @@ public String getString(int index) throws IOException {

public void close() {
for (InputStream in : ins) {
/*IoUtils.*/closeQuietly(in);
IoUtils.closeQuietly(in);
}
}
}
Expand Down Expand Up @@ -806,11 +740,21 @@ public OutputStream newOutputStream(int index) throws IOException {
if (!entry.readable) {
written[index] = true;
}
File dirtyFile = entry.getDirtyFile(index);
FileOutputStream outputStream;
try {
return new FaultHidingOutputStream(new FileOutputStream(entry.getDirtyFile(index)));
outputStream = new FileOutputStream(dirtyFile);
} catch (FileNotFoundException e) {
return NULL_OUTPUT_STREAM;
// Attempt to recreate the cache directory.
directory.mkdirs();
try {
outputStream = new FileOutputStream(dirtyFile);
} catch (FileNotFoundException e2) {
// We are unable to recover. Silently eat the writes.
return NULL_OUTPUT_STREAM;
}
}
return new FaultHidingOutputStream(outputStream);
}
}

Expand All @@ -820,10 +764,10 @@ public OutputStream newOutputStream(int index) throws IOException {
public void set(int index, String value) throws IOException {
Writer writer = null;
try {
writer = new OutputStreamWriter(newOutputStream(index), /*Charsets.*/UTF_8);
writer = new OutputStreamWriter(newOutputStream(index), Charsets.UTF_8);
writer.write(value);
} finally {
/*IoUtils.*/closeQuietly(writer);
IoUtils.closeQuietly(writer);
}
}

Expand Down Expand Up @@ -943,7 +887,7 @@ private void setLengths(String[] strings) throws IOException {
}

private IOException invalidLengths(String[] strings) throws IOException {
throw new IOException("unexpected journal line: " + Arrays.toString(strings));
throw new IOException("unexpected journal line: " + java.util.Arrays.toString(strings));
}

public File getCleanFile(int i) {
Expand All @@ -954,4 +898,4 @@ public File getDirtyFile(int i) {
return new File(directory, key + "." + i + ".tmp");
}
}
}
}
34 changes: 34 additions & 0 deletions library/src/com/handlerexploit/common/utils/IOUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.handlerexploit.common.utils;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;

/** From libcore.io.IoUtils */
class IoUtils {
static void deleteContents(File dir) throws IOException {
File[] files = dir.listFiles();
if (files == null) {
throw new IllegalArgumentException("not a directory: " + dir);
}
for (File file : files) {
if (file.isDirectory()) {
deleteContents(file);
}
if (!file.delete()) {
throw new IOException("failed to delete file: " + file);
}
}
}

static void closeQuietly(/*Auto*/Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
}
22 changes: 22 additions & 0 deletions library/src/com/handlerexploit/common/utils/Streams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.handlerexploit.common.utils;

import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;

/** From libcore.io.Streams */
class Streams {
static String readFully(Reader reader) throws IOException {
try {
StringWriter writer = new StringWriter();
char[] buffer = new char[1024];
int count;
while ((count = reader.read(buffer)) != -1) {
writer.write(buffer, 0, count);
}
return writer.toString();
} finally {
reader.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@
* The default charset is US_ASCII.
*/
class StrictLineReader implements Closeable {
static final Charset US_ASCII = Charset.forName("US-ASCII");
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
private static final byte CR = (byte)'\r';
private static final byte LF = (byte)'\n';

Expand Down Expand Up @@ -115,8 +112,7 @@ public StrictLineReader(InputStream in, int capacity, Charset charset) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity <= 0");
}
if (!(charset.equals(US_ASCII) || charset.equals(UTF_8) ||
charset.equals(ISO_8859_1))) {
if (!(charset.equals(Charsets.US_ASCII))) {
throw new IllegalArgumentException("Unsupported encoding");
}

Expand Down Expand Up @@ -164,7 +160,7 @@ public String readLine() throws IOException {
for (int i = pos; i != end; ++i) {
if (buf[i] == LF) {
int lineEnd = (i != pos && buf[i - 1] == CR) ? i - 1 : i;
String res = new String(buf, pos, lineEnd - pos, charset.displayName());
String res = new String(buf, pos, lineEnd - pos, charset.name());
pos = i + 1;
return res;
}
Expand All @@ -176,10 +172,9 @@ public String readLine() throws IOException {
public String toString() {
int length = (count > 0 && buf[count - 1] == CR) ? count - 1 : count;
try {
return new String(buf, 0, length, charset.displayName());
return new String(buf, 0, length, charset.name());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
throw new AssertionError(e); // Since we control the charset this will never happen.
}
}
};
Expand Down Expand Up @@ -245,3 +240,4 @@ private void fillBuf() throws IOException {
end = result;
}
}

31 changes: 7 additions & 24 deletions library/src/com/handlerexploit/prime/utils/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,20 @@ public final class ImageManager {

private ImageManager(Context context) {
mCacheDirectory = getCacheDirectory(context).getAbsolutePath();
mDiskLruCache = open(mCacheDirectory);
}

public static synchronized ImageManager getInstance(Context context) {
if (sInstance == null) {
sInstance = new ImageManager(context);
}
return sInstance;
}

private static DiskLruCache open(String cacheDirectory) {
try {
return DiskLruCache.open(getImageCacheDirectory(cacheDirectory), 1, 1, Configuration.DISK_CACHE_SIZE_KB * 1024);
File directory = new File(mCacheDirectory, "/images/");
mDiskLruCache = DiskLruCache.open(directory, 1, 1, Configuration.DISK_CACHE_SIZE_KB * 1024);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
throw new RuntimeException(e);
}
}

private static File getImageCacheDirectory(String cacheDirectory) {
return new File(cacheDirectory, "/images/");
public static synchronized ImageManager getInstance(Context context) {
if (sInstance == null) {
sInstance = new ImageManager(context);
}
return sInstance;
}

public void setPreferredConfig(Bitmap.Config preferredConfig) {
Expand Down Expand Up @@ -384,16 +377,6 @@ private static boolean copyUrlToDiskLruCache(String key, String source) {
OutputStream outputStream = null;
try {
lock.lock();
synchronized (sInstance) {
if (!getImageCacheDirectory(sInstance.mCacheDirectory).exists()) {
/*
* We are in an unexpected state, our cache directory was
* destroyed without our static instance being destroyed
* also. The best thing we can do here is start over.
*/
sInstance.mDiskLruCache = open(sInstance.mCacheDirectory);
}
}

/*
* We block here because Editor.edit will return null if another
Expand Down

0 comments on commit 1d36c22

Please sign in to comment.