Object built-in object

ObjectBufferedStream

Cache read object

The BufferedStream object is a buffered stream object used for binary stream reading. It can cache its underlying stream and provide text reading capabilities. When using the BufferedStream object, you only need to pass in the stream object to be processed as a construction parameter. Creation method:

1
var reader = new io.BufferedStream(stream);

BufferedStream inherits fromStreamobject, havingStreamAll methods and properties of the object. Among them, the stream attribute is used to query the stream object when creating the cache object. The BufferedStream object also supports the EOL attribute for querying and setting the end-of-line identifier (default, posix:\"\n\"; windows:\"\r\n\") and the charset attribute for querying and setting the current object processing Character set for text, default is utf-8.

When the BufferedStream object reads stream data, it adopts a block method to first read the data into the buffer and then obtain the data from the buffer. This can effectively reduce the number of network interactions when reading stream data and improve the reading efficiency. Get efficiency.

The BufferedStream object also provides the write method to write the given data to the stream, and when the underlying stream object is blocked in writing, wait for it to accept the data before proceeding to the next step. The Flush method writes the file buffer contents to a physical device. The close method closes the current stream object. The specific implementation of some methods can be implemented in subclasses.

When using the BufferedStream object, you need to be careful not to mix it with other underlying stream objects that are already in use, otherwise it may cause repeated reading of data or reading errors.

Here is a sample code that uses a BufferedStream object to read the contents of a file:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var fs = require('fs'); var io = require('io'); var filename = "test.txt"; // open file var file = fs.openFile(filename); // create BufferedStream object var reader = new io.BufferedStream(file); // read file content var lines = reader.readLines(); for (var i = 0; i < lines.length; i++) console.log(lines[i]); // close file file.close();

inheritance relationship

Constructor

BufferedStream

BufferedStream constructor

1
new BufferedStream(Stream stm);

Call parameters:

  • stm:Stream, the binary underlying stream object of BufferedStream

member properties

stream

Stream, Query the stream object when creating the cache object

1
readonly Stream BufferedStream.stream;

charset

String, query and set the character set of the current object when processing text, the default is utf-8

1
String BufferedStream.charset;

EOL

String, query and set the line ending identifier. By default, posix:\"\n\"; windows:\"\r\n\"

1
String BufferedStream.EOL;

fd

Integer, queryStreamThe corresponding file descriptor value, implemented by subclasses

1
readonly Integer BufferedStream.fd;

member function

readText

Read text with specified characters

1
String BufferedStream.readText(Integer size) async;

Call parameters:

  • size: Integer, specifies the number of text characters to read, whichever is UTF8 or the specified number of encoding bytes.

Return results:

  • String, returns the read text string. If there is no data to read or the connection is interrupted, null is returned.

readLine

Read a line of text. The end-of-line identifier is based on the setting of the EOL attribute. By default, posix:\"\n\"; windows:\"\r\n\"

1
String BufferedStream.readLine(Integer maxlen = -1) async;

Call parameters:

  • maxlen: Integer, specifies the maximum string read this time, based on the number of utf8 encoding bytes. There is no limit on the number of characters by default.

Return results:

  • String, returns the read text string. If there is no data to read or the connection is interrupted, null is returned.

readLines

Read a group of text lines in array mode. The end-of-line identifier is based on the setting of the EOL attribute. By default, posix:\"\n\"; windows:\"\r\n\"

1
Array BufferedStream.readLines(Integer maxlines = -1);

Call parameters:

  • maxlines: Integer, specifies the maximum number of lines to be read this time. By default, all text lines are read.

Return results:

  • Array, returns an array of read text lines. If there is no data to read or the connection is interrupted, the array will be empty.

readUntil

Read a text string, ending with the specified bytes

1 2
String BufferedStream.readUntil(String mk, Integer maxlen = -1) async;

Call parameters:

  • mk: String, the string specifying the end
  • maxlen: Integer, specifies the maximum string read this time, based on the number of utf8 encoding bytes. There is no limit on the number of characters by default.

Return results:

  • String, returns the read text string. If there is no data to read or the connection is interrupted, null is returned.

writeText

Write a string

1
BufferedStream.writeText(String txt) async;

Call parameters:

  • txt: String, specifies the string to be written

writeLine

Write a string and write a newline character

1
BufferedStream.writeLine(String txt) async;

Call parameters:

  • txt: String, specifies the string to be written

read

Read data of specified size from the stream

1
Buffer BufferedStream.read(Integer bytes = -1) async;

Call parameters:

  • bytes: Integer, specifies the amount of data to be read. The default is to read data blocks of random size. The size of the data read depends on the device.

Return results:

  • Buffer, returns the data read from the stream. If there is no data to read or the connection is interrupted, null is returned.

write

Write the given data to the stream

1
BufferedStream.write(Buffer data) async;

Call parameters:

  • data:Buffer, given the data to be written

flush

Write file buffer contents to physical device

1
BufferedStream.flush() async;

close

Close the current stream object

1
BufferedStream.close() async;

copyTo

Copy stream data to target stream

1 2
Long BufferedStream.copyTo(Stream stm, Long bytes = -1) async;

Call parameters:

  • stm:Stream, target stream object
  • bytes: Long, number of bytes copied

Return results:

  • Long, returns the number of bytes copied

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String BufferedStream.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value BufferedStream.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable