public class ChunkList extends Object implements Iterable<Chunk>
chunks
with data.
This class is used for containment and transfer of variable-sized binary data
within the system. It uses a list of limited-capacity chunks
to accommodate data of any size.
This class does not implement Collection
interface but some of
its methods conform with methods of standard collections (such as
ArrayList
and Queue
) from Java Collections Framework.
Chunk lists are designed to be reusable in much the same way as individual
chunks
; they have similar reuse, sharing and owning API
methods.
Chunk lists may be acquired from pools via getChunkList(owner)
method and recycled via recycle(owner)
method.
They also have owners, supports handover
and may be marked as read-only
.
A read-only
chunk list may contain only
read-only
chunks.
All chunks within the chunk list (except for the read-only ones) are
owned by the chunk list. Thus, adding and removing
chunks into/from the list implies handover
of these chunks from external owner to the list and vice versa.
Only the owner of the list may add and remove chunks to/from the list.
The ChunkList
cannot contain null
chunks.
ChunkList
implements Iterable
interface. The particular
feature of its Iterator
is that it is reusable: after the invocation
of Iterator.hasNext()
returns false
the iterator
becomes invalid and may no longer be used, however several iterator
instances may still be acquired and used concurrently. This feature provides
garbage-free "foreach" iteration over chunk lists.
The only only exception are read-only
chunk lists that can be used (for read only)
concurrently from multiple threads.
Chunk
,
ChunkPool
,
ChunkedInput
,
ChunkedOutput
Modifier and Type | Field and Description |
---|---|
protected Chunk[] |
chunkArray |
static ChunkList |
EMPTY
Read-only empty chunk list.
|
protected int |
head |
protected WeakReference<ChunkPool> |
poolReference |
protected int |
tail |
Modifier | Constructor and Description |
---|---|
protected |
ChunkList(ChunkPool pool,
Object owner)
Constructs new chunk list of specified owner corresponding
to a specified
pool . |
|
ChunkList(Object owner)
Constructs empty chunk list of specified owner not corresponding
to any
pool . |
Modifier and Type | Method and Description |
---|---|
void |
add(byte[] bytes,
int offset,
int length)
Adds to this chunk list data from specified byte array.
|
void |
add(Chunk chunk,
Object owner)
Adds given chunk to the end of this chunk list.
|
void |
addAll(ChunkList chunks,
Object owner)
Adds chunks from specified chunk list into the end of this one.
|
protected void |
checkOwner(Object owner)
Throws an exception if the chunk list is not owned by specified owner.
|
protected void |
checkRange(int index) |
Chunk |
get(int index)
Returns the chunk located at the specified position of the list.
|
ChunkPool |
getPool()
Returns chunk pool that this chunk list was allocated from or
null if none. |
long |
getTotalLength()
Returns the total number of bytes stored in chunks, i.e.
|
void |
handOver(Object oldOwner,
Object newOwner)
Hands over the chunk list to another owner.
|
boolean |
isEmpty()
Checks whether this chunk list is empty, i.e.
|
boolean |
isReadOnly()
Checks whether the chunk list is read-only.
|
Iterator<Chunk> |
iterator()
Returns an
iterator over the chunks. |
void |
markReadOnly(Object owner)
Marks this chunk list as
isReadOnly() . |
Chunk |
poll(Object owner)
Retrieves a chunk from the beginning of the list, hands it over to the
owner of this chunk list and returns it.
|
Chunk |
pollLast(Object owner)
Retrieves a chunk from the end of the list, hands it over to the
owner of this chunk list and returns it.
|
void |
recycle(Object owner)
Returns this chunk list as well as all the chunks it contains into the pool.
|
void |
setChunkRange(int index,
int offset,
int length,
Object owner)
Sets range for specified chunk.
|
int |
size()
Returns the number of chunks in this list.
|
String |
toString()
Returns a string representation of the object.
|
static ChunkList |
wrap(byte[] bytes,
int offset,
int length,
Object owner)
Constructs a new chunk list of specified owner that wraps specified byte array
with specified range of data, and does not correspond to any
pool . |
static ChunkList |
wrap(byte[] bytes,
Object owner)
Constructs a new chunk list of specified owner that wraps specified byte array
and does not correspond to any
pool . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
public static final ChunkList EMPTY
protected final WeakReference<ChunkPool> poolReference
protected Chunk[] chunkArray
protected int head
protected int tail
public ChunkList(Object owner)
pool
.owner
- owner of new chunk listpublic static ChunkList wrap(byte[] bytes, Object owner)
pool
.
Note: wrapping shall be used only with 'throw-away' arrays that will not be used by source code after wrapping.
bytes
- the byte array to wrapowner
- owner of new chunk listNullPointerException
- if bytes
is null
.public static ChunkList wrap(byte[] bytes, int offset, int length, Object owner)
pool
.
Note: wrapping shall be used only with 'throw-away' arrays that will not be used by source code after wrapping.
bytes
- the byte array to wrapoffset
- position of the first meaningful bytelength
- total number of meaningful bytesowner
- owner of new chunk listNullPointerException
- if bytes
is null
.IndexOutOfBoundsException
- if the region defined by offset
and length
is out of underlying byte array rangepublic ChunkPool getPool()
null
if none.null
if none.public int size()
getTotalLength()
method should be used.public boolean isEmpty()
false
if there are any chunks
even if they are all empty.true
if there is no chunks in this chunk listpublic Chunk get(int index)
index
- position of a chunk to getChunk
located at the specified positionIndexOutOfBoundsException
- if index is out of boundspublic long getTotalLength()
chunk.getLength()
.public Iterator<Chunk> iterator()
iterator
over the chunks.
The returned iterator does not support remove
operation.
Iterator returned by this method may be reused again by someone else
after it have traversed all the elements in this ChunkList
,
therefore it is not allowed to use the iterator after its
hasNext()
method has returned false
.
However it is still allowed to acquire and use several instances
of iterator (it will generate garbage).
Read-only chunk lists can be iterated concurrently from multiple threads. Each call to this method creates its own instance of iterator for read-only chunk lists.
public void add(Chunk chunk, Object owner)
read-only
or have the same
owner as this chunk list. The chunk will be handed over to this
chunk list's internal owner object after invocation of this method.chunk
- a chunk to addowner
- current owner of this chunk list and given chunkNullPointerException
- if chunk
is null
IllegalStateException
- if this chunk list's owner differs from
the one specified (in particular, if it is read-only) or if the given
chunk is not read-only and its owner differs from the one specifiedpublic void addAll(ChunkList chunks, Object owner)
read-only
or have the same owner as this chunk list.
All chunks added will be handed over to this chunk list's internal
owner object and the given chunk list will be recycled after invocation
of this method.chunks
- chunk list with chunks to addowner
- current owner of both this and given chunk lists
(if the given chunk list is not read-only).NullPointerException
- if chunks
is null
IllegalArgumentException
- if chunks
is this
IllegalStateException
- if this chunk list's owner differs from
the one specified (in particular, if it is read-only) or if the given
chunk list is not read-only and its owner differs from the one specifiedpublic void add(byte[] bytes, int offset, int length)
bytes
- byte array to take data fromoffset
- offset of data within the arraylength
- length of datapublic Chunk poll(Object owner)
owner
- owner of this chunk list (which will also become the owner
of the taken chunk after invocation of this method), unless
the taken chunk was read-only.null
if the list is emptyIllegalStateException
- if this chunk list's owner differs from
the one specified or if the chunk list is read-only.public Chunk pollLast(Object owner)
owner
- owner of this chunk list (which will also become the owner
of the taken chunk after invocation of this method).null
if the list is emptyIllegalStateException
- if this chunk list's owner differs from
the one specified (in particular, if the chunk list is read-only)public void setChunkRange(int index, int offset, int length, Object owner)
index
- position of a chunk to set rangeoffset
- position of the first meaningful bytelength
- total number of meaningful bytesowner
- owner of the chunk listIndexOutOfBoundsException
- if the region defined by
offset
and length
is out of chunk's underlying
byte array rangeIllegalStateException
- if the chunk list's owner differs from
the one specified (in particular, if the chunk list or chunk is read-only).Chunk.setRange(int, int, java.lang.Object)
public boolean isReadOnly()
true
if this chunk list is read-only.markReadOnly(Object)
,
Chunk.isReadOnly()
public void markReadOnly(Object owner)
isReadOnly()
. All contained chunks
also become read-only after invocation of this method.
Does nothing if the list is already read-only.owner
- current owner of this chunk listIllegalStateException
- if the chunk list is not read-only
and its current owner differs from the one specifiedChunk.markReadOnly(Object)
public void handOver(Object oldOwner, Object newOwner)
read-only
.oldOwner
- old (current) owner of the chunk listnewOwner
- new owner of the chunk listIllegalStateException
- if the chunk list is not read-only
and its current owner differs from the one specifiedpublic void recycle(Object owner)
read-only
.
A reference to this chunk list is considered to become invalid after
invocation of this method and may no longer be used, unless the chunk list was read-only.owner
- current owner of the chunk listIllegalStateException
- if the chunk list is not read-only
and its current owner differs from the one specifiedChunk.recycle(Object)
protected void checkOwner(Object owner)
owner
- expected owner of the chunk listIllegalStateException
- if the chunk list's current owner differs from the one specifiedprotected void checkRange(int index)
Copyright © 2002–2025 Devexperts LLC. All rights reserved.