when does FileInputStream.read() block?
Asked Answered
W

4

5

The question is similar to the following two questions.

But I still cannot fully understand it.

So far I think the read() method in following code will block due to the empty file 'test.txt'.

FileInputStream fis = new FileInputStream("c:/test.txt");
System.out.println(fis.read());
System.out.println("to the end");

Actually it will print -1, I want to know why.

The javadoc says This method blocks if no input is yet available.

What does 'no input is available' mean?

thanks.

Wrest answered 5/3, 2013 at 8:11 Comment(0)
A
5

The answer to your question can be found in the JavaDoc for .read():

This method blocks if no input is yet available.

and

Returns: the next byte of data, or -1 if the end of the file is reached.

So, an empty file will get you an immediate -1 (instead of read() blocking) as

  • there is input available, since the file exists
  • ...but it is empty, so immediate EOF.

The ...No input is yet available... situation could occur eg. when one was to read from a named pipe instead of a plain file, and the other side of the pipe hasn't written anything yet.

Cheers,

Ammonic answered 5/3, 2013 at 8:16 Comment(5)
When reading from eg. a pipe instead of a file, nothing will we available until the other "side" has written to the pipe - in that scenario .read() would block.Ammonic
Could you please give me a sample about how to read from a pipe? thanks, because I cannot find related functions in apis of FileInputstream.Wrest
If you have access to a linux box it's as easy as mkfifo /tmp/mynamedpipe and then open /tmp/mynamedpipe as you would do with a regular file. Using echo "whatever" > /tmp/mynamedpipe you can pass data through the pipe.Ammonic
You get -1 at EOF regardless of whether the file is empty. All files have EOFs.Strickle
@EJP I don't dispute that but I believe the OPs point was why he got -1 instead of read() blocking for an empty file. I have clarified that in the answer now.Ammonic
C
3

FileInputStream can be used to read from things other than ordinary files. One obvious example is a named pipe: if you try to read from a pipe before the other side has written to it, the read operation will block.

Clicker answered 5/3, 2013 at 8:21 Comment(2)
How to read from a named pipe? How to code it? I cannot find the function in the source code of FileInputStream.Wrest
@liamxu: You open it like any other file. If you want to learn more, I suggest the Wikipedia page I've linked to.Clicker
A
1

This maybe interperted as follows: FileInputStream.read invokes a native method, the native method makes the read system call and blocks waiting for OS to read the bytes from file into a buffer and returns when ready. That is, FileInputStream.read uses synchronous I/O to reads data from a file as opposed to non-blocking, asynchronous I/O.

Atiana answered 5/3, 2013 at 8:37 Comment(0)
S
1

You can't interpret 'no input is available' as 'you are positioned at EOF and no more input will ever be available'. They are different conditions. The latter returns -1.

In general, all reads from files block until the data is available. The disk has to come around to the right point and the head has to seek to the right track. You also need to consider files that are on shared drives, or files that are named pipes, both of which involve network operations, which can also block.

Strickle answered 5/3, 2013 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.