Rewritten to support I/O via channels.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1957 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-11-18 18:51:33 +00:00
parent 01b203fd13
commit d6b84eb0b8
2 changed files with 199 additions and 165 deletions
@@ -1,5 +1,5 @@
//
// $Id: FramingOutputStream.java,v 1.2 2002/11/05 02:16:46 mdb Exp $
// $Id: FramingOutputStream.java,v 1.3 2002/11/18 18:51:33 mdb Exp $
package com.threerings.io;
@@ -7,29 +7,29 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
/**
* 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 down another output stream, prefixed by the length
* (written as an integer) of those contents. 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 <code>write()</code>.
* 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
* <code>write()</code>.
*
* <p><em>Note:</em> The framing output stream does not synchronize writes
* to its internal buffer. It is intended to only be accessed from a
* single thread.
*
* <p>Implementation note: maybe this should derive from
* <code>FilterOutputStream</code> and be tied to a single
* <code>OutputStream</code> for its lifetime.
*/
public class FramingOutputStream extends OutputStream
{
public FramingOutputStream ()
{
_buffer = new byte[INITIAL_BUFFER_SIZE];
_count = 4; // leave room for the frame size at the beginning
_buffer = ByteBuffer.allocate(INITIAL_BUFFER_SIZE);
_buffer.put(HEADER_PAD);
}
/**
@@ -39,18 +39,12 @@ public class FramingOutputStream extends OutputStream
*/
public void write (int b)
{
// expand our buffer if necessary
int newcount = _count + 1;
if (newcount > _buffer.length) {
// increase the buffer size in large increments
byte[] newbuf = new byte[Math.max(_buffer.length << 1, newcount)];
System.arraycopy(_buffer, 0, newbuf, 0, _count);
_buffer = newbuf;
}
// copy and advance
_buffer[_count] = (byte)b;
_count = newcount;
try {
_buffer.put((byte)b);
} catch (BufferOverflowException boe) {
expand(1);
_buffer.put((byte)b);
}
}
/**
@@ -71,59 +65,68 @@ public class FramingOutputStream extends OutputStream
return;
}
// expand the buffer if necessary
int newcount = _count + len;
if (newcount > _buffer.length) {
// increase the buffer size in large increments
byte[] newbuf = new byte[Math.max(_buffer.length << 1, newcount)];
System.arraycopy(_buffer, 0, newbuf, 0, _count);
_buffer = newbuf;
try {
_buffer.put(b, off, len);
} catch (BufferOverflowException boe) {
expand(len);
_buffer.put(b, off, len);
}
// copy and advance
System.arraycopy(b, off, _buffer, _count, len);
_count = newcount;
}
/**
* Writes the contents of this framing output stream to the target
* output stream, prefixed by an integer with value equal to the
* number of bytes written following that integer. It then resets the
* framing output stream to prepare for another framed message.
*
* @return the total number of bytes written.
* Expands our buffer to accomodate the specified capacity.
*/
public int writeFrameAndReset (OutputStream target)
throws IOException
protected final void expand (int needed)
{
// prefix the frame with the byte count in network byte order (the
// format used by DataOutputStream)
int count = _count - 4;
_buffer[0] = (byte)((count >>> 24) & 0xFF);
_buffer[1] = (byte)((count >>> 16) & 0xFF);
_buffer[2] = (byte)((count >>> 8) & 0xFF);
_buffer[3] = (byte)((count >>> 0) & 0xFF);
// write the data
target.write(_buffer, 0, _count);
// System.err.println("Wrote frame " + (_count-4));
// reset our internal buffer
reset();
return count + 4;
int ocapacity = _buffer.capacity();
int ncapacity = _buffer.position() + needed;
if (ncapacity > ocapacity) {
// increase the buffer size in large increments
ncapacity = Math.max(ocapacity << 1, ncapacity);
ByteBuffer newbuf = ByteBuffer.allocate(ncapacity);
newbuf.put((ByteBuffer)_buffer.flip());
_buffer = newbuf;
}
}
public void reset ()
/**
* 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 {@link #reset} when the frame has been written.
*/
public ByteBuffer frameAndReturnBuffer ()
{
// leave room for the frame size at the beginning
_count = 4;
// flip the buffer which will limit it to it's current position
_buffer.flip();
// then write the frame length and rewind back to the start of the
// buffer so that all the data is available
int count = _buffer.limit();
_buffer.put((byte)((count >>> 24) & 0xFF));
_buffer.put((byte)((count >>> 16) & 0xFF));
_buffer.put((byte)((count >>> 8) & 0xFF));
_buffer.put((byte)((count >>> 0) & 0xFF));
_buffer.rewind();
return _buffer;
}
protected byte[] _buffer;
protected int _count;
/**
* Resets our internal buffer and prepares to write a new frame.
*/
public void resetFrame ()
{
_buffer.clear();
_buffer.put(HEADER_PAD);
}
/** The buffer in which we store our frame data. */
protected ByteBuffer _buffer;
/** The default initial size of the internal buffer. */
protected static final int INITIAL_BUFFER_SIZE = 32;
/** We pad the beginning of our buffer so that we can write the frame
* length when the time comes. */
protected static final byte[] HEADER_PAD = new byte[4];
}