com.healthmarketscience.rmiio.util
Class PipeBuffer

java.lang.Object
  extended by com.healthmarketscience.rmiio.util.PipeBuffer

public class PipeBuffer
extends Object

Utility class for implementing a pipe which will never block (at the expense of possibly utilizing more memory). This is useful for single threaded pipe implementations (the java.io pipe implementations require separate threads for reading and writing due to the possibility of the writer or reader blocking). Additionally, packet based read/write support is implemented, which can be a significant speed advantage when working in situations were buffers can be passed around instead of copied (e.g. RMI usage). In general, this implementation favors speed over memory usage, so buffers will not be copied if at all possible, even if they are not making the most efficient usage of memory. This class has no synchronization as it is designed for use by a single thread.

Author:
James Ahlborn

Nested Class Summary
static class PipeBuffer.InputStreamAdapter
          PacketInputStream implementation which reads from a PipeBuffer.
static class PipeBuffer.OutputStreamAdapter
          PacketOutputStream implementation which writes to a PipeBuffer.
 
Field Summary
static int DEFAULT_PACKET_SIZE
          default target size of packets returned from packet related methods and internal buffer allocation
 
Constructor Summary
PipeBuffer()
           
PipeBuffer(int packetSize)
           
 
Method Summary
 void clear()
          Clears all remaining bytes from this buffer and releases all internal buffers.
 void closeRead()
          Indicates that the "reader" of this PipeBuffer is finished.
 void closeWrite()
          Indicates that the "writer" of this PipeBuffer is finished.
 int getPacketSize()
           
 boolean hasRemaining()
           
 boolean isReadClosed()
           
 boolean isWriteClosed()
           
 int packetsAvailable()
           
 void read(byte[] buf, int pos, int len)
          Reads the given number of bytes into the given buffer starting at the given position.
 byte[] readPacket()
          Reads a packet of data from this buffer.
 long remaining()
           
 void skip(long len)
          Skips the given number of bytes in this buffer.
 void write(byte[] buf, int pos, int len)
          Writes the given number of bytes from the given buffer starting at the given position.
 void writePacket(byte[] buf, int pos, int len)
          Writes a packet of data to this buffer, where the initial data in the packet will start at the given position and have the given length.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PACKET_SIZE

public static final int DEFAULT_PACKET_SIZE
default target size of packets returned from packet related methods and internal buffer allocation

See Also:
Constant Field Values
Constructor Detail

PipeBuffer

public PipeBuffer()

PipeBuffer

public PipeBuffer(int packetSize)
Parameters:
packetSize - "suggested" size for packets returned from readPacket() as well as buffers allocated internally. in the interest of speed, actual packet sizes and internal buffer sizes may vary from this value.
Method Detail

isReadClosed

public boolean isReadClosed()
Returns:
if closeRead() has been called

closeRead

public void closeRead()
Indicates that the "reader" of this PipeBuffer is finished. Calling this method does not affect the operation of this PipeBuffer, it merely causes isReadClosed() to return true from now on. Useful for coordinating between reader and writer of the PipeBuffer.


isWriteClosed

public boolean isWriteClosed()
Returns:
if closeWrite() has been called

closeWrite

public void closeWrite()
Indicates that the "writer" of this PipeBuffer is finished. Calling this method does not affect the operation of this PipeBuffer, it merely causes isWriteClosed() to return true from now on. Useful for coordinating between reader and writer of the PipeBuffer.


getPacketSize

public int getPacketSize()

hasRemaining

public boolean hasRemaining()
Returns:
true if there are bytes to read in the buffer, false otherwise

remaining

public long remaining()
Returns:
the number of bytes which can be read from this buffer

readPacket

public byte[] readPacket()
Reads a packet of data from this buffer. This method will always return data unless there is no data at all in this buffer, in which case a BufferUnderflowException will be thrown. As such, the actual size of the returned packet may vary.


read

public void read(byte[] buf,
                 int pos,
                 int len)
Reads the given number of bytes into the given buffer starting at the given position. Throws a BufferUnderflowException if there are fewer bytes in this buffer than the given length.


skip

public void skip(long len)
Skips the given number of bytes in this buffer. Throws a BufferUnderflowException if there are fewer bytes in this buffer than the given length.


writePacket

public void writePacket(byte[] buf,
                        int pos,
                        int len)
Writes a packet of data to this buffer, where the initial data in the packet will start at the given position and have the given length. Regardless of the given length and position, the entire given buffer will now be owned by this buffer and should never be used again by the caller. Note, this call will never block.


write

public void write(byte[] buf,
                  int pos,
                  int len)
Writes the given number of bytes from the given buffer starting at the given position. Note, this call will never block.


clear

public void clear()
Clears all remaining bytes from this buffer and releases all internal buffers.


packetsAvailable

public int packetsAvailable()
Returns:
the number of "full" packets available in this buffer (where the actual definition of "full" is up to this buffer). Regardless of this value, if this buffer has bytes, they can be read by a call to readPacket (although this may be less than efficient if this value is 0).


Copyright © 2006–2016 Health Market Science. All rights reserved.