MD5 校验值:1036132062d4d223cd1a714f5bbea98b
CacheUtils.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package im.skmzhmurqt.ui.wallet.utils; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Process; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.io.Serializable; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import org.json.JSONArray; import org.json.JSONObject; public class CacheUtils { private static final int MAX_COUNT = Integer.MAX_VALUE; private static final int MAX_SIZE = 50000000; public static final int TIME_DAY = 86400; public static final int TIME_HOUR = 3600; private static Map<String, CacheUtils> mInstanceMap = new HashMap(); private ACacheManager mCache; public static CacheUtils get(Context ctx) { return get(ctx, "ACache"); } public static CacheUtils get(Context ctx, String cacheName) { File f = new File(ctx.getCacheDir(), cacheName); return get(f, 50000000L, Integer.MAX_VALUE); } public static CacheUtils get(File cacheDir) { return get(cacheDir, 50000000L, Integer.MAX_VALUE); } public static CacheUtils get(Context ctx, long max_zise, int max_count) { File f = new File(ctx.getCacheDir(), "ACache"); return get(f, max_zise, max_count); } public static CacheUtils get(File cacheDir, long max_zise, int max_count) { CacheUtils manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid()); if (manager == null) { CacheUtils manager2 = new CacheUtils(cacheDir, max_zise, max_count); mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager2); return manager2; } return manager; } private static String myPid() { return "_" + Process.myPid(); } private CacheUtils(File cacheDir, long max_size, int max_count) { if (!cacheDir.exists() && !cacheDir.mkdirs()) { throw new RuntimeException("can't make dirs in " + cacheDir.getAbsolutePath()); } this.mCache = new ACacheManager(cacheDir, max_size, max_count); } class xFileOutputStream extends FileOutputStream { File file; public xFileOutputStream(File file) throws FileNotFoundException { super(file); this.file = file; } @Override public void close() throws IOException { super.close(); CacheUtils.this.mCache.put(this.file); } } public void put(String key, String value) { File file = this.mCache.newFile(key); BufferedWriter out = null; try { try { out = new BufferedWriter(new FileWriter(file), 1024); out.write(value); } catch (IOException e) { e.printStackTrace(); if (out != null) { try { out.flush(); out.close(); } catch (IOException e2) { e = e2; e.printStackTrace(); this.mCache.put(file); } } } try { out.flush(); out.close(); } catch (IOException e3) { e = e3; e.printStackTrace(); this.mCache.put(file); } this.mCache.put(file); } catch (Throwable th) { if (out != null) { try { out.flush(); out.close(); } catch (IOException e4) { e4.printStackTrace(); } } this.mCache.put(file); throw th; } } public void put(String key, String value, int saveTime) { put(key, Utils.newStringWithDateInfo(saveTime, value)); } public String getAsString(String key) { File file = this.mCache.get(key); if (!file.exists()) { return null; } BufferedReader in = null; try { try { in = new BufferedReader(new FileReader(file)); String readString = ""; while (true) { String currentLine = in.readLine(); if (currentLine == null) { break; } readString = readString + currentLine; } if (Utils.isDue(readString)) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } if (1 != 0) { remove(key); } return null; } String clearDateInfo = Utils.clearDateInfo(readString); try { in.close(); } catch (IOException e2) { e2.printStackTrace(); } if (0 != 0) { remove(key); } return clearDateInfo; } catch (IOException e3) { e3.printStackTrace(); if (in != null) { try { in.close(); } catch (IOException e4) { e4.printStackTrace(); } } if (0 != 0) { remove(key); } return null; } } finally { } } public void put(String key, JSONObject value) { put(key, value.toString()); } public void put(String key, JSONObject value, int saveTime) { put(key, value.toString(), saveTime); } public JSONObject getAsJSONObject(String key) { String JSONString = getAsString(key); try { JSONObject obj = new JSONObject(JSONString); return obj; } catch (Exception e) { e.printStackTrace(); return null; } } public void put(String key, JSONArray value) { put(key, value.toString()); } public void put(String key, JSONArray value, int saveTime) { put(key, value.toString(), saveTime); } public JSONArray getAsJSONArray(String key) { String JSONString = getAsString(key); try { JSONArray obj = new JSONArray(JSONString); return obj; } catch (Exception e) { e.printStackTrace(); return null; } } public void put(String key, byte[] value) { File file = this.mCache.newFile(key); FileOutputStream out = null; try { try { out = new FileOutputStream(file); out.write(value); try { out.flush(); out.close(); } catch (IOException e) { e = e; e.printStackTrace(); this.mCache.put(file); } } catch (Exception e2) { e2.printStackTrace(); if (out != null) { try { out.flush(); out.close(); } catch (IOException e3) { e = e3; e.printStackTrace(); this.mCache.put(file); } } } this.mCache.put(file); } catch (Throwable th) { if (out != null) { try { out.flush(); out.close(); } catch (IOException e4) { e4.printStackTrace(); } } this.mCache.put(file); throw th; } } public OutputStream put(String key) throws FileNotFoundException { return new xFileOutputStream(this.mCache.newFile(key)); } public InputStream get(String key) throws FileNotFoundException { File file = this.mCache.get(key); if (!file.exists()) { return null; } return new FileInputStream(file); } public void put(String key, byte[] value, int saveTime) { put(key, Utils.newByteArrayWithDateInfo(saveTime, value)); } public byte[] getAsBinary(String key) { RandomAccessFile RAFile = null; try { try { File file = this.mCache.get(key); if (!file.exists()) { if (0 != 0) { try { RAFile.close(); } catch (IOException e) { e.printStackTrace(); } } if (0 != 0) { remove(key); } return null; } RAFile = new RandomAccessFile(file, "r"); byte[] byteArray = new byte[(int) RAFile.length()]; RAFile.read(byteArray); if (Utils.isDue(byteArray)) { try { RAFile.close(); } catch (IOException e2) { e2.printStackTrace(); } if (1 != 0) { remove(key); } return null; } byte[] clearDateInfo = Utils.clearDateInfo(byteArray); try { RAFile.close(); } catch (IOException e3) { e3.printStackTrace(); } if (0 != 0) { remove(key); } return clearDateInfo; } catch (Exception e4) { e4.printStackTrace(); if (RAFile != null) { try { RAFile.close(); } catch (IOException e5) { e5.printStackTrace(); } } if (0 != 0) { remove(key); } return null; } } finally { } } public void put(String key, Serializable value) { put(key, value, -1); } public void put(String key, Serializable value, int saveTime) { ObjectOutputStream oos = null; try { try { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos2 = new ObjectOutputStream(baos); oos2.writeObject(value); byte[] data = baos.toByteArray(); if (saveTime != -1) { put(key, data, saveTime); } else { put(key, data); } oos2.close(); } catch (Throwable th) { try { oos.close(); } catch (IOException e) { } throw th; } } catch (Exception e2) { e2.printStackTrace(); oos.close(); } } catch (IOException e3) { } } public Object getAsObject(String key) { byte[] data = getAsBinary(key); if (data == null) { return null; } ByteArrayInputStream bais = null; ObjectInputStream ois = null; try { try { bais = new ByteArrayInputStream(data); ois = new ObjectInputStream(bais); Object reObject = ois.readObject(); try { bais.close(); } catch (IOException e) { e.printStackTrace(); } try { ois.close(); } catch (IOException e2) { e2.printStackTrace(); } return reObject; } catch (Exception e3) { e3.printStackTrace(); if (bais != null) { try { bais.close(); } catch (IOException e4) { e4.printStackTrace(); } } if (ois != null) { try { ois.close(); } catch (IOException e5) { e5.printStackTrace(); } } return null; } } finally { } } public void put(String key, Bitmap value) { put(key, Utils.Bitmap2Bytes(value)); } public void put(String key, Bitmap value, int saveTime) { put(key, Utils.Bitmap2Bytes(value), saveTime); } public Bitmap getAsBitmap(String key) { if (getAsBinary(key) == null) { return null; } return Utils.Bytes2Bimap(getAsBinary(key)); } public void put(String key, Drawable value) { put(key, Utils.drawable2Bitmap(value)); } public void put(String key, Drawable value, int saveTime) { put(key, Utils.drawable2Bitmap(value), saveTime); } public Drawable getAsDrawable(String key) { if (getAsBinary(key) == null) { return null; } return Utils.bitmap2Drawable(Utils.Bytes2Bimap(getAsBinary(key))); } public File file(String key) { File f = this.mCache.newFile(key); if (f.exists()) { return f; } return null; } public boolean remove(String key) { return this.mCache.remove(key); } public void clear() { this.mCache.clear(); } public class ACacheManager { private final AtomicInteger cacheCount; protected File cacheDir; private final AtomicLong cacheSize; private final int countLimit; private final Map<File, Long> lastUsageDates; private final long sizeLimit; private ACacheManager(File cacheDir, long sizeLimit, int countLimit) { this.lastUsageDates = Collections.synchronizedMap(new HashMap()); this.cacheDir = cacheDir; this.sizeLimit = sizeLimit; this.countLimit = countLimit; this.cacheSize = new AtomicLong(); this.cacheCount = new AtomicInteger(); calculateCacheSizeAndCacheCount(); } private void calculateCacheSizeAndCacheCount() { new Thread(new Runnable() { @Override public void run() { int size = 0; int count = 0; File[] cachedFiles = ACacheManager.this.cacheDir.listFiles(); if (cachedFiles != null) { for (File cachedFile : cachedFiles) { size = (int) (size + ACacheManager.this.calculateSize(cachedFile)); count++; ACacheManager.this.lastUsageDates.put(cachedFile, Long.valueOf(cachedFile.lastModified())); } ACacheManager.this.cacheSize.set(size); ACacheManager.this.cacheCount.set(count); } } }).start(); } public void put(File file) { int curCacheCount = this.cacheCount.get(); while (curCacheCount + 1 > this.countLimit) { long freedSize = removeNext(); this.cacheSize.addAndGet(-freedSize); curCacheCount = this.cacheCount.addAndGet(-1); } this.cacheCount.addAndGet(1); long valueSize = calculateSize(file); long curCacheSize = this.cacheSize.get(); while (curCacheSize + valueSize > this.sizeLimit) { long freedSize2 = removeNext(); curCacheSize = this.cacheSize.addAndGet(-freedSize2); } this.cacheSize.addAndGet(valueSize); Long currentTime = Long.valueOf(System.currentTimeMillis()); file.setLastModified(currentTime.longValue()); this.lastUsageDates.put(file, currentTime); } public File get(String key) { File file = newFile(key); Long currentTime = Long.valueOf(System.currentTimeMillis()); file.setLastModified(currentTime.longValue()); this.lastUsageDates.put(file, currentTime); return file; } public File newFile(String key) { return new File(this.cacheDir, key.hashCode() + ""); } public boolean remove(String key) { File image = get(key); return image.delete(); } public void clear() { this.lastUsageDates.clear(); this.cacheSize.set(0L); File[] files = this.cacheDir.listFiles(); if (files != null) { for (File f : files) { f.delete(); } } } private long removeNext() { if (this.lastUsageDates.isEmpty()) { return 0L; } Long oldestUsage = null; File mostLongUsedFile = null; Set<Map.Entry<File, Long>> entries = this.lastUsageDates.entrySet(); synchronized (this.lastUsageDates) { for (Map.Entry<File, Long> entry : entries) { if (mostLongUsedFile == null) { mostLongUsedFile = entry.getKey(); oldestUsage = entry.getValue(); } else { Long lastValueUsage = entry.getValue(); if (lastValueUsage.longValue() < oldestUsage.longValue()) { oldestUsage = lastValueUsage; mostLongUsedFile = entry.getKey(); } } } } long fileSize = calculateSize(mostLongUsedFile); if (mostLongUsedFile.delete()) { this.lastUsageDates.remove(mostLongUsedFile); } return fileSize; } public long calculateSize(File file) { return file.length(); } } public static class Utils { private static final char mSeparator = ' '; private Utils() { } public static boolean isDue(String str) { return isDue(str.getBytes()); } public static boolean isDue(byte[] data) { String[] strs = getDateInfoFromDate(data); if (strs != null && strs.length == 2) { String saveTimeStr = strs[0]; while (saveTimeStr.startsWith("0")) { saveTimeStr = saveTimeStr.substring(1, saveTimeStr.length()); } long saveTime = Long.valueOf(saveTimeStr).longValue(); long deleteAfter = Long.valueOf(strs[1]).longValue(); if (System.currentTimeMillis() > (1000 * deleteAfter) + saveTime) { return true; } } return false; } public static String newStringWithDateInfo(int second, String strInfo) { return createDateInfo(second) + strInfo; } public static byte[] newByteArrayWithDateInfo(int second, byte[] data2) { byte[] data1 = createDateInfo(second).getBytes(); byte[] retdata = new byte[data1.length + data2.length]; System.arraycopy(data1, 0, retdata, 0, data1.length); System.arraycopy(data2, 0, retdata, data1.length, data2.length); return retdata; } public static String clearDateInfo(String strInfo) { if (strInfo != null && hasDateInfo(strInfo.getBytes())) { return strInfo.substring(strInfo.indexOf(32) + 1, strInfo.length()); } return strInfo; } public static byte[] clearDateInfo(byte[] data) { if (hasDateInfo(data)) { return copyOfRange(data, indexOf(data, mSeparator) + 1, data.length); } return data; } private static boolean hasDateInfo(byte[] data) { return data != null && data.length > 15 && data[13] == 45 && indexOf(data, mSeparator) > 14; } private static String[] getDateInfoFromDate(byte[] data) { if (hasDateInfo(data)) { String saveDate = new String(copyOfRange(data, 0, 13)); String deleteAfter = new String(copyOfRange(data, 14, indexOf(data, mSeparator))); return new String[]{saveDate, deleteAfter}; } return null; } private static int indexOf(byte[] data, char c) { for (int i = 0; i < data.length; i++) { if (data[i] == c) { return i; } } return -1; } private static byte[] copyOfRange(byte[] original, int from, int to) { int newLength = to - from; if (newLength < 0) { throw new IllegalArgumentException(from + " > " + to); } byte[] copy = new byte[newLength]; System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; } private static String createDateInfo(int second) { String currentTime = System.currentTimeMillis() + ""; while (currentTime.length() < 13) { currentTime = "0" + currentTime; } return currentTime + "-" + second + mSeparator; } public static byte[] Bitmap2Bytes(Bitmap bm) { if (bm == null) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } public static Bitmap Bytes2Bimap(byte[] b) { if (b.length == 0) { return null; } return BitmapFactory.decodeByteArray(b, 0, b.length); } public static Bitmap drawable2Bitmap(Drawable drawable) { if (drawable == null) { return null; } int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != -1 ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; Bitmap bitmap = Bitmap.createBitmap(w, h, config); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); drawable.draw(canvas); return bitmap; } public static Drawable bitmap2Drawable(Bitmap bm) { if (bm == null) { return null; } BitmapDrawable bd = new BitmapDrawable(bm); bd.setTargetDensity(bm.getDensity()); return new BitmapDrawable(bm); } } }