【OpenHarmony】优化系统输入输出流能力库:okio

53 阅读5分钟

简介

okio是一个通过数据流、序列化和文件系统来优化系统输入输出流的能力的库。它依托于系统能力,提供字符串的编解码转换能力,基础数据类型的读写能力以及支持对文件读写的能力。okio为数据容器提供缓冲区,并覆盖了NIO包所支持的特性功能。将okio作为httpclient的一个组件使它更容易访问、存储和处理数据。

下载安装

ohpm install @ohos/okio 

使用说明:

import okio from '@ohos/okio';
  1. 在Buffer里写入和读取Utf8 :
   performWriteUtf8() {
       var buffer = new okio.Buffer();
       buffer.writeUtf8("test");
       var readUtfValue = buffer.readUtf8();
   }
  1. 在Buffer里写入和读取Int:
   performWriteInt() {
       var buffer = new okio.Buffer();
       buffer.writeInt(10);
       var readInt = buffer.readInt();
   }
  1. 在Buffer里写入和读取String :
   performWriteString() {
       var buffer = new okio.Buffer();
       buffer.writeString("Test");
       var readString = buffer.readString();
   }
  1. 在Buffer里写入和读取IntLe:
   performWriteIntLe() {
       var buffer = new okio.Buffer();
       buffer.writeIntLe(25);
       var readIntLe = buffer.readIntLe();
   }
  1. 在Buffer里写入和读取Short:
   performWriteShort() {
       var buffer = new okio.Buffer();
       buffer.writeShort(25);
       var readShort = buffer.readShort();
   }
  1. 在Buffer里写入和读取ShortLe:
   performWriteShortLe() {
       var buffer = new okio.Buffer();
       buffer.writeShortLe(100);
       var readShortLe = buffer.readShortLe();
   }
  1. 在Buffer里写入和读取Byte:
   performWriteByte() {
       var buffer = new okio.Buffer();
       buffer.writeByte(9)
       var readByte = buffer.readByte();
   }
  1. 在Buffer里写入和读取Utf8CodePoint :
   performWriteUtf8CodePoint() {
       var buffer = new okio.Buffer();
       buffer.writeUtf8CodePoint(99);
       var readUtf8CodePointValue = buffer.readUtf8CodePoint();
   }
  1. 将Base64编码的字符串写入ByteString :
   decodeBase64() {
       let decodeBase64 = byteStringCompanObj.decodeBase64('SGVsbG8gd29ybGQ=');
       let decodeBase64Value = JSON.stringify(decodeBase64);
   }
  1. 将十六进制字符串写入ByteString :
    decodeHex() {
        let decodehex = byteStringCompanObj.decodeHex('48656C6C6F20776F726C640D0A');
        let decodeHexValue = JSON.stringify(decodehex);
    }
  1. 将Utf8格式的字符串写入ByteString :
    encodeUtf8() {
        let encodeUtf8 = byteStringCompanObj.encodeUtf8('Hello world #4 ❤ ( ͡ㆆ ͜ʖ ͡ㆆ)');
        let encodeUtf8Value = JSON.stringify(encodeUtf8);
    }
  1. 将字符串写入ByteString :
    ofValue() {
        let ofData = byteStringCompanObj.of(["Hello", "world"]);
        let ofOutputValue = JSON.stringify(ofData);
    }

DD一下:欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。

`欢迎大家关注工粽号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
  1. 将内容写入文件 :
    writeFile() {
        let fileUri = '/data/data/com.openharmony.ohos.okioapplication/cache/test.txt';
        let writeInputValue = "openharmony";
        var sink = new okio.Sink(fileUri);
        var isAppend = false;
    	sink.write(writeInputValue,isAppend);
    }
  1. 读取文件内容:
    readFileValue() {
        let fileUri = '/data/data/com.openharmony.ohos.okioapplication/cache/test.txt';
        var source = new okio.Source(fileUri);
        source.read().then(function (data) {
            that.readValue = data;
        }).catch(function (error) {
            //Error
        });
    }

接口说明

Buffer

接口名参数返回值说明
writeUtf8data: stringBuffer在Buffer中写入Utf8类型的数据
readUtf8string从Buffer中读取字符串
writeIntdata: intBuffer在Buffer中写入Int类型数据
readIntint从Buffer中读取Int类型数据
writeStringdata: stringBuffer在Buffer中写入字符串类型的数据
readStringstring从Buffer中读取字符串
writeIntLedata: intLeBuffer在Buffer中写入IntLe类型数据
readIntLeintLe从Buffer中读取IntLe类型数据
writeShortLedata: shortLeBuffer在Buffer中写入ShortLe类型数据
readShortLeshortLe从Buffer中读取ShortLe类型数据
writeShortdata: ShortBuffer在Buffer中写入Short类型数据
readShortshort从Buffer中读取Short类型数据
writeBytedata: byteBuffer在Buffer中写入Byte类型数据
readBytebyte从Buffer中读取Byte类型数据
writeUtf8CodePointdata: Utf8CodePointBuffer在Buffer中写入Utf8CodePoint
readUtf8CodePointUtf8CodePoint从Buffer中读取Utf8CodePoint
readUtf8ByteCountbyteCountstring从Buffer中返读取长度为byteCount的字符串数据并返回
writableSegmentminimumCapacityint根据写入的数据长度获取可用于写入的Segment
writeSubStringstring, beginIndex, endIndexBuffer在Buffer中写入传入的字符串的子字符串并返回该Buffer
writeUtf8BeginEndIndexstring, beginIndex, endIndexBuffer在Buffer中写入传入的字符串的子字符串并返回该Buffer
getCommonResultpos: intstring在Buffer中读取pos长度的数据并以string类型返回
skipByteCountbytecount: intvoid跳过bytecount从Buffer中读取数据
readByteArraybyteCountstring从Buffer中读取byteCount长度的数据并以字符串形式返回
readFullysink: intBuffer按照给定的长度读取数据并返回Buffer
readsink, offset,int从offset开始读取byteCount的数据

ByteString

接口名参数返回值说明
ByteStringdata: buffervoid获取数据缓冲区并创建ByteString示实例
decodeBase64data: Base64StringByteString对输入的Base64编码的数据进行解码,并将结果以ByteString的形式返回
decodeHexdata: HexStringByteString对输入的十六进制数据进行解码,并将结果以ByteString的形式返回
encodeUtf8data: stringByteString对输入的字符串进行UTF-8编码,并将结果以ByteString的形式返回
ofdata: arrayByteString返回一个复制了参数data的数据的新ByteString
toAsciiLowercasedata: stringByteString将输入的字符串转换为小写形式,并将结果以ByteString的形式返回
toAsciiUppercasedata: stringByteString将输入的字符串转换为大写形式,并将结果以ByteString的形式返回
toByteArrayByteArray将ByteString中字节数组复制后以ByteArray形式返回
getWithIndexindex: intByteValue以整数作为输入并返回 ByteValue
getSizeDataLength返回ByteString中的字节数
internalArrayByteArray返回ByteString中的原始字节,而不是复制的数据
hashCodeHashcode返回ByteString的哈希吗值
compareToOtherother: BufferByteArray用于比较两个ByteString并返回:
(a) 如果ByteString的大小相同,字符串相等,则返回0;
(b) 如果原始ByteString的第一个字符小于比较ByteString的第一个字符,则返回-1;
(c)如果原始ByteString的第一个字符大于比较ByteString的第一个字符,则返回1;

FIleHandler(Sink, Source)

接口名参数返回值说明
readstring从文件中读取内容并返回
writewriteInputValue: string, isAppend: booleanvoid如果 isAppend 设置为 false,则将字符串值写入新文件;如果 isAppend 设置为 true,则附加更新到现有文件

目录结构

|---- okio  
|     |---- entry  # 示例代码文件夹
|     |---- library  # okio 库文件夹
|           |---- index.ets  # okio对外接口
|     |---- README.MD  # 安装使用方法