Node.js StringDecoder 和 Buffer.toString([encoding]) 的区别

举报
福州司马懿 发表于 2021/11/19 05:35:10 2021/11/19
【摘要】 Class: new StringDecoder([encoding]) stringDecoder.end([buffer])stringDecoder.write(buffer) st...

Class: new StringDecoder([encoding])

  • stringDecoder.end([buffer])
  • stringDecoder.write(buffer)

stringDecoder.end([buffer])

Added in: v0.9.3
buffer A Buffer containing the bytes to decode.
Returns any remaining input stored in the internal buffer as a string. Bytes representing incomplete UTF-8 and UTF-16 characters will be replaced with substitution characters appropriate for the character encoding.

If the buffer argument is provided, one final call to stringDecoder.write() is performed before returning the remaining input.

参数buffer的类型是,是一个待解码的缓冲区。
返回任何在内部缓冲区存储的剩余输入字符。剩余的不完整的UTF-8或UTF-16编码的字节将会被替换为合适的字符串编码。
如果有提供buffer参数,那么会调用一次stringDecoder.write(),然后返回剩余的输入字符。

stringDecoder.write(buffer)

Added in: v0.1.99
buffer A Buffer containing the bytes to decode.
Returns a decoded string, ensuring that any incomplete multibyte characters at the end of the Buffer are omitted from the returned string and stored in an internal buffer for the next call to stringDecoder.write() or stringDecoder.end().

参数buffer的类型是,是一个待解码的缓冲区。
返回一个解码后的字符串,确保任何在Buffer末尾的不完整的多字节字符都将会从返回的字符串中被省略,并且存储在内部缓冲区中直到下一个stringDecoder.write()或者stringDecoder.end()被调用。

const buf1 = Buffer.from('西山居');
//输出 <Buffer e8 a5 bf e5 b1 b1 e5 b1 85>

const buf2 = Buffer.from([0, 0, 0xe8, 0xa5, 0xbf, 0xe5, 0xb1, 0xb1, 0xe5, 0xb1, 0x85]);

const buf3 = Buffer.from([0xe8, 0xa5, 0xbf, 0xe5, 0xb1, 0xb1, 0xe5, 0xb1, 0x85, 0, 0 ]);

const buf4 = Buffer.from([0xe8, 0xa5, 0xbf, 0xe5, 0, 0, 0xb1, 0xb1, 0xe5, 0xb1, 0x85]);

buf1.toString();
//返回 '西山居'
buf2.toString();
//返回 '\u0000\u0000西山居'
buf3.toString();
//返回 '西山居\u0000\u0000'
buf4.toString();
//返回 '西�\u0000\u0000��'

const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf-8');
decoder.write(buf1);
//返回 '西山居'
decoder.write(buf2);
//返回 '\u0000\u0000西山居'
decoder.write(buf3);
//返回 '西山居\u0000\u0000'
decoder.write(buf4);
//返回 '西�\u0000\u0000��'

decoder.end(buf1);
//返回 '西山居'
decoder.end(buf2);
//返回 '\u0000\u0000西山居'
decoder.end(buf3);
//返回 '西山居\u0000\u0000'
decoder.end(buf4);
//返回 '西�\u0000\u0000��'
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

咋一看,StringDecoder和Buffer.toString([encoding])并没有什么区别。但是真正的区别在下面:

When a Buffer instance is written to the StringDecoder instance, an internal buffer is used to ensure that the decoded string does not contain any incomplete multibyte characters. These are held in the buffer until the next call to stringDecoder.write() or until stringDecoder.end() is called.

当一个Buffer实例被写到StringDecoder实例的时候,一个内部的buffer将被用来确保待解码的字符串不会包含任何不完整的多字节字符。它们会被保留在buffer中直到下一个stringDecoder.write()被调用或者stringDecoder.end()被调用。

const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf-8');

decoder.write(Buffer.from([0xe8]));
//返回 ''
decoder.write(Buffer.from([0xa5]));
//返回 ''
decoder.end(Buffer.from([0xbf]));
//返回 '西'
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/52612442

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。