Rice Pastry API

org.mpisws.p2p.transport.util
Class SocketInputBuffer

java.lang.Object
  extended by org.mpisws.p2p.transport.util.SocketInputBuffer
All Implemented Interfaces:
InputBuffer

public class SocketInputBuffer
extends java.lang.Object
implements InputBuffer

An easy way to read a complete object in from a socket. Wraps a Socket as an InputBuffer Throws an InsufficientBytesExceptoin when there is not enough data available locally, but caches the data so you can attempt to deserialize the object from the beginning when there are additional bytes. Not thread safe! The read operations will either: a) succeed b) throw a InsufficientBytesException, which you should probably retry later when there are more bytes (automatically calls reset) c) throw a ClosedChannelException, which means the socket was closed, d) other IOException If you don't complete reading the object, and want to start from the beginning of the cache, call reset(). If you complete reading the object, but want to reuse the SocketInputBuffer, call clear(). Note that the bytesRemaining() field always returns UNKNOWN because Java's socket api doesn't give us this information To find the size of the cache call size(). This is the amount of usable bytes in the cache, not the capacity. SocketInputBuffer automatically grows. Implementation notes: readPtr/writePtr are operating on the same byte[] called cache

Author:
Jeff Hoye

Field Summary
 
Fields inherited from interface rice.p2p.commonapi.rawserialization.InputBuffer
UNKNOWN
 
Constructor Summary
SocketInputBuffer(P2PSocket socket)
           
SocketInputBuffer(P2PSocket socket, int size)
           
 
Method Summary
 int bytesRemaining()
          How much data is left in the InputBuffer.
 void clear()
          Clears the cache from memory, resetting it to the initial size.
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 boolean readBoolean()
           
 byte readByte()
           
 char readChar()
           
 double readDouble()
           
 float readFloat()
           
 int readInt()
           
 int readInternal()
           
 int readInternal(byte[] b)
           
 int readInternal(byte[] b, int off, int len)
           
 long readLong()
           
 short readShort()
           
 java.lang.String readUTF()
           
 void reset()
          Resets the read pointer to the beginning of the cache.
 int size()
          Note that this is not the number of bytes that can be read without throwing an exception, since some of these bytes may already have been consumed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SocketInputBuffer

public SocketInputBuffer(P2PSocket socket)

SocketInputBuffer

public SocketInputBuffer(P2PSocket socket,
                         int size)
Method Detail

bytesRemaining

public int bytesRemaining()
Description copied from interface: InputBuffer
How much data is left in the InputBuffer. May be UNKNOWN

Specified by:
bytesRemaining in interface InputBuffer

reset

public void reset()
Resets the read pointer to the beginning of the cache.


size

public int size()
Note that this is not the number of bytes that can be read without throwing an exception, since some of these bytes may already have been consumed. This is mostly useful for debugging.

Returns:
the number of useful bytes in the cache

readInternal

public int readInternal(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
Parameters:
b -
off -
len -
Returns:
the number of bytes read
Throws:
java.io.IOException

readInternal

public int readInternal(byte[] b)
                 throws java.io.IOException
Parameters:
b -
Returns:
the number of bytes read
Throws:
java.io.IOException

readInternal

public int readInternal()
                 throws java.io.IOException
Returns:
the value of one byte
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Specified by:
read in interface InputBuffer
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Specified by:
read in interface InputBuffer
Throws:
java.io.IOException

readByte

public byte readByte()
              throws java.io.IOException
Specified by:
readByte in interface InputBuffer
Throws:
java.io.IOException

readBoolean

public boolean readBoolean()
                    throws java.io.IOException
Specified by:
readBoolean in interface InputBuffer
Throws:
java.io.IOException

readChar

public char readChar()
              throws java.io.IOException
Specified by:
readChar in interface InputBuffer
Throws:
java.io.IOException

readDouble

public double readDouble()
                  throws java.io.IOException
Specified by:
readDouble in interface InputBuffer
Throws:
java.io.IOException

readFloat

public float readFloat()
                throws java.io.IOException
Specified by:
readFloat in interface InputBuffer
Throws:
java.io.IOException

readInt

public int readInt()
            throws java.io.IOException
Specified by:
readInt in interface InputBuffer
Throws:
java.io.IOException

readLong

public long readLong()
              throws java.io.IOException
Specified by:
readLong in interface InputBuffer
Throws:
java.io.IOException

readShort

public short readShort()
                throws java.io.IOException
Specified by:
readShort in interface InputBuffer
Throws:
java.io.IOException

readUTF

public java.lang.String readUTF()
                         throws java.io.IOException
Specified by:
readUTF in interface InputBuffer
Throws:
java.io.IOException

clear

public void clear()
           throws java.io.IOException
Clears the cache from memory, resetting it to the initial size. This is a good thing to do after you read an object.

Throws:
java.io.IOException

Rice Pastry API

Copyright © 2001-2005 - Rice Pastry.


Imprint-Dataprotection