Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if an InputStream is empty without reading from it?

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

like image 892
Jenny Smith Avatar asked Oct 06 '09 08:10

Jenny Smith


People also ask

What happens if you don't close an InputStream?

The operating system will only allow a single process to open a certain number of files, and if you don't close your input streams, it might forbid the JVM from opening any more.

What is null InputStream?

All Implemented Interfaces: Closeable, AutoCloseable. public class NullInputStream extends InputStream. A functional, light weight InputStream that emulates a stream of a specified size. This implementation provides a light weight object for testing with an InputStream where the contents don't matter.

How do you use InputStream skip?

InputStream is = new URL(someUrl). openStream(); long length = is. skip(Long. MAX_VALUE);

Does InputStream need to be closed?

It depends on stream implementation. InputStream is just an "interface" in terms of close() . InputStreamReader will not close an interface. It will close the underlying data resource (like file descriptor) if it is.


1 Answers

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).

like image 127
skaffman Avatar answered Oct 16 '22 05:10

skaffman