I have the following statement:
DataInputStream is = new DataInputStream(process.getInputStream());
I would like to print the contents of this input stream but I dont know the size of this stream. How should I read this stream and print it?
It is common to all Streams, that the length is not known in advance. Using a standard InputStream
the usual solution is to simply call read
until -1
is returned.
But I assume, that you have wrapped a standard InputStream
with a DataInputStream
for a good reason: To parse binary data. (Note: Scanner
is for textual data only.)
The JavaDoc for DataInputStream
shows you, that this class has two different ways to indicate EOF - each method either returns -1
or throws an EOFException
. A rule of thumb is:
InputStream
uses the "return -1
" convention,InputStream
throws the EOFException
.If you use readShort
for example, read until an exception is thrown, if you use "read()", do so until -1
is returned.
Tip: Be very careful in the beginning and lookup each method you use from DataInputStream
- a rule of thumb can break.