Class StreamUtils

java.lang.Object
org.apache.nifi.stream.io.StreamUtils

public class StreamUtils extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static long
    copy(InputStream source, OutputStream destination)
     
    static void
    copy(InputStream source, OutputStream destination, long numBytes)
    Copies numBytes from source to destination.
    static byte[]
    copyExclusive(InputStream in, OutputStream out, int maxBytes, byte[]... stoppers)
    Copies data from in to out until either we are out of data (returns null) or we hit one of the byte patterns identified by the stoppers parameter (returns the byte pattern matched).
    static byte[]
    copyInclusive(InputStream in, OutputStream out, int maxBytes, byte[]... stoppers)
    Copies data from in to out until either we are out of data (returns null) or we hit one of the byte patterns identified by the stoppers parameter (returns the byte pattern matched).
    static void
    fillBuffer(InputStream source, byte[] destination)
    Reads data from the given input stream, copying it to the destination byte array.
    static int
    fillBuffer(InputStream source, byte[] destination, boolean ensureCapacity)
    Reads data from the given input stream, copying it to the destination byte array.
    static void
    read(InputStream source, byte[] destination, int byteCount)
    Reads byteCount bytes of data from the given InputStream, writing to the provided byte array.
    static void
    skip(InputStream stream, long bytesToSkip)
    Skips the specified number of bytes from the InputStream If unable to skip that number of bytes, throws EOFException

    Methods inherited from class java.lang.Object

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

    • StreamUtils

      public StreamUtils()
  • Method Details

    • copy

      public static long copy(InputStream source, OutputStream destination) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(InputStream source, OutputStream destination, long numBytes) throws IOException
      Copies numBytes from source to destination. If numBytes are not available from source, throws EOFException
      Parameters:
      source - the source of bytes to copy
      destination - the destination to copy bytes to
      numBytes - the number of bytes to copy
      Throws:
      IOException - if any issues occur while copying
    • fillBuffer

      public static void fillBuffer(InputStream source, byte[] destination) throws IOException
      Reads data from the given input stream, copying it to the destination byte array. If the InputStream has less data than the given byte array, throws an EOFException
      Parameters:
      source - the source to copy bytes from
      destination - the destination to fill
      Throws:
      IOException - if any issues occur reading bytes
    • fillBuffer

      public static int fillBuffer(InputStream source, byte[] destination, boolean ensureCapacity) throws IOException
      Reads data from the given input stream, copying it to the destination byte array. If the InputStream has less data than the given byte array, throws an EOFException if ensureCapacity is true and otherwise returns the number of bytes copied
      Parameters:
      source - the source to read bytes from
      destination - the destination to fill
      ensureCapacity - whether or not to enforce that the InputStream have at least as much data as the capacity of the destination byte array
      Returns:
      the number of bytes actually filled
      Throws:
      IOException - if unable to read from the underlying stream
    • read

      public static void read(InputStream source, byte[] destination, int byteCount) throws IOException
      Reads byteCount bytes of data from the given InputStream, writing to the provided byte array.
      Parameters:
      source - the InputStream to read from
      destination - the destination for the data
      byteCount - the number of bytes to copy
      Throws:
      IllegalArgumentException - if the given byte array is smaller than byteCount elements.
      EOFException - if the InputStream does not have byteCount bytes in the InputStream
      IOException - if unable to read from the InputStream
    • copyInclusive

      public static byte[] copyInclusive(InputStream in, OutputStream out, int maxBytes, byte[]... stoppers) throws IOException
      Copies data from in to out until either we are out of data (returns null) or we hit one of the byte patterns identified by the stoppers parameter (returns the byte pattern matched). The bytes in the stopper will be copied.
      Parameters:
      in - the source to read bytes from
      out - the destination to write bytes to
      maxBytes - the max bytes to copy
      stoppers - patterns of bytes which if seen will cause the copy to stop
      Returns:
      the byte array matched, or null if end of stream was reached
      Throws:
      IOException - if issues occur reading or writing bytes to the underlying streams
    • copyExclusive

      public static byte[] copyExclusive(InputStream in, OutputStream out, int maxBytes, byte[]... stoppers) throws IOException
      Copies data from in to out until either we are out of data (returns null) or we hit one of the byte patterns identified by the stoppers parameter (returns the byte pattern matched). The byte pattern matched will NOT be copied to the output and will be un-read from the input.
      Parameters:
      in - the source to read bytes from
      out - the destination to write bytes to
      maxBytes - the maximum number of bytes to copy
      stoppers - byte patterns which will cause the copy to stop if found
      Returns:
      the byte array matched, or null if end of stream was reached
      Throws:
      IOException - for issues reading or writing to underlying streams
    • skip

      public static void skip(InputStream stream, long bytesToSkip) throws IOException
      Skips the specified number of bytes from the InputStream If unable to skip that number of bytes, throws EOFException
      Parameters:
      stream - the stream to skip over
      bytesToSkip - the number of bytes to skip
      Throws:
      IOException - if any issues reading or skipping underlying stream