Copyright (c) 2013 Adcash OU.
All rights reserved under Creative Commons Attribution 3.0 Unported
http://creativecommons.org/licenses/by/3.0/
Redistribution and use in source and binary forms, with or...
If you think the Android project android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.adcash.mobileads;
//fromwww.java2s.comimport android.content.Context;
import android.net.Uri;
import android.support.v4.util.LruCache;
import android.util.Log;
import com.adcash.mobileads.util.Files;
import com.adcash.mobileads.util.Streams;
import java.io.*;
/*
* Please use putStream, getUri, and removeStream (instead of put, get, and remove).
* The original methods do not perform necessary hashing of fileNames
*/publicclass DiskLruCache extends LruCache<String, File> {
privatefinal Context mContext;
privatefinal String mCacheDirectoryName;
privatefinalFile mCacheDirectory;
public DiskLruCache(Context context, String cacheDirectoryName, int maxSizeBytes) throws IllegalArgumentException, IOException {
super(maxSizeBytes);
if (context == null) {
thrownew IllegalArgumentException("context may not be null.");
} elseif (cacheDirectoryName == null) {
thrownew IllegalArgumentException("cacheDirectoryName may not be null.");
} elseif (maxSizeBytes < 0) {
thrownew IllegalArgumentException("maxSizeBytes must be positive.");
}
mContext = context;
mCacheDirectoryName = cacheDirectoryName;
mCacheDirectory = Files.createDirectory(context.getFilesDir() + File.separator + mCacheDirectoryName);
if (mCacheDirectory == null) {
thrownew IOException("Unable to obtain access to directory " + mCacheDirectoryName);
}
loadFilesFromDisk();
}
File getCacheDirectory() {
return mCacheDirectory;
}
Uri getUri(final String key) {
File value = get(Utils.sha1(key));
if (value == null) {
return null;
}
return Uri.parse(value.getAbsolutePath());
}
synchronizedboolean putStream(final String fileName, final InputStream content) {
if (fileName == null || content == null) {
return false;
}
String hashedFileName = Utils.sha1(fileName);
if (getUri(hashedFileName) != null) {
return false;
}
File file = createFile(hashedFileName, content);
if (file == null || !file.exists()) {
return false;
}
put(hashedFileName, file);
return true;
}
synchronizedFile removeStream(final String fileName) {
if (fileName == null) {
return null;
}
return remove(Utils.sha1(fileName));
}
privateFile createFile(String fileName, InputStream content) {
File file = newFile(mContext.getFilesDir() + File.separator + mCacheDirectoryName + File.separator + fileName);
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
return null;
}
try {
Streams.copyContent(content, fileOutputStream);
} catch (IOException e) {
file.delete();
return null;
} finally {
Streams.closeStream(fileOutputStream);
}
return file;
}
privatevoid loadFilesFromDisk() {
File[] allFiles = mCacheDirectory.listFiles();
if (allFiles != null) {
for (finalFile file : allFiles) {
put(file.getName(), file);
}
}
}
/*
* From android.support.v4.util.LruCache
*/
@Override
protectedvoid entryRemoved(finalboolean evicted, final String key, finalFile oldValue, finalFile newValue) {
super.entryRemoved(evicted, key, oldValue, newValue);
if (oldValue != null) {
if (!oldValue.delete()) {
Log.d("Adcash", "Unable to delete file from cache: " + oldValue.getName());
}
}
}
@Override
protectedint sizeOf(String key, File value) {
if (value != null && value.exists() && value.length() > 0) {
return Files.intLength(value);
}
return super.sizeOf(key, value);
}
}