当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


node.js Stream writable.setDefaultEncoding()用法及代码示例


writable.setDefaultEncoding()方法是Stream模块的内置应用程序编程接口,用于为Writable流设置默认编码。

用法:

writable.setDefaultEncoding( encoding ) 

参数:此方法接受单参数编码,该参数保留要用于可写流的编码。


返回值:它返回默认编码。

以下示例说明了Node.js中writable.setDefaultEncoding()方法的使用:

范例1:

// Node.js program to demonstrate the      
// writable.setDefaultEncoding() method   
const stream = require('stream'); 
  
// Creating a stream and creating  
// a write function 
const writable = new stream.Writable({ 
  
  // Write function with its  
  // parameters 
  write:function(chunk, encoding, next) { 
  
    // Converting the chunk of 
    // data to string 
    console.log(chunk.toString()); 
    next(); 
  } 
}); 
  
// Writing data 
writable.write('hi'); 
  
// Calling setDefaultEncoding method 
writable.setDefaultEncoding("utf8");

输出:

hi
Writable {
  _writableState:
   WritableState {
     objectMode:false,
     highWaterMark:16384,
     finalCalled:false,
     needDrain:false,
     ending:false,
     ended:false,
     finished:false,
     destroyed:false,
     decodeStrings:true,
     defaultEncoding:'utf8',
     length:0,
     writing:false,
     corked:0,
     sync:false,
     bufferProcessing:false,
     onwrite:[Function:bound onwrite],
     writecb:null,
     writelen:0,
     bufferedRequest:null,
     lastBufferedRequest:null,
     pendingcb:1,
     prefinished:false,
     errorEmitted:false,
     emitClose:true,
     autoDestroy:false,
     bufferedRequestCount:0,
     corkedRequestsFree:
      { next:null,
        entry:null,
        finish:[Function:bound onCorkedFinish] } },
  writable:true,
  _write:[Function:write],
  domain:null,
  _events:[Object:null prototype] {},
  _eventsCount:0,
  _maxListeners:undefined }

此处,默认值在输出中返回。

范例2:

// Node.js program to demonstrate the      
// writable.setDefaultEncoding() method   
const stream = require('stream'); 
  
// Creating a stream and creating  
// a write function 
const writable = new stream.Writable({ 
  
  // Write function with its  
  // parameters 
  write:function(chunk, encoding, next) { 
  
    // Converting the chunk of 
    // data to string 
    console.log(chunk.toString()); 
    next(); 
  } 
}); 
  
// Writing data 
writable.write('hi'); 
  
// Calling setDefaultEncoding method 
writable.setDefaultEncoding("ascii");

输出:

hi
Writable {
  _writableState:
   WritableState {
     objectMode:false,
     highWaterMark:16384,
     finalCalled:false,
     needDrain:false,     ending:false,
     ended:false,
     finished:false,
     destroyed:false,
     decodeStrings:true,
     defaultEncoding:'ascii',
     length:0,
     writing:false,
     corked:0,
     sync:false,
     bufferProcessing:false,
     onwrite:[Function:bound onwrite],
     writecb:null,
     writelen:0,
     bufferedRequest:null,
     lastBufferedRequest:null,
     pendingcb:1,
     prefinished:false,
     errorEmitted:false,
     emitClose:true,
     autoDestroy:false,
     bufferedRequestCount:0,
     corkedRequestsFree:
      { next:null,
        entry:null,
        finish:[Function:bound onCorkedFinish] } },
  writable:true,
  _write:[Function:write],
  domain:null,
  _events:[Object:null prototype] {},
  _eventsCount:0,
  _maxListeners:undefined }

此处,默认值为“ascii”。

参考: https://nodejs.org/api/stream.html#stream_writable_setdefaultencoding_encoding



相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | Stream writable.setDefaultEncoding() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。