Package com.randomnoun.common
Class StreamUtil
java.lang.Object
com.randomnoun.common.StreamUtil
Utility class to copy streams synchronously and asynchronously.
- Author:
- knoxg
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Thread
copyAndCloseThread
(InputStream input, OutputStream output, int bufSize) Returns a thread that, when started, will pipe all data from one inputstream to an outputstream.static void
copyStream
(InputStream input, OutputStream output) Copies the data from an inputStream to an outputstreamstatic void
copyStream
(InputStream input, OutputStream output, int bufSize) Copies the data from an inputStream to an outputstream (used to mimic pipes).static long
copyStreamCount
(InputStream input, OutputStream output, int bufSize) Copies a stream and return number of bytes copiedstatic Thread
copyThread
(InputStream input, OutputStream output, int bufSize) Returns a thread that, when started, will pipe all data from one inputstream to an outputstream.static byte[]
getByteArray
(InputStream input) Reads all available data from an InputStream, and returns it in a single byte array.static int
indexOf
(InputStream input, String searchText) Scans this input stream until the text in 'searchText' is found.static String
readUntil
(InputStream input, String searchText) Reads this input stream until the text in 'searchText' is found.
-
Constructor Details
-
StreamUtil
public StreamUtil()Creates a new StreamUtils object.
-
-
Method Details
-
copyStream
public static void copyStream(InputStream input, OutputStream output, int bufSize) throws IOException Copies the data from an inputStream to an outputstream (used to mimic pipes).- Parameters:
input
- The stream to retrieve information fromoutput
- The stream to send data tobufSize
- buffer size in bytes- Throws:
IOException
-
copyStreamCount
public static long copyStreamCount(InputStream input, OutputStream output, int bufSize) throws IOException Copies a stream and return number of bytes copied- Parameters:
input
- The stream to retrieve information fromoutput
- The stream to send data tobufSize
- buffer size in bytes- Throws:
IOException
-
copyStream
Copies the data from an inputStream to an outputstream- Parameters:
input
- The stream to retrieve information fromoutput
- The stream to send data to- Throws:
IOException
-
getByteArray
Reads all available data from an InputStream, and returns it in a single byte array.- Parameters:
input
- The stream to receive information from- Returns:
- A byte array containing the contents of the stream
- Throws:
IOException
-
copyThread
Returns a thread that, when started, will pipe all data from one inputstream to an outputstream. The thread will complete when the inputStream returns EOF.- Parameters:
input
- The stream to retrieve information fromoutput
- The stream to send data tobufSize
- buffer size in bytes
-
indexOf
Scans this input stream until the text in 'searchText' is found. Returns the number of bytes skipped if the search text was found, or -1 if the text was not found.- Parameters:
input
- The input stream to scansearchText
- The text we are searching for- Throws:
IOException
- if an IO Exception occurs reading the stream
-
readUntil
Reads this input stream until the text in 'searchText' is found. Returns a String containing the data read, including the searchText. Returns null if the text was not found.- Parameters:
input
- The input stream to scansearchText
- The text we are searching for- Throws:
IOException
- if an IO Exception occurs reading the stream
-
copyAndCloseThread
Returns a thread that, when started, will pipe all data from one inputstream to an outputstream. The thread will complete when the inputStream returns EOF. The output stream will also be closed.- Parameters:
input
- The stream to retrieve information fromoutput
- The stream to send data tobufSize
- buffer size in bytes
-