Class InputStreamHelper

java.lang.Object
org.drasyl.util.InputStreamHelper

public final class InputStreamHelper extends Object
Class that provides utils for InputStreams.
  • Method Details

    • readAllBytes

      public static byte[] readAllBytes(InputStream is) throws IOException
      Reads all remaining bytes from the input stream. This method blocks until all remaining bytes have been read and end of stream is detected, or an exception is thrown. This method does not close the input stream.

      When this stream reaches end of stream, further invocations of this method will return an empty byte array.

      Note that this method is intended for simple cases where it is convenient to read all bytes into a byte array. It is not intended for reading input streams with large amounts of data.

      The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.

      If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.

      Source: https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/io/InputStream.java

      Returns:
      a byte array containing the bytes read from this input stream
      Throws:
      IOException - if an I/O error occurs
      OutOfMemoryError - if an array of the required size cannot be allocated.
      Since:
      9
    • readNBytes

      public static byte[] readNBytes(InputStream is, int len) throws IOException
      Reads up to a specified number of bytes from the input stream. This method blocks until the requested number of bytes has been read, end of stream is detected, or an exception is thrown. This method does not close the input stream.

      The length of the returned array equals the number of bytes read from the stream. If len is zero, then no bytes are read and an empty byte array is returned. Otherwise, up to len bytes are read from the stream. Fewer than len bytes may be read if end of stream is encountered.

      When this stream reaches end of stream, further invocations of this method will return an empty byte array.

      Note that this method is intended for simple cases where it is convenient to read the specified number of bytes into a byte array. The total amount of memory allocated by this method is proportional to the number of bytes read from the stream which is bounded by len. Therefore, the method may be safely called with very large values of len provided sufficient memory is available.

      The behavior for the case where the input stream is asynchronously closed, or the thread interrupted during the read, is highly input stream specific, and therefore not specified.

      If an I/O error occurs reading from the input stream, then it may do so after some, but not all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is strongly recommended that the stream be promptly closed if an I/O error occurs.

      Source: https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/java.base/share/classes/java/io/InputStream.java

      Parameters:
      len - the maximum number of bytes to read
      Returns:
      a byte array containing the bytes read from this input stream
      Throws:
      IllegalArgumentException - if length is negative
      IOException - if an I/O error occurs
      OutOfMemoryError - if an array of the required size cannot be allocated.

      Source: InputStream.readNBytes(int)