Analysis of java.io input-output stream filter stream buffer stream and data stream _java

Source: Internet
Author: User

java.io uses design patterns such as adapter pattern decoration mode to solve the socket and input-output problems of character streams.

A byte stream can only be processed at a time, in order to more easily manipulate the data, it adds a socket flow.

Problem Introduction:

Why is the buffer stream more efficient than ordinary file-byte throttling?

An operation without buffering, which writes a byte every time a byte is read.

Because disk-related IO operations are much slower than memory operations, the flow without buffering is inefficient.

A buffered stream that can be read many bytes at a time, but not written to disk, but first put into memory.

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

This is the difference between the two.

Approximate process:

No buffer, then every read once, will send an IO operation;

There is a buffer, the first time read, will read x bytes into the buffer, and then subsequent read will be read from the cache, when read to the end of the buffer, will read again x bytes into the buffer.

Obviously, the second approach, which reduces IO operations, is more efficient, and the downside is that there is more memory footprint.

The Java input-output stream is designed in a multi-layer package

The lowest inputstream and OutputStream are based on byte throttling, there is no caching mechanism, generally used bufferinputstream and bufferoutputstream for encapsulation.

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

Bufferoutputstream.write (BUF) will output the contents of the BUF to the output stream, but remember to flush;

There is a more useful printstream and PrintWriter similar can be automatically refreshed just for the byte stream.

A byte stream is typically used to transfer binary files, and the character streams are often packaged with reader.

The most commonly used have Bufferinputstreamreader and Printwrinter, Bufferinputstreamreader ReadLine method is very practical to encounter \r\d will automatically flush.

Printwrinter the Println method can automatically refresh without flush as long as the Refresh property is set to true in the constructor.

FilterInputStream and Filteroutputstream: Filter flow, buffer stream and data stream are inherited from this.

For a buffer stream, the data is actually sent to the output stream only when it is full, but the flush () method can be used artificially to send data out of the buffer that has not yet been filled, and it is not possible to determine how the file is encoded and difficult to apply on the network.

The most commonly used is the data stream, which allows the sender and receiver to follow the same code.

DataInputStream and DataOutputStream: You can accept a row of data, you can encode it, it can be a socket stream, you can socket file byte stream and network byte throttling, read and write in the same order, otherwise read will appear abnormal.

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

The above is a small set to introduce the java.io input and output stream filter stream buffer flow and data flow, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.