How the "Java" buffered stream improves performance

Source: Internet
Author: User

Objective

This article is written in rough, only for the work of the gap in the essay.


The traditional Java IO is based on blocking, and his working state is "read/write, wait, read/write, wait ...".

The buffer stream has two types of bytes and characters, the principle is almost not readable, this is a byte buffer path to discuss.


One, buffered input stream

Bufferedinputstream extends FileInputStream, the design idea of buffer flow is based on the decorator design pattern, which needs to pass in a node stream when the buffer stream is constructed.

Why can buffered input streams improve efficiency?

A simple word is space change time. The Read method is a byte-by-byte return data, but he actually reads the BUF byte at a time, and then returns it to the Read method from the BUF, if it is taken out, read once, and so forth. In other words, with the buffering technique, the number of times the buffered stream calls the local IO becomes file.length/buf.length, and the default buf is 8,192 bytes, so the Read method has a performance improvement of about 8,192 times times. Of course, this is not a cost to spend, the cost is to consume more than 8,191 bytes of memory.


Read (arr[]) is finally using the Read (arr[], int, int) methods, which are presented here together.

The Read (Arr) method, which uses the buffering technique, if the arr.length>= Buf.length, instead of using BUF, the data on the disk will be populated directly into ARR, so that the best performance can be guaranteed, but the risk introduced is that the size of ARR is not well controlled, causing memory tension; if arr.length< Buf.length, then still read the entire buf, and then from BUF the data system.arraycopy to arr, without reading the disk again to buf, so repeat, in fact, eventually the disk interaction is not bufferedinputstream, but through the constructor injected its His node stream native read (arr[]) to achieve, code more, and csdn pictures of convulsions, no longer I posted out, self-viewing source bar!

Second, buffered output stream

Bufferedoutputstream extends FileOutputStream.

The idea of the read (int) method is still space-changing, using the buffering technique, each time it is written buf, until BUF is full, the data will be brushed to disk (the process of brushing the disk is implemented by calling the write (arr[] of the node stream passed in the constructor method), rather than directly calling native Write (int), without buffering, each byte consumes the local IO resource, writes a byte, uses an IO resource, and then blocks and then writes, so repeat.


Wirte (arr[]) eventually calls write (arr[], int, int), and the idea is to brush the data in arr to disk. Using buffer stream Technology, if arr.length>=buf.length, the data in ARR will be directly brushed; if arr.length< Buf.length, the data is written to buf until the BUF is full to brush the disk, and the process of brushing the disk is done by calling the write (arr[], int, int) of the incoming node stream in the constructor method.

Iii. Summary

Buffer stream can improve performance, mainly using the BUF space in memory to achieve, reduce the direct consumption of the system IO resources, thereby improving performance.

How the "Java" buffered stream improves performance

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.