org.mortbay.io
Interface Buffer

All Superinterfaces:
Cloneable
All Known Implementing Classes:
AbstractBuffer

public interface Buffer
extends Cloneable

Byte Buffer interface. This is a byte buffer that is designed to work like a FIFO for bytes. Puts and Gets operate on different pointers into the buffer and the valid _content of the buffer is always between the getIndex and the putIndex. This buffer interface is designed to be similar, but not dependant on the java.nio buffers, which may be used to back an implementation of this Buffer. The main difference is that NIO buffer after a put have their valid _content before the position and a flip is required to access that data. For this buffer it is always true that: markValue <= getIndex <= putIndex <= capacity

Version:
1.0
Author:
gregw

Nested Class Summary
static interface Buffer.CaseInsensitve
           
 
Field Summary
static int IMMUTABLE
           
static boolean NON_VOLATILE
           
static int READONLY
           
static int READWRITE
           
static boolean VOLATILE
           
 
Method Summary
 byte[] array()
          Get the underlying array, if one exists.
 byte[] asArray()
           
 Buffer asImmutableBuffer()
           
 Buffer asMutableBuffer()
           
 Buffer asNonVolatileBuffer()
           
 Buffer asReadOnlyBuffer()
           
 Buffer buffer()
          Get the unerlying buffer.
 int capacity()
          The capacity of the buffer.
 void clear()
          Clear the buffer.
 void compact()
          Compact the buffer by discarding bytes before the postion (or mark if set).
 boolean equalsIgnoreCase(Buffer buffer)
           
 byte get()
          Get the byte at the current getIndex and increment it.
 int get(byte[] b, int offset, int length)
          Get bytes from the current postion and put them into the passed byte array.
 Buffer get(int length)
           
 int getIndex()
          The index within the buffer that will next be read or written.
 boolean hasContent()
           
 boolean isImmutable()
           
 boolean isReadOnly()
           
 boolean isVolatile()
           
 int length()
          The number of bytes from the getIndex to the putIndex
 void mark()
          Set the mark to the current getIndex.
 void mark(int offset)
          Set the mark relative to the current getIndex
 int markIndex()
          The current index of the mark.
 byte peek()
          Get the byte at the current getIndex without incrementing the getIndex.
 byte peek(int index)
          Get the byte at a specific index in the buffer.
 int peek(int index, byte[] b, int offset, int length)
           
 Buffer peek(int index, int length)
           
 int poke(int index, Buffer src)
          Put the contents of the buffer at the specific index.
 void poke(int index, byte b)
          Put a specific byte to a specific getIndex.
 int poke(int index, byte[] b, int offset, int length)
          Put a specific byte to a specific getIndex.
 int put(Buffer src)
          Write the bytes from the source buffer to the current getIndex.
 void put(byte b)
          Put a byte to the current getIndex and increment the getIndex.
 int put(byte[] b)
          Put a byte to the current getIndex and increment the getIndex.
 int put(byte[] b, int offset, int length)
          Put a byte to the current getIndex and increment the getIndex.
 int putIndex()
          The index of the first element that should not be read.
 int readFrom(InputStream in, int max)
          Read the buffer's contents from the input stream
 void reset()
          Reset the current getIndex to the mark
 void setGetIndex(int newStart)
          Set the buffers start getIndex.
 void setMarkIndex(int newMark)
          Set a specific value for the mark.
 void setPutIndex(int newLimit)
           
 int skip(int n)
          Skip _content.
 Buffer slice()
           
 Buffer sliceFromMark()
           
 Buffer sliceFromMark(int length)
           
 int space()
          the space remaining in the buffer.
 String toDetailString()
           
 void writeTo(OutputStream out)
          Write the buffer's contents to the output stream
 

Field Detail

IMMUTABLE

public static final int IMMUTABLE
See Also:
Constant Field Values

READONLY

public static final int READONLY
See Also:
Constant Field Values

READWRITE

public static final int READWRITE
See Also:
Constant Field Values

VOLATILE

public static final boolean VOLATILE
See Also:
Constant Field Values

NON_VOLATILE

public static final boolean NON_VOLATILE
See Also:
Constant Field Values
Method Detail

array

public byte[] array()
Get the underlying array, if one exists.

Returns:
a byte[] backing this buffer or null if none exists.

asArray

public byte[] asArray()
Returns:
a byte[] value of the bytes from the getIndex to the putIndex.

buffer

public Buffer buffer()
Get the unerlying buffer. If this buffer wraps a backing buffer.

Returns:
The root backing buffer or this if there is no backing buffer;

asNonVolatileBuffer

public Buffer asNonVolatileBuffer()
Returns:
a non volitile version of this Buffer value

asReadOnlyBuffer

public Buffer asReadOnlyBuffer()
Returns:
a readonly version of this Buffer.

asImmutableBuffer

public Buffer asImmutableBuffer()
Returns:
an immutable version of this Buffer.

asMutableBuffer

public Buffer asMutableBuffer()
Returns:
an immutable version of this Buffer.

capacity

public int capacity()
The capacity of the buffer. This is the maximum putIndex that may be set.

Returns:
an int value

space

public int space()
the space remaining in the buffer.

Returns:
capacity - putIndex

clear

public void clear()
Clear the buffer. getIndex=0, putIndex=0.


compact

public void compact()
Compact the buffer by discarding bytes before the postion (or mark if set). Bytes from the getIndex (or mark) to the putIndex are moved to the beginning of the buffer and the values adjusted accordingly.


get

public byte get()
Get the byte at the current getIndex and increment it.

Returns:
The byte value from the current getIndex.

get

public int get(byte[] b,
               int offset,
               int length)
Get bytes from the current postion and put them into the passed byte array. The getIndex is incremented by the number of bytes copied into the array.

Parameters:
b - The byte array to fill.
offset - Offset in the array.
length - The max number of bytes to read.
Returns:
The number of bytes actually read.

get

public Buffer get(int length)
Parameters:
length - an int value
Returns:
a Buffer value

getIndex

public int getIndex()
The index within the buffer that will next be read or written.

Returns:
an int value >=0 <= putIndex()

hasContent

public boolean hasContent()
Returns:
true of putIndex > getIndex

equalsIgnoreCase

public boolean equalsIgnoreCase(Buffer buffer)
Returns:
a boolean value true if case sensitive comparison on this buffer

isImmutable

public boolean isImmutable()
Returns:
a boolean value true if the buffer is immutable and that neither the buffer contents nor the indexes may be changed.

isReadOnly

public boolean isReadOnly()
Returns:
a boolean value true if the buffer is readonly. The buffer indexes may be modified, but the buffer contents may not. For example a View onto an immutable Buffer will be read only.

isVolatile

public boolean isVolatile()
Returns:
a boolean value true if the buffer contents may change via alternate paths than this buffer. If the contents of this buffer are to be used outside of the current context, then a copy must be made.

length

public int length()
The number of bytes from the getIndex to the putIndex

Returns:
an int == putIndex()-getIndex()

mark

public void mark()
Set the mark to the current getIndex.


mark

public void mark(int offset)
Set the mark relative to the current getIndex

Parameters:
offset - an int value to add to the current getIndex to obtain the mark value.

markIndex

public int markIndex()
The current index of the mark.

Returns:
an int index in the buffer or -1 if the mark is not set.

peek

public byte peek()
Get the byte at the current getIndex without incrementing the getIndex.

Returns:
The byte value from the current getIndex.

peek

public byte peek(int index)
Get the byte at a specific index in the buffer.

Parameters:
index - an int value
Returns:
a byte value

peek

public Buffer peek(int index,
                   int length)
Parameters:
index - an int value
length - an int value
Returns:
The Buffer value from the requested getIndex.

peek

public int peek(int index,
                byte[] b,
                int offset,
                int length)
Parameters:
index - an int value
b - The byte array to peek into
offset - The offset into the array to start peeking
length - an int value
Returns:
The number of bytes actually peeked

poke

public int poke(int index,
                Buffer src)
Put the contents of the buffer at the specific index.

Parameters:
index - an int value
src - a Buffer. If the source buffer is not modified
Returns:
The number of bytes actually poked

poke

public void poke(int index,
                 byte b)
Put a specific byte to a specific getIndex.

Parameters:
index - an int value
b - a byte value

poke

public int poke(int index,
                byte[] b,
                int offset,
                int length)
Put a specific byte to a specific getIndex.

Parameters:
index - an int value
b - a byte array value
Returns:
The number of bytes actually poked

put

public int put(Buffer src)
Write the bytes from the source buffer to the current getIndex.

Parameters:
src - The source Buffer it is not modified.
Returns:
The number of bytes actually poked

put

public void put(byte b)
Put a byte to the current getIndex and increment the getIndex.

Parameters:
b - a byte value

put

public int put(byte[] b,
               int offset,
               int length)
Put a byte to the current getIndex and increment the getIndex.

Parameters:
b - a byte value
Returns:
The number of bytes actually poked

put

public int put(byte[] b)
Put a byte to the current getIndex and increment the getIndex.

Parameters:
b - a byte value
Returns:
The number of bytes actually poked

putIndex

public int putIndex()
The index of the first element that should not be read.

Returns:
an int value >= getIndex()

reset

public void reset()
Reset the current getIndex to the mark


setGetIndex

public void setGetIndex(int newStart)
Set the buffers start getIndex.

Parameters:
newStart - an int value

setMarkIndex

public void setMarkIndex(int newMark)
Set a specific value for the mark.

Parameters:
newMark - an int value

setPutIndex

public void setPutIndex(int newLimit)
Parameters:
newLimit - an int value

skip

public int skip(int n)
Skip _content. The getIndex is updated by min(remaining(), n)

Parameters:
n - The number of bytes to skip
Returns:
the number of bytes skipped.

slice

public Buffer slice()
Returns:
a volitile Buffer from the postion to the putIndex.

sliceFromMark

public Buffer sliceFromMark()
Returns:
a volitile Buffer value from the mark to the putIndex

sliceFromMark

public Buffer sliceFromMark(int length)
Parameters:
length - an int value
Returns:
a valitile Buffer value from the mark of the length requested.

toDetailString

public String toDetailString()
Returns:
a String value describing the state and contents of the buffer.

writeTo

public void writeTo(OutputStream out)
             throws IOException
Write the buffer's contents to the output stream

Parameters:
out -
Throws:
IOException

readFrom

public int readFrom(InputStream in,
                    int max)
             throws IOException
Read the buffer's contents from the input stream

Parameters:
in - input stream
max - maximum number of bytes that may be read
Returns:
actual number of bytes read or -1 for EOF
Throws:
IOException