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] 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) }