FreeHEP API
Version current

org.freehep.util.io
Class ByteCountOutputStream

java.lang.Object
  extended byjava.io.OutputStream
      extended byjava.io.FilterOutputStream
          extended byorg.freehep.util.io.CompressableOutputStream
              extended byorg.freehep.util.io.BitOutputStream
                  extended byorg.freehep.util.io.ByteOrderOutputStream
                      extended byorg.freehep.util.io.ByteCountOutputStream
All Implemented Interfaces:
DataOutput, FinishableOutputStream
Direct Known Subclasses:
TaggedOutputStream

public class ByteCountOutputStream
extends ByteOrderOutputStream

Allows to write to some internal buffer and keep count of any set of stacked buffers written. When a pushBuffer is called, a new buffer is created. When a popBuffer is called, the number of bytes written to the popped buffer is returned. From this moment on one is writing again to the underlying buffer/outputstream. A header can be written and the header can be written. By calling append(), the previous buffer will be appended to the underlying one. This way one can write into a buffer, retrieve its length, create some header bytes and insert those in front of the just written buffer.

Version:
$Id: ByteCountOutputStream.java,v 1.11 2003/11/26 17:39:42 duns Exp $
Author:
Mark Donszelmann, Ian Graham - added popBufferBytes() for use by CGMOutputStream
Source Code:
ByteCountOutputStream.java

Field Summary
 
Fields inherited from class org.freehep.util.io.ByteOrderOutputStream
little, written
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
ByteCountOutputStream(OutputStream out, boolean littleEndian)
           
 
Method Summary
 void append()
          Inserts the bytes written as header and puts the write pointer at the end of the stream.
 void close()
          closes the stream, inserting any non-written header.
 int getBufferLength()
           
 int getLength()
           
 int popBuffer()
          returns the number of bytes written since the last pushBuffer call.
 byte[] popBufferBytes()
          Similar to pop buffer, but returns the actual byte[] buffer and then removes it from the bufferList so that subsequent appends will have no action.
 void pushBuffer()
           
 void write(int b)
           
 
Methods inherited from class org.freehep.util.io.ByteOrderOutputStream
size, writeAsciiZString, writeBoolean, writeByte, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeInt, writeLong, writeShort, writeShort, writeString, writeUnsignedByte, writeUnsignedByte, writeUnsignedInt, writeUnsignedInt, writeUnsignedShort, writeUnsignedShort, writeUTF, writeUTF
 
Methods inherited from class org.freehep.util.io.BitOutputStream
byteAlign, finish, flushByte, minBits, minBits, minBits, writeBitFlag, writeFBits, writeSBits, writeUBits
 
Methods inherited from class org.freehep.util.io.CompressableOutputStream
startCompressing, write
 
Methods inherited from class java.io.FilterOutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.io.DataOutput
write, write
 

Constructor Detail

ByteCountOutputStream

public ByteCountOutputStream(OutputStream out,
                             boolean littleEndian)
Method Detail

write

public void write(int b)
           throws IOException
Specified by:
write in interface DataOutput
Overrides:
write in class ByteOrderOutputStream
Throws:
IOException

pushBuffer

public void pushBuffer()
                throws IOException
Throws:
IOException

popBuffer

public int popBuffer()
              throws IOException
returns the number of bytes written since the last pushBuffer call. It also puts the write pointer at the start of the buffer, to be able to "insert" a header. If no buffer was ever pushed, or the last one has been popped -1 is returned.

Throws:
IOException

popBufferBytes

public byte[] popBufferBytes()
                      throws IOException
Similar to pop buffer, but returns the actual byte[] buffer and then removes it from the bufferList so that subsequent appends will have no action. When using this method, the caller is responsible for writing all buffered data as desired. The byte[] array will usually be larger than the actual content, so to determine the length of the actual data, you must call getBufferLength() before invoking this method.

Throws:
IOException

getBufferLength

public int getBufferLength()

getLength

public int getLength()

append

public void append()
            throws IOException
Inserts the bytes written as header and puts the write pointer at the end of the stream.

Throws:
IOException

close

public void close()
           throws IOException
closes the stream, inserting any non-written header.

Overrides:
close in class BitOutputStream
Throws:
IOException

FreeHEP API
Version current

Copyright © 2000-2004 FreeHEP, All Rights Reserved.