views:

1241

answers:

7

I want to know if an InputStreamn is empty, but without using the methods read(). Is there a way to know if it's empty without reading from it?

+1  A: 

I think you are looking for inputstream.available(). It does not tell you whether its empty but it can give you an indication as to whether data is there to be read or not.

Aviator
`available()` tells you if there's data ready to be read, it doesn't necessarily tell you if the stream is empty.
skaffman
@skaffman: thanks a lot! I thought it was sufficient :). Edited my answer to reflect it.
Aviator
available() didn't do quite what I needed, but it was the closest solution.
Jenny Smith
"didn't do quite what I needed" what do you need actually? Is it to allocate a buffer before loading it, or...?
penpen
I needed to see if there's something in the InputStream without reading from it.
Jenny Smith
+8  A: 

No, you can't. InputStream is designed to work with remote resources, so you can't know if it's there until you actually read from it.

You may be able to use a java.io.PushbackInputStream, however, which allows you to read from the stream to see if there's something there, and then "push it back" up the stream (that's not how it really works, but that's the way it behaves to client code).

skaffman
I never knew about PushbackInputStream, +1 to you.
Chris Dennett
A: 

Depending on which subclass it really is: http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#available() might be sufficient.

James
A: 

You can use the available() method to ask the stream whether there is any data available at the moment you call it. However, that function isn't guaranteed to work on all types of input streams. That means that you can't use available() to determine whether a call to read() will actually block or not.

Greg Hewgill
Please tell me a case where available() wouldn't work correctly.
Jenny Smith
The available() method of InputStream itself returns 0 by default; if a subclass does not override this method then 0 will always be returned.
Greg Hewgill
... I know it doesn't work on a SSL Socket, but this is not the case for me.
Jenny Smith
Yes, I know by default returns 0, but in the Javadoc, all the subclasses of the InputStream class have the method available() overriding the method in the superclass, and returning the number of bytes available in the stream.
Jenny Smith
+1  A: 

If the InputStream you're using supports mark/reset support, you could also attempt to read the first byte of the stream and then reset it to its original position:

input.mark(1);
final int bytesRead = input.read(new byte[1]);
input.reset();
if (bytesRead != -1) {
    //stream not empty
} else {
    //stream empty
}

If you don't control what kind of InputStream your're using, you can use the markSupported() method to check whether mark/reset will work on the stream, and fall back to the available() method or the java.io.PushbackInputStream method otherwise.

Henning
+1  A: 

How about using inputStreamReader.ready() to find out?

import java.io.InputStreamReader;

/// ...

InputStreamReader reader = new InputStreamReader(inputStream);
if (reader.ready()) {
    // do something
}

// ...
shaolang
A: 

import java.io.*; public class FileTest { public static void main (String [] args) throws IOException { BufferedReader wow=new BufferedReader (new InputStreamReader (System.in));

    System.out.print ("Enter File Name: ");
    String fileInput=wow.readLine();

    FileInputStream in=new FileInputStream(fileInput);
    //FileInputStream out=new FileInputStream(fileOutput);
}

}

joseph