org.jboss.portal.common.io
Class IOTools

java.lang.Object
  extended by org.jboss.portal.common.io.IOTools

public class IOTools
extends java.lang.Object

IO tools.

Version:
$Revision: 1.1 $
Author:
Julien Viet

Field Summary
static org.apache.log4j.Logger log
          The logger.
 
Constructor Summary
IOTools()
           
 
Method Summary
static
<S extends java.io.Serializable>
S
clone(S serializable)
          Clone an object implementing the Serializable interface.
static
<S extends java.io.Serializable>
S
clone(S serializable, java.lang.ClassLoader classLoader)
          Clone an object implementing the Serializable interface.
static void copy(java.io.InputStream in, java.io.OutputStream out)
           
static void copy(java.io.InputStream in, java.io.OutputStream out, int bufferSize)
          Pipe an incoming stream in an outcoming stream until no bytes is available from the input stream.
static void copy(java.io.Reader reader, java.io.Writer writer)
          Pipe an input stream in an output stream.
static void copy(java.io.Reader reader, java.io.Writer writer, int bufferSize)
          Pipe an incoming stream in an outcoming stream.
static byte[] getBytes(java.io.InputStream in)
           
static byte[] getBytes(java.io.InputStream in, int bufferSize)
          Get the bytes from the provided input stream.
static java.io.BufferedInputStream safeBufferedWrapper(java.io.InputStream in)
          Check that the provided input stream is buffered.
static java.io.BufferedOutputStream safeBufferedWrapper(java.io.OutputStream out)
          Check that the provided output stream is buffered.
static void safeClose(java.io.Closeable out)
          Attempt to close an Closeable object.
static void safeClose(java.lang.Object closable)
          Attempt to close an object.
static byte[] serialize(java.io.Serializable serializable)
           
static
<T> byte[]
serialize(Serialization<T> serialization, SerializationFilter filter, T t)
           
static
<T> byte[]
serialize(Serialization<T> serialization, T t)
           
static java.io.Serializable unserialize(byte[] bytes)
          Unserialize the bytes into an object.
static java.io.Serializable unserialize(byte[] bytes, java.lang.ClassLoader classLoader)
          Unserialize the bytes into an object.
static
<T> T
unserialize(Serialization<T> serialization, byte[] bytes)
           
static
<T> T
unserialize(Serialization<T> serialization, SerializationFilter filter, byte[] bytes)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

public static final org.apache.log4j.Logger log
The logger.

Constructor Detail

IOTools

public IOTools()
Method Detail

safeClose

public static void safeClose(java.lang.Object closable)

Attempt to close an object. Null argument value is authorized and no operation will be performed in that use case.

It will try to obtain a close() method by reflection and it will be invoked only if the method is public and not static. If the method is called, any Error or RuntimeException will be rethrown, any other kind of throwable will not be rethrown in any form.

Parameters:
closable - the object to close

safeClose

public static void safeClose(java.io.Closeable out)

Attempt to close an Closeable object. Null argument value is authorized and no operation will be performed in that use case. IOException thrown are logged using the error level but not propagated.

Parameters:
out - the stream to close

getBytes

public static byte[] getBytes(java.io.InputStream in)
                       throws java.io.IOException,
                              java.lang.IllegalArgumentException
Parameters:
in - the input stream
Returns:
the bytes read from the stream
Throws:
java.io.IOException
java.lang.IllegalArgumentException - if the input stream is null
See Also:
getBytes(java.io.InputStream, int)

getBytes

public static byte[] getBytes(java.io.InputStream in,
                              int bufferSize)
                       throws java.io.IOException,
                              java.lang.IllegalArgumentException
Get the bytes from the provided input stream. No attempt will be made to close the stream.

Parameters:
in - the input stream
bufferSize - the buffer size used to copy the bytes
Returns:
the bytes read from the stream
Throws:
java.io.IOException
java.lang.IllegalArgumentException - if the input stream is null or the buffer size < 1

copy

public static void copy(java.io.InputStream in,
                        java.io.OutputStream out)
                 throws java.io.IOException
Parameters:
in - the incoming stream
out - the outcoming stream
Throws:
java.lang.IllegalArgumentException - if an argument is null
java.io.IOException
See Also:
copy(java.io.InputStream, java.io.OutputStream, int)

copy

public static void copy(java.io.InputStream in,
                        java.io.OutputStream out,
                        int bufferSize)
                 throws java.io.IOException
Pipe an incoming stream in an outcoming stream until no bytes is available from the input stream. No attempts will be made to close the streams.

Parameters:
in - the incoming stream
out - the outcoming stream
bufferSize - the buffer size
Throws:
java.lang.IllegalArgumentException - if bufferSize < 1 or an argument is null
java.io.IOException

copy

public static void copy(java.io.Reader reader,
                        java.io.Writer writer)
                 throws java.io.IOException
Pipe an input stream in an output stream.

Parameters:
reader - the incoming reader
writer - the outcoming writer
Throws:
java.lang.IllegalArgumentException - if an argument is null
java.io.IOException

copy

public static void copy(java.io.Reader reader,
                        java.io.Writer writer,
                        int bufferSize)
                 throws java.io.IOException
Pipe an incoming stream in an outcoming stream.

Parameters:
reader - the incoming reader
writer - the outcoming writer
bufferSize - the buffer size
Throws:
java.lang.IllegalArgumentException - if bufferSize < 1 or an argument is null
java.io.IOException

clone

public static <S extends java.io.Serializable> S clone(S serializable)
                                            throws java.lang.IllegalArgumentException,
                                                   java.io.IOException
Clone an object implementing the Serializable interface.

Parameters:
serializable - the serializable object to clone
Returns:
a clone
Throws:
java.lang.IllegalArgumentException - if the serializable object is null
java.io.IOException - any IOException

clone

public static <S extends java.io.Serializable> S clone(S serializable,
                                                       java.lang.ClassLoader classLoader)
                                            throws java.lang.IllegalArgumentException,
                                                   java.io.IOException,
                                                   java.lang.ClassNotFoundException
Clone an object implementing the Serializable interface. The specified classloader will be used to perform the unserialization. If no classloader is specified and the object is not null then the classloader used is the one returned by serializable.getClass().getClassLoader().

Parameters:
serializable - the serializable object to clone
Returns:
a clone
Throws:
java.lang.IllegalArgumentException - if the serializable object is null
java.io.IOException - any IOException
java.lang.ClassNotFoundException

serialize

public static byte[] serialize(java.io.Serializable serializable)
                        throws java.lang.IllegalArgumentException,
                               java.io.IOException
Throws:
java.lang.IllegalArgumentException
java.io.IOException

unserialize

public static java.io.Serializable unserialize(byte[] bytes)
                                        throws java.lang.IllegalArgumentException,
                                               java.io.IOException,
                                               java.lang.ClassNotFoundException
Unserialize the bytes into an object. The thread context classloader is used to perform unserialization.

Parameters:
bytes - the bytes to unserialize
Returns:
the unserialized object
Throws:
java.lang.IllegalArgumentException - if the byte array is null
java.io.IOException - any IOException
java.lang.ClassNotFoundException - any ClassNotFoundException

unserialize

public static java.io.Serializable unserialize(byte[] bytes,
                                               java.lang.ClassLoader classLoader)
                                        throws java.lang.IllegalArgumentException,
                                               java.io.IOException,
                                               java.lang.ClassNotFoundException
Unserialize the bytes into an object. If the provided classloader is not null, this classloader is used to perform the unserialization otherwise the thread current context classloader is used.

Parameters:
bytes - the bytes to unserialize
classLoader - the classloader
Returns:
the unserialized object
Throws:
java.lang.IllegalArgumentException - if the byte array is null
java.io.IOException - any IOException
java.lang.ClassNotFoundException - any ClassNotFoundException

serialize

public static <T> byte[] serialize(Serialization<T> serialization,
                                   T t)

serialize

public static <T> byte[] serialize(Serialization<T> serialization,
                                   SerializationFilter filter,
                                   T t)

unserialize

public static <T> T unserialize(Serialization<T> serialization,
                                byte[] bytes)

unserialize

public static <T> T unserialize(Serialization<T> serialization,
                                SerializationFilter filter,
                                byte[] bytes)

safeBufferedWrapper

public static java.io.BufferedInputStream safeBufferedWrapper(java.io.InputStream in)
Check that the provided input stream is buffered. If the argument is already an instance of BufferedInputStream no operation will be performed, otherwise a instance of BufferedInputStream will be created and returned. If the provided argument is null, the null value is returned.

Parameters:
in - the stream
Returns:
a buffered wrapper

safeBufferedWrapper

public static java.io.BufferedOutputStream safeBufferedWrapper(java.io.OutputStream out)
Check that the provided output stream is buffered. If the argument is already an instance of BufferedOutputStream no operation will be performed, otherwise a instance of BufferedOutputStream will be created and returned. If the provided argument is null, the null value is returned.

Parameters:
out - the stream
Returns:
a buffered wrapper


Copyright © 2008. All Rights Reserved.