鸿蒙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);
  }
}
相关推荐
Magnetic_h30 分钟前
【iOS】内存管理及部分Runtime复习
笔记·学习·macos·ios·objective-c·cocoa·xcode
用户091 小时前
将挂起函数或流转换为回调
android
li理1 小时前
鸿蒙应用开发深度解析:从基础列表到瀑布流,全面掌握界面布局艺术
前端·前端框架·harmonyos
一条上岸小咸鱼2 小时前
Kotlin 控制流(一):条件和循环
android·kotlin
博睿谷IT99_3 小时前
OSPF 的工作过程、Router ID 机制、报文结构
开发语言·网络·华为·智能路由器·网络工程师·华为认证·数据通信
枯骨成佛15 小时前
MTK Android 14 通过属性控制系统设置显示双栏或者单栏
android
雨白15 小时前
Android 自定义 View:范围裁切和几何变换
android
jiushiapwojdap16 小时前
Flutter上手记:为什么我的按钮能同时在iOS和Android上跳舞?[特殊字符][特殊字符]
android·其他·flutter·ios
万少17 小时前
可可图片编辑 HarmonyOS(2) 选择图片和保存到图库
harmonyos
小小小小小星18 小时前
鸿蒙开发性能优化实战指南:从工具到代码全解析
性能优化·harmonyos