ca.idms.io
Class TraceBufferOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.ByteArrayOutputStream
          extended by ca.idms.io.TraceBufferOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class TraceBufferOutputStream
extends java.io.ByteArrayOutputStream

A TraceBufferOutputStream extends ByteArrayOutputStream to provide tracing and buffering of an underlying OutputStream.

Data written to the stream is collected in a buffer, which grows as needed. When the flush method is called, the contents of the buffer are displayed in the trace stream, if enabled, and written to the output stream. The ByteArrayInputStream methods are used to write data to the buffer.

This class also provides methods to optimize buffering by explicitly setting the size buffer and allowing access to the buffer without copying it.

Version:
2.00, 12/14/99
Author:
Dave Ross
See Also:
TraceObject, ByteArrayOutputStream

Field Summary
protected  java.io.OutputStream out
          The actual output stream.
protected  TraceObject trace
          The trace control object.
 
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
 
Constructor Summary
TraceBufferOutputStream(TraceObject trace, int len)
          Creates a buffered trace stream with no underlying output stream.
TraceBufferOutputStream(TraceObject trace, java.io.OutputStream out)
          Creates a buffered trace stream with a default buffer size.
TraceBufferOutputStream(TraceObject trace, java.io.OutputStream out, int len)
          Creates a buffered trace stream to write data to the specified underlying output stream.
 
Method Summary
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out to the stream.
 byte[] getBuffer()
          Gets a reference to the buffer.
 int getCount()
          Gets the number of valid bytes in the buffer.
 void setBuffer(byte[] buf, int len)
          Sets the buffer.
 void setCount(int len)
          Set the number of valid bytes in the buffer.
 void setSize(int len)
          Sets the size of the buffer.
 void snap(boolean f)
          Displays the contents of this buffer if tracing is enabled.
 
Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toByteArray, toString, toString, toString, write, write, writeTo
 
Methods inherited from class java.io.OutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

trace

protected TraceObject trace
The trace control object.


out

protected java.io.OutputStream out
The actual output stream.

Constructor Detail

TraceBufferOutputStream

public TraceBufferOutputStream(TraceObject trace,
                               java.io.OutputStream out,
                               int len)
Creates a buffered trace stream to write data to the specified underlying output stream.

Parameters:
trace - the trace control object
out - the underlying output stream.
len - the initial size of the buffer.

TraceBufferOutputStream

public TraceBufferOutputStream(TraceObject trace,
                               java.io.OutputStream out)
Creates a buffered trace stream with a default buffer size.

Parameters:
trace - the trace control object
out - the underlying output stream.

TraceBufferOutputStream

public TraceBufferOutputStream(TraceObject trace,
                               int len)
Creates a buffered trace stream with no underlying output stream.

Parameters:
trace - the trace control object
len - the initial size of the buffer.
Method Detail

setSize

public void setSize(int len)
Sets the size of the buffer. This method allows optimization of the buffer size. The previous contents of the buffer are released.

Parameters:
len - new buffer size.

getBuffer

public byte[] getBuffer()
Gets a reference to the buffer. In contrast to ByteArrayOutputStream.toByteArray(), this method returns a reference to the actual byte array, rather than a copy of the array contents.

Note that subsequent writes to this stream can change the contents of the buffer, and changing the contents of the buffer can corrupt the state of the stream.

Returns:
buf.

setBuffer

public void setBuffer(byte[] buf,
                      int len)
Sets the buffer. This method allows an existing byte array to be inserted into the output stream. The previous contents of the buffer are released.

Parameters:
buf - the new buffer contents
len - the length used in the array

getCount

public int getCount()
Gets the number of valid bytes in the buffer. This is equivalent to ByteArrayOutputStream.size(), named to be consistent with TraceBufferInputStream.

Returns:
count

setCount

public void setCount(int len)
Set the number of valid bytes in the buffer. This can be used to reset the position in the buffer to overwrite it's contents.

Parameters:
len - new count, ignored if out of range
See Also:
getCount()

snap

public void snap(boolean f)
Displays the contents of this buffer if tracing is enabled. Enabled by TraceObject.trace() and TraceObject.snapBytes.

See Also:
TraceObject

flush

public void flush()
           throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out to the stream.

The buffer is displayed in the log if tracing bytes is enabled. This method will throw a NullPointerException if there is no underlying output stream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
TraceObject


Copyright © 2009 CA, All rights reserved