org.rhq.core.util.stream
Class StreamUtil

java.lang.Object
  extended by org.rhq.core.util.stream.StreamUtil

public class StreamUtil
extends Object

Provides some utilities to work on streams and some (de)serialization methods..

Author:
John Mazzitelli

Method Summary
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, 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

slurp

public static byte[] slurp(InputStream stream)
                    throws RuntimeException
Reads in the entire contents of the given input stream and returns the data in a byte array. Be careful - if the stream has a lot of data, you run the risk of an OutOfMemoryError.

Parameters:
stream - the stream to read
Returns:
the stream's data
Throws:
RuntimeException - if an IO exception occurred while reading the stream

slurp

public static String slurp(Reader reader)
                    throws RuntimeException
Equivalent of slurp(InputStream) but using a reader instead of input stream.

Parameters:
reader -
Returns:
Throws:
RuntimeException

copy

public static long copy(InputStream input,
                        OutputStream output)
                 throws RuntimeException
Copies data from the input stream to the output stream. Upon completion or on an exception, the streams will be closed.

Parameters:
input - the originating stream that contains the data to be copied
output - the destination stream where the data should be copied to
Returns:
the number of bytes copied from the input to the output stream
Throws:
RuntimeException - if failed to read or write the data

copy

public static long copy(Reader rdr,
                        Writer wrt)
                 throws RuntimeException
Equivalent of copy(InputStream, OutputStream) but using reader and writer instead of streams.

Parameters:
rdr -
wrt -
Returns:
Throws:
RuntimeException

copy

public static long copy(InputStream input,
                        OutputStream output,
                        boolean closeStreams)
                 throws RuntimeException
Copies data from the input stream to the output stream. Upon completion or on an exception, the streams will be closed but only if closeStreams is true. If closeStreams is false, the streams are left open; the caller has the responsibility to close them.

Parameters:
input - the originating stream that contains the data to be copied
output - the destination stream where the data should be copied to
closeStreams - if true, the streams will be closed before the method returns
Returns:
the number of bytes copied from the input to the output stream
Throws:
RuntimeException - if failed to read or write the data

copy

public static long copy(Reader rdr,
                        Writer wrt,
                        boolean closeStreams)
                 throws RuntimeException
Equivalent of copy(InputStream, OutputStream, boolean) only using reader and writer instead of input stream and output stream.

Parameters:
rdr -
wrt -
closeStreams -
Returns:
Throws:
RuntimeException

copy

public static long copy(InputStream input,
                        OutputStream output,
                        long startByte,
                        long length)
                 throws RuntimeException
Copies data from the input stream to the output stream. The caller has the responsibility to close them. This method allows you to copy a byte range from the input stream. The start byte is the index (where the first byte of the stream is index #0) that starts to be copied. 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.

Parameters:
input - the originating stream that contains the data to be copied
output - the destination stream where the data should be copied to
startByte - 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 EOF
Returns:
the number of bytes copied from the input to the output stream (usually length, but if length was larger than the number of bytes in input after the start byte, this return value will be less than length.
Throws:
RuntimeException - if failed to read or write the data

serialize

public static byte[] serialize(Serializable object)
                        throws RuntimeException
Given a serializable object, this will return the object's serialized byte array representation.

Parameters:
object - the object to serialize
Returns:
the serialized bytes
Throws:
RuntimeException - if failed to serialize the object

deserialize

public static Object deserialize(byte[] serializedData)
                          throws RuntimeException
Deserializes the given serialization data and returns the object.

Parameters:
serializedData - the serialized data as a byte array
Returns:
the deserialized object
Throws:
RuntimeException - if failed to deserialize the object

safeClose

public static void safeClose(Closeable stream)
Can be used to safely close a stream. No-op if the stream is null.

Parameters:
stream - the stream to close or null


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.