Class InputStreams

java.lang.Object
io.bytestreams.codec.core.util.InputStreams

public final class InputStreams extends Object
Utility methods for working with input streams.
  • Method Details

    • markable

      public static InputStream markable(InputStream input)
      Returns a stream that supports mark, wrapping the given stream only if it does not already.

      Only for a codec that reads its input to end-of-stream. Wrapping buffers ahead, so a codec that returns while bytes remain would leave them in a wrapper its caller cannot see, and whatever decodes next would read from the wrong position. A codec that stops short must instead require markSupported of its caller, as repeatWhile does, or avoid rewinding altogether, as choice does.

      Composite codecs that peek before decoding an item should pass the result down rather than the original stream, so that combinators needing to rewind can be nested inside them. ByteArrayInputStream and BufferedInputStream already support mark and are returned unchanged; FileInputStream, socket streams and PushbackInputStream do not.

      Parameters:
      input - the stream to make markable, read to end-of-stream by the caller
      Returns:
      input if it supports mark, otherwise a buffered wrapper
      Throws:
      NullPointerException - if input is null
    • atEndOfStream

      public static boolean atEndOfStream(InputStream input) throws IOException
      Reports whether the stream is exhausted, consuming nothing when it is not.

      The stream must support mark; pass it through markable(InputStream) first.

      Parameters:
      input - a markable stream
      Returns:
      true if the next read would return end-of-stream
      Throws:
      IOException - if an I/O error occurs
    • readFully

      public static byte[] readFully(InputStream input, int length) throws IOException
      Reads exactly the specified number of bytes from the input stream.
      Parameters:
      input - the input stream to read from
      length - the exact number of bytes to read
      Returns:
      a byte array containing the bytes read
      Throws:
      IllegalArgumentException - if length is negative
      IOException - if an I/O error occurs
      EOFException - if the stream ends before the required bytes are read