Merge pull request #2604 from iyhkhtrk/master

Js添加Str和ByteArray互转的方法
pull/2610/head
kunfei 2 years ago committed by GitHub
commit 2e28ef7e6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      app/src/main/assets/help/jsHelp.md
  2. 34
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 5
      app/src/main/java/io/legado/app/utils/EncoderUtils.kt

@ -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.hexDecodeToString(hex: String)
utf8 编码为hexString
java.hexEncodeToString(utf8: String)
```
* 文件
> 所有对于文件的读写删操作都是相对路径,只能操作阅读缓存/android/data/{package}/cache/内的文件

@ -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? {

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

Loading…
Cancel
Save