Dark Horse Programmer--read () method for Java byte stream return value to int (reprint)

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

I have not been aware of the Java stream read () method return value Why is int, today on the Internet to find the answer. The following is reproduced, the original address: http://blog.sina.com.cn/s/blog_9e351f9b01015kgp.html

We all know that the IO operation in Java is divided into Word stream and character stream, for Byte stream, as the name implies is reading the data in bytes, so we characters commonly used the throttle to read the binary stream (slices, music and other files). The question is why is the read () method defined in the byte stream return a value of type int? Since it reads one byte at a time, why doesn't it return a byte type? (I don't know if anyone has the same confusion as I do, but since we have a problem, we have to solve it.) )

So I read the Java source code, the following first paste out the source (take Bufferedinputstream/bufferedoutputstream as an example):

[Java] View plaincopy

Implementation of the Read () method in Bufferedinputstream

[Java] View plaincopy

public synchronized int read () throws IOException {

if (POS >= count) {

Fill ();

if (POS >= count)

return-1;

}

return Getbufifopen () [pos++] & 0xff;//this side Getbufifopen () return is byte[], and & 0xFF is to ensure that the char type is extended upward into int, without symbolic expansion, But 0 expansion.

[Java] View plaincopy

public synchronized int read () throws IOException {

if (POS >= count) {

Fill ();

if (POS >= count)

return-1;

}

return Getbufifopen () [pos++] & 0xff;//this side Getbufifopen () return is byte[], and & 0xFF is to ensure that the char type is extended upward into int, without symbolic expansion, But 0 expansion.

The information we get from the source code is until the Getbufifopen () method returns, and all we get is the byte type, but why is the final return value of the method int?

First, I'll explain the symbol extension briefly, which is the extended sign bit when the byte is converted upward into a wider type. This is for positive 0, negative complement 1, for example, define byte B =-1, in the computer it is eight bits 1111 1111, when expanded to 32-bit integer, usually 1111 1111 1111 1111 1111 1111 1111 1111, the symbol extension, and the To an unsigned extension, also known as a 0 extension, the result is 0000 0000 0000 0000 0000 0000 1111 1111 (in effect this value has become 255). A further discussion of over-upward transformation and forced downward transformation, I'll talk about it later. Here is the second message that we can get from the Java source code, that is, the comment section above, the last line of the read () method expands the reading of byte 0 into int, which means that if we read it directly it may be wrong. Here I wonder why the writer () method in Bufferedoutputstream can read bytes correctly? So I'll check the corresponding source code:

[Java] View plaincopy

Public synchronized void write (int b) throws IOException {

if (count >= buf.length) {

Flushbuffer ();

}

buf[count++] = (byte) b;

}

[Java] View plaincopy

Public synchronized void write (int b) throws IOException {

if (count >= buf.length) {

Flushbuffer ();

}

buf[count++] = (byte) b;

}

As you can see, here it decisively turns int into byte (after intercept eight bits). As a result, the message is that the Java byte stream reads byte into int and then goes back to byte to save it. Why not?

After some thought, I had an initial answer: When reading a byte data with the input stream, there are sometimes 8 consecutive 1 cases, which represents-1 within the computer, exactly matching the flow end tag. Therefore, in order to avoid the flow operation data to end prematurely, the read bytes are extended to the int type. Keep the byte data in front of the 0, to avoid the occurrence of-1. And actually read the final end of the document is achieved through this sentence:

[Java] View plaincopy

if (POS >= count) return-1;

[Java] View plaincopy

if (POS >= count) return-1;

So we used the-1 This end sign is returned through this sentence, instead of the input stream read to a-1.

This solves the doubts ahead of us and confirms the need to do so. The reading and writing of character streams can also be analyzed in a similar way, not to repeat them here.

Dark Horse Programmer--read () method for Java byte stream return value to int (reprint)

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.