MCPcopy Create free account
hub / github.com/apache/fory / BrowserBuffer

Class BrowserBuffer

javascript/packages/core/lib/platformBuffer.ts:34–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32}
33
34export class BrowserBuffer extends Uint8Array implements PlatformBuffer {
35 write(string: string, offset: number, encoding: SupportedEncodings = "utf8"): void {
36 switch (encoding) {
37 case "utf8":
38 return this.utf8Write(string, offset);
39 case "utf16le":
40 return this.ucs2Write(string, offset);
41 case "latin1":
42 return this.latin1Write(string, offset);
43 default:
44 break;
45 }
46 }
47
48 private ucs2Write(string: string, offset: number): void {
49 for (let i = 0; i < string.length; i++) {
50 const codePoint = string.charCodeAt(i);
51 this[offset++] = codePoint & 0xFF;
52 this[offset++] = (codePoint >> 8) & 0xFF;
53 }
54 }
55
56 toString(encoding: SupportedEncodings = "utf8", start = 0, end = this.length): string {
57 switch (encoding) {
58 case "utf8":
59 return this.utf8Slice(start, end);
60 case "utf16le":
61 return this.utf16LESlice(start, end);
62 case "latin1":
63 return this.latin1Slice(start, end);
64 default:
65 return "";
66 }
67 }
68
69 static alloc(size: number) {
70 return new BrowserBuffer(new Uint8Array(size));
71 }
72
73 private latin1Write(string: string, offset: number) {
74 let index = 0;
75 for (; index < string.length; index++) {
76 this[offset++] = string.charCodeAt(index);
77 }
78 }
79
80 private utf8Write(string: string, offset: number) {
81 utf8Encoder.encodeInto(string, this.subarray(offset));
82 }
83
84 private latin1Slice(start: number, end: number) {
85 if (end - start < 1) {
86 return "";
87 }
88 let str = "";
89 for (let i = start; i < end;) {
90 str += String.fromCharCode(this[i++]);
91 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected