Class StreamUtils

java.lang.Object
org.pepsoft.util.StreamUtils

public class StreamUtils extends Object
Utility methods for working with InputStreams and OutputStreams.
  • Method Details

    • load

      public static byte[] load(InputStream in) throws IOException
      Load all bytes from an input stream and close it. Note that there may not be more than Integer.MAX_VALUE bytes left in the stream.
      Parameters:
      in - The input stream to load.
      Returns:
      All bytes from the input stream.
      Throws:
      IOException - If an I/O error occurs while loading the bytes or closing the stream.
    • copy

      public static void copy(InputStream in, OutputStream out) throws IOException
      Copy all bytes from the input stream to the output stream and close both streams.
      Parameters:
      in - The input stream to copy from.
      out - The output stream to copy to.
      Throws:
      IOException - If an I/O error occurs while copying the bytes or closing the streams.
    • copy

      public static void copy(InputStream in, OutputStream out, int maximumBytes) throws IOException
      Copy all bytes (up to a specified maximum) from the input stream to the output stream and close both streams. This version allows the specification of a maximum number of bytes to copy. If the input stream contains more bytes, the method throws an IOException.
      Parameters:
      in - The input stream to copy from.
      out - The output stream to copy to.
      maximumBytes - The maximum number of bytes to allow in the input stream.
      Throws:
      IOException - If an I/O error occurs while copying the bytes or closing the streams, or if the input stream contained more than the specified number of bytes.
    • copy

      public static void copy(InputStream in, OutputStream out, ProgressReceiver progressReceiver, long byteCount) throws IOException, ProgressReceiver.OperationCancelled
      Copy all bytes from the input stream to the output stream and close both streams.
      Parameters:
      in - The input stream to copy from.
      out - The output stream to copy to.
      progressReceiver - An optional progress receiver to report copy progress to.
      byteCount - The number of bytes in the input stream. If the input stream contains more bytes, an IOException is thrown. Specify -1 to disable a byte limit. If a progress receiver is specified then this parameter is mandatory.
      Throws:
      IOException - If an I/O error occurs while copying the bytes or closing the streams, or if there were more.
      ProgressReceiver.OperationCancelled - If a progress receiver was provided and it threw an OperationCancelled exception.