Filter data flow in Java language

Keep creating and accelerate growth! This is the 28th day that I have participated in the "Nuggets Daily New Plan·October Update Challenge". Click to view event details.

Filter data stream

In order to solve the problems of speed and data format differences between different data streams and improve the efficiency of input/output operations (especially when programs require a large number of input and output operations), Java thoughtfully provides filter streams. On the basis of the existing data flow, the filtered data flow is related to the existing data flow. The filtered flow mainly includes filtering the input data flow and filtering the output flow. Among them, filtering the input data stream reads data (in the form of bytes or characters) from the input data stream, processes the data, and then provides data in a specific format to the memory. Filtering the output data stream reads data in a specific format from the memory, processes it, and then provides byte data to the output data stream.

1b624b370e85e042679e3989f142dc7.jpg

  • Filter flows mainly include buffer data flows, data data flows, pipeline data flows, object data flows, etc.

Buffer data stream

The buffer data stream is equivalent to adding a buffer to the data stream. When byte (character) data arrives in the buffer data stream, it enters the buffer data stream in blocks. Other data streams start from this buffer data stream in bytes (characters). ) The buffer data stream for reading data processing bytes includes the buffer input data stream (BufferedInputStream) and the buffer output data stream (BufferedOutputStream). In addition, you can set the buffer size. Controls the speed of input and output operations.

Input data stream (BufferedInputStream)

The data member buf of the BufferedInputStream class is a bit array, with a default value of 2048 bytes. When reading the data source, BufferedInputStream will try to fill buf as much as possible. When using the read() method, the data in buf is actually read first, rather than directly reading the data source. When there is insufficient data in buf, BufferedInputStream will implement the read() method of the given InputStream object, that is, extract data from the specified device. ```java // Some field descriptions in the BufferedInputStream class protected byte[]:buf is an internal buffer array that stores data. The default size of the array is 2048 bytes.

protected int: Compute an index one greater than the index of the last significant byte in the buffer

protected int: After marklink calls the mark method, the maximum amount of advance reading allowed before the subsequent call to the reset method fails.

··· ```

Output data stream (BufferedOutputStream)

The data member buf of BufferedOutputStream is a bit array, but its default number of bytes is 512. When using the write() method to write data, the data will actually be written to buf first, and will be implemented when buf is full. Use the write() method of a certain OutputStream object to write buf data to the destination instead of writing to the destination every time.

Constructor method of BufferedOutputStream class
  1. BufferedOutputStream(OutputStream outputStream[,int s]):Creates a spoof buffered output stream to write data of the specified buffer size to the specified output stream. If no buffer size is specified, the default buffer size (1024 bytes) is used.
  2. void flush(): Refresh this buffered output stream (compared to OutputStream, this method is only available in the BufferedOutputStream class)
  • Note: When turning off a buffered output stream, you should force the output stream to buffer the data in the output stream. Otherwise, there is still data in the buffer that has not been completely read.

  • In general. The buffered input stream buffer size, the buffered output stream buffer size, and the size of the specified array are equal. Each time the valid data read from the buffered input stream is saved in data b, and the length len of the valid data is recorded, then the first len ​​valid bytes of data in the array are written into the buffered output stream.

1d1440c3354b58d9ea2a6f746b5c830.jpgSince there is no guarantee that the actual length of the file is a multiple of the buffer size of the buffered input stream, invalid data will not be written to the buffered data stream the last time the buffered input stream is written.

Guess you like

Origin blog.csdn.net/y943711797/article/details/132974505