From a1c96d95959354667695a1ea6c03179e939e31f3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 12 Jun 2023 22:23:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/utils/StringUtils.kt | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/StringUtils.kt b/app/src/main/java/io/legado/app/utils/StringUtils.kt index 770c48dc0..aec561c4d 100644 --- a/app/src/main/java/io/legado/app/utils/StringUtils.kt +++ b/app/src/main/java/io/legado/app/utils/StringUtils.kt @@ -2,14 +2,20 @@ package io.legado.app.utils import android.annotation.SuppressLint import android.text.TextUtils.isEmpty - +import android.util.Base64 +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.IOException import java.text.DecimalFormat import java.text.SimpleDateFormat import java.util.* import java.util.regex.Matcher import java.util.regex.Pattern +import java.util.zip.GZIPInputStream +import java.util.zip.GZIPOutputStream import kotlin.math.abs + @Suppress("unused", "MemberVisibilityCanBePrivate") object StringUtils { private const val HOUR_OF_DAY = 24 @@ -167,17 +173,20 @@ object StringUtils { result = 0 tmp = 0 } + tmpNum == 10000 -> { result += tmp result *= tmpNum tmp = 0 } + tmpNum >= 10 -> { if (tmp == 0) tmp = 1 result += tmpNum * tmp tmp = 0 } + else -> { tmp = if (i >= 2 && i == cn.size - 1 && ChnMap[cn[i - 1]]!! > 10) tmpNum * ChnMap[cn[i - 1]]!! / 10 @@ -256,7 +265,7 @@ object StringUtils { while (start < end && (s[end].code <= 0x20 || s[end] == ' ')) { --end } - if (end < len) ++end + ++end return if (start > 0 || end < len) s.substring(start, end) else s } @@ -287,4 +296,58 @@ object StringUtils { return buf.toString() } + /** + * 压缩字符串 + */ + fun compress(str: String): Result { + return kotlin.runCatching { + if (str.isEmpty()) { + return@runCatching str + } + val out = ByteArrayOutputStream() + var gzip: GZIPOutputStream? = null + return@runCatching try { + gzip = GZIPOutputStream(out) + gzip.write(str.toByteArray()) + Base64.encodeToString(out.toByteArray(), Base64.NO_WRAP) + } finally { + gzip?.runCatching { + close() + } + out.runCatching { + close() + } + } + } + } + + /** + * 解压字符串 + */ + @Throws(IOException::class) + fun unCompress(str: String): Result { + return kotlin.runCatching { + val outputStream = ByteArrayOutputStream() + var inputStream: ByteArrayInputStream? = null + var ginZip: GZIPInputStream? = null + return@runCatching try { + val compressed = Base64.decode(str, Base64.NO_WRAP) + inputStream = ByteArrayInputStream(compressed) + ginZip = GZIPInputStream(inputStream) + ginZip.copyTo(outputStream) + outputStream.toString() + } finally { + ginZip?.runCatching { + close() + } + inputStream?.runCatching { + close() + } + outputStream.runCatching { + close() + } + } + } + } + }