org.freehep.util.io
Class ByteCountOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- org.freehep.util.io.CompressableOutputStream
-
- org.freehep.util.io.BitOutputStream
-
- org.freehep.util.io.ByteOrderOutputStream
-
- org.freehep.util.io.ByteCountOutputStream
-
- All Implemented Interfaces:
- java.io.Closeable, java.io.DataOutput, java.io.Flushable, java.lang.AutoCloseable, 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.
-
-
Constructor Summary
Constructors Constructor and Description ByteCountOutputStream(java.io.OutputStream out, boolean littleEndian)Create a Byte Count output stream from given stream
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidappend()Inserts the bytes written as header and puts the write pointer at the end of the stream.voidclose()closes the stream, inserting any non-written header.intgetBufferLength()intgetLength()intpopBuffer()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.voidpushBuffer()Pushes the buffer and strat writing to a new one.voidwrite(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, minBits, minBits, minBits, writeBitFlag, writeFBits, writeSBits, writeUBits
-
Methods inherited from class org.freehep.util.io.CompressableOutputStream
startCompressing, write
-
-
-
-
Constructor Detail
-
ByteCountOutputStream
public ByteCountOutputStream(java.io.OutputStream out, boolean littleEndian)Create a Byte Count output stream from given stream- Parameters:
out- stream to write tolittleEndian- true if stream should be little endian
-
-
Method Detail
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Overrides:
writein classByteOrderOutputStream- Throws:
java.io.IOException
-
pushBuffer
public void pushBuffer() throws java.io.IOExceptionPushes the buffer and strat writing to a new one.- Throws:
java.io.IOException- if the write fails
-
popBuffer
public int popBuffer() throws java.io.IOExceptionreturns 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.- Returns:
- number of bytes written or -1
- Throws:
java.io.IOException- if the write fails
-
popBufferBytes
public byte[] popBufferBytes() throws java.io.IOExceptionSimilar 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.- Returns:
- byte array of bytes that need to be written
- Throws:
java.io.IOException- if write fails
-
getBufferLength
public int getBufferLength()
- Returns:
- valid number of bytes in the buffer
-
getLength
public int getLength()
- Returns:
- total number of bytes written
-
append
public void append() throws java.io.IOExceptionInserts the bytes written as header and puts the write pointer at the end of the stream.- Throws:
java.io.IOException- if write fails
-
close
public void close() throws java.io.IOExceptioncloses the stream, inserting any non-written header.- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfacejava.lang.AutoCloseable- Overrides:
closein classBitOutputStream- Throws:
java.io.IOException
-
-
DMelt 3.0 © DataMelt by jWork.ORG