Common to all flows is that the length is not known in advance. Using the standard InputStream , the usual solution is to simply call read until -1 is returned.
But I assume that you put a standard InputStream with a DataInputStream for a good reason: parse binary data. (Note: Scanner used only for text data.)
The JavaDoc for DataInputStream shows you that this class has two different ways to specify EOF: each method returns -1 or throws a EOFException . Rule of thumb:
- Each method that inherits from
InputStream uses the "return -1 " convention, - Each method NOT inherited from
InputStream throws an 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 at the beginning and find every method that you use from a DataInputStream - the rule may break.
source share