org.objectweb.jonathan.resources.api
Class Chunk

java.lang.Object
  extended by org.objectweb.jonathan.resources.api.Chunk
Direct Known Subclasses:
TcpIpChunkProvider

public class Chunk
extends Object

A chunk represents a part of an array of bytes. Chunks are linked to form messages that may be sent from an adress space to another. Their use avoids unnecessarily copying arrays of bytes, and helps recovering these arrays without resorting to garbage collection (thanks to chunk factories).

Chunks should not be used concurrently.


Field Summary
 byte[] data
          The associated array of bytes.
 Chunk next
          The next chunk in the chain
 int offset
          The index of the first valid (written) byte in this chunk.
 int top
          Index of the last valid (written) byte in this chunk + 1.
 
Constructor Summary
Chunk(byte[] data, int offset, int top)
          Constructs a new chunk with the specified data and offsets.
 
Method Summary
 Chunk duplicate()
          Duplicates the whole chunk.
 Chunk duplicate(int offset, int top)
          Partially duplicates this chunk.
 void release()
          Releases the chunk.
 String toString()
          Returns a string representation of the target chunk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

data

public byte[] data
The associated array of bytes.


top

public int top
Index of the last valid (written) byte in this chunk + 1.


offset

public int offset
The index of the first valid (written) byte in this chunk.


next

public Chunk next
The next chunk in the chain

Constructor Detail

Chunk

public Chunk(byte[] data,
             int offset,
             int top)
Constructs a new chunk with the specified data and offsets.

Parameters:
data - the byte array containing the data.
offset - the offset of the first valid byte in the chunk.
top - offset + the number of valid bytes in the chunk.
Method Detail

duplicate

public Chunk duplicate()
                throws org.objectweb.jonathan.apis.kernel.JonathanException
Duplicates the whole chunk.

The default implementation copies the buffer, and creates a new chunk with it.

Returns:
a copy of this chunk.
Throws:
org.objectweb.jonathan.exceptions.JonathanException - if an IO error occurs.
org.objectweb.jonathan.apis.kernel.JonathanException

duplicate

public Chunk duplicate(int offset,
                       int top)
                throws org.objectweb.jonathan.apis.kernel.JonathanException
Partially duplicates this chunk. 'offset' must be greater than the target chunk's offset, 'top' must be less or equal than the target's top.

The default implementation copies the appropriate portion of the buffer, and creates a new chunk with it.

Parameters:
offset - the offset of the chunk copy.
top - the top of the chunk copy.
Returns:
a chunk containing the specified part of the target chunk.
Throws:
org.objectweb.jonathan.exceptions.JonathanException - if an error occurs.
org.objectweb.jonathan.apis.kernel.JonathanException

release

public void release()
Releases the chunk. The data of a chunk may be obtained from managed buffers. It may thus be necessary to tell when the data encapsulated by a chunk may be reused.

The default implementation resets offset and top to 0.


toString

public String toString()
Returns a string representation of the target chunk.

Overrides:
toString in class Object
Returns:
a string representation of the target chunk.