简体   繁体   中英

What does an int value returned by InputStream.read() represent?

The documentation says read() returns the byte as an int value between 0 and 255 .

What does this value represent?

In two's complement, the number -1 is represented by all bits set to a 1.

Here is a byte value of -1 :*

1111 1111

Here is an int value of -1 :*

1111 1111 1111 1111 1111 1111 1111 1111

If we take the byte value of -1 and store it in an int without extending the sign, it becomes 255 :

0000 0000 0000 0000 0000 0000 1111 1111

This is just the behavior of two's complement. Storing bytes in an int leaves us leftover bits we can use to indicate other things.

So input stream returns byte values from 0 - 255 , and -1 to indicate end of stream. To get the byte values, cast the int to a byte:

int byteAsInt = inputStream.read();
if(byteAsInt > -1) {
     byte byteValue = (byte)byteAsInt;

     // use the byte
}

The int values 128 - 255 will become interpreted as negative numbers when it is casted to the byte. Alternatively, you can perform "unsigned" arithmetic on the int.

The bytes themselves can mean anything. For example if they are from a .txt file, they are probably straight ASCII codes. Other formats can be much more complicated.


* 4.2:

The integral types are byte , short , int , and long , whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively […].

read() reads the next byte and then return it.

The reason it return it as an int, instead of a byte as you would expect, is that a byte is signed(Have values between -127 and 128) so it can't be used to return a value between 0-255

It represents the next byte of data. The reason it returns an int and not a byte is that byte type is between -128 and 127, and this method uses the value -1 to indicate the end of the stream.

It doesn't "represent" anything. It's whatever the InputStream's next byte is.

For instance, a FileInputStream lets you read the bytes from a file. If the first byte in that file is 37, then read() will return 37. If the second byte in the file is 231, then the second call to read() will return 231. The bytes are returned as unsigned values, meaning that each byte can have a value between 0 and 255 (inclusive).

If the value is non-negative and you want it as a byte , you can just cast it:

int nextByteAsInt = inputStream.read();
if (nextByteAsInt >= 0) {
    byte nextByte = (byte) nextByteAsInt;
    doSomethingWithByte(nextByte);
} else {
    inputStreamIsFinished();
}

When all of the file has been read, read() will return -1. The reason read() doesn't just return a byte is that if needs to be able to return that -1 to represent that end of stream.

To get at what the bytes "mean," you have to know their context -- what you're reading. A byte from a text file will mean one thing, a file from a compressed or binary file will mean another, etc.

Read() returns the next byte in the byte stream, converted to an unsigned int. When a byte is converted to an unsigned integer, it will be between 0 and 255 , since the bits are 1+2+4+8+16+32+64+128 = 255 . What it represents depends on what kind of data was converted to binary and sent over the byte stream.

This isn't in your question; so I didn't include it in my original answer, but it seems to be where this question often comes from:

It is important to note that Read() will return -1 to indicate end of stream. 0 to 255 represents all the possible states of the next byte (8 bits), but Read() returns a 32-bit two's complement integer. This means that the returned byte will take up the first 8 bits, leaving the rest of the 32 bits of the retuned integer all 0 . So for end of stream, Read() returns 32 bits all set to 1 , which represents -1 in two's complement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM