• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileInputStream.read(byte []) question

 
Sridhar Venkat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following snippet of code I'm using in a program and I wanted to know if there could be a situation in which the read method of FileInputStream would return 0 as the number of bytes read even though there is data available. ( What I was thinking of was a situation in which an exception is not thrown, but read just returns 0 , and the next read succeeds in the reading the data ). Could this happen due to buffer's getting filled up in Linux/Unix ??
If such a situation could arise then the code below would be vulnerable.

Thanks
Sridhar
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine that the file you're reading is a log file of an active Web server. One read might return 0 because there's no more data to read. The next read might return more data.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. It wouldn't return -1 for end of file because the web server has not closed the file? Or would it block until there were bytes available? Guess I'll have to try it!
 
Sridhar Venkat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Imagine that the file you're reading is a log file of an active Web server. One read might return 0 because there's no more data to read. The next read might return more data.

That is definitely a possibility, but the file that will be opened will be a copy of the original file and so that particular case is avoided.
Thanks ( I hadn't thought of this )
Sridhar
 
Chris De Vries
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think that it will return 0. According to the javadocs:

public int read(byte[] b)
throws IOException
Reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.
...
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!