Package org.apache.nifi.stream.io
Class StreamUtils
java.lang.Object
org.apache.nifi.stream.io.StreamUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic long
copy
(InputStream source, OutputStream destination) static void
copy
(InputStream source, OutputStream destination, long numBytes) CopiesnumBytes
fromsource
todestination
.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 thestoppers
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 thestoppers
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) ReadsbyteCount
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
-
Constructor Details
-
StreamUtils
public StreamUtils()
-
-
Method Details
-
copy
- Throws:
IOException
-
copy
public static void copy(InputStream source, OutputStream destination, long numBytes) throws IOException CopiesnumBytes
fromsource
todestination
. IfnumBytes
are not available fromsource
, throws EOFException- Parameters:
source
- the source of bytes to copydestination
- the destination to copy bytes tonumBytes
- the number of bytes to copy- Throws:
IOException
- if any issues occur while copying
-
fillBuffer
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 fromdestination
- 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 ifensureCapacity
is true and otherwise returns the number of bytes copied- Parameters:
source
- the source to read bytes fromdestination
- the destination to fillensureCapacity
- 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
ReadsbyteCount
bytes of data from the given InputStream, writing to the provided byte array.- Parameters:
source
- the InputStream to read fromdestination
- the destination for the databyteCount
- the number of bytes to copy- Throws:
IllegalArgumentException
- if the given byte array is smaller thanbyteCount
elements.EOFException
- if the InputStream does not havebyteCount
bytes in the InputStreamIOException
- 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 thestoppers
parameter (returns the byte pattern matched). The bytes in the stopper will be copied.- Parameters:
in
- the source to read bytes fromout
- the destination to write bytes tomaxBytes
- the max bytes to copystoppers
- 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 thestoppers
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 fromout
- the destination to write bytes tomaxBytes
- the maximum number of bytes to copystoppers
- 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
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 overbytesToSkip
- the number of bytes to skip- Throws:
IOException
- if any issues reading or skipping underlying stream
-