|
1 | 1 | # Data |
2 | 2 |
|
3 | | -`Data` 类用于表示二进制数据,提供多种方法用于数据的创建、转换、压缩、解压、拼接、读取等操作。可用于处理图像、文件、音频、编码数据等各种原始字节数据。 |
| 3 | +The `Data` class represents binary data in memory and provides a wide range of methods for manipulating, converting, compressing, decompressing, and transforming that data. It is useful for working with raw byte buffers, encoded files, images, and more. |
4 | 4 |
|
5 | 5 | --- |
6 | 6 |
|
7 | | -## CompressionAlgorithm(压缩算法枚举) |
| 7 | +## CompressionAlgorithm (Enum) |
8 | 8 |
|
9 | | -该枚举用于指定 `Data` 的压缩或解压算法: |
| 9 | +Specifies which compression algorithm to use when compressing or decompressing a `Data` instance: |
10 | 10 |
|
11 | | -| 枚举值 | 描述 | |
12 | | -| ------- | --------------------- | |
13 | | -| `lzfse` | LZFSE 压缩算法,快速且高效。 | |
14 | | -| `lz4` | LZ4 压缩算法,压缩和解压速度极快。 | |
15 | | -| `lzma` | LZMA 算法,压缩率高,压缩速度较慢。 | |
16 | | -| `zlib` | Zlib 算法,通用且广泛支持的压缩格式。 | |
| 11 | +| Value | Description | |
| 12 | +| ------- | --------------------------------------------------------- | |
| 13 | +| `lzfse` | Fast and efficient compression using the LZFSE algorithm. | |
| 14 | +| `lz4` | Very fast compression with moderate compression ratio. | |
| 15 | +| `lzma` | High compression ratio, slower performance. | |
| 16 | +| `zlib` | Standard and widely-used compression format. | |
17 | 17 |
|
18 | 18 | --- |
19 | 19 |
|
20 | | -## 实例属性与方法 |
| 20 | +## Instance Properties and Methods |
21 | 21 |
|
22 | 22 | ### `size: number` |
23 | 23 |
|
24 | | -当前数据的字节长度(只读属性)。 |
| 24 | +The length of the data in bytes (read-only). |
25 | 25 |
|
26 | 26 | --- |
27 | 27 |
|
28 | 28 | ### `resetBytes(startIndex: number, endIndex: number): void` |
29 | 29 |
|
30 | | -将数据中指定范围内的字节清零。 |
| 30 | +Resets a range of bytes to zero. |
31 | 31 |
|
32 | | -* `startIndex`:起始索引(包含) |
33 | | -* `endIndex`:结束索引(不包含) |
| 32 | +* `startIndex`: Starting index (inclusive) |
| 33 | +* `endIndex`: Ending index (exclusive) |
34 | 34 |
|
35 | | -若索引超出范围将抛出异常。 |
| 35 | +Throws an error if the indices are out of bounds. |
36 | 36 |
|
37 | 37 | --- |
38 | 38 |
|
39 | 39 | ### `advanced(amount: number): Data` |
40 | 40 |
|
41 | | -返回一个新的 `Data` 实例,去除前 `amount` 个字节。 |
| 41 | +Returns a new `Data` instance by removing the first `amount` bytes from the buffer. |
42 | 42 |
|
43 | 43 | --- |
44 | 44 |
|
45 | 45 | ### `replaceSubrange(startIndex, endIndex, data): void` |
46 | 46 |
|
47 | | -将当前数据中指定范围的字节替换为另一个 `Data` 实例的数据。 |
| 47 | +Replaces a specified byte range with the content of another `Data` instance. |
48 | 48 |
|
49 | 49 | --- |
50 | 50 |
|
51 | 51 | ### `compressed(algorithm: CompressionAlgorithm): Data` |
52 | 52 |
|
53 | | -使用指定的压缩算法压缩当前数据,返回压缩后的新 `Data` 实例。 |
| 53 | +Compresses the current data using the specified algorithm and returns a new `Data` instance. |
54 | 54 |
|
55 | | -如果数据为空或无法压缩将抛出异常。 |
| 55 | +Throws an error if the data is empty or cannot be compressed. |
56 | 56 |
|
57 | 57 | --- |
58 | 58 |
|
59 | 59 | ### `decompressed(algorithm: CompressionAlgorithm): Data` |
60 | 60 |
|
61 | | -使用指定的算法对当前数据进行解压,返回解压后的 `Data` 实例。 |
| 61 | +Decompresses the current data using the specified algorithm and returns a new `Data` instance. |
62 | 62 |
|
63 | | -压缩与解压时使用的算法必须一致。 |
| 63 | +Must use the same algorithm that was used to compress the data. |
64 | 64 |
|
65 | 65 | --- |
66 | 66 |
|
67 | 67 | ### `slice(start?: number, end?: number): Data` |
68 | 68 |
|
69 | | -返回数据的子集片段,形成新的 `Data` 实例。 |
| 69 | +Returns a new `Data` instance representing a slice of the original. |
70 | 70 |
|
71 | | -* `start`:起始索引(默认 0) |
72 | | -* `end`:结束索引(默认到末尾) |
| 71 | +* `start`: Starting index (default is `0`) |
| 72 | +* `end`: Ending index (default is the end of the data) |
73 | 73 |
|
74 | 74 | --- |
75 | 75 |
|
76 | 76 | ### `append(other: Data): void` |
77 | 77 |
|
78 | | -将另一个 `Data` 实例的数据追加到当前数据末尾。 |
| 78 | +Appends another `Data` instance to the end of the current one. |
79 | 79 |
|
80 | 80 | --- |
81 | 81 |
|
82 | | -### `getBytes(): Uint8Array | null`(已废弃) |
| 82 | +### `getBytes(): Uint8Array | null` (Deprecated) |
83 | 83 |
|
84 | | -请改用 `toUint8Array()`。 |
| 84 | +Use `toUint8Array()` instead. |
85 | 85 |
|
86 | 86 | --- |
87 | 87 |
|
88 | 88 | ### `toUint8Array(): Uint8Array | null` |
89 | 89 |
|
90 | | -将数据转换为 `Uint8Array`。 |
| 90 | +Converts the data into a `Uint8Array`. |
91 | 91 |
|
92 | 92 | --- |
93 | 93 |
|
94 | 94 | ### `toArrayBuffer(): ArrayBuffer` |
95 | 95 |
|
96 | | -将数据转换为 `ArrayBuffer`。 |
| 96 | +Converts the data into an `ArrayBuffer`. |
97 | 97 |
|
98 | 98 | --- |
99 | 99 |
|
100 | 100 | ### `toBase64String(): string` |
101 | 101 |
|
102 | | -将数据编码为 Base64 字符串。 |
| 102 | +Returns a Base64-encoded string representation of the data. |
103 | 103 |
|
104 | 104 | --- |
105 | 105 |
|
106 | 106 | ### `toHexString(): string` |
107 | 107 |
|
108 | | -将数据编码为十六进制字符串。 |
| 108 | +Returns a hexadecimal string representation of the data. |
109 | 109 |
|
110 | 110 | --- |
111 | 111 |
|
112 | 112 | ### `toRawString(encoding?: string): string | null` |
113 | 113 |
|
114 | | -将数据转换为字符串,支持指定编码(默认 `"utf-8"`)。 |
| 114 | +Converts the data into a string using the specified encoding (default: `"utf-8"`). |
115 | 115 |
|
116 | 116 | --- |
117 | 117 |
|
118 | 118 | ### `toIntArray(): number[]` |
119 | 119 |
|
120 | | -将数据转换为由整数表示的字节数组。 |
| 120 | +Returns an array of integers representing the bytes of the data. |
121 | 121 |
|
122 | 122 | --- |
123 | 123 |
|
124 | | -## 静态方法 |
| 124 | +## Static Methods |
125 | 125 |
|
126 | 126 | ### `Data.fromIntArray(array: number[]): Data` |
127 | 127 |
|
128 | | -从整数数组创建 `Data` 实例。 |
| 128 | +Creates a `Data` instance from an array of integers. |
129 | 129 |
|
130 | 130 | --- |
131 | 131 |
|
132 | | -### `Data.fromString(str: string, encoding?: string): Data | null`(已废弃) |
| 132 | +### `Data.fromString(str: string, encoding?: string): Data | null` (Deprecated) |
133 | 133 |
|
134 | | -请使用 `Data.fromRawString()` 代替。 |
| 134 | +Use `Data.fromRawString()` instead. |
135 | 135 |
|
136 | 136 | --- |
137 | 137 |
|
138 | 138 | ### `Data.fromRawString(str: string, encoding?: string): Data | null` |
139 | 139 |
|
140 | | -从字符串创建 `Data` 实例,支持指定编码(默认 `"utf-8"`)。 |
| 140 | +Creates a `Data` instance from a raw string, using the specified encoding (default: `"utf-8"`). |
141 | 141 |
|
142 | 142 | --- |
143 | 143 |
|
144 | 144 | ### `Data.fromFile(filePath: string): Data | null` |
145 | 145 |
|
146 | | -从本地文件路径读取数据,返回 `Data` 实例。 |
| 146 | +Reads binary data from a file path and returns a `Data` instance. |
147 | 147 |
|
148 | 148 | --- |
149 | 149 |
|
150 | 150 | ### `Data.fromArrayBuffer(buffer: ArrayBuffer): Data | null` |
151 | 151 |
|
152 | | -从 `ArrayBuffer` 创建 `Data` 实例。 |
| 152 | +Creates a `Data` instance from an `ArrayBuffer`. |
153 | 153 |
|
154 | 154 | --- |
155 | 155 |
|
156 | 156 | ### `Data.fromUint8Array(bytes: Uint8Array): Data | null` |
157 | 157 |
|
158 | | -从 `Uint8Array` 创建 `Data` 实例。 |
| 158 | +Creates a `Data` instance from a `Uint8Array`. |
159 | 159 |
|
160 | 160 | --- |
161 | 161 |
|
162 | 162 | ### `Data.fromBase64String(base64: string): Data | null` |
163 | 163 |
|
164 | | -从 Base64 编码字符串创建 `Data` 实例。 |
| 164 | +Creates a `Data` instance from a Base64-encoded string. |
165 | 165 |
|
166 | 166 | --- |
167 | 167 |
|
168 | 168 | ### `Data.fromHexString(hex: string): Data | null` |
169 | 169 |
|
170 | | -从十六进制字符串创建 `Data` 实例。 |
| 170 | +Creates a `Data` instance from a hexadecimal string. |
171 | 171 |
|
172 | 172 | --- |
173 | 173 |
|
174 | 174 | ### `Data.fromJPEG(image: UIImage, compressionQuality?: number): Data | null` |
175 | 175 |
|
176 | | -将图像转为 JPEG 格式的 `Data` 实例。 |
| 176 | +Converts a `UIImage` to JPEG format data. |
177 | 177 |
|
178 | | -* `compressionQuality`:JPEG 压缩质量,范围 0.0 ~ 1.0,默认值为 1.0(最高质量) |
| 178 | +* `compressionQuality`: Compression quality between `0.0` and `1.0` (default: `1.0`) |
179 | 179 |
|
180 | 180 | --- |
181 | 181 |
|
182 | 182 | ### `Data.fromPNG(image: UIImage): Data | null` |
183 | 183 |
|
184 | | -将图像转为 PNG 格式的 `Data` 实例。 |
| 184 | +Converts a `UIImage` to PNG format data. |
185 | 185 |
|
186 | 186 | --- |
187 | 187 |
|
188 | 188 | ### `Data.combine(dataList: Data[]): Data` |
189 | 189 |
|
190 | | -将多个 `Data` 实例合并为一个新实例。 |
| 190 | +Combines multiple `Data` instances into one. |
191 | 191 |
|
192 | | -如果列表为空或所有数据为空,则返回空数据。 |
| 192 | +Returns `null` if the list is empty or all items are empty. |
0 commit comments