ewe.io
Class OutputStream

java.lang.Object
  extended byewe.io.OutputStream
All Implemented Interfaces:
Streamable
Direct Known Subclasses:
ByteArrayOutputStream, EncryptedOutputStream, FileOutputStream, FilterOutputStream, SocketBase.SocketOutputStream, ZipOutputStream

public class OutputStream
extends Object
implements Streamable

An OutputStream is used to write bytes - however all write operations will block the current thread. This is not meant to be a replacement for a ewe.io.Stream - rather it is meant to make it easier to port Java applications.

This can be used either as the base class for creating your own OutputStream objects, OR it can be used to adapt a Stream object into an OutputStream.


Field Summary
protected  boolean closed
           
protected  OutputStream out
           
protected  RandomStream rs
           
protected  Stream stream
           
 
Constructor Summary
protected OutputStream()
           
protected OutputStream(OutputStream out)
           
  OutputStream(RandomStream rs)
           
  OutputStream(Stream stream)
           
 
Method Summary
 void close()
           
 void flush()
           
 FastStream getFastStream()
          If the underlying Stream object implements FastStream this method will return that Stream object.
 String getName()
          Get the name associated with this Streamable object.
 void shutdown()
          Shutdown the stream but do not close any underlying IO stream.
 Handle toStream(boolean randomStream, String mode)
          Requests the possibly asynchronous creation of a Stream object for reading or writing.
 Stream toWritableStream()
          Convert this OutputStream to a Stream implementation that allows writing.
 void write(byte[] buffer)
          Write a number of bytes of data to the output stream equal to the length of the provided buffer.
 void write(byte[] buffer, int start, int length)
          Write a number of bytes of data to the output stream.
 void write(int value)
          Writes a single byte to the stream.
protected  void writeSingleByteToMultiByteWrite(int value)
          This writes a single byte using the OutputStream write(byte[] buffer,int start,int lengh) method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

stream

protected Stream stream

rs

protected RandomStream rs

out

protected OutputStream out

closed

protected boolean closed
Constructor Detail

OutputStream

protected OutputStream()

OutputStream

public OutputStream(Stream stream)

OutputStream

public OutputStream(RandomStream rs)

OutputStream

protected OutputStream(OutputStream out)
Method Detail

toStream

public Handle toStream(boolean randomStream,
                       String mode)
Description copied from interface: Streamable
Requests the possibly asynchronous creation of a Stream object for reading or writing.

Specified by:
toStream in interface Streamable
Parameters:
randomStream - if this is true then a request is being made for a RandomAccessStream, otherwise either a readable or writable Stream will be returned.
mode - For RandomAccessStreams this can be "r" or "rw". For writable Streams this should be "w" or "a" (for append) and for readable Streams it should be "r".
Returns:
A Handle used to form monitoring the creation process and for retrieving the final value.

The calling code should wait on the Handle.Success flag to be set. If this happens the returnValue of the Handle will be set to the acquired Stream. If the Handle fails then the errorObject of the Handle will be set to an IOException.


getName

public String getName()
Description copied from interface: Streamable
Get the name associated with this Streamable object.

Specified by:
getName in interface Streamable

close

public void close()
           throws IOException
Throws:
IOException

flush

public void flush()
           throws IOException
Throws:
IOException

shutdown

public void shutdown()
              throws IOException
Shutdown the stream but do not close any underlying IO stream. Further writes will throw an IOException. For example if this OutputStream is on a Socket, this will prevent further outputs but will still allow reading on the InputStream.

Throws:
IOException - on error.

writeSingleByteToMultiByteWrite

protected void writeSingleByteToMultiByteWrite(int value)
                                        throws IOException
This writes a single byte using the OutputStream write(byte[] buffer,int start,int lengh) method. If you override the multi-byte write operation instead of the single byte write operation, then override void write(int value) to call this method.

Throws:
IOException

write

public void write(int value)
           throws IOException
Writes a single byte to the stream. This method blocks until the byte is written.

Throws:
IOException - if an I/O error occured.

write

public void write(byte[] buffer,
                  int start,
                  int length)
           throws IOException
Write a number of bytes of data to the output stream. This will block until all the bytes are written.

Parameters:
buffer - the source buffer for the data.
start - The start offset in the buffer.
length - The number of bytes to write.
Throws:
IOException - if an I/O error occurs during writing.

write

public void write(byte[] buffer)
           throws IOException
Write a number of bytes of data to the output stream equal to the length of the provided buffer.

Parameters:
buffer - the source buffer for the data.
Returns:
the total number of bytes written from the buffer - which is always equal to the length of the buffer.
Throws:
IOException - if an I/O error occurs during writing.

toWritableStream

public Stream toWritableStream()
Convert this OutputStream to a Stream implementation that allows writing. This will never return null.


getFastStream

public FastStream getFastStream()
If the underlying Stream object implements FastStream this method will return that Stream object. If this OutputStream also happens to implement FastStream, then this OutputStream will be returned. Otherwise the method will return null.