ProgramixGenericLib v5.0.1

com.programix.io
Class StreamConverter

java.lang.Object
  extended by com.programix.io.StreamConverter

public class StreamConverter
extends Object

Converts an OutputStream to an InputStream.

Bytes can be written to the OutputStream (accessed by calling getOutputStream()) up the the capacity of the internal buffer. While the buffer is full, further attempts to write bytes block until another thread removes bytes from the buffer via the StreamConverter's InputStream. Flushing the OutputStream blocks the calling thread until the buffer has been emptied by the thread reading from the InputStream. Closing the OutputStream automatically flushes the stream first, and then closes the stream.

Bytes can be read from the InputStream (accessed by calling getInputStream()). While the buffer is empty, attempts to read bytes block until another thread adds bytes to the buffer via the StreamConverter's OutputStream. Closing the InputStream stops any more bytes from being written to the OutputStream (an exception is thrown if this is attempted).

If the OutputStream is closed first, the close operation can not complete until the buffer is flushed (empty). Any further attempts to write bytes results in an exception being thrown. The InputStream can otherwise take its time getting around to closing. The close method on the OutputStream may be safely called more than once.

If the InputStream is closed first, the internals also force the closing of the OutputStream. Any further attempts to read from the InputStream result in an IOException. Similarly, any further attempts to write to the OutputStream also result in an exception being thrown (there is no way that they could ever be read!). The close method on the InputStream may be safely called more than once.

Most often, two threads will be interacting with the StreamConverter: one doing the writing to the OutputStream and one doing the reading from the InputStream. It is possible that only one thread is used to first write and then to read, but this should be approached with caution as this single thread could get eternally blocked trying to write when the buffer is full or trying to read when the buffer is empty.

Author:
Paul Hyde

Field Summary
static int DEFAULT_BUFFER_SIZE
          The default size used for the internal buffer between the input stream and the output stream.
 
Constructor Summary
StreamConverter()
           
StreamConverter(int bufferSize)
           
 
Method Summary
 InputStream getInputStream()
           
 OutputStream getOutputStream()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
The default size used for the internal buffer between the input stream and the output stream. This value is always 2048.

See Also:
Constant Field Values
Constructor Detail

StreamConverter

public StreamConverter(int bufferSize)

StreamConverter

public StreamConverter()
Method Detail

getOutputStream

public OutputStream getOutputStream()

getInputStream

public InputStream getInputStream()

ProgramixGenericLib v5.0.1

Copyright © 2001-2009 Programix Incorporated. All rights reserved. ProgramixGenericLib is free and is OSI Certified Open Source Software under the BSD license.