Java io FilterInputStream BufferedInputStream

Source: Internet
Author: User

1. Structure of FilterInputStream

 

/**
* A FilterInputStream contains some other input stream, which it
* Uses as its basic source of data, possibly transforming the data along
* Way or providing additional functionality. The class
* FilterInputStream itself simply overrides all methods
* InputStream with versions that pass all requests to
* Contained input stream. Subclasses of FilterInputStream may
* Further override some of these methods and may also provide additional
* Methods and fields.
*
* @ Author Jonathan Payne
* @ Version % I %, % G %
* @ Since JDK1.0
*/
Public class FilterInputStream extends InputStream
FilterInputStream inherits from InputStream and can use other input streams as the data source.

FilterInputStream simply overwrites the method in InputStream. Let's take a look at BufferedInputStream.

2. BufferedInputStream Class Structure

/**
* A BufferedInputStream adds functionality to another input
* Stream-namely, the ability to buffer the input and to support
* Mark and reset methods. When
* BufferedInputStream is created, an internal buffer array is
* Created. As bytes from the stream are read or skipped, the internal buffer is
* Refilled as necessary from the contained input stream, bytes at a time.
* The mark operation remembers a point in the input stream and
* Reset operation causes all the bytes read since the most recent
* Mark operation to be reread before new bytes are taken from
* Contained input stream.
*
*/
Public class BufferedInputStream extends FilterInputStream
BufferedInputStream adds some functions for other input streams, and maintains a buffer array internally.

Create BufferedInputStream

Private static int defaultBufferSize = 8192;
Protected volatile byte buf [];

Public BufferedInputStream (InputStream in ){
This (in, defabuffbuffersize );
}

Public BufferedInputStream (InputStream in, int size ){
Super (in );
If (size <= 0 ){
Throw new IllegalArgumentException ("Buffer size <= 0 ");
}
Buf = new byte [size];
}
Initialize a byte array and read data of the specified length from the input stream to the array at a time. When the program needs to read bytes,

Read from the byte array directly.

3. BufferedInputStream Buffer

Through the above constructor, we can see that the byte array has the buffer function. Each read data is read from the buffer array.

Protected int count; // total amount of valid data in the Current Buffer
Protected int pos; // indicates the current read location, that is, the current subscript of the byte array. The next read will be from this location.
Protected int markpos =-1; // The Position of the tag
Protected int marklimit; // maximum mark length, that is, the maximum length from the mark position to the current pos.

Public synchronized int read () throws IOException {
If (pos> = count ){
Fill ();
If (pos> = count)
Return-1;
}
Return getbucket open () [pos ++] & 0xff;
}
The read method shows that when all the data in the buffer zone is read, the buffer zone is filled. In this way, data is read from the buffer.

Read, not from the real data source.

3.1 getbucket open () [pos ++] & 0xff

We know that when the file ends, it is represented by-1. If there is no negative number in the response stream,-1 is returned as OK. However, byte or int itself contains the value-1, so-1 cannot be returned directly.

In java, the byte size is 8 bits, and the int size is 32 bits; the java binary uses the complement form.

0xff is an int type, and the binary value is 0000 0000 0000 0000 0000 0000 1111 1111

The preceding and the operations actually read byte are first forcibly converted to int, for example, byte-1 (the highest bit indicates the symbol bit, and the negative number is expressed as 1111 1111 in the form of a complement code)

Binary 1111 1111 1111 1111 1111 1111 1111 1111 after int Conversion

After & 0xff, the value is 0 in the upper position. The final returned result is 0000 0000 0000 0000 0000 0000 1111, And the int value is 1111.

Its-128 ~ -1 is converted to int 128 ~ A positive number of 256. In this way, we can use-1 to indicate that the file has been read.

But the key is that the value of the data has changed and whether the original bytes can be obtained when the data to be read is actually used.

In the example above, when a 256 value is returned for reading data, the forced type is converted to byte. (byte) 256 to get the-1 Value of byte,

Because the byte only has 8 bits, when the int's high position is discarded, only 1111 is left. In the byte's high bit 1 indicates that the symbol bit is negative, and the final result is the byte's-1;

Similarly, byte-128 (1000 0000) is converted to int 128 (0000 0000 0000 0000 0000 0000 1000), and The 0000 1000 of byte is restored after forced type conversion.

3.2 thread security

We know that the ++ operation is actually thread unsafe. The public methods in BufferedInputStream are all synchronized, and the entire cache array is guaranteed to be

Thread-safe. synchronized is not added to the close method to avoid excessive system consumption.

Author: Brother Niu

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.