Class InputStreamUtil


  • public final class InputStreamUtil
    extends java.lang.Object
    Utility methods for InputStream that are stand-ins for a small subset of DataInput methods. This avoids pushing a DataInputStream just to get this functionality.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int SKIP_FRAGMENT_SIZE  
    • Constructor Summary

      Constructors 
      Constructor Description
      InputStreamUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void readFully​(java.io.InputStream in, byte[] b, int offset, int len)
      Read a number of bytes into an array.
      static int readLoop​(java.io.InputStream in, byte[] b, int offset, int len)
      Read a number of bytes into an array.
      static int readUnsignedByte​(java.io.InputStream in)
      Read an unsigned byte from an InputStream, throwing an EOFException if the end of the input is reached.
      static void skipFully​(java.io.InputStream is, long skippedBytes)
      Skips requested number of bytes, throws EOFException if there is too few bytes in the stream.
      static long skipPersistent​(java.io.InputStream in, long bytesToSkip)
      Tries harder to skip the requested number of bytes.
      static long skipUntilEOF​(java.io.InputStream is)
      Skips until EOF, returns number of bytes skipped.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • InputStreamUtil

        public InputStreamUtil()
    • Method Detail

      • readUnsignedByte

        public static int readUnsignedByte​(java.io.InputStream in)
                                    throws java.io.IOException
        Read an unsigned byte from an InputStream, throwing an EOFException if the end of the input is reached.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.EOFException - if the end of the stream is reached
        See Also:
        DataInput.readUnsignedByte()
      • readFully

        public static void readFully​(java.io.InputStream in,
                                     byte[] b,
                                     int offset,
                                     int len)
                              throws java.io.IOException
        Read a number of bytes into an array.
        Throws:
        java.io.IOException - if an I/O error occurs.
        java.io.EOFException - if the end of the stream is reached
        See Also:
        DataInput.readFully(byte[])
      • readLoop

        public static int readLoop​(java.io.InputStream in,
                                   byte[] b,
                                   int offset,
                                   int len)
                            throws java.io.IOException
        Read a number of bytes into an array. Keep reading in a loop until len bytes are read or EOF is reached or an exception is thrown. Return the number of bytes read. (InputStream.read(byte[],int,int) does not guarantee to read len bytes even if it can do so without reaching EOF or raising an exception.)
        Throws:
        java.io.IOException - if an I/O error occurs.
      • skipUntilEOF

        public static long skipUntilEOF​(java.io.InputStream is)
                                 throws java.io.IOException
        Skips until EOF, returns number of bytes skipped.
        Parameters:
        is - InputStream to be skipped.
        Returns:
        number of bytes skipped in fact.
        Throws:
        java.io.IOException - if IOException occurs. It doesn't contain EOFException.
        java.lang.NullPointerException - if the param 'is' equals null.
      • skipFully

        public static void skipFully​(java.io.InputStream is,
                                     long skippedBytes)
                              throws java.io.IOException
        Skips requested number of bytes, throws EOFException if there is too few bytes in the stream.
        Parameters:
        is - InputStream to be skipped.
        skippedBytes - number of bytes to skip. if skippedBytes ≷= zero, do nothing.
        Throws:
        java.io.EOFException - if EOF meets before requested number of bytes are skipped.
        java.io.IOException - if IOException occurs. It doesn't contain EOFException.
        java.lang.NullPointerException - if the param 'is' equals null.
      • skipPersistent

        public static final long skipPersistent​(java.io.InputStream in,
                                                long bytesToSkip)
                                         throws java.io.IOException
        Tries harder to skip the requested number of bytes.

        Note that even if the method fails to skip the requested number of bytes, it will not throw an exception. If this happens, the caller can be sure that end-of-stream has been reached.

        Parameters:
        in - byte stream
        bytesToSkip - the number of bytes to skip
        Returns:
        The number of bytes skipped.
        Throws:
        java.io.IOException - if reading from the stream fails