Typical lifecycle looks like this:
new Encoder
(setMaxCapacity?
encode)*
Suppose headers are represented by Map<String, List<String>>.
A supplier and a consumer of ByteBuffers in forms of
Supplier<ByteBuffer> and Consumer<ByteBuffer> respectively.
Then to encode headers, the following approach might be used:
for (Map.Entry<String, List<String>> h : headers.entrySet()) {
String name = h.getKey();
for (String value : h.getValue()) {
encoder.header(name, value); // Set up header
boolean encoded;
do {
ByteBuffer b = buffersSupplier.get();
encoded = encoder.encode(b); // Encode the header
buffersConsumer.accept(b);
} while (!encoded);
}
}
Though the specification does not define
how an encoder is to be implemented, a default implementation is provided by
the method header(CharSequence, CharSequence, boolean).
To provide a custom encoding implementation, Encoder has to be
extended. A subclass then can access methods for encoding using specific
representations (e.g. literal,
indexed, etc.)
API Note
An Encoder provides an incremental way of encoding headers.
encode(ByteBuffer) takes a buffer a returns a boolean indicating
whether, or not, the buffer was sufficiently sized to hold the
remaining of the encoded representation.
This way, there's no need to provide a buffer of a specific size, or to resize (and copy) the buffer on demand, when the remaining encoded representation will not fit in the buffer's remaining space. Instead, an array of existing buffers can be used, prepended with a frame that encloses the resulting header block afterwards.
Splitting the encoding operation into header set up and header encoding,
separates long lived arguments (name, value,
sensitivity, etc.) from the short lived ones (e.g. buffer),
simplifying each operation itself.
Implementation Note
The default implementation does not use dynamic table. It reports to a
coupled Decoder a size update with the value of 0, and never changes
it afterwards.
| Modifier and Type | Field and Description |
|---|---|
| private final BulkSizeUpdateWriter | |
| private boolean | |
| private boolean | |
| private int | |
| private static final AtomicLong | |
| private boolean | |
| private final HeaderTable | |
| private final QuickHuffman. | |
| private final long | |
| private final IndexedWriter | |
| private int | |
| private final LiteralNeverIndexedWriter | |
| private final LiteralWithIndexingWriter | |
| private final LiteralWriter | |
| private final HPACK. | |
| private int | |
| private long | |
| private final SizeUpdateWriter | |
| private BinaryRepresentationWriter |
| Access | Constructor and Description |
|---|---|
| public | Encoder(int
a non-negative integer maxCapacity)Constructs an |
| Modifier and Type | Method and Description |
|---|---|
| protected int | Returns: actual capacityupper bound maxCapacity)Calculates actual capacity to be used by this encoder in response to a request to update maximum table size. |
| protected final void | |
| public final boolean | Returns: true if the current header has been fully encoded,
false otherwisethe buffer to encode the header into, may be empty headerBlock)Encodes the set up header into the given buffer. |
| protected final HeaderTable | |
| protected final int | |
| public void | header(CharSequence
the name name, CharSequence the value value)Sets up the given header |
| public void | header(CharSequence
the name name, CharSequence the value value, boolean whether or not the value is sensitive sensitive)Sets up the given header |
| protected final void | |
| protected final boolean | |
| protected final void | |
| protected final void | |
| protected final void | |
| protected final void | literalNeverIndexed(CharSequence name, boolean nameHuffman, CharSequence value, boolean valueHuffman)
|
| protected final void | |
| protected final void | literalWithIndexing(CharSequence name, boolean nameHuffman, CharSequence value, boolean valueHuffman)
|
| protected final HPACK. | |
| private boolean | |
| public void | |
| private void | |
| protected final void | |
| protected final int |
| bulkSizeUpdateWriter | back to summary |
|---|---|
| private final BulkSizeUpdateWriter bulkSizeUpdateWriter | |
| capacityUpdate | back to summary |
|---|---|
| private boolean capacityUpdate | |
| configuredCapacityUpdate | back to summary |
|---|---|
| private boolean configuredCapacityUpdate | |
| currCapacity | back to summary |
|---|---|
| private int currCapacity | |
| ENCODERS_IDS | back to summary |
|---|---|
| private static final AtomicLong ENCODERS_IDS | |
| encoding | back to summary |
|---|---|
| private boolean encoding | |
| headerTable | back to summary |
|---|---|
| private final HeaderTable headerTable | |
| huffmanWriter | back to summary |
|---|---|
| private final QuickHuffman. | |
| id | back to summary |
|---|---|
| private final long id | |
| indexedWriter | back to summary |
|---|---|
| private final IndexedWriter indexedWriter | |
| lastCapacity | back to summary |
|---|---|
| private int lastCapacity | |
| literalNeverIndexedWriter | back to summary |
|---|---|
| private final LiteralNeverIndexedWriter literalNeverIndexedWriter | |
| literalWithIndexingWriter | back to summary |
|---|---|
| private final LiteralWithIndexingWriter literalWithIndexingWriter | |
| literalWriter | back to summary |
|---|---|
| private final LiteralWriter literalWriter | |
| logger | back to summary |
|---|---|
| private final HPACK. | |
| maxCapacity | back to summary |
|---|---|
| private int maxCapacity | |
| minCapacity | back to summary |
|---|---|
| private long minCapacity | |
| sizeUpdateWriter | back to summary |
|---|---|
| private final SizeUpdateWriter sizeUpdateWriter | |
| writer | back to summary |
|---|---|
| private BinaryRepresentationWriter writer | |
| Encoder | back to summary |
|---|---|
| public Encoder(int maxCapacity) Constructs an The value has to be agreed between decoder and encoder out-of-band, e.g. by a protocol that uses HPACK (see 4.2. Maximum Table Size).
| |
| calculateCapacity | back to summary |
|---|---|
| protected int calculateCapacity(int maxCapacity) Calculates actual capacity to be used by this encoder in response to a request to update maximum table size. Default implementation does not add anything to the headers table,
hence this method returns It is an error to return a value
| |
| checkEncoding | back to summary |
|---|---|
| protected final void checkEncoding() | |
| encode | back to summary |
|---|---|
| public final boolean encode(ByteBuffer headerBlock) Encodes the set up header into the given buffer. The encoder writes as much as possible of the header's binary representation into the given buffer, starting at the buffer's position, and increments its position to reflect the bytes written. The buffer's mark and limit will not be modified. Once the method has returned
| |
| getHeaderTable | back to summary |
|---|---|
| protected final HeaderTable getHeaderTable() | |
| getMaxCapacity | back to summary |
|---|---|
| protected final int getMaxCapacity() | |
| header | back to summary |
|---|---|
| public void header(CharSequence name, CharSequence value) throws IllegalStateException Sets up the given header Fixates
| |
| header | back to summary |
|---|---|
| public void header(CharSequence name, CharSequence value, boolean sensitive) throws IllegalStateException Sets up the given header If the Fixates
| |
| indexed | back to summary |
|---|---|
| protected final void indexed(int index) throws IndexOutOfBoundsException | |
| isHuffmanBetterFor | back to summary |
|---|---|
| protected final boolean isHuffmanBetterFor(CharSequence value) | |
| literal | back to summary |
|---|---|
| protected final void literal(int index, CharSequence value, boolean useHuffman) throws IndexOutOfBoundsException | |
| literal | back to summary |
|---|---|
| protected final void literal(CharSequence name, boolean nameHuffman, CharSequence value, boolean valueHuffman) | |
| literalNeverIndexed | back to summary |
|---|---|
| protected final void literalNeverIndexed(int index, CharSequence value, boolean valueHuffman) throws IndexOutOfBoundsException | |
| literalNeverIndexed | back to summary |
|---|---|
| protected final void literalNeverIndexed(CharSequence name, boolean nameHuffman, CharSequence value, boolean valueHuffman) | |
| literalWithIndexing | back to summary |
|---|---|
| protected final void literalWithIndexing(int index, CharSequence value, boolean valueHuffman) throws IndexOutOfBoundsException | |
| literalWithIndexing | back to summary |
|---|---|
| protected final void literalWithIndexing(CharSequence name, boolean nameHuffman, CharSequence value, boolean valueHuffman) | |
| logger | back to summary |
|---|---|
| protected final HPACK. | |
| prependWithCapacityUpdate | back to summary |
|---|---|
| private boolean prependWithCapacityUpdate(ByteBuffer headerBlock) | |
| setMaxCapacity | back to summary |
|---|---|
| public void setMaxCapacity(int capacity) Sets a maximum capacity of the header table. The value has to be agreed between decoder and encoder out-of-band, e.g. by a protocol that uses HPACK (see 4.2. Maximum Table Size). May be called any number of times after or before a complete header has been encoded. If the encoder decides to change the actual capacity, an update will be encoded before a new encoding operation starts.
| |
| setMaxCapacity0 | back to summary |
|---|---|
| private void setMaxCapacity0(int capacity) | |
| sizeUpdate | back to summary |
|---|---|
| protected final void sizeUpdate(int capacity) throws IllegalArgumentException | |
| tableIndexOf | back to summary |
|---|---|
| protected final int tableIndexOf(CharSequence name, CharSequence value) | |