public class StreamUtil extends Object
| Modifier and Type | Method and Description |
|---|---|
static long |
copy(InputStream input,
OutputStream output)
Copies data from the input stream to the output stream.
|
static long |
copy(InputStream input,
OutputStream output,
boolean closeStreams)
Copies data from the input stream to the output stream.
|
static long |
copy(InputStream input,
OutputStream output,
boolean closeStreams,
boolean htmlEscape)
Copies data from the input stream to the output stream.
|
static long |
copy(InputStream input,
OutputStream output,
long startByte,
long length)
Copies data from the input stream to the output stream.
|
static long |
copy(Reader rdr,
Writer wrt)
Equivalent of
copy(InputStream, OutputStream) but using reader and writer instead
of streams. |
static long |
copy(Reader rdr,
Writer wrt,
boolean closeStreams)
Equivalent of
copy(InputStream, OutputStream, boolean) only using reader and writer
instead of input stream and output stream. |
static Object |
deserialize(byte[] serializedData)
Deserializes the given serialization data and returns the object.
|
static void |
safeClose(Closeable stream)
Can be used to safely close a stream.
|
static byte[] |
serialize(Serializable object)
Given a serializable object, this will return the object's serialized byte array representation.
|
static byte[] |
slurp(InputStream stream)
Reads in the entire contents of the given input stream and returns the data in a byte array.
|
static String |
slurp(Reader reader)
Equivalent of
slurp(InputStream) but using a reader instead of input stream. |
public static byte[] slurp(InputStream stream) throws RuntimeException
OutOfMemoryError.stream - the stream to readRuntimeException - if an IO exception occurred while reading the streampublic static String slurp(Reader reader) throws RuntimeException
slurp(InputStream) but using a reader instead of input stream.reader - RuntimeExceptionpublic static long copy(InputStream input, OutputStream output) throws RuntimeException
input - the originating stream that contains the data to be copiedoutput - the destination stream where the data should be copied toRuntimeException - if failed to read or write the datapublic static long copy(Reader rdr, Writer wrt) throws RuntimeException
copy(InputStream, OutputStream) but using reader and writer instead
of streams.rdr - wrt - RuntimeExceptionpublic static long copy(InputStream input, OutputStream output, boolean closeStreams) throws RuntimeException
closeStreams is true. If closeStreams is
false, the streams are left open; the caller has the responsibility to close them.input - the originating stream that contains the data to be copiedoutput - the destination stream where the data should be copied tocloseStreams - if true, the streams will be closed before the method returnsRuntimeException - if failed to read or write the datapublic static long copy(InputStream input, OutputStream output, boolean closeStreams, boolean htmlEscape) throws RuntimeException
closeStreams is true. If closeStreams is
false, the streams are left open; the caller has the reponsibility to close them.
If htmlEscape is true the input stream is read into a String and all HTML entities
are escaped using forHTMLTag(String) prior to being copied to output stream.
input - the originating stream that contains the data to be copiedoutput - the destination stream where the data should be copied tocloseStreams - if true, the streams will be closed before the method returnshtmlEscape - if true, the input stream will be HTML escaped before being written to output streamtrueRuntimeException - if failed to read or write the datapublic static long copy(Reader rdr, Writer wrt, boolean closeStreams) throws RuntimeException
copy(InputStream, OutputStream, boolean) only using reader and writer
instead of input stream and output stream.rdr - wrt - closeStreams - RuntimeExceptionpublic static long copy(InputStream input, OutputStream output, long startByte, long length) throws RuntimeException
length indicates how many bytes to copy, a
negative length indicates copy everything up to the EOF of the input stream.
Because this method must leave the given input stream intact in case the caller wants to continue reading from
the input stream (that is, in case the caller wants to read the next byte after the final byte read by this
method), this method will not wrap the input stream with a buffered input stream. Because of this, this method is
less efficient than copy(InputStream, OutputStream, boolean). If you do not care to continue reading
from the input stream after this method completes, it is recommended you wrap your input stream in a
BufferedInputStream and pass that buffered stream to this method.
input - the originating stream that contains the data to be copiedoutput - the destination stream where the data should be copied tostartByte - the first byte to copy from the input stream (byte indexes start at #0)length - the number of bytes to copy - if -1, then copy all until EOFinput after the start byte, this return value will be less than
length.RuntimeException - if failed to read or write the datapublic static byte[] serialize(Serializable object) throws RuntimeException
object - the object to serializeRuntimeException - if failed to serialize the objectpublic static Object deserialize(byte[] serializedData) throws RuntimeException
serializedData - the serialized data as a byte arrayRuntimeException - if failed to deserialize the objectpublic static void safeClose(Closeable stream)
stream - the stream to close or nullCopyright © 2008-2014 Red Hat, Inc.. All Rights Reserved.