Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does InputStream read() return an int and not a short?

Tags:

java

I was reading the byte stream trial and noticed the following statement

Notice that read() returns an int value. If the input is a stream of bytes, why doesn't read() return a byte value? Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream.

The given reason for using an int is that they can identify EOF by a -1. (seems shallow)

So the next bigger primitive type is short and it also supports -1 so why not use it?

From what i gather: (reasons to use int)

  1. Due to performance int is preferred. (this)
  2. int variable holds a character value in its last 16 bits (from character trial)
  3. Other more abstract streams would need to read more than just one byte (something that i guess (happens with character streams))

Are my reasons correct? Am i missing something (like error correction)?

like image 389
boxed__l Avatar asked Jan 11 '14 13:01

boxed__l


People also ask

Why does InputStream read return an int?

It returns an int because when the stream can no longer be read, it returns -1. If it returned a byte, then -1 could not be returned to indicate a lack of input because -1 is a valid byte.

What does InputStream read return?

read. Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255 . If no byte is available because the end of the stream has been reached, the value -1 is returned.

What does the read method of InputStream return when an attempt is made to read at the end of the stream?

An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

What type of data is being read in an InputStream?

InputStream is a source for reading data. A stream can represent various kinds of sources, including disk files, devices, other programs, and memory arrays. Streams support many different types of data, including simple bytes, primitive data types, localized characters, and objects.

What is the range of read in InputStream?

read () : java.io.InputStream.read () reads next byte of data from the Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

How to return the next byte of input in InputStream?

Applications that are defining subclass of InputStream must provide method, returning the next byte of input. A reset () method is invoked which re-positions the stream to the recently marked position.

What is the use of read () method of InputStream in Java?

When this read () method is called on the input stream then this read () method reads one character of the input stream at a time. It overrides read () method of FilterInputStream class.

When does an I/O error occur reading from the input stream?

If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state.


3 Answers

The most important reason to prefer int over short is that short is kind of a second-class citizen: all integer literals, as well as all arithmetical operations, are int-typed so you've got short->int promotion happening all over the place. Plus there is very little or no argument against the usage of int.

like image 89
Marko Topolnik Avatar answered Nov 15 '22 04:11

Marko Topolnik


There is only one scenario where using short will give you an advantage: large arrays of short. To be sure, you can use them only when it is clear that the numbers to be stored fit the bounds.

In all other cases, it makes no real difference whether you have short or int. For example:

class A {
    short s;
    double d;
}

will not use less memory than:

class B {
    int s;
    double d;
}

because of alignment issues. So while the first one only has 10 bytes netto data, as compared to the second one that has 12, when you allocate an object it will still get aligned to some 8-byte boundary. Even if it is only a 4 byte boundary, the memory usage will be the same.

like image 33
Ingo Avatar answered Nov 15 '22 04:11

Ingo


This is an interesting question :-) . It is true, that they had to use signed integer value type to represent EOF, but the preference of int over short is probably really just performance.

As I found on a different StackOverflow thread where this was discussed, the Java VM would automatically use int internally even if the definition used short.

The Java documentation states, that short should be used in large arrays and situations where memory really matters - source - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html . That is apparently no the case here, because we always get just one value.

like image 28
Martin Zikmund Avatar answered Nov 15 '22 05:11

Martin Zikmund