Home > Web Front-end > JS Tutorial > Instructions for using the buffer.write method in node.js_node.js

Instructions for using the buffer.write method in node.js_node.js

WBOY
Release: 2016-05-16 16:27:47
Original
1575 people have browsed it

Method description:

Write the string to the offset of the buffer using the specified encoding.

Returns how many octets were written.

If the Buffer does not have enough space to fit the entire string, then only the part of the string will be written.

Grammar:

Copy code The code is as follows:

buffer.write(string, [offset], [length], [encoding])

Receive parameters:

string String, the data written to the buffer.
offet number, optional, default 0. The location where the data is written to the buffer.
length Number, optional, default: buffer.length – offset, the length of the data to be written
encoding String, the encoding format to be used, optional, the default is "utf8".

Example:

Copy code The code is as follows:

buf = new Buffer(256);

len = buf.write('u00bd u00bc = u00be', 0);

console.log(len " bytes: " buf.toString('utf8', 0, len));

Source code:

Copy code The code is as follows:

Buffer.prototype.write = function(string, offset, length, encoding) {
  // allow write(string, encoding)
  if (util.isString(offset) && util.isUndefined(length)) {
    encoding = offset;
    offset = 0;
  // allow write(string, offset[, length], encoding)
  } else if (isFinite(offset)) {
    offset = ~~offset;
    if (isFinite(length)) {
      length = ~~length;
    } else {
      encoding = length;
      length = undefined;
    }
  // XXX legacy write(string, encoding, offset, length) - remove in v0.13
  } else {
    if (!writeWarned) {
      if (process.throwDeprecation)
        throw new Error(writeMsg);
      else if (process.traceDeprecation)
        console.trace(writeMsg);
      else
        console.error(writeMsg);
      writeWarned = true;
    }
    var swap = encoding;
    encoding = offset;
    offset = ~~length;
    length = swap;
  }
  var remaining = this.length - offset;
  if (util.isUndefined(length) || length > remaining)
    length = remaining;
  encoding = !!encoding ? (encoding '').toLowerCase() : 'utf8';
  if (string.length > 0 && (length < 0 || offset < 0))
    throw new RangeError('attempt to write beyond buffer bounds');
  var ret;
  switch (encoding) {
    case 'hex':
      ret = this.hexWrite(string, offset, length);
      break;
    case 'utf8':
    case 'utf-8':
      ret = this.utf8Write(string, offset, length);
      break;
    case 'ascii':
      ret = this.asciiWrite(string, offset, length);
      break;
    case 'binary':
      ret = this.binaryWrite(string, offset, length);
      break;
    case 'base64':
      // Warning: maxLength not taken into account in base64Write
      ret = this.base64Write(string, offset, length);
      break;
    case 'ucs2':
    case 'ucs-2':
    case 'utf16le':
    case 'utf-16le':
      ret = this.ucs2Write(string, offset, length);
      break;
    default:
      throw new TypeError('Unknown encoding: ' encoding);
  }
  return ret;
};
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template