Top Description Fields Constructors Methods
io.rsocket.metadata

public Class CompositeMetadataCodec

extends Object
Class Inheritance
Imports
io.netty.buffer.ByteBuf, .ByteBufAllocator, .ByteBufUtil, .CompositeByteBuf, io.netty.util.CharsetUtil, io.rsocket.util.NumberUtils, reactor.util.annotation.Nullable

A flyweight class that can be used to encode/decode composite metadata information to/from ByteBuf. This is intended for low-level efficient manipulation of such buffers. See CompositeMetadata for an Iterator-like approach to decoding entries.

Field Summary

Modifier and TypeField and Description
pack-priv static final int
pack-priv static final byte

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static int
computeNextEntryIndex(int currentEntryIndex, ByteBuf headerSlice, ByteBuf contentSlice)

public static ByteBuf[]

Returns:

a ByteBuf array of length 2 containing the mime header buffer slice and the content buffer slice, or one of the zero-length error constant arrays
decodeMimeAndContentBuffersSlices
(ByteBuf
the source ByteBuf that originally contains one or more metadata entries
compositeMetadata
,
int
the ByteBuf#readerIndex() to start decoding from. original reader index is kept on the source buffer
entryIndex
,
boolean
should produced metadata entry buffers slices be retained?
retainSlices
)

Decode the next metadata entry (a mime header + content pair of ByteBuf) from a ByteBuf that contains at least enough bytes for one more such entry.

public static byte

Returns:

the compressed mime id, between 0 and 127, or a negative id if the input is invalid
decodeMimeIdFromMimeBuffer
(ByteBuf
the buffer that should next contain the compressed mime id byte
mimeBuffer
)

Decode a 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).

public static CharSequence

Returns:

the decoded custom mime type, as a CharSequence, or null if the input is invalid
decodeMimeTypeFromMimeBuffer
(ByteBuf
the mime header ByteBuf that contains length + custom mime type
flyweightMimeBuffer
)

Decode a CharSequence custome mime type from a ByteBuf, assuming said buffer properly contains such a mime type.

public static void
encodeAndAddMetadata(CompositeByteBuf
the buffer that will hold all composite metadata information.
compositeMetaData
,
ByteBufAllocator
the ByteBufAllocator to use to create intermediate buffers as needed.
allocator
,
String
the custom mime type to encode.
customMimeType
,
ByteBuf
the metadata value to encode.
metadata
)

Encode a new sub-metadata information into a composite metadata buffer, without checking if the String can be matched with a well known compressable mime type.

public static void
encodeAndAddMetadata(CompositeByteBuf
the buffer that will hold all composite metadata information.
compositeMetaData
,
ByteBufAllocator
the ByteBufAllocator to use to create intermediate buffers as needed.
allocator
,
WellKnownMimeType
the WellKnownMimeType to encode.
knownMimeType
,
ByteBuf
the metadata value to encode.
metadata
)

Encode a new sub-metadata information into a composite metadata buffer.

pack-priv static void
encodeAndAddMetadata(CompositeByteBuf
the buffer that will hold all composite metadata information.
compositeMetaData
,
ByteBufAllocator
the ByteBufAllocator to use to create intermediate buffers as needed.
allocator
,
byte unknownCompressedMimeType, ByteBuf
the metadata value to encode.
metadata
)

Encode a new sub-metadata information into a composite metadata buffer.

public static void
encodeAndAddMetadataWithCompression(CompositeByteBuf
the buffer that will hold all composite metadata information.
compositeMetaData
,
ByteBufAllocator
the ByteBufAllocator to use to create intermediate buffers as needed.
allocator
,
String
the mime type to encode, as a String. well known mime types are compressed.
mimeType
,
ByteBuf
the metadata value to encode.
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).

pack-priv static ByteBuf

Returns:

the encoded mime and metadata length information
encodeMetadataHeader
(ByteBufAllocator
the ByteBufAllocator to use to create the buffer.
allocator
,
String
a custom mime type to encode.
customMime
,
int
the metadata length to append to the buffer as an unsigned 24 bits integer.
metadataLength
)

Encode a custom mime type and a metadata value length into a newly allocated ByteBuf.

pack-priv static ByteBuf

Returns:

the encoded mime and metadata length information
encodeMetadataHeader
(ByteBufAllocator
the ByteBufAllocator to use to create the buffer.
allocator
,
byte
a byte identifier of a WellKnownMimeType to encode.
mimeType
,
int
the metadata length to append to the buffer as an unsigned 24 bits integer.
metadataLength
)

Encode a well known mime type and a metadata value length into a newly allocated ByteBuf.

public static boolean

Returns:

whether there is another entry available at a given index
hasEntry
(ByteBuf
the buffer to inspect
compositeMetadata
,
int
the index to check at
entryIndex
)

Returns whether there is another entry available at a given index

public static boolean

Returns:

whether the header represents a well-known MIME type
isWellKnownMimeType
(ByteBuf
the header to inspect
header
)

Returns whether the header represents a well-known MIME type.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

STREAM_METADATA_KNOWN_MASKback to summary
pack-priv static final int STREAM_METADATA_KNOWN_MASK
STREAM_METADATA_LENGTH_MASKback to summary
pack-priv static final byte STREAM_METADATA_LENGTH_MASK

Constructor Detail

CompositeMetadataCodecback to summary
private CompositeMetadataCodec()

Method Detail

computeNextEntryIndexback to summary
public static int computeNextEntryIndex(int currentEntryIndex, ByteBuf headerSlice, ByteBuf contentSlice)
decodeMimeAndContentBuffersSlicesback to summary
public static ByteBuf[] decodeMimeAndContentBuffersSlices(ByteBuf compositeMetadata, int entryIndex, boolean retainSlices)

Decode the next metadata entry (a mime header + content pair of 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:

  • made up of a single byte: this represents an encoded mime id, which can be further decoded using decodeMimeIdFromMimeBuffer(ByteBuf)
  • made up of 2 or more bytes: this represents an encoded mime String + its length, which can be further decoded using 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.
Parameters
compositeMetadata:ByteBuf

the source ByteBuf that originally contains one or more metadata entries

entryIndex:int

the ByteBuf#readerIndex() to start decoding from. original reader index is kept on the source buffer

retainSlices:boolean

should produced metadata entry buffers slices be retained?

Returns:ByteBuf[]

a ByteBuf array of length 2 containing the mime header buffer slice and the content buffer slice, or one of the zero-length error constant arrays

decodeMimeIdFromMimeBufferback to summary
public static byte decodeMimeIdFromMimeBuffer(ByteBuf mimeBuffer)

Decode a 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.

Parameters
mimeBuffer:ByteBuf

the buffer that should next contain the compressed mime id byte

Returns:byte

the compressed mime id, between 0 and 127, or a negative id if the input is invalid

See Also
decodeMimeTypeFromMimeBuffer(ByteBuf)
decodeMimeTypeFromMimeBufferback to summary
public static CharSequence decodeMimeTypeFromMimeBuffer(ByteBuf flyweightMimeBuffer)

Decode a 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.

Parameters
flyweightMimeBuffer:ByteBuf

the mime header ByteBuf that contains length + custom mime type

Returns:CharSequence

the decoded custom mime type, as a CharSequence, or null if the input is invalid

Annotations
@Nullable
See Also
decodeMimeIdFromMimeBuffer(ByteBuf)
encodeAndAddMetadataback to summary
public static void encodeAndAddMetadata(CompositeByteBuf compositeMetaData, ByteBufAllocator allocator, String customMimeType, ByteBuf metadata)

Encode a new sub-metadata information into a composite 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)

Parameters
compositeMetaData:CompositeByteBuf

the buffer that will hold all composite metadata information.

allocator:ByteBufAllocator

the ByteBufAllocator to use to create intermediate buffers as needed.

customMimeType:String

the custom mime type to encode.

metadata:ByteBuf

the metadata value to encode.

encodeAndAddMetadataback to summary
public static void encodeAndAddMetadata(CompositeByteBuf compositeMetaData, ByteBufAllocator allocator, WellKnownMimeType knownMimeType, ByteBuf metadata)

Encode a new sub-metadata information into a composite metadata buffer.

Parameters
compositeMetaData:CompositeByteBuf

the buffer that will hold all composite metadata information.

allocator:ByteBufAllocator

the ByteBufAllocator to use to create intermediate buffers as needed.

knownMimeType:WellKnownMimeType

the WellKnownMimeType to encode.

metadata:ByteBuf

the metadata value to encode.

encodeAndAddMetadataback to summary
pack-priv static void encodeAndAddMetadata(CompositeByteBuf compositeMetaData, ByteBufAllocator allocator, byte unknownCompressedMimeType, ByteBuf metadata)

Encode a new sub-metadata information into a composite metadata buffer.

Parameters
compositeMetaData:CompositeByteBuf

the buffer that will hold all composite metadata information.

allocator:ByteBufAllocator

the ByteBufAllocator to use to create intermediate buffers as needed.

unknownCompressedMimeType:byte

the id of the WellKnownMimeType#UNKNOWN_RESERVED_MIME_TYPE to encode.

metadata:ByteBuf

the metadata value to encode.

encodeAndAddMetadataWithCompressionback to summary
public 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).

Prefer using encodeAndAddMetadata(CompositeByteBuf, ByteBufAllocator, String, ByteBuf) if you already know that the mime type is not a WellKnownMimeType.

Parameters
compositeMetaData:CompositeByteBuf

the buffer that will hold all composite metadata information.

allocator:ByteBufAllocator

the ByteBufAllocator to use to create intermediate buffers as needed.

mimeType:String

the mime type to encode, as a String. well known mime types are compressed.

metadata:ByteBuf

the metadata value to encode.

See Also
encodeAndAddMetadata(CompositeByteBuf, ByteBufAllocator, WellKnownMimeType, ByteBuf)
encodeMetadataHeaderback to summary
pack-priv static ByteBuf encodeMetadataHeader(ByteBufAllocator allocator, String customMime, int metadataLength)

Encode a custom mime type and a metadata value length into a newly allocated ByteBuf.

This larger representation encodes the mime type representation's length on a single byte, then the representation itself, then the unsigned metadata value length on 3 additional bytes.

Parameters
allocator:ByteBufAllocator

the ByteBufAllocator to use to create the buffer.

customMime:String

a custom mime type to encode.

metadataLength:int

the metadata length to append to the buffer as an unsigned 24 bits integer.

Returns:ByteBuf

the encoded mime and metadata length information

encodeMetadataHeaderback to summary
pack-priv static ByteBuf encodeMetadataHeader(ByteBufAllocator allocator, byte mimeType, int metadataLength)

Encode a well known mime type and a metadata value length into a newly allocated ByteBuf.

This compact representation encodes the mime type via its ID on a single byte, and the unsigned value length on 3 additional bytes.

Parameters
allocator:ByteBufAllocator

the ByteBufAllocator to use to create the buffer.

mimeType:byte

a byte identifier of a WellKnownMimeType to encode.

metadataLength:int

the metadata length to append to the buffer as an unsigned 24 bits integer.

Returns:ByteBuf

the encoded mime and metadata length information

hasEntryback to summary
public static boolean hasEntry(ByteBuf compositeMetadata, int entryIndex)

Returns whether there is another entry available at a given index

Parameters
compositeMetadata:ByteBuf

the buffer to inspect

entryIndex:int

the index to check at

Returns:boolean

whether there is another entry available at a given index

isWellKnownMimeTypeback to summary
public static boolean isWellKnownMimeType(ByteBuf header)

Returns whether the header represents a well-known MIME type.

Parameters
header:ByteBuf

the header to inspect

Returns:boolean

whether the header represents a well-known MIME type