Uint8Array.prototype.setFromBase64()
Uint8Array 例項的 setFromBase64() 方法使用來自 base64 編碼字串的位元組填充此 Uint8Array 物件,並返回一個指示讀取和寫入了多少位元組的物件。
此方法最適合填充預先分配的陣列緩衝區。如果您只想從 base64 編碼字串建立新的 Uint8Array 物件,請改用靜態方法 Uint8Array.fromBase64()。
語法
setFromBase64(string)
setFromBase64(string, options)
引數
string-
一個 base64 字串,用於編碼要寫入
Uint8Array的位元組。它與Uint8Array.fromBase64()的string引數具有相同的要求。請注意,字串僅讀取到陣列被填滿為止,因此該點之後的所有無效 base64 語法都會被忽略。 options可選-
一個自定義 base64 字串解釋過程的物件。它與
Uint8Array.fromBase64()的options引數具有相同的要求。
返回值
包含以下屬性的物件:
read-
從輸入字串中讀取的 base64 字元數。如果解碼的資料適合陣列,則這是輸入字串的長度(包括填充);否則,它是適合陣列的最後一個完整 4 字元塊的長度。塊永遠不會被拆分(因為剩餘位不能在不完全重新編碼的情況下部分“放回” base64);如果下一個塊無法放入陣列的剩餘部分,它將完全未被讀取,導致陣列的最後一兩個位元組未被寫入。
written-
寫入
Uint8Array的位元組數。永遠不會大於此Uint8Array的byteLength。
異常
SyntaxError-
如果輸入字串包含指定字母表之外的字元,或者最後一個塊不滿足
lastChunkHandling選項,則會丟擲此錯誤。 TypeError-
在以下情況之一中丟擲
- 輸入字串不是字串。
options物件不是物件或undefined。- 選項不是預期的值或
undefined。
示例
解碼 base64 字串
此示例使用預設的 alphabet 和 lastChunkHandling 選項將 base64 字串解碼到現有的 Uint8Array 中。
const uint8Array = new Uint8Array(16);
const result = uint8Array.setFromBase64("PGI+ TURO PC9i Pg==");
console.log(result); // { read: 19, written: 10 }
console.log(uint8Array);
// Uint8Array(16) [60, 98, 62, 77, 68, 78, 60, 47, 98, 62, 0, 0, 0, 0, 0, 0]
將大字串解碼到小陣列中
如果字串包含的資料超過了陣列的容量,則方法只會寫入陣列能容納的位元組數,而不會丟棄任何位。
const uint8Array = new Uint8Array(8);
const result = uint8Array.setFromBase64("PGI+ TURO PC9i Pg==");
console.log(result); // { read: 9, written: 6 }
console.log(uint8Array);
// Uint8Array(8) [60, 98, 62, 77, 68, 78, 0, 0]
請注意,陣列的最後兩個位元組未被寫入。為了解碼這兩個位元組,我們需要讀取至少三個額外的 base64 字元,這代表 18 位。這無法放入陣列剩餘的兩個位元組中,因此我們只能寫入 2 個塊,即 6 個位元組。
在特定偏移量設定資料
setFromBase64() 方法始終從 Uint8Array 的開頭開始寫入。如果您想寫入陣列的中間部分,可以改用 TypedArray.prototype.subarray() 進行寫入。
const uint8Array = new Uint8Array(16);
// Start writing at offset 2
const result = uint8Array.subarray(2).setFromBase64("PGI+ TURO PC9i Pg==");
console.log(result); // { read: 19, written: 10 }
console.log(uint8Array);
// Uint8Array(16) [0, 0, 60, 98, 62, 77, 68, 78, 60, 47, 98, 62, 0, 0, 0, 0]
流式解碼
此示例改編自 原始提案。它使用 stream 選項模仿了 TextDecoder API。請注意使用 lastChunkHandling: "stop-before-partial" 來處理不完整的塊。
class Base64Decoder {
#extra = "";
decode(chunk = "", options = {}) {
const opts = { ...options };
// match TextEncoder API
if (opts.stream) {
opts.lastChunkHandling = "stop-before-partial";
}
chunk = this.#extra + chunk;
this.#extra = "";
// For simplicity, allocate new memory every time
// the calculation below is guaranteed to be enough,
// but may be too much if there is whitespace
// if you're really concerned about memory, a TextDecoder style API is a bad choice
let buffer = new Uint8Array(Math.ceil((chunk.length * 3) / 4));
const { read, written } = buffer.setFromBase64(chunk, opts);
buffer = buffer.subarray(0, written);
this.#extra = chunk.slice(read);
return buffer;
}
}
const decoder = new Base64Decoder();
console.log(decoder.decode("SG Vsb ", { stream: true }));
// Uint8Array(3) [72, 101, 108]
console.log(decoder.decode("G8gV29ybGR ", { stream: true }));
// Uint8Array(6) [108, 111, 32, 87, 111, 114]
console.log(decoder.decode(""));
// Uint8Array(2) [108, 100]
規範
| 規範 |
|---|
| Uint8Array 與 base64 的相互轉換 # sec-uint8array.prototype.setfrombase64 |
瀏覽器相容性
載入中…