From 30d14ee69c5d4c78667faaabad77494f0d7dfde3 Mon Sep 17 00:00:00 2001 From: iyhkhtrk <104120418+iyhkhtrk@users.noreply.github.com> Date: Tue, 29 Nov 2022 18:52:28 +0800 Subject: [PATCH 1/5] Update EncoderUtils.kt --- app/src/main/java/io/legado/app/utils/EncoderUtils.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt index fec85ff2c..54c528f03 100644 --- a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt +++ b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt @@ -37,5 +37,10 @@ object EncoderUtils { fun base64Encode(str: String, flags: Int = Base64.NO_WRAP): String? { return Base64.encodeToString(str.toByteArray(), flags) } + + @JvmOverloads + fun base64DecodeToByteArray(str: String, flags: Int = Base64.DEFAULT): ByteArray { + return Base64.decode(str, flags) + } } From e7159e46c75ec2221fe66f5a6ce71c385da8c0ab Mon Sep 17 00:00:00 2001 From: iyhkhtrk <104120418+iyhkhtrk@users.noreply.github.com> Date: Tue, 29 Nov 2022 19:09:11 +0800 Subject: [PATCH 2/5] Update JsExtensions.kt --- .../java/io/legado/app/help/JsExtensions.kt | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 4f9d91343..fed7a88fd 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -1,8 +1,8 @@ package io.legado.app.help import android.net.Uri -import android.util.Base64 import androidx.annotation.Keep +import cn.hutool.core.codec.Base64 import cn.hutool.core.util.HexUtil import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.dateFormat @@ -313,12 +313,34 @@ interface JsExtensions : JsEncodeUtils { } return response } + + /* Str转ByteArray */ + fun strToBytes(str: String): ByteArray { + return str.toByteArray(charset("UTF-8")) + } + + fun strToBytes(str: String, charset: String): ByteArray { + return str.toByteArray(charset(charset)) + } + + /* ByteArray转Str */ + fun bytesToStr(bytes: ByteArray): String { + return String(bytes, charset("UTF-8")) + } + + fun bytesToStr(bytes: ByteArray, charset: String): String { + return String(bytes, charset(charset)) + } /** * js实现base64解码,不能删 */ - fun base64Decode(str: String): String { - return EncoderUtils.base64Decode(str, Base64.NO_WRAP) + fun base64Decode(str: String?): String { + return Base64.decodeStr(str) + } + + fun base64Decode(str: String?, charset: String): String { + return Base64.decodeStr(str, charset(charset)) } fun base64Decode(str: String, flags: Int): String { @@ -329,18 +351,18 @@ interface JsExtensions : JsEncodeUtils { if (str.isNullOrBlank()) { return null } - return Base64.decode(str, Base64.DEFAULT) + return EncoderUtils.base64DecodeToByteArray(str, 0) } fun base64DecodeToByteArray(str: String?, flags: Int): ByteArray? { if (str.isNullOrBlank()) { return null } - return Base64.decode(str, flags) + return EncoderUtils.base64DecodeToByteArray(str, flags) } fun base64Encode(str: String): String? { - return EncoderUtils.base64Encode(str, Base64.NO_WRAP) + return EncoderUtils.base64Encode(str, 2) } fun base64Encode(str: String, flags: Int): String? { @@ -353,12 +375,12 @@ interface JsExtensions : JsEncodeUtils { } /* hexString 解码为utf8String*/ - fun hexDecodeToString(hex: String): String? { + fun hexDecode(hex: String): String? { return HexUtil.decodeHexStr(hex) } /* utf8 编码为hexString */ - fun hexEncodeToString(utf8: String): String? { + fun hexEncode(utf8: String): String? { return HexUtil.encodeHexStr(utf8) } From a6147c2d802a4825aadb0115d0a97757837620ba Mon Sep 17 00:00:00 2001 From: iyhkhtrk <104120418+iyhkhtrk@users.noreply.github.com> Date: Tue, 29 Nov 2022 19:18:30 +0800 Subject: [PATCH 3/5] Update jsHelp.md --- app/src/main/assets/help/jsHelp.md | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index 61531c215..b3e15989d 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -131,18 +131,28 @@ java.getZipStringContent(url: String, path: String) * base64 > flags参数可省略,默认Base64.NO_WRAP,查看[flags参数说明](https://blog.csdn.net/zcmain/article/details/97051870) ``` -java.base64Decode(str: String, flags: Int) +java.base64Decode(str: String) +java.base64Decode(str: String, charset: String) java.base64DecodeToByteArray(str: String, flags: Int) java.base64Encode(str: String, flags: Int) ``` +* ByteArray +``` +Str转Bytes +java.strToBytes(str: String) +java.strToBytes(str: String, charset: String) +Bytes转Str +java.bytesToStr(bytes: ByteArray) +java.bytesToStr(bytes: ByteArray, charset: String) +``` * Hex ``` -/* HexString 解码为字节数组 */ -hexDecodeToByteArray(hex: String): ByteArray? -/* hexString 解码为utf8String*/ -hexDecodeToString(hex: String): String? -/* utf8 编码为hexString */ -hexEncodeToString(utf8: String): String? +HexString 解码为字节数组 +java.hexDecodeToByteArray(hex: String) +hexString 解码为utf8String +java.hexDecode(hex: String) +utf8 编码为hexString +java.hexEncode(utf8: String) ``` * 文件 > 所有对于文件的读写删操作都是相对路径,只能操作阅读缓存/android/data/{package}/cache/内的文件 From 1b806c1f6b7fe95edb363f5846d37ad2d862fb61 Mon Sep 17 00:00:00 2001 From: iyhkhtrk <104120418+iyhkhtrk@users.noreply.github.com> Date: Wed, 30 Nov 2022 14:05:22 +0800 Subject: [PATCH 4/5] Update JsExtensions.kt --- app/src/main/java/io/legado/app/help/JsExtensions.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index fed7a88fd..d1e6460bf 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -375,12 +375,12 @@ interface JsExtensions : JsEncodeUtils { } /* hexString 解码为utf8String*/ - fun hexDecode(hex: String): String? { + fun hexDecodeToString(hex: String): String? { return HexUtil.decodeHexStr(hex) } /* utf8 编码为hexString */ - fun hexEncode(utf8: String): String? { + fun hexEncodeToString(utf8: String): String? { return HexUtil.encodeHexStr(utf8) } From 92335e844fd01dcdaec31e5e0b1b7f0546876d01 Mon Sep 17 00:00:00 2001 From: iyhkhtrk <104120418+iyhkhtrk@users.noreply.github.com> Date: Wed, 30 Nov 2022 14:06:46 +0800 Subject: [PATCH 5/5] Update jsHelp.md --- app/src/main/assets/help/jsHelp.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index b3e15989d..f5c43e30d 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -150,9 +150,9 @@ java.bytesToStr(bytes: ByteArray, charset: String) HexString 解码为字节数组 java.hexDecodeToByteArray(hex: String) hexString 解码为utf8String -java.hexDecode(hex: String) +java.hexDecodeToString(hex: String) utf8 编码为hexString -java.hexEncode(utf8: String) +java.hexEncodeToString(utf8: String) ``` * 文件 > 所有对于文件的读写删操作都是相对路径,只能操作阅读缓存/android/data/{package}/cache/内的文件