public class CompositeMetadataCodec extends Object
ByteBuf
. This is intended for low-level efficient manipulation of such buffers. See CompositeMetadata
for an Iterator-like approach to decoding entries.Modifier and Type | Method and Description |
---|---|
static int |
computeNextEntryIndex(int currentEntryIndex,
ByteBuf headerSlice,
ByteBuf contentSlice) |
static ByteBuf[] |
decodeMimeAndContentBuffersSlices(ByteBuf compositeMetadata,
int entryIndex,
boolean retainSlices)
|
static byte |
decodeMimeIdFromMimeBuffer(ByteBuf mimeBuffer)
Decode a
byte compressed mime id from a ByteBuf , assuming said buffer properly
contains such an id. |
static CharSequence |
decodeMimeTypeFromMimeBuffer(ByteBuf flyweightMimeBuffer)
Decode a
CharSequence custome mime type from a ByteBuf , assuming said buffer
properly contains such a mime type. |
static void |
encodeAndAddMetadata(CompositeByteBuf compositeMetaData,
ByteBufAllocator allocator,
String customMimeType,
ByteBuf metadata)
|
static void |
encodeAndAddMetadata(CompositeByteBuf compositeMetaData,
ByteBufAllocator allocator,
WellKnownMimeType knownMimeType,
ByteBuf metadata)
Encode a new sub-metadata information into a composite metadata
buffer . |
static void |
encodeAndAddMetadataWithCompression(CompositeByteBuf compositeMetaData,
ByteBufAllocator allocator,
String mimeType,
ByteBuf metadata)
Encode a new sub-metadata information into a composite metadata
buffer , first verifying if the passed String matches a WellKnownMimeType (in
which case it will be encoded in a compressed fashion using the mime id of that type). |
static boolean |
hasEntry(ByteBuf compositeMetadata,
int entryIndex)
Returns whether there is another entry available at a given index
|
static boolean |
isWellKnownMimeType(ByteBuf header)
Returns whether the header represents a well-known MIME type.
|
public static int computeNextEntryIndex(int currentEntryIndex, ByteBuf headerSlice, ByteBuf contentSlice)
public static ByteBuf[] decodeMimeAndContentBuffersSlices(ByteBuf compositeMetadata, int entryIndex, boolean retainSlices)
ByteBuf
) from a ByteBuf
that contains at least enough bytes for one more such entry. These buffers are
actually slices of the full metadata buffer, and this method doesn't move the full metadata
buffer's ByteBuf.readerIndex()
. As such, it requires the user to provide an index
to read from. The next index is computed by calling computeNextEntryIndex(int,
ByteBuf, ByteBuf)
. Size of the first buffer (the "header buffer") drives which decoding method
should be further applied to it.
The header buffer is either:
decodeMimeIdFromMimeBuffer(ByteBuf)
decodeMimeTypeFromMimeBuffer(ByteBuf)
. Note the
encoded length, in the first byte, is skipped by this decoding method because the
remaining length of the buffer is that of the mime string.
compositeMetadata
- the source ByteBuf
that originally contains one or more
metadata entriesentryIndex
- the ByteBuf.readerIndex()
to start decoding from. original reader
index is kept on the source bufferretainSlices
- should produced metadata entry buffers slices
be
retained
?ByteBuf
array of length 2 containing the mime header buffer
slice and the content buffer slice, or one of the
zero-length error constant arrayspublic static byte decodeMimeIdFromMimeBuffer(ByteBuf mimeBuffer)
byte
compressed mime id from a ByteBuf
, assuming said buffer properly
contains such an id.
The buffer must have exactly one readable byte, which is assumed to have been tested for
mime id encoding via the STREAM_METADATA_KNOWN_MASK
mask (firstByte &
STREAM_METADATA_KNOWN_MASK) == STREAM_METADATA_KNOWN_MASK
).
If there is no readable byte, the negative identifier of WellKnownMimeType.UNPARSEABLE_MIME_TYPE
is returned.
mimeBuffer
- the buffer that should next contain the compressed mime id bytedecodeMimeTypeFromMimeBuffer(ByteBuf)
@Nullable public static CharSequence decodeMimeTypeFromMimeBuffer(ByteBuf flyweightMimeBuffer)
CharSequence
custome mime type from a ByteBuf
, assuming said buffer
properly contains such a mime type.
The buffer must at least have two readable bytes, which distinguishes it from the compressed id
case. The first byte is a size and the
remaining bytes must correspond to the CharSequence
, encoded fully in US_ASCII. As a
result, the first byte can simply be skipped, and the remaining of the buffer be decoded to the
mime type.
If the mime header buffer is less than 2 bytes long, returns null
.
flyweightMimeBuffer
- the mime header ByteBuf
that contains length + custom mime
typeCharSequence
, or null if the input is
invaliddecodeMimeIdFromMimeBuffer(ByteBuf)
public static void encodeAndAddMetadata(CompositeByteBuf compositeMetaData, ByteBufAllocator allocator, String customMimeType, ByteBuf metadata)
buffer
, without checking if the String
can be matched with a well known compressable
mime type. Prefer using this method and encodeAndAddMetadata(CompositeByteBuf,
ByteBufAllocator, WellKnownMimeType, ByteBuf)
if you know in advance whether or not the mime
is well known. Otherwise use encodeAndAddMetadataWithCompression(CompositeByteBuf,
ByteBufAllocator, String, ByteBuf)
compositeMetaData
- the buffer that will hold all composite metadata information.allocator
- the ByteBufAllocator
to use to create intermediate buffers as needed.customMimeType
- the custom mime type to encode.metadata
- the metadata value to encode.public static void encodeAndAddMetadata(CompositeByteBuf compositeMetaData, ByteBufAllocator allocator, WellKnownMimeType knownMimeType, ByteBuf metadata)
buffer
.compositeMetaData
- the buffer that will hold all composite metadata information.allocator
- the ByteBufAllocator
to use to create intermediate buffers as needed.knownMimeType
- the WellKnownMimeType
to encode.metadata
- the metadata value to encode.public static void encodeAndAddMetadataWithCompression(CompositeByteBuf compositeMetaData, ByteBufAllocator allocator, String mimeType, ByteBuf metadata)
buffer
, first verifying if the passed String
matches a WellKnownMimeType
(in
which case it will be encoded in a compressed fashion using the mime id of that type).
Prefer using encodeAndAddMetadata(CompositeByteBuf, ByteBufAllocator, String,
ByteBuf)
if you already know that the mime type is not a WellKnownMimeType
.
compositeMetaData
- the buffer that will hold all composite metadata information.allocator
- the ByteBufAllocator
to use to create intermediate buffers as needed.mimeType
- the mime type to encode, as a String
. well known mime types are
compressed.metadata
- the metadata value to encode.encodeAndAddMetadata(CompositeByteBuf, ByteBufAllocator, WellKnownMimeType, ByteBuf)
public static boolean hasEntry(ByteBuf compositeMetadata, int entryIndex)
compositeMetadata
- the buffer to inspectentryIndex
- the index to check atpublic static boolean isWellKnownMimeType(ByteBuf header)
header
- the header to inspect