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?
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.
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.
InputStream is = new URL(someUrl). openStream(); long length = is. skip(Long. MAX_VALUE);
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With