Package com.jsgalactic.axiom.http
Class Body.Builder
java.lang.Object
com.jsgalactic.axiom.http.Body.Builder
- Enclosing class:
Body
Collects bytes into a private array and turns it into a
Body without copying it again.
The builder owns its array until build(); it never exposes it, so the body's
immutability cannot be broken through the builder. Not thread-safe, single use: after
build() every method throws IllegalStateException.-
Method Summary
Modifier and TypeMethodDescriptionbuild()Finishes the body.intcapacity()Returns the number of bytes that can be written before the capacity must grow.capacity(int capacity) Grows the capacity to exactlycapacitybytes, keeping the content.intlength()Returns the number of bytes written so far.write(ByteBuffer source) Appends the remaining bytes of a buffer, which are consumed, so one read goes straight from the source into the array.
-
Method Details
-
length
public int length()Returns the number of bytes written so far.- Returns:
- length in bytes
-
capacity
public int capacity()Returns the number of bytes that can be written before the capacity must grow.- Returns:
- capacity in bytes
-
capacity
Grows the capacity to exactlycapacitybytes, keeping the content. The old and the new array coexist while the content is moved.- Parameters:
capacity- new capacity; at least the current length- Returns:
- this builder
- Throws:
IllegalArgumentException- if the capacity is below the current length
-
write
Appends the remaining bytes of a buffer, which are consumed, so one read goes straight from the source into the array.- Parameters:
source- bytes to append- Returns:
- this builder
- Throws:
BufferOverflowException- if the bytes do not fit in the capacity
-
build
Finishes the body. When the array is exactly full it becomes the body as is; otherwise it is trimmed once.- Returns:
- the body
- Throws:
IllegalArgumentException- if the content type contains control or non-Latin-1 charactersIllegalStateException- if the builder was already used
-