ca.idms.io
Class TraceBufferInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.ByteArrayInputStream
          extended by ca.idms.io.TraceBufferInputStream
All Implemented Interfaces:
java.io.Closeable

public class TraceBufferInputStream
extends java.io.ByteArrayInputStream

A TraceBufferInputStream extends ByteArrayInputStream to provide tracing and buffering of an underlying InputStream.

When the read(int) method is called, int bytes of data are read from the input stream into a buffer, which is allocated as needed, and the contents of the buffer are displayed in the trace stream, if enabled. The ByteArrayInputStream methods are used to read data from 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:
1.00, 02/18/99
Author:
Dave Ross
See Also:
TraceObject, ByteArrayInputStream

Field Summary
protected  java.io.InputStream in
          The underlying input stream.
protected  TraceObject trace
          The trace control object.
 
Fields inherited from class java.io.ByteArrayInputStream
buf, count, mark, pos
 
Constructor Summary
TraceBufferInputStream(TraceObject trace, byte[] buf)
          Creates a buffered trace stream from a byte array.
TraceBufferInputStream(TraceObject trace, byte[] buf, int off, int len)
          Creates a buffered trace stream from a byte array.
TraceBufferInputStream(TraceObject trace, java.io.InputStream in)
          Creates a buffered trace stream with a default buffer size.
TraceBufferInputStream(TraceObject trace, java.io.InputStream in, int len)
          Creates a buffered trace stream to read data from the specified underlying input stream.
 
Method Summary
 void close()
          Closes the ByteArrayInputStream and releases the buffer.
 byte[] getBuffer()
          Gets the buffer.
 int getCount()
          Gets the number of valid bytes in the buffer.
 int getPosition()
          Gets the index of the next character to be read.
 int read(int len)
          Reads the next buffer of data from the input stream.
 void setSize(int len)
          Sets the size of the buffer.
 long skip(long n)
          Skips over n bytes of data in the buffer.
 void snap()
          Displays the contents of this buffer if tracing is enabled.
 
Methods inherited from class java.io.ByteArrayInputStream
available, mark, markSupported, read, read, reset
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

trace

protected TraceObject trace
The trace control object.


in

protected java.io.InputStream in
The underlying input stream.

Constructor Detail

TraceBufferInputStream

public TraceBufferInputStream(TraceObject trace,
                              java.io.InputStream in,
                              int len)
Creates a buffered trace stream to read data from the specified underlying input stream.

Parameters:
trace - the trace control object
in - the underlying input stream.
len - the initial size of the buffer.

TraceBufferInputStream

public TraceBufferInputStream(TraceObject trace,
                              java.io.InputStream in)
Creates a buffered trace stream with a default buffer size.

Parameters:
trace - the trace control object
in - the underlying input stream.

TraceBufferInputStream

public TraceBufferInputStream(TraceObject trace,
                              byte[] buf)
Creates a buffered trace stream from a byte array.

Parameters:
trace - the trace control object
buf - the byte array.

TraceBufferInputStream

public TraceBufferInputStream(TraceObject trace,
                              byte[] buf,
                              int off,
                              int len)
Creates a buffered trace stream from a byte array.

Parameters:
trace - the trace control object
buf - the byte array.
off - offset into the byte array.
len - length of sub array.
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.


getBuffer

public byte[] getBuffer()
Gets the buffer. This method allows access to the buffer without copying large byte arrays any more than necessary. Note that subsequent reads from the underlying input stream can change the contents of the buffer.

Returns:
a reference to the underlying buffer.

getCount

public int getCount()
Gets the number of valid bytes in the buffer.


getPosition

public int getPosition()
Gets the index of the next character to be read.


snap

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

See Also:
TraceObject

read

public int read(int len)
         throws java.io.IOException
Reads the next buffer of data from the input stream. This reallocates the input buffer, if necessary, and reads up to len bytes from the underlying input stream.

Parameters:
len - the number of bytes to read.
Returns:
the number of bytes read.
Throws:
java.io.EOFException - if end of file on input.
java.io.IOException - if an I/O error occurs.

skip

public long skip(long n)
Skips over n bytes of data in the buffer.

This method also avoids what appears to be bug in DataInputStream, which enters an infinite loop when n is greater than the number of bytes available. In that case this pretends it skipped the requested number of bytes.

Overrides:
skip in class java.io.ByteArrayInputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the requested number of bytes.

close

public void close()
           throws java.io.IOException
Closes the ByteArrayInputStream and releases the buffer.

Note that the underlying input stream is not closed.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.ByteArrayInputStream
Throws:
java.io.IOException - if an I/O error occurs.


Copyright © 2009 CA, All rights reserved