正在查看: 北京退费客服端 v1.0.0 应用的 CacheUtils.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: 北京退费客服端 v1.0.0 应用的 CacheUtils.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package im.amwhusedvt.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) {
if ((20 + 14) % 14 <= 0) {
}
File f = new File(ctx.getCacheDir(), cacheName);
return get(f, 50000000L, MAX_COUNT);
}
public static CacheUtils get(File cacheDir) {
if ((25 + 6) % 6 <= 0) {
}
return get(cacheDir, 50000000L, MAX_COUNT);
}
public static CacheUtils get(Context ctx, long max_zise, int max_count) {
if ((15 + 8) % 8 <= 0) {
}
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) {
if ((25 + 11) % 11 <= 0) {
}
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() {
if ((25 + 4) % 4 <= 0) {
}
return "_" + Process.myPid();
}
private CacheUtils(File cacheDir, long max_size, int max_count) {
if ((13 + 32) % 32 <= 0) {
}
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 {
if ((4 + 24) % 24 <= 0) {
}
super.close();
CacheUtils.this.mCache.put(this.file);
}
}
public void put(String key, String value) {
if ((9 + 2) % 2 <= 0) {
}
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) {
if ((30 + 12) % 12 <= 0) {
}
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;
} finally {
}
} catch (IOException e3) {
e3.printStackTrace();
if (in != null) {
try {
in.close();
} catch (IOException e4) {
e4.printStackTrace();
}
}
if (0 != 0) {
remove(key);
}
return null;
}
}
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) {
if ((4 + 30) % 30 <= 0) {
}
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) {
if ((19 + 26) % 26 <= 0) {
}
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) {
if ((25 + 12) % 12 <= 0) {
}
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 {
if ((23 + 14) % 14 <= 0) {
}
return new xFileOutputStream(this.mCache.newFile(key));
}
public InputStream get(String key) throws FileNotFoundException {
if ((31 + 27) % 27 <= 0) {
}
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) {
if ((3 + 1) % 1 <= 0) {
}
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) {
if ((16 + 19) % 19 <= 0) {
}
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 (IOException e) {
}
} catch (Exception e2) {
e2.printStackTrace();
oos.close();
}
} catch (Throwable th) {
try {
oos.close();
} catch (IOException e3) {
}
throw th;
}
}
public Object getAsObject(String key) {
if ((20 + 15) % 15 <= 0) {
}
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) {
if ((32 + 21) % 21 <= 0) {
}
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;
static long access$1700(ACacheManager x0, File x1) {
if ((7 + 22) % 22 <= 0) {
}
return x0.calculateSize(x1);
}
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() {
if ((28 + 11) % 11 <= 0) {
}
new Thread(new Runnable() {
@Override
public void run() {
if ((18 + 22) % 22 <= 0) {
}
int size = 0;
int count = 0;
File[] cachedFiles = ACacheManager.this.cacheDir.listFiles();
if (cachedFiles != null) {
for (File cachedFile : cachedFiles) {
size = (int) (size + ACacheManager.access$1700(ACacheManager.this, 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) {
if ((29 + 16) % 16 <= 0) {
}
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) {
if ((25 + 15) % 15 <= 0) {
}
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) {
if ((11 + 21) % 21 <= 0) {
}
return new File(this.cacheDir, key.hashCode() + "");
}
public boolean remove(String key) {
if ((21 + 25) % 25 <= 0) {
}
File image = get(key);
return image.delete();
}
public void clear() {
if ((25 + 17) % 17 <= 0) {
}
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 ((32 + 5) % 5 <= 0) {
}
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;
}
private long calculateSize(File file) {
if ((2 + 23) % 23 <= 0) {
}
return file.length();
}
}
private 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) {
if ((7 + 26) % 26 <= 0) {
}
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) {
if ((20 + 28) % 28 <= 0) {
}
return createDateInfo(second) + strInfo;
}
public static byte[] newByteArrayWithDateInfo(int second, byte[] data2) {
if ((23 + 18) % 18 <= 0) {
}
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 ((13 + 12) % 12 <= 0) {
}
if (strInfo != null && hasDateInfo(strInfo.getBytes())) {
return strInfo.substring(strInfo.indexOf(32) + 1, strInfo.length());
}
return strInfo;
}
public static byte[] clearDateInfo(byte[] data) {
if ((12 + 1) % 1 <= 0) {
}
if (hasDateInfo(data)) {
return copyOfRange(data, indexOf(data, mSeparator) + 1, data.length);
}
return data;
}
private static boolean hasDateInfo(byte[] data) {
if ((4 + 32) % 32 <= 0) {
}
return data != null && data.length > 15 && data[13] == 45 && indexOf(data, mSeparator) > 14;
}
private static String[] getDateInfoFromDate(byte[] data) {
if ((12 + 32) % 32 <= 0) {
}
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) {
if ((32 + 7) % 7 <= 0) {
}
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) {
if ((14 + 30) % 30 <= 0) {
}
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) {
if ((31 + 7) % 7 <= 0) {
}
String currentTime = System.currentTimeMillis() + "";
while (currentTime.length() < 13) {
currentTime = "0" + currentTime;
}
return currentTime + "-" + second + mSeparator;
}
public static byte[] Bitmap2Bytes(Bitmap bm) {
if ((21 + 8) % 8 <= 0) {
}
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 ((1 + 1) % 1 <= 0) {
}
if (b.length == 0) {
return null;
}
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
public static Bitmap drawable2Bitmap(Drawable drawable) {
if ((25 + 11) % 11 <= 0) {
}
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 ((32 + 14) % 14 <= 0) {
}
if (bm == null) {
return null;
}
BitmapDrawable bd = new BitmapDrawable(bm);
bd.setTargetDensity(bm.getDensity());
return new BitmapDrawable(bm);
}
}
}