com.faunos.util.io
Class ImmutableBuffer

java.lang.Object
  extended by com.faunos.util.io.ImmutableBuffer

public class ImmutableBuffer
extends Object

A ByteBuffer holder that exposes an immutable view of it. The point here is that a buffer's position and limit are part of its state, so read-only buffers in fact have mutable state. This class is an attempt to lock down that part of a buffer's state so that we can, for example, create such things as thread-safe, buffer "constants".

Author:
Babak Farhang

Constructor Summary
ImmutableBuffer(ByteBuffer buffer)
          Creates a new instance based on the remaining bytes in the given buffer.
ImmutableBuffer(ByteBuffer buffer, boolean slice)
          Creates a new instance based on the remaining bytes in the given buffer.
 
Method Summary
 ByteBuffer buffer()
          Returns a new, read-only view of the contained buffer.
 int size()
          Returns the byte size of the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ImmutableBuffer

public ImmutableBuffer(ByteBuffer buffer)
Creates a new instance based on the remaining bytes in the given buffer. That is, the new instance encapsulates a read-only view of the current state of the given buffer.

Parameters:
buffer - the buffer from which the read-only view is constructed. The caller agrees not to change the position or limit of the input buffer.

ImmutableBuffer

public ImmutableBuffer(ByteBuffer buffer,
                       boolean slice)
Creates a new instance based on the remaining bytes in the given buffer. That is, the new instance encapsulates a read-only view of a slice of the current state of the given buffer.

Parameters:
slice - if true, then the buffer is sliced even if the remaining number of bytes and capacity of the buffer are the same; o.w. (false) the caller is implicitly agreeing not to ever change the position or limit of the input buffer.
Method Detail

buffer

public final ByteBuffer buffer()
Returns a new, read-only view of the contained buffer.


size

public final int size()
Returns the byte size of the buffer. This is just buffer().remaining().



SourceForge.net Logo