Basic support for datagram messaging. Hope I haven't fucked
anything up! git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5090 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// $Id$
|
||||
|
||||
package com.threerings.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import java.nio.BufferUnderflowException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.InvalidMarkException;
|
||||
|
||||
/**
|
||||
* Reads input from a {@link ByteBuffer}.
|
||||
*/
|
||||
public class ByteBufferInputStream extends InputStream
|
||||
{
|
||||
/**
|
||||
* Creates a new input stream to read from the specified buffer.
|
||||
*/
|
||||
public ByteBufferInputStream (ByteBuffer buffer)
|
||||
{
|
||||
_buffer = buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a reference to the underlying buffer.
|
||||
*/
|
||||
public ByteBuffer getBuffer ()
|
||||
{
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public int read ()
|
||||
{
|
||||
try {
|
||||
return (_buffer.get() & 0xFF);
|
||||
} catch (BufferUnderflowException e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public int read (byte[] b, int offset, int length)
|
||||
throws IOException
|
||||
{
|
||||
length = Math.min(length, _buffer.remaining());
|
||||
if (length <= 0) {
|
||||
return -1;
|
||||
}
|
||||
_buffer.get(b, offset, length);
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public long skip (long n)
|
||||
throws IOException
|
||||
{
|
||||
n = Math.min(n, _buffer.remaining());
|
||||
_buffer.position((int)(_buffer.position() + n));
|
||||
return n;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public int available ()
|
||||
{
|
||||
return _buffer.remaining();
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public boolean markSupported ()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public void mark (int readLimit)
|
||||
{
|
||||
_buffer.mark();
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public void reset ()
|
||||
throws IOException
|
||||
{
|
||||
try {
|
||||
_buffer.reset();
|
||||
} catch (InvalidMarkException e) {
|
||||
throw new IOException("No mark set.");
|
||||
}
|
||||
}
|
||||
|
||||
/** The buffer from which we read. */
|
||||
protected ByteBuffer _buffer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user