Class Assembler
Data is read either in order, as a byte stream, or unordered, as chunks with their offsets. A stream starts ordered; it may switch to unordered reads once, but never back.
Bounded buffering. Inserted buffers are kept as views, not copied, and their backing
allocation is accounted for: when a peer's small or gapped frames pin much more memory than the
data they carry, or the number of buffered pieces grows past a bound, the assembler copies the
pieces into fresh compact buffers. A peer that keeps more than 1024 separate
pieces after compaction makes insert(long, java.nio.ByteBuffer, int) fail. The total buffered is bounded by flow control,
which the caller enforces.
Threads. Not thread-safe; an assembler belongs to one stream of one connection, which serializes access.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe most separate pieces kept for one stream. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongThe number of bytes the application has read.voidclear()Discards all buffered data, keeping the read offset.longend()One past the highest offset received.booleanensureOrdering(boolean ordered) Selects ordered or unordered reads.booleaninsert(long offset, ByteBuffer bytes) Adds received data held in a buffer of its own; seeinsert(long, ByteBuffer, int).booleaninsert(long offset, ByteBuffer bytes, int allocationSize) Adds received data.booleanWhether reads are ordered.read(int maxLength, boolean ordered) Takes the next chunk.voidreinit()Discards everything and returns to the initial, ordered state.
-
Field Details
-
MAX_CHUNKS
public static final int MAX_CHUNKSThe most separate pieces kept for one stream. An insert fails only if compaction cannot get the count back down to this.- See Also:
-
-
Constructor Details
-
Assembler
public Assembler()An empty, ordered assembler.
-
-
Method Details
-
reinit
public void reinit()Discards everything and returns to the initial, ordered state. -
ensureOrdering
public boolean ensureOrdering(boolean ordered) Selects ordered or unordered reads. Switching to unordered is allowed once; an ordered read after that is not.- Parameters:
ordered- whether the next reads are ordered- Returns:
- false if
orderedis requested after an unordered read
-
isOrdered
public boolean isOrdered()Whether reads are ordered.- Returns:
- false once
ensureOrdering(false)has been called
-
read
Takes the next chunk.- Parameters:
maxLength- the most bytes to returnordered- true for the bytes at the read offset, false for any buffered chunk- Returns:
- the chunk, or null if nothing (in order) is buffered
-
insert
Adds received data. The assembler keeps a view ofbytes, so the caller must not reuse its contents afterwards; copy data out of a reusable packet buffer first.- Parameters:
offset- the stream offset of the first bytebytes- the data, between its position and limit; the buffer itself is not changedallocationSize- the size of the allocation behindbytes, at least its length; pass the length for a buffer that holds only this data- Returns:
- false if the stream is fragmented into too many pieces, which only a misbehaving peer causes
-
insert
Adds received data held in a buffer of its own; seeinsert(long, ByteBuffer, int).- Parameters:
offset- the stream offset of the first bytebytes- the data, between its position and limit- Returns:
- false if the stream is fragmented into too many pieces
-
bytesRead
public long bytesRead()The number of bytes the application has read.- Returns:
- in ordered mode, the read offset
-
end
public long end()One past the highest offset received.- Returns:
- the high-water mark
-
clear
public void clear()Discards all buffered data, keeping the read offset.
-