|
|
@ -15,6 +15,7 @@ import java.io.* |
|
|
|
import java.util.* |
|
|
|
import java.util.* |
|
|
|
import java.util.concurrent.atomic.AtomicInteger |
|
|
|
import java.util.concurrent.atomic.AtomicInteger |
|
|
|
import java.util.concurrent.atomic.AtomicLong |
|
|
|
import java.util.concurrent.atomic.AtomicLong |
|
|
|
|
|
|
|
import kotlin.math.min |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -47,25 +48,11 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
fun put(key: String, value: String) { |
|
|
|
fun put(key: String, value: String) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val file = mCache.newFile(key) |
|
|
|
val file = mCache.newFile(key) |
|
|
|
var out: BufferedWriter? = null |
|
|
|
file.writeText(value) |
|
|
|
try { |
|
|
|
mCache.put(file) |
|
|
|
out = BufferedWriter(FileWriter(file), 1024) |
|
|
|
} catch (e: Exception) { |
|
|
|
out.write(value) |
|
|
|
e.printStackTrace() |
|
|
|
} catch (ignored: IOException) { |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (out != null) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
out.flush() |
|
|
|
|
|
|
|
out.close() |
|
|
|
|
|
|
|
} catch (ignored: IOException) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mCache.put(file) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (ignored: Exception) { |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -197,22 +184,8 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun put(key: String, value: ByteArray) { |
|
|
|
fun put(key: String, value: ByteArray) { |
|
|
|
val file = mCache.newFile(key) |
|
|
|
val file = mCache.newFile(key) |
|
|
|
var out: FileOutputStream? = null |
|
|
|
file.writeBytes(value) |
|
|
|
try { |
|
|
|
mCache.put(file) |
|
|
|
out = FileOutputStream(file) |
|
|
|
|
|
|
|
out.write(value) |
|
|
|
|
|
|
|
} catch (ignored: Exception) { |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (out != null) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
out.flush() |
|
|
|
|
|
|
|
out.close() |
|
|
|
|
|
|
|
} catch (ignored: IOException) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mCache.put(file) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -232,15 +205,13 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
* @return byte 数据 |
|
|
|
* @return byte 数据 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun getAsBinary(key: String): ByteArray? { |
|
|
|
fun getAsBinary(key: String): ByteArray? { |
|
|
|
var raf: RandomAccessFile? = null |
|
|
|
|
|
|
|
var removeFile = false |
|
|
|
var removeFile = false |
|
|
|
try { |
|
|
|
try { |
|
|
|
val file = mCache[key] |
|
|
|
val file = mCache[key] |
|
|
|
if (!file.exists()) |
|
|
|
if (!file.exists()) |
|
|
|
return null |
|
|
|
return null |
|
|
|
raf = RandomAccessFile(file, "r") |
|
|
|
|
|
|
|
val byteArray = ByteArray(raf.length().toInt()) |
|
|
|
val byteArray = file.readBytes() |
|
|
|
raf.read(byteArray) |
|
|
|
|
|
|
|
return if (!Utils.isDue(byteArray)) { |
|
|
|
return if (!Utils.isDue(byteArray)) { |
|
|
|
Utils.clearDateInfo(byteArray) |
|
|
|
Utils.clearDateInfo(byteArray) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -251,14 +222,6 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
e.printStackTrace() |
|
|
|
e.printStackTrace() |
|
|
|
return null |
|
|
|
return null |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
if (raf != null) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
raf.close() |
|
|
|
|
|
|
|
} catch (e: IOException) { |
|
|
|
|
|
|
|
e.printStackTrace() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (removeFile) |
|
|
|
if (removeFile) |
|
|
|
remove(key) |
|
|
|
remove(key) |
|
|
|
} |
|
|
|
} |
|
|
@ -274,10 +237,10 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
@JvmOverloads |
|
|
|
@JvmOverloads |
|
|
|
fun put(key: String, value: Serializable, saveTime: Int = -1) { |
|
|
|
fun put(key: String, value: Serializable, saveTime: Int = -1) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val baos = ByteArrayOutputStream() |
|
|
|
val byteArrayOutputStream = ByteArrayOutputStream() |
|
|
|
ObjectOutputStream(baos).use { oos -> |
|
|
|
ObjectOutputStream(byteArrayOutputStream).use { oos -> |
|
|
|
oos.writeObject(value) |
|
|
|
oos.writeObject(value) |
|
|
|
val data = baos.toByteArray() |
|
|
|
val data = byteArrayOutputStream.toByteArray() |
|
|
|
if (saveTime != -1) { |
|
|
|
if (saveTime != -1) { |
|
|
|
put(key, data, saveTime) |
|
|
|
put(key, data, saveTime) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -460,15 +423,15 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun isDue(data: ByteArray): Boolean { |
|
|
|
fun isDue(data: ByteArray): Boolean { |
|
|
|
try { |
|
|
|
try { |
|
|
|
val strs = getDateInfoFromDate(data) |
|
|
|
val text = getDateInfoFromDate(data) |
|
|
|
if (strs != null && strs.size == 2) { |
|
|
|
if (text != null && text.size == 2) { |
|
|
|
var saveTimeStr = strs[0] |
|
|
|
var saveTimeStr = text[0] |
|
|
|
while (saveTimeStr.startsWith("0")) { |
|
|
|
while (saveTimeStr.startsWith("0")) { |
|
|
|
saveTimeStr = saveTimeStr |
|
|
|
saveTimeStr = saveTimeStr |
|
|
|
.substring(1) |
|
|
|
.substring(1) |
|
|
|
} |
|
|
|
} |
|
|
|
val saveTime = java.lang.Long.valueOf(saveTimeStr) |
|
|
|
val saveTime = java.lang.Long.valueOf(saveTimeStr) |
|
|
|
val deleteAfter = java.lang.Long.valueOf(strs[1]) |
|
|
|
val deleteAfter = java.lang.Long.valueOf(text[1]) |
|
|
|
if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) { |
|
|
|
if (System.currentTimeMillis() > saveTime + deleteAfter * 1000) { |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
@ -544,7 +507,7 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
val copy = ByteArray(newLength) |
|
|
|
val copy = ByteArray(newLength) |
|
|
|
System.arraycopy( |
|
|
|
System.arraycopy( |
|
|
|
original, from, copy, 0, |
|
|
|
original, from, copy, 0, |
|
|
|
Math.min(original.size - from, newLength) |
|
|
|
min(original.size - from, newLength) |
|
|
|
) |
|
|
|
) |
|
|
|
return copy |
|
|
|
return copy |
|
|
|
} |
|
|
|
} |
|
|
@ -561,9 +524,9 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int) |
|
|
|
* Bitmap → byte[] |
|
|
|
* Bitmap → byte[] |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun bitmap2Bytes(bm: Bitmap): ByteArray { |
|
|
|
fun bitmap2Bytes(bm: Bitmap): ByteArray { |
|
|
|
val baos = ByteArrayOutputStream() |
|
|
|
val byteArrayOutputStream = ByteArrayOutputStream() |
|
|
|
bm.compress(Bitmap.CompressFormat.PNG, 100, baos) |
|
|
|
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream) |
|
|
|
return baos.toByteArray() |
|
|
|
return byteArrayOutputStream.toByteArray() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
|