📜  Node.js stringDecoder.end() 方法

📅  最后修改于: 2022-05-13 01:56:22.593000             🧑  作者: Mango

Node.js stringDecoder.end() 方法

stringDecoder.end() 方法用于将存储在内部缓冲区中的所有剩余输入作为字符串返回。此方法可确保将任何不完整的 UTF-8 和 UTF-16字符替换为适合字符编码的替换字符。

如果提供了可选的缓冲区参数,则在返回剩余缓冲区输入之前,使用数据调用一次 stringDecoder.write() 方法。

句法:

stringDecoder.end( [buffer] )

参数:此函数接受如上所述和如下所述的单个参数:

  • buffer:它是一个 Buffer、TypedArray 或 DataView,其中包含必须解码的字节。它是一个可选参数。

返回值:它将存储在缓冲区中的剩余输入作为字符串返回。
下面的程序说明了 Node.js 中的stringDecoder.end()方法:

示例 1:

Javascript
// Import the string_decoder module
// and get the StringDecoder class 
// using object destructuring
const { StringDecoder } = require("string_decoder");
  
const decoder = new StringDecoder("utf-8");
  
// Using the end() method
const text_one = Buffer.from("GeeksforGeeks", "utf-8");
let decoded_text = decoder.end(text_one);
console.log("Decoded Text:", decoded_text);
  
// The Euro Symbol is denoted using
// the bytes [0xE2, 0x82, 0xAC]
  
console.log("Decoding the Euro Symbol:");
  
// Decoding the euro symbol
// Using the write() method to
// write the first two parts
console.log(decoder.write(Buffer.from([0xE2])));
console.log(decoder.write(Buffer.from([0x82])));
  
// Finishing the symbol using the end() method
// with that gives an additional call to write()
// using the last part of the buffer
console.log(decoder.end(Buffer.from([0xAC])));


Javascript
// Import the string_decoder module
// and get the StringDecoder class 
// using object destructuring
const { StringDecoder } = require("string_decoder");
const decoder = new StringDecoder("utf-8");
// The Cent Symbol is denoted using
// Buffer.from([0xc2, 0xa2])
  
// Decoding the complete cent symbol from buffer
let cent_symbol = Buffer.from([0xc2, 0xa2]);
let cent_symbol_out = decoder.end(cent_symbol);
console.log("Complete Cent Symbol:", cent_symbol_out);
  
// Decoding incomplete cent symbol using
// Buffer.write() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.write(cent_symbol);
console.log("Cent Symbol using write():",
                       cent_symbol_out);
  
// Decoding incomplete cent symbol
// using Buffer.end() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.end(cent_symbol);
console.log("Cent Symbol using end():",
                     cent_symbol_out);


输出:

Decoded Text: GeeksforGeeks
Decoding the Euro Symbol:


€

示例 2:

Javascript

// Import the string_decoder module
// and get the StringDecoder class 
// using object destructuring
const { StringDecoder } = require("string_decoder");
const decoder = new StringDecoder("utf-8");
// The Cent Symbol is denoted using
// Buffer.from([0xc2, 0xa2])
  
// Decoding the complete cent symbol from buffer
let cent_symbol = Buffer.from([0xc2, 0xa2]);
let cent_symbol_out = decoder.end(cent_symbol);
console.log("Complete Cent Symbol:", cent_symbol_out);
  
// Decoding incomplete cent symbol using
// Buffer.write() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.write(cent_symbol);
console.log("Cent Symbol using write():",
                       cent_symbol_out);
  
// Decoding incomplete cent symbol
// using Buffer.end() method
cent_symbol = Buffer.from([0xc2]);
cent_symbol_out = decoder.end(cent_symbol);
console.log("Cent Symbol using end():",
                     cent_symbol_out);

输出:

Complete Cent Symbol: ¢
Cent Symbol using write():
Cent Symbol using end(): ??

参考: https://nodejs.org/api/string_decoder.html#string_decoder_stringdecoder_end_buffer