com.threerings.io
Class FramingOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by com.threerings.io.ByteBufferOutputStream
          extended by com.threerings.io.FramingOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class FramingOutputStream
extends ByteBufferOutputStream

The framing output stream accumulates output into a byte array just like the byte array output stream, but can then be instructed to send its contents over a channel, prefixed by the length (written as an integer) of the entire frame (contents plus length prefix). It does this efficiently so that data is copied as little as possible and so that the output stream to which the data is written need not be buffered because the framed output is written in a single call to write().

Note: The framing output stream does not synchronize writes to its internal buffer. It is intended to only be accessed from a single thread.


Field Summary
protected static byte[] HEADER_PAD
          We pad the beginning of our buffer so that we can write the frame length when the time comes.
 
Fields inherited from class com.threerings.io.ByteBufferOutputStream
_buffer, INITIAL_BUFFER_SIZE
 
Constructor Summary
FramingOutputStream()
           
 
Method Summary
 ByteBuffer flip()
          Flips and returns the buffer.
 ByteBuffer frameAndReturnBuffer()
          Writes the frame length to the beginning of our buffer and returns it for writing to the appropriate channel.
 void reset()
          Resets our internal buffer.
 void resetFrame()
          Resets our internal buffer and prepares to write a new frame.
 
Methods inherited from class com.threerings.io.ByteBufferOutputStream
expand, getBuffer, write, write
 
Methods inherited from class java.io.OutputStream
close, flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADER_PAD

protected static final byte[] HEADER_PAD
We pad the beginning of our buffer so that we can write the frame length when the time comes.

Constructor Detail

FramingOutputStream

public FramingOutputStream()
Method Detail

flip

public ByteBuffer flip()
Description copied from class: ByteBufferOutputStream
Flips and returns the buffer. The returned buffer will have a position of zero and a limit equal to the number of bytes written. Call ByteBufferOutputStream.reset() to reset the buffer before writing again.

Overrides:
flip in class ByteBufferOutputStream

reset

public void reset()
Description copied from class: ByteBufferOutputStream
Resets our internal buffer.

Overrides:
reset in class ByteBufferOutputStream

frameAndReturnBuffer

public ByteBuffer frameAndReturnBuffer()
Writes the frame length to the beginning of our buffer and returns it for writing to the appropriate channel. This should be followed by a call to resetFrame() when the frame has been written.


resetFrame

public void resetFrame()
Resets our internal buffer and prepares to write a new frame.