net.sf.mmm.util.io.base
Class StreamUtilImpl

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractComponent
      extended by net.sf.mmm.util.component.base.AbstractLoggableComponent
          extended by net.sf.mmm.util.io.base.StreamUtilImpl
All Implemented Interfaces:
StreamUtil

@Singleton
@Named
public class StreamUtilImpl
extends AbstractLoggableComponent
implements StreamUtil

This is the implementation of the StreamUtil interface.

Since:
1.0.0
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
getInstance()

Nested Class Summary
protected static class StreamUtilImpl.AbstractAsyncTransferrer
          This is the abstract base class for the Callable that transfers data of streams or readers/writers.
protected static class StreamUtilImpl.AsyncTransferrerImpl
          This is the default implementation of the AsyncTransferrer interface.
protected  class StreamUtilImpl.BaseTransferrer
          This is an abstract implementation of the AsyncTransferrer interface, that implements Runnable defining the main flow.
protected  class StreamUtilImpl.ReaderTransferrer
          This inner class is used to transfer a Reader to a Writer.
protected  class StreamUtilImpl.StreamTransferrer
          This inner class is used to transfer an InputStream to an OutputStream.
 
Field Summary
private  Pool<byte[]> byteArrayPool
           
private  Pool<char[]> charArrayPool
           
private  Executor executor
           
private static StreamUtil instance
           
 
Constructor Summary
StreamUtilImpl()
          The constructor.
 
Method Summary
 void close(Channel channel)
          This method closes the given channel without throwing an Exception.
 void close(InputStream inputStream)
          This method closes the given inputStream without throwing an Exception.
 void close(OutputStream outputStream)
          This method closes the given outputStream without throwing an IOException.
 void close(Reader reader)
          This method closes the given reader without throwing an Exception.
 void close(Writer writer)
          This method closes the given writer without throwing an IOException.
protected  void doInitialize()
          This method performs the actual initialization.
protected  Pool<byte[]> getByteArrayPool()
          This method gets the byte-array Pool used to transfer streams.
protected  Pool<char[]> getCharArrayPool()
          This method gets the char-array Pool used to transfer Readers and Writers.
protected  Executor getExecutor()
          This method gets the Executor used to run asynchronous tasks.
static StreamUtil getInstance()
          This method gets the singleton instance of this StreamUtilImpl.
 Properties loadProperties(InputStream inStream)
          This method loads the Properties from the given inStream and closes it.
 Properties loadProperties(Reader reader)
          This method loads the Properties from the given reader and closes it.
 String read(Reader reader)
          This method reads the contents of the given reader into a string.
 void setByteArrayPool(Pool<byte[]> byteArrayPool)
          This method sets the byte-array-pool.
 void setCharArrayPool(Pool<char[]> charArrayPool)
          This method sets the char-array-pool.
 void setExecutor(Executor executor)
          This method sets the executor.
 PrintWriter toPrintWriter(Appendable appendable)
          This method converts the given Appendable to a PrintWriter.
 Writer toWriter(Appendable appendable)
          This method converts the given Appendable to a Writer.
 long transfer(FileInputStream inStream, OutputStream outStream, boolean keepOutStreamOpen)
          This method transfers the contents of the given inStream to the given outStream using NIO-Channels.
 long transfer(InputStream inStream, FileOutputStream outStream, boolean keepOutStreamOpen, long size)
          This method transfers the contents of the given inStream to the given outStream using NIO-Channels.
 long transfer(InputStream inStream, OutputStream outStream, boolean keepOutStreamOpen)
          This method transfers the contents of the given inStream to the given outStream.
 long transfer(Reader reader, Writer writer, boolean keepWriterOpen)
          This method transfers the contents of the given reader to the given writer.
 AsyncTransferrer transferAsync(InputStream inStream, OutputStream outStream, boolean keepOutStreamOpen)
          This method transfers the contents of the given inStream to the given outStream.
 AsyncTransferrer transferAsync(InputStream inStream, OutputStream outStream, boolean keepOutStreamOpen, TransferCallback callback)
          This method transfers the contents of the given inStream to the given outStream.
 AsyncTransferrer transferAsync(Reader reader, Writer writer, boolean keepWriterOpen)
          This method transfers the contents of the given reader to the given writer.
 AsyncTransferrer transferAsync(Reader reader, Writer writer, boolean keepWriterOpen, TransferCallback callback)
          This method transfers the contents of the given reader to the given writer.
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent
getLogger, setLogger
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent
doInitialized, getInitializationState, initialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instance

private static StreamUtil instance
See Also:
getInstance()

executor

private Executor executor
See Also:
getExecutor()

byteArrayPool

private Pool<byte[]> byteArrayPool
See Also:
getByteArrayPool()

charArrayPool

private Pool<char[]> charArrayPool
See Also:
getCharArrayPool()
Constructor Detail

StreamUtilImpl

public StreamUtilImpl()
The constructor.

Method Detail

getInstance

public static StreamUtil getInstance()
This method gets the singleton instance of this StreamUtilImpl.
This design is the best compromise between easy access (via this indirection you have direct, static access to all offered functionality) and IoC-style design which allows extension and customization.
For IoC usage, simply ignore all static getInstance() methods and construct new instances via the container-framework of your choice (like plexus, pico, springframework, etc.). To wire up the dependent components everything is properly annotated using common-annotations (JSR-250). If your container does NOT support this, you should consider using a better one.

Returns:
the singleton instance.

getExecutor

protected Executor getExecutor()
This method gets the Executor used to run asynchronous tasks. It may use a thread-pool.

Returns:
the executor.

setExecutor

@Inject
public void setExecutor(Executor executor)
This method sets the executor.

Parameters:
executor - the executor to set.

getByteArrayPool

protected Pool<byte[]> getByteArrayPool()
This method gets the byte-array Pool used to transfer streams.
The implementation should create byte-arrays with a suitable length (at least 512, suggested is 4096). Override this method to use a real pool implementation.

Returns:
the Pool instance.

setByteArrayPool

public void setByteArrayPool(Pool<byte[]> byteArrayPool)
This method sets the byte-array-pool.

Parameters:
byteArrayPool - the byteArrayPool to set

getCharArrayPool

protected Pool<char[]> getCharArrayPool()
This method gets the char-array Pool used to transfer Readers and Writers. The implementation should create char-arrays with a suitable length (at least 512, suggested is 2048).
Override this method to use a real pool implementation.

Returns:
the Pool instance.

setCharArrayPool

public void setCharArrayPool(Pool<char[]> charArrayPool)
This method sets the char-array-pool.

Parameters:
charArrayPool - the charArrayPool to set

doInitialize

protected void doInitialize()
This method performs the actual initialization. It is called when AbstractComponent.initialize() is invoked for the first time.
ATTENTION:
When you override this method from a sub-class you need to do a super.AbstractComponent.doInitialize().

Overrides:
doInitialize in class AbstractLoggableComponent

read

public String read(Reader reader)
            throws IOException
This method reads the contents of the given reader into a string.
ATTENTION:
Only use this method if you know what you are doing. This method will cause that the complete content of the given reader is read into memory.

Specified by:
read in interface StreamUtil
Parameters:
reader - is where to read the content from. It will be closed at the end.
Returns:
the content of the given reader.
Throws:
IOException - if an error occurred with an I/O error.

transfer

public long transfer(Reader reader,
                     Writer writer,
                     boolean keepWriterOpen)
              throws IOException
This method transfers the contents of the given reader to the given writer.

Specified by:
transfer in interface StreamUtil
Parameters:
reader - is where to read the content from. Will be closed at the end.
writer - is where to write the content to. Will be closed at the end if keepWriterOpen is false.
keepWriterOpen - if true the given writer will remain open so that additional content can be appended. Else if false, the writer will be closed.
Returns:
the number of bytes that have been transferred.
Throws:
IOException - if the operation failed. Closing is guaranteed even in exception state.

transfer

public long transfer(FileInputStream inStream,
                     OutputStream outStream,
                     boolean keepOutStreamOpen)
              throws IOException
This method transfers the contents of the given inStream to the given outStream using NIO-Channels.

Specified by:
transfer in interface StreamUtil
Parameters:
inStream - is where to read the content from. Will be closed at the end.
outStream - is where to write the content to. Will be closed at the end if keepOutStreamOpen is false.
keepOutStreamOpen - if true the given outStream will remain open so that additional content can be appended. Else if false, the outStream will be closed.
Returns:
the number of bytes that have been transferred.
Throws:
IOException - if the operation failed. Closing is guaranteed even in exception state.

transfer

public long transfer(InputStream inStream,
                     FileOutputStream outStream,
                     boolean keepOutStreamOpen,
                     long size)
              throws IOException
This method transfers the contents of the given inStream to the given outStream using NIO-Channels.

Specified by:
transfer in interface StreamUtil
Parameters:
inStream - is where to read the content from. Will be closed at the end.
outStream - is where to write the content to. Will be closed at the end if keepOutStreamOpen is false.
keepOutStreamOpen - if true the given outStream will remain open so that additional content can be appended. Else if false, the outStream will be closed.
size - is the number of bytes to transfer.
Returns:
the number of bytes that have been transferred.
Throws:
IOException - if the operation failed. Closing is guaranteed even in exception state.

transfer

public long transfer(InputStream inStream,
                     OutputStream outStream,
                     boolean keepOutStreamOpen)
              throws IOException
This method transfers the contents of the given inStream to the given outStream.

Specified by:
transfer in interface StreamUtil
Parameters:
inStream - is where to read the content from. Will be closed at the end.
outStream - is where to write the content to. Will be closed at the end if keepOutStreamOpen is false.
keepOutStreamOpen - if true the given outStream will remain open so that additional content can be appended. Else if false, the outStream will be closed.
Returns:
the number of bytes that have been transferred.
Throws:
IOException - if the operation failed. Closing is guaranteed even in exception state.

transferAsync

public AsyncTransferrer transferAsync(InputStream inStream,
                                      OutputStream outStream,
                                      boolean keepOutStreamOpen)
This method transfers the contents of the given inStream to the given outStream.

Specified by:
transferAsync in interface StreamUtil
Parameters:
inStream - is where to read the content from. Will be closed at the end.
outStream - is where to write the content to. Will be closed at the end if keepOutStreamOpen is false.
keepOutStreamOpen - if true the given outStream will remain open so that additional content can be appended. Else if false, the outStream will be closed.
Returns:
the number of bytes that have been transferred.

transferAsync

public AsyncTransferrer transferAsync(InputStream inStream,
                                      OutputStream outStream,
                                      boolean keepOutStreamOpen,
                                      TransferCallback callback)
This method transfers the contents of the given inStream to the given outStream.

Specified by:
transferAsync in interface StreamUtil
Parameters:
inStream - is where to read the content from. Will be closed at the end.
outStream - is where to write the content to. Will be closed at the end if keepOutStreamOpen is false.
keepOutStreamOpen - if true the given outStream will remain open so that additional content can be appended. Else if false, the outStream will be closed.
callback - is the callback that is invoked if the transfer is done.
Returns:
the number of bytes that have been transferred.

transferAsync

public AsyncTransferrer transferAsync(Reader reader,
                                      Writer writer,
                                      boolean keepWriterOpen)
This method transfers the contents of the given reader to the given writer.

Specified by:
transferAsync in interface StreamUtil
Parameters:
reader - is where to read the content from. Will be closed at the end.
writer - is where to write the content to. Will be closed at the end if keepWriterOpen is false.
keepWriterOpen - if true the given writer will remain open so that additional content can be appended. Else if false, the writer will be closed.
Returns:
the number of bytes that have been transferred.

transferAsync

public AsyncTransferrer transferAsync(Reader reader,
                                      Writer writer,
                                      boolean keepWriterOpen,
                                      TransferCallback callback)
This method transfers the contents of the given reader to the given writer.

Specified by:
transferAsync in interface StreamUtil
Parameters:
reader - is where to read the content from. Will be closed at the end.
writer - is where to write the content to. Will be closed at the end if keepWriterOpen is false.
keepWriterOpen - if true the given writer will remain open so that additional content can be appended. Else if false, the writer will be closed.
callback - is the callback that is invoked if the transfer is done.
Returns:
the number of bytes that have been transferred.

loadProperties

public Properties loadProperties(InputStream inStream)
                          throws IOException
This method loads the Properties from the given inStream and closes it.
ATTENTION:
This method loads the properties using the encoding ISO-8859-1 . Use StreamUtil.loadProperties(Reader) instead to use an explicit encoding (e.g. UTF-8).

Specified by:
loadProperties in interface StreamUtil
Parameters:
inStream - is the InputStream to the properties data.
Returns:
the properties read from the given inStream.
Throws:
IOException - if the operation failed. Closing is guaranteed even in exception state.
See Also:
Properties.load(InputStream)

loadProperties

public Properties loadProperties(Reader reader)
                          throws IOException
This method loads the Properties from the given reader and closes it.

Specified by:
loadProperties in interface StreamUtil
Parameters:
reader - is the Reader to the properties data.
Returns:
the properties read from the given reader.
Throws:
IOException - if the operation failed. Closing is guaranteed even in exception state.
See Also:
Properties.load(Reader)

toPrintWriter

public PrintWriter toPrintWriter(Appendable appendable)
This method converts the given Appendable to a PrintWriter.

Specified by:
toPrintWriter in interface StreamUtil
Parameters:
appendable - is the Appendable to wrap.
Returns:
the adapting PrintWriter.

toWriter

public Writer toWriter(Appendable appendable)
This method converts the given Appendable to a Writer.

Specified by:
toWriter in interface StreamUtil
Parameters:
appendable - is the Appendable to wrap.
Returns:
the adapting Writer.

close

public void close(InputStream inputStream)
This method closes the given inputStream without throwing an Exception. If an exception occurs, it will only be logged.

Specified by:
close in interface StreamUtil
Parameters:
inputStream - is the input-stream to close.

close

public void close(OutputStream outputStream)
This method closes the given outputStream without throwing an IOException.

Specified by:
close in interface StreamUtil
Parameters:
outputStream - is the output-stream to close.

close

public void close(Writer writer)
This method closes the given writer without throwing an IOException.

Specified by:
close in interface StreamUtil
Parameters:
writer - is the writer to close.

close

public void close(Reader reader)
This method closes the given reader without throwing an Exception. If an exception occurs, it will only be logged.

Specified by:
close in interface StreamUtil
Parameters:
reader - is the reader to close.

close

public void close(Channel channel)
This method closes the given channel without throwing an Exception.

Specified by:
close in interface StreamUtil
Parameters:
channel - is the channel to close.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.