diff --git a/README.md b/README.md index 9f0518e9782f3780a59f5e89e4ec3d6fed44af58..ac52445d6f9d83c1a1c2c00da81fc36f221780b4 100644 --- a/README.md +++ b/README.md @@ -1,298 +1,238 @@ # okio -okio is a library that provides support for system input and output through data streams, serialization and the file system, and defines buffers, which are containers for data, and provides an overview of the other NIO packages. To make it much easier to access, store, and process your data. It started as a component of OkHttp. - -## Usage Instructions -Import okio as below from node_modules after adding okio_ohos module in project root directory: -`` -import okio from 'okio; -`` - -1. To write and read Utf8 data in buffer: -`` - performWriteUtf8() { - var buffer = new okio.Buffer(); - buffer.writeUtf8("test"); - var readUtfValue = buffer.readUtf8(); - } -`` - -2. To write and read int data in buffer: -`` - performWriteInt() { - var buffer = new okio.Buffer(); - buffer.writeInt(10); - var readInt = buffer.readInt(); - } -`` - -3. To write and read String data in buffer: -`` - performWriteString() { - var buffer = new okio.Buffer(); - buffer.writeString("Test"); - var readString = buffer.readString(); - } -`` - -4. To write and read intLe data in buffer: -`` - performWriteIntLe() { - var buffer = new okio.Buffer(); - buffer.writeIntLe(25); - var readIntLe = buffer.readIntLe(); - } -`` - -5. To write and read short data in buffer: -`` - performWriteShort() { - var buffer = new okio.Buffer(); - buffer.writeShort(25); - var readShort = buffer.readShort(); - } -`` - -6. To write and read shortLe data in buffer: -`` - performWriteShortLe() { - var buffer = new okio.Buffer(); - buffer.writeShortLe(100); - var readShortLe = buffer.readShortLe(); - } -`` - -7. To write and read byte data in buffer: -`` - performWriteByte() { - var buffer = new okio.Buffer(); - buffer.writeByte(9) - var readByte = buffer.readByte(); - } -`` - -8. To write and read Utf8CodePoint data in buffer: -`` - performWriteUtf8CodePoint() { - var buffer = new okio.Buffer(); - buffer.writeUtf8CodePoint(99); - var readUtf8CodePointValue = buffer.readUtf8CodePoint(); - } -`` +## 简介 -9. To write Base64 string data in ByteString format: -`` - decodeBase64() { - let decodeBase64 = byteStringCompanObj.decodeBase64('SGVsbG8gd29ybGQ='); - let decodeBase64Value = JSON.stringify(decodeBase64); - } -`` +okio 是一个通过数据流、序列化和文件系统来优化系统输入输出流的能力的库。okio为数据容器提供缓冲区,并覆盖了NIO包所支持的特性功能。将okio作为httpclient的一个组件使它更容易访问、存储和处理数据。 + +## 下载安装 + +```javascript +npm install @ohos/okio --save +``` + +OpenHarmony npm环境配置等更多内容,请参考[如何安装OpenHarmony npm](https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md) + +## 使用说明: +在项目根目录添加okio模块后,从node_modules导入okio,如下所示: + +```javascript +import okio from 'okio'; +``` -10. To write Hex string data in ByteString format: -`` +1. 在Buffer里写入和读取Utf8 : + + ```javascript + performWriteUtf8() { + var buffer = new okio.Buffer(); + buffer.writeUtf8("test"); + var readUtfValue = buffer.readUtf8(); + } + ``` + +2. 在Buffer里写入和读取int : + + ```javascript + performWriteInt() { + var buffer = new okio.Buffer(); + buffer.writeInt(10); + var readInt = buffer.readInt(); + } + ``` + +3. 在Buffer里写入和读取String : + + ```javascript + performWriteString() { + var buffer = new okio.Buffer(); + buffer.writeString("Test"); + var readString = buffer.readString(); + } + ``` + +4. 在Buffer里写入和读取intLe : + + ```javascript + performWriteIntLe() { + var buffer = new okio.Buffer(); + buffer.writeIntLe(25); + var readIntLe = buffer.readIntLe(); + } + ``` + +5. 在Buffer里写入和读取short : + + ```javascript + performWriteShort() { + var buffer = new okio.Buffer(); + buffer.writeShort(25); + var readShort = buffer.readShort(); + } + ``` + +6. 在Buffer里写入和读取shortLe : + + ```javascript + performWriteShortLe() { + var buffer = new okio.Buffer(); + buffer.writeShortLe(100); + var readShortLe = buffer.readShortLe(); + } + ``` + +7. 在Buffer里写入和读取byte : + + ```javascript + performWriteByte() { + var buffer = new okio.Buffer(); + buffer.writeByte(9) + var readByte = buffer.readByte(); + } + ``` + +8. 在Buffer里写入和读取Utf8CodePoint : + + ```javascript + performWriteUtf8CodePoint() { + var buffer = new okio.Buffer(); + buffer.writeUtf8CodePoint(99); + var readUtf8CodePointValue = buffer.readUtf8CodePoint(); + } + ``` + +9. 将Base64加密过的字符串写入ByteString : + + ```javascript + decodeBase64() { + let decodeBase64 = byteStringCompanObj.decodeBase64('SGVsbG8gd29ybGQ='); + let decodeBase64Value = JSON.stringify(decodeBase64); + } + ``` + +10. 将十六进制字符串写入ByteString : + + ```javascript decodeHex() { let decodehex = byteStringCompanObj.decodeHex('48656C6C6F20776F726C640D0A'); let decodeHexValue = JSON.stringify(decodehex); } -`` + ``` -11. To write Utf8 string data in ByteString format: -`` +11. 将Utf8格式的字符串写入ByteString : + + ```javascript encodeUtf8() { let encodeUtf8 = byteStringCompanObj.encodeUtf8('Hello world #4 ❤ ( ͡ㆆ ͜ʖ ͡ㆆ)'); let encodeUtf8Value = JSON.stringify(encodeUtf8); } -`` + ``` + +12. 将字符串写入ByteString : -12. To write type of string data in ByteString format: -`` + ```javascript ofValue() { let ofData = byteStringCompanObj.of(["Hello", "world"]); let ofOutputValue = JSON.stringify(ofData); } -`` + ``` -13. To write content on file: -`` +13. T将Utf8格式的字符串写入ByteString : + + ```javascript 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); + sink.write(writeInputValue,isAppend); } -`` + ``` -14. To read content from file: -`` +14. 在文件中写入内容: + + ```javascript 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 }); } -`` + ``` -## API +## 接口说明 ### Buffer -**writeUtf8(data: string): Buffer;** -Take Utf8 as input to write into Buffer - -**readUtf8(): string;** -Returns string from Buffer - -**writeInt(data: int): Buffer; -Take int as input to write into Buffer - -**readInt(): int;** -Returns int from Buffer - -**writeString(data: string): Buffer;** -Take string as input to write into Buffer - -**readString(): string;** -Returns string from Buffer - -**writeIntLe(data: intLe): Buffer; -Take intLe as input to write into Buffer - -**readIntLe(): intLe;** -Returns int from Buffer - -**writeShortLe(data: shortLe): Buffer;** -Take shortLe as input to write into Buffer - -**readShortLe(): shortLe;** -Returns shortLe from Buffer - -**writeShort(data: short): Buffer;** -Take short as input to write into Buffer -**readShort(): short;** -Returns short from Buffer - -**writeByte(data: byte): Buffer;** -Take byte as input to write into Buffer - -**readByte(): byte;** -Returns byte from Buffer - -**writeUtf8CodePoint(data: Utf8CodePoint): Buffer;** -Take Utf8CodePoint as input to write into Buffer - -**readUtf8CodePoint(): Utf8CodePoint;** -Returns Utf8CodePoint from Buffer - -**readUtf8ByteCount(byteCount): string** -Returns string from Buffer - -**writableSegment(minimumCapacity): int;** -Write segment capacity and return it - -**writeSubString(string, beginIndex, endIndex): Buffer** -Write sub string of string and returns Buffer - -**writeUtf8BeginEndIndex(string, beginIndex, endIndex): Buffer** -Write sub string of string and returns Buffer - -**getCommonResult(pos: int): string;** -get common result with position and returns string - -**skipByteCount(bytecount: int)** -skip the number of bytes when you read from buffer - -**readByteArray(byteCount): string** -read byte array and returns string - -**readFully(sink: int): Buffer** -read data as per given length and returns Buffer - -**read(sink, offset, byteCount): int** -reads data and returns length +| 接口名 | 参数 | 返回值 | 说明 | +| ---------------------- | ---------------------------- | ------------- | --------------------------------------------------- | +| writeUtf8 | data: string | Buffer | 在Buffer中写入Utf8类型的数据 | +| readUtf8 | 无 | string | 从Buffer中返回字符串 | +| writeInt | data: int | Buffer | 在Buffer中写入Int类型数据 | +| readInt | 无 | int | 从Buffer中返回int类型数据 | +| writeString | data: string | Buffer | 在Buffer中写入字符串类型的数据 | +| readString | 无 | string | 从Buffer中返回字符串 | +| writeIntLe | data: intLe | Buffer | 在Buffer中写入intLe类型数据 | +| readIntLe | 无 | intLe | 从Buffer中返回intLe类型数据 | +| writeShortLe | data: shortLe | Buffer | 在Buffer中写入shortLe类型数据 | +| readShortLe | 无 | shortLe | 从Buffer中返回shortLe类型数据 | +| writeShort | data: Short | Buffer | 在Buffer中写入Short类型数据 | +| readShort | 无 | short | 从Buffer中返回Short类型数据 | +| writeByte | data: byte | Buffer | 在Buffer中写入byte类型数据 | +| readByte | 无 | byte | 从Buffer中返回byte类型数据 | +| writeUtf8CodePoint | data: Utf8CodePoint | Buffer | 在Buffer中写入Utf8CodePoint | +| readUtf8CodePoint | 无 | Utf8CodePoint | 从Buffer中返回Utf8CodePoint | +| readUtf8ByteCount | byteCount | string | 从Buffer中返回长度为byteCount的字符串 | +| writableSegment | minimumCapacity | int | 根据写入的数据长度返回可用于写入的Segment | +| writeSubString | string, beginIndex, endIndex | Buffer | 在Buffer中写入传入的字符串的子字符串并返回改缓冲区 | +| writeUtf8BeginEndIndex | string, beginIndex, endIndex | Buffer | 在Buffer中写入传入的字符串的子字符串并返回改缓冲区 | +| getCommonResult | pos: int | string | 在Buffer中读取pos长度的数据并以string类型返回 | +| skipByteCount | bytecount: int | void | 跳过bytecount从Buffer中读取数据 | +| readByteArray | byteCount | string | 从Buffer中读取byteCount长度的数据并以字符串形式返回 | +| readFully | sink: int | Buffer | 按照给定的长度读取数据并返回Buffer | +| read | sink, offset, | int | 从offset开始读取byteCount的数据 | ### ByteString -**ByteString(data: buffer);** -Take data buffer and create ByteString instance - -**decodeBase64(data: Base64String): ByteString;** -Takes Base64-encoded bytes as input, decodes it and returns their value as a byte string - -**decodeHex(data: HexString): ByteString;** -Takes Hex-encoded bytes as input, decodes it and returns their value as a byte string. - -**encodeUtf8(data: string): ByteString;** -Takes string as input and returns a new byte string containing the UTF-8 bytes of input - -**of(data: array): ByteString;** -Returns a new byte string containing a clone of the bytes of data. - -**toAsciiLowercase(data: string): ByteString;** -Take string as input and returns Ascii lowercase value in ByteString with array - -**toAsciiUppercase(data: string): ByteString;** -Take string as input and returns Ascii uppercase value in ByteString with array - -**toByteArray():ByteArray;** -Returns a byte array containing a copy of the bytes in this ByteString. - -**getWithIndex(index: int):ByteValue;** -Take integer as input and returns ByteValue - -**getSize():DataLength;** -Returns the number of bytes in this ByteString. - -**internalArray():ByteArray;** -Returns the bytes of this string without a defensive copy. Do not mutate! - -**hashCode():Hashcode;** -Returns the hashcode value - -**compareToOther(other: Buffer): ByteArray** -It can be used to compare two bytestrings and returns: -(a) 0 if bytestring’s size is same and strings are equal -(b) -1 if original bytestring’s 1st character is less than comparing bytestring’s 1st character -(c) 1 if original byte string’s 1st character is higher than comparing bytestring’s 1st character +| 接口名 | 参数 | 返回值 | 说明 | +| ---------------- | ------------------ | ---------- | ------------------------------------------------------------ | +| ByteString | data: buffer | void | 获取数据缓冲区并创建ByteString示实例 | +| decodeBase64 | data: Base64String | ByteString | 对输入的Base64编码的数据进行解码,并将结果以ByteString的形式返回 | +| decodeHex | data: HexString | ByteString | 对输入的十六进制数据进行解码,并将结果以ByteString的形式返回 | +| encodeUtf8 | data: string | ByteString | 对输入的字符串进行UTF-8编码,并将结果以ByteString的形式返回 | +| of | data: array | ByteString | 返回一个复制了参数data的数据的新ByteString | +| toAsciiLowercase | data: string | ByteString | 将输入的字符串转换为小写形式,并将结果以ByteString的形式返回 | +| toAsciiUppercase | data: string | ByteString | 将输入的字符串转换为大写形式,并将结果以ByteString的形式返回 | +| toByteArray | 无 | ByteArray | 将ByteString中字节数组复制后以ByteArray形式返回 | +| getWithIndex | index: int | ByteValue | 以整数作为输入并返回 ByteValue | +| getSize | 无 | DataLength | 返回ByteString中的字节数 | +| internalArray | 无 | ByteArray | 返回ByteString中的原始字节,而不是复制的数据 | +| hashCode | 无 | Hashcode | 返回ByteString的哈希吗值 | +| compareToOther | other: Buffer | ByteArray | 用于比较两个ByteString并返回:
(a) 如果ByteString的大小相同,字符串相等,则返回0;
(b) 如果原始ByteString的第一个字符小于比较ByteString的第一个字符,则返回-1;
(c)如果原始ByteString的第一个字符大于比较ByteString的第一个字符,则返回1;
| ### FIleHandler(Sink, Source) -**read(): string;** -Read content from file and returns it - -**write(writeInputValue: string, isAppend: boolean);** -Writes string value to new file if isAPpend set to false or update in the existing file if isAppend is set to true - -## Installation Instructions -1. For using okio_ohos module in sample application, include the below dependency in entry package.json. Before this add okio module in project root directory. - -``` -"dependencies": { - "okio": "file:../okio" - } -``` +| 接口名 | 参数 | 返回值 | 说明 | +| ------ | ------------------------------------------ | ------ | ------------------------------------------------------------ | +| read | 无 | string | 从文件中读取内容并返回 | +| write | writeInputValue: string, isAppend: boolean | void | 如果 isAppend 设置为 false,则将字符串值写入新文件;如果 isAppend 设置为 true,则在现有文件中更新 | +## 兼容性 +支持OpenHarmony API version 8 及以上版本 -## Compatibility -Supports OpenHarmony API version 8 and above - -## Directory Structure -```` +## 目录结构 +````javascript |---- okio -| |---- entry # sample app usage -| |---- okio # okio library -| |---- index.ets # External interface -| |---- README.MD # installation and usage +| |---- entry # 示例代码文件夹 +| |---- okio # okio 库文件夹 +| |---- index.ets # okio对外接口 +| |---- README.MD # 安装使用方法 ```` -## Code Contribution -If you find any problems during usage, you can submit an [Issue](https://gitee.com/openharmony-tpc/okio/issues) to us. Of course, we also welcome you to send us [PR](https://gitee.com/openharmony-tpc/httpclient/pulls). +## 贡献代码 -## Open source License -This project is based on [Apache License 2.0](https://gitee.com/openharmony-tpc/okio/blob/master/LICENSE.txt) ,please enjoy and participate in open source freely. +使用过程中发现任何问题都可以提[Issue](https://gitee.com/openharmony-tpc/okio/issues) 给我们,当然,我们也非常欢迎你给我们提[PR](https://gitee.com/openharmony-tpc/okio/pulls)。 +## 开源协议 +本项目基于 [Apache License 2.0](https://gitee.com/openharmony-tpc/okio/blob/master/LICENSE.txt),请自由地享受和参与开源。