By using the available() method in the InputStream class. From the Javadoc:
Returns the number of bytes that can be read (or skipped over) from
this input stream without blocking by the next caller of a method for
this input stream. The next caller might be the same thread or or
another thread.