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

前端 未结 8 869
无人及你
无人及你 2020-12-08 18:10

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?

8条回答
  •  囚心锁ツ
    2020-12-08 18:57

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

提交回复
热议问题