A brief analysis of Java.IO input and output stream filter streams buffer and data

  • 2020-05-10 18:11:07
  • OfStack

java.io USES design patterns such as adapter pattern decoration pattern to solve character stream sockets and I/o problems.

A byte stream can only process one byte at a time, so a socket stream is added for easier data manipulation.

Problem introduction:

Why are buffer flows more efficient than normal file byte streams?

For unbuffered operations, one byte is written for every byte read.

Since IO operations involving disk are much slower than memory operations, flows without buffering are inefficient.

A buffered stream can read many bytes at a time, but is not written to disk, just stored in memory.

When the buffer size of the time 1 sex write to the disk, this way can reduce the number of disk operations, speed will improve a lot!

That's the difference.

General process:

If there is no cache, the IO operation will be sent once for every read1.

There is a cache. The first time read is read, x bytes will be put into the cache. Then the subsequent read will be read from the cache, and when read reaches the end of the cache, x bytes will be read again and put into the cache.

Obviously, the second approach, which is more efficient with fewer IO operations, has the disadvantage of taking up a lot of memory.

The java I/o stream is designed in a multi-layer package

The lowest level of InputStream and OutputStream are based on byte stream and have no caching mechanism. Generally, BufferInputStream and BufferOutputStream are used after encapsulation.

The read method of BufferInputStream is thread-blocking, and BufferInputStream.read (buf) reads all of the input stream into buf before returning.

BufferOutputStream. write (buf); Output the contents of buf to the output stream, but remember to flush;

There is also a nice PrintStream and PrintWriter which are similar to each other in that they refresh automatically for byte streams.

Byte stream 1 is often used to transfer binary files, for example, and character streams are often wrapped with reader.

The most commonly used are BufferInputStreamReader and PrintWrinter, and BufferInputStreamReader's readline method is very practical. When encountered with \r\d, it will automatically flush.

As long as PrintWrinter sets the refresh property to true in the constructor, its println method can automatically refresh without flush.

FilterInputStream and FilterOutputStream: filter streams, buffer streams and data streams are all inherited here.

For the buffer stream, the data is only actually sent to the output stream when the buffer is full, but you can use the flush () method to manually send the data out of the unfilled buffer. Unable to determine the file encoding method, difficult to apply on the network.

The most commonly used is that the data stream allows the sender and receiver to process it in the same encoding.

DataInputStream and DataOutputStream: can accept 1 line of data, can encode it, can also be a socket stream, can socket file byte stream and network byte stream, read and write order must be 1, otherwise read will appear abnormal.

DataInputStream is used to decorate other input streams by "allowing applications to read basic Java data types from the underlying input stream in a machine-independent manner". Applications can use DataOutputStream(data output stream) to write data read by DataInputStream(data input stream).


Related articles: