鸿蒙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);
  }
}
相关推荐
玲小珑9 分钟前
Auto.js 入门指南(七)定时任务调度
android·前端
墨狂之逸才40 分钟前
adb常用命令调试
android
YoungForYou1 小时前
Android端部署NCNN
android
王二蛋与他的张大花1 小时前
HarmonyOS运动开发:如何用mpchart绘制运动配速图表
harmonyos
程序员小刘1 小时前
【HarmonyOS 5】生活与服务开发实践详解以及服务卡片案例
华为·生活·harmonyos
移动开发者1号1 小时前
Jetpack Compose瀑布流实现方案
android·kotlin
移动开发者1号1 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·kotlin
移动开发者1号1 小时前
ListView与RecyclerView区别总结
android·kotlin
移动开发者1号1 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·kotlin
Jouzzy3 小时前
【iOS安全】iPhone X iOS 16.7.11 (20H360) WinRa1n 越狱教程
安全·ios·iphone