鸿蒙NEXT开发Base64工具类(ArkTs)

复制代码
import util from '@ohos.util';

/**
 * Base64 工具类
 * author: 鸿蒙布道师
 * since: 2025/03/31
 */
export class Base64Util {
  /**
   * 创建 Base64Helper 实例
   * @returns Base64Helper 实例
   */
  private static createBase64Helper(): util.Base64Helper {
    return new util.Base64Helper();
  }

  /**
   * 编码为 Uint8Array(异步)
   * @param array 输入的 Uint8Array 数据
   * @returns 编码后的 Uint8Array 对象
   */
  static encode(array: Uint8Array): Promise<Uint8Array> {
    const base64 = Base64Util.createBase64Helper();
    return base64.encode(array);
  }

  /**
   * 编码为 Uint8Array(同步)
   * @param array 输入的 Uint8Array 数据
   * @returns 编码后的 Uint8Array 对象
   */
  static encodeSync(array: Uint8Array): Uint8Array {
    const base64 = Base64Util.createBase64Helper();
    return base64.encodeSync(array);
  }

  /**
   * 编码为字符串(异步)
   * @param array 输入的 Uint8Array 数据
   * @param options 可选参数
   * @returns 编码后的字符串
   */
  static encodeToStr(array: Uint8Array, options?: util.Type): Promise<string> {
    const base64 = Base64Util.createBase64Helper();
    return base64.encodeToString(array, options);
  }

  /**
   * 编码为字符串(同步)
   * @param array 输入的 Uint8Array 数据
   * @param options 可选参数
   * @returns 编码后的字符串
   */
  static encodeToStrSync(array: Uint8Array, options?: util.Type): string {
    const base64 = Base64Util.createBase64Helper();
    return base64.encodeToStringSync(array, options);
  }

  /**
   * 解码为 Uint8Array(异步)
   * @param input 输入的 Uint8Array 或字符串
   * @param options 可选参数
   * @returns 解码后的 Uint8Array 对象
   */
  static decode(input: Uint8Array | string, options?: util.Type): Promise<Uint8Array> {
    const base64 = Base64Util.createBase64Helper();
    return base64.decode(input, options);
  }

  /**
   * 解码为 Uint8Array(同步)
   * @param input 输入的 Uint8Array 或字符串
   * @param options 可选参数
   * @returns 解码后的 Uint8Array 对象
   */
  static decodeSync(input: Uint8Array | string, options?: util.Type): Uint8Array {
    const base64 = Base64Util.createBase64Helper();
    return base64.decodeSync(input, options);
  }
}

代码如下:
TypeScript 复制代码
import util from '@ohos.util';

/**
 * Base64 工具类
 * author: 鸿蒙布道师
 * since: 2025/03/31
 */
export class Base64Util {
  /**
   * 创建 Base64Helper 实例
   * @returns Base64Helper 实例
   */
  private static createBase64Helper(): util.Base64Helper {
    return new util.Base64Helper();
  }

  /**
   * 编码为 Uint8Array(异步)
   * @param array 输入的 Uint8Array 数据
   * @returns 编码后的 Uint8Array 对象
   */
  static encode(array: Uint8Array): Promise<Uint8Array> {
    const base64 = Base64Util.createBase64Helper();
    return base64.encode(array);
  }

  /**
   * 编码为 Uint8Array(同步)
   * @param array 输入的 Uint8Array 数据
   * @returns 编码后的 Uint8Array 对象
   */
  static encodeSync(array: Uint8Array): Uint8Array {
    const base64 = Base64Util.createBase64Helper();
    return base64.encodeSync(array);
  }

  /**
   * 编码为字符串(异步)
   * @param array 输入的 Uint8Array 数据
   * @param options 可选参数
   * @returns 编码后的字符串
   */
  static encodeToStr(array: Uint8Array, options?: util.Type): Promise<string> {
    const base64 = Base64Util.createBase64Helper();
    return base64.encodeToString(array, options);
  }

  /**
   * 编码为字符串(同步)
   * @param array 输入的 Uint8Array 数据
   * @param options 可选参数
   * @returns 编码后的字符串
   */
  static encodeToStrSync(array: Uint8Array, options?: util.Type): string {
    const base64 = Base64Util.createBase64Helper();
    return base64.encodeToStringSync(array, options);
  }

  /**
   * 解码为 Uint8Array(异步)
   * @param input 输入的 Uint8Array 或字符串
   * @param options 可选参数
   * @returns 解码后的 Uint8Array 对象
   */
  static decode(input: Uint8Array | string, options?: util.Type): Promise<Uint8Array> {
    const base64 = Base64Util.createBase64Helper();
    return base64.decode(input, options);
  }

  /**
   * 解码为 Uint8Array(同步)
   * @param input 输入的 Uint8Array 或字符串
   * @param options 可选参数
   * @returns 解码后的 Uint8Array 对象
   */
  static decodeSync(input: Uint8Array | string, options?: util.Type): Uint8Array {
    const base64 = Base64Util.createBase64Helper();
    return base64.decodeSync(input, options);
  }
}
相关推荐
Gracker4 小时前
Android Weekly #202520
android
weixin_411191845 小时前
原生安卓与flutter混编的实现
android·flutter
呼啸长风5 小时前
记一次未成功的 MMKV Pull Request
android·ios·开源
zhanshuo5 小时前
让鸿蒙应用飞起来!ArkUI 图形渲染性能优化全攻略
harmonyos
zhanshuo6 小时前
在 ArkUI 中实现丝滑嵌套滚动:让你的页面像抖音一样顺滑
harmonyos
围巾哥萧尘6 小时前
iOS App开发上架全流程🧣
ios
小墙程序员7 小时前
Android 性能优化(六)使用 Callstacks Sample 和 Java/Kotlin Method Recording 分析方法的耗时
android·性能优化·android studio
simple_lau8 小时前
鸿蒙开发中如何快速定位丢帧
harmonyos·arkts·arkui
hcgeng9 小时前
android中相近方法对比
android·方法比对