At least two calls to read() would be required to detect the end of a non-empty stream. One to read the content, then another to return EOF. As an example, if the buffer were 256 bytes, and the file only 200 bytes, a call to read(byte[]) would return 200 (or a sequence of call results would sum to 200), then subsequent calls would return -1 to signal EOF. It's not totally clear how you've interpreted the Javadoc for InputStream, but it says clearly that it returns the number of bytes read, and returns -1 only when there were no more data to be read. If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b. Further: Returns: The total number of bytes read into the buffer, or -1 [if] there [are] no more data because the end of the stream has been reached.
May 07, 2012 Convert InputStream to File? Use FIleOutputStream (to write to the file ) and FileInputStream (to read the file) in this case. How to write an InputStream to a File - using Java, Guava and the Commons IO library.
Comments are closed.