|
| 1 | +// 创建 Buffer |
| 2 | +function from(data, encoding = "utf8") { |
| 3 | + try { |
| 4 | + return Buffer.from(data, encoding); |
| 5 | + } catch (error) { |
| 6 | + throw new Error(`创建Buffer失败: ${error.message}`); |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +// 转换为字符串 |
| 11 | +function toString(buffer, encoding = "utf8", start = 0, end = buffer.length) { |
| 12 | + try { |
| 13 | + return buffer.toString(encoding, start, end); |
| 14 | + } catch (error) { |
| 15 | + throw new Error(`转换字符串失败: ${error.message}`); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// 写入数据 |
| 20 | +function write( |
| 21 | + buffer, |
| 22 | + string, |
| 23 | + offset = 0, |
| 24 | + length = buffer.length, |
| 25 | + encoding = "utf8" |
| 26 | +) { |
| 27 | + try { |
| 28 | + return buffer.write(string, offset, length, encoding); |
| 29 | + } catch (error) { |
| 30 | + throw new Error(`写入数据失败: ${error.message}`); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// 填充数据 |
| 35 | +function fill( |
| 36 | + buffer, |
| 37 | + value, |
| 38 | + offset = 0, |
| 39 | + end = buffer.length, |
| 40 | + encoding = "utf8" |
| 41 | +) { |
| 42 | + try { |
| 43 | + return buffer.fill(value, offset, end, encoding); |
| 44 | + } catch (error) { |
| 45 | + throw new Error(`填充数据失败: ${error.message}`); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// 复制数据 |
| 50 | +function copy( |
| 51 | + source, |
| 52 | + target, |
| 53 | + targetStart = 0, |
| 54 | + sourceStart = 0, |
| 55 | + sourceEnd = source.length |
| 56 | +) { |
| 57 | + try { |
| 58 | + return source.copy(target, targetStart, sourceStart, sourceEnd); |
| 59 | + } catch (error) { |
| 60 | + throw new Error(`复制数据失败: ${error.message}`); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// 比较数据 |
| 65 | +function compare(buf1, buf2) { |
| 66 | + try { |
| 67 | + return Buffer.compare(buf1, buf2); |
| 68 | + } catch (error) { |
| 69 | + throw new Error(`比较数据失败: ${error.message}`); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// 连接 Buffer |
| 74 | +function concat(buffers, totalLength) { |
| 75 | + try { |
| 76 | + return Buffer.concat(buffers, totalLength); |
| 77 | + } catch (error) { |
| 78 | + throw new Error(`连接Buffer失败: ${error.message}`); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// 查找数据 |
| 83 | +function indexOf(buffer, value, byteOffset = 0, encoding = "utf8") { |
| 84 | + try { |
| 85 | + return buffer.indexOf(value, byteOffset, encoding); |
| 86 | + } catch (error) { |
| 87 | + throw new Error(`查找数据失败: ${error.message}`); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// 切片数据 |
| 92 | +function slice(buffer, start = 0, end = buffer.length) { |
| 93 | + try { |
| 94 | + return buffer.slice(start, end); |
| 95 | + } catch (error) { |
| 96 | + throw new Error(`切片数据失败: ${error.message}`); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// 交换字节序 |
| 101 | +function swap(buffer, size) { |
| 102 | + try { |
| 103 | + switch (size) { |
| 104 | + case 16: |
| 105 | + return buffer.swap16(); |
| 106 | + case 32: |
| 107 | + return buffer.swap32(); |
| 108 | + case 64: |
| 109 | + return buffer.swap64(); |
| 110 | + default: |
| 111 | + throw new Error("不支持的字节大小"); |
| 112 | + } |
| 113 | + } catch (error) { |
| 114 | + throw new Error(`交换字节序失败: ${error.message}`); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = { |
| 119 | + from, |
| 120 | + toString, |
| 121 | + write, |
| 122 | + fill, |
| 123 | + copy, |
| 124 | + compare, |
| 125 | + concat, |
| 126 | + indexOf, |
| 127 | + slice, |
| 128 | + swap, |
| 129 | + // 编码类型 |
| 130 | + encodings: [ |
| 131 | + "utf8", |
| 132 | + "utf16le", |
| 133 | + "latin1", |
| 134 | + "base64", |
| 135 | + "hex", |
| 136 | + "ascii", |
| 137 | + "binary", |
| 138 | + "ucs2", |
| 139 | + ], |
| 140 | +}; |
0 commit comments