final class Segment
extends java.lang.Object
Each segment in a buffer is a circularly-linked list node referencing the following and preceding segments in the buffer.
Each segment in the pool is a singly-linked list node referencing the rest of segments in the pool.
The underlying byte arrays of segments may be shared between buffers and byte strings. When a
segment's byte array is shared the segment may not be recycled, nor may its byte data be changed.
The lone exception is that the owner segment is allowed to append to the segment, writing data at
limit
and beyond. There is a single owning segment for each byte array. Positions,
limits, prev, and next references are not shared.
Modifier and Type | Field and Description |
---|---|
(package private) byte[] |
data |
(package private) int |
limit
The first byte of available data ready to be written to.
|
(package private) Segment |
next
Next segment in a linked or circularly-linked list.
|
(package private) boolean |
owner
True if this segment owns the byte array and can append to it, extending
limit . |
(package private) int |
pos
The next byte of application data byte to read in this segment.
|
(package private) Segment |
prev
Previous segment in a circularly-linked list.
|
(package private) static int |
SHARE_MINIMUM
Segments will be shared when doing so avoids
arraycopy() of this many bytes. |
(package private) boolean |
shared
True if other segments or byte strings use the same byte array.
|
(package private) static int |
SIZE
The size of all segments in bytes.
|
Constructor and Description |
---|
Segment() |
Segment(byte[] data,
int pos,
int limit,
boolean shared,
boolean owner) |
Modifier and Type | Method and Description |
---|---|
void |
compact()
Call this when the tail and its predecessor may both be less than half
full.
|
Segment |
pop()
Removes this segment of a circularly-linked list and returns its successor.
|
Segment |
push(Segment segment)
Appends
segment after this segment in the circularly-linked list. |
(package private) Segment |
sharedCopy()
Returns a new segment that shares the underlying byte array with this.
|
Segment |
split(int byteCount)
Splits this head of a circularly-linked list into two segments.
|
(package private) Segment |
unsharedCopy()
Returns a new segment that its own private copy of the underlying byte array.
|
void |
writeTo(Segment sink,
int byteCount)
Moves
byteCount bytes from this segment to sink . |
static final int SIZE
static final int SHARE_MINIMUM
arraycopy()
of this many bytes.final byte[] data
int pos
int limit
boolean shared
boolean owner
limit
.Segment next
Segment prev
Segment()
Segment(byte[] data, int pos, int limit, boolean shared, boolean owner)
Segment sharedCopy()
Segment unsharedCopy()
@Nullable public Segment pop()
public Segment push(Segment segment)
segment
after this segment in the circularly-linked list.
Returns the pushed segment.public Segment split(int byteCount)
[pos..pos+byteCount)
. The second
segment contains the data in [pos+byteCount..limit)
. This can be
useful when moving partial segments from one buffer to another.
Returns the new head of the circularly-linked list.
public void compact()
public void writeTo(Segment sink, int byteCount)
byteCount
bytes from this segment to sink
.