Interface NetworkBuffer.Type<T extends @UnknownNullability Object>
- Type Parameters:
T- the value type, which may be nullable
- All Known Implementing Classes:
DebugSubscriptionImpl
- Enclosing interface:
NetworkBuffer
public static interface NetworkBuffer.Type<T extends @UnknownNullability Object>
Reads and writes values of
T in a NetworkBuffer, so that reading returns the value that was written.
Unlike StructCodec, types are always written linearly into a NetworkBuffer.
Prefer NetworkBufferTemplate to build types for records, which keeps both directions in sync.-
Method Summary
Modifier and TypeMethodDescriptiondefault NetworkBuffer.Type<T> lengthPrefixed(int maxLength) Creates a type that encodes each value as aNetworkBuffer.BYTE_ARRAY, which is aNetworkBuffer.VAR_INTlength followed by the bytes of the value.default NetworkBuffer.Type<@Unmodifiable @UnknownNullability List<T>> list()Creates a type for an unmodifiable list of this type, accepting up toInteger.MAX_VALUEelements.default NetworkBuffer.Type<@Unmodifiable @UnknownNullability List<T>> list(int maxSize) Creates a type for an unmodifiable list of this type, encoded as aNetworkBuffer.VAR_INTsize followed by each element.default <V extends @UnknownNullability Object>
NetworkBuffer.Type<@Unmodifiable @UnknownNullability Map<T, V>> mapValue(NetworkBuffer.Type<V> valueType) Creates a type for an unmodifiable map with keys of this type, accepting up toInteger.MAX_VALUEentries.default <V extends @UnknownNullability Object>
NetworkBuffer.Type<@Unmodifiable @UnknownNullability Map<T, V>> mapValue(NetworkBuffer.Type<V> valueType, int maxSize) Creates a type for an unmodifiable map with keys of this type, encoded as aNetworkBuffer.VAR_INTsize followed by each key and value.default NetworkBuffer.Type<T> maxLength(long maxLength) Creates a type that encodes values the same way as this type, but rejects values longer thanmaxLengthbytes.default NetworkBuffer.Type<@Nullable T> optional()Creates a type that also acceptsnull, encoded as aNetworkBuffer.BOOLEANthat istruewhen a value of this type follows.read(NetworkBuffer buffer) Reads a value frombufferat its read index.default NetworkBuffer.Type<@Unmodifiable @UnknownNullability Set<T>> set()Creates a type for an unmodifiable set of this type, accepting up toInteger.MAX_VALUEelements.default NetworkBuffer.Type<@Unmodifiable @UnknownNullability Set<T>> set(int maxSize) Creates a type for an unmodifiable set of this type, encoded as aNetworkBuffer.VAR_INTsize followed by each element.default longReturns the number of bytes thatwrite(NetworkBuffer, Object)uses forvalue, without registries.default longsizeOf(T value, @Nullable Registries registries) Returns the number of bytes thatwrite(NetworkBuffer, Object)uses forvalue.default <S extends @UnknownNullability Object>
NetworkBuffer.Type<S> Creates a type that converts between the values of this type andS.default <R extends @UnknownNullability Object>
NetworkBuffer.Type<R> unionType(Function<? super T, NetworkBuffer.Type<? extends R>> serializers, Function<? super R, ? extends T> keyFunc) Creates a type for the subtypes ofR, such as the permitted types of a sealed interface.voidwrite(NetworkBuffer buffer, T value) Writes a value tobufferat its write index.
-
Method Details
-
write
Writes a value tobufferat its write index.- Parameters:
buffer- the buffer to write tovalue- the value
-
read
Reads a value frombufferat its read index.- Parameters:
buffer- the buffer to read from- Returns:
- the value
-
sizeOf
Returns the number of bytes thatwrite(NetworkBuffer, Object)uses forvalue. The value is written to a dummy buffer, which counts bytes without storing them and cannot be read. Override this method rather thansizeOf(Object), which calls it without registries.- Parameters:
value- the value to measureregistries- the registries for types that need them, ornull- Returns:
- the size in bytes
-
sizeOf
Returns the number of bytes thatwrite(NetworkBuffer, Object)uses forvalue, without registries.- Parameters:
value- the value to measure- Returns:
- the size in bytes
- See Also:
-
transform
default <S extends @UnknownNullability Object> NetworkBuffer.Type<S> transform(Function<? super T, ? extends S> to, Function<? super S, ? extends T> from) Creates a type that converts between the values of this type andS.- Type Parameters:
S- the converted type- Parameters:
to- the function applied to each value read by this typefrom- the function applied to each value before this type writes it- Returns:
- the converted type
-
mapValue
default <V extends @UnknownNullability Object> NetworkBuffer.Type<@Unmodifiable @UnknownNullability Map<T,V>> mapValue(NetworkBuffer.Type<V> valueType, int maxSize) Creates a type for an unmodifiable map with keys of this type, encoded as aNetworkBuffer.VAR_INTsize followed by each key and value. Reading throwsIllegalArgumentExceptionwhen the size is negative or greater thanmaxSize, or a key repeats.- Type Parameters:
V- the value type- Parameters:
valueType- the type of the valuesmaxSize- the largest size accepted when reading- Returns:
- the map type
-
mapValue
default <V extends @UnknownNullability Object> NetworkBuffer.Type<@Unmodifiable @UnknownNullability Map<T,V>> mapValue(NetworkBuffer.Type<V> valueType) Creates a type for an unmodifiable map with keys of this type, accepting up toInteger.MAX_VALUEentries. UsemapValue(Type, int)when the size has a strict upper bound.- Type Parameters:
V- the value type- Parameters:
valueType- the type of the values- Returns:
- the map type
-
list
Creates a type for an unmodifiable list of this type, encoded as aNetworkBuffer.VAR_INTsize followed by each element. Writingnullencodes an empty list. Reading throwsIllegalArgumentExceptionwhen the size is negative or greater thanmaxSize.- Parameters:
maxSize- the largest size accepted when reading- Returns:
- the list type
-
list
Creates a type for an unmodifiable list of this type, accepting up toInteger.MAX_VALUEelements. Uselist(int)when the size has a strict upper bound.- Returns:
- the list type
-
set
Creates a type for an unmodifiable set of this type, encoded as aNetworkBuffer.VAR_INTsize followed by each element. Writingnullencodes an empty set. Reading throwsIllegalArgumentExceptionwhen the size is negative or greater thanmaxSize, or an element repeats.- Parameters:
maxSize- the largest size accepted when reading- Returns:
- the set type
-
set
Creates a type for an unmodifiable set of this type, accepting up toInteger.MAX_VALUEelements. Useset(int)when the size has a strict upper bound.- Returns:
- the set type
-
optional
Creates a type that also acceptsnull, encoded as aNetworkBuffer.BOOLEANthat istruewhen a value of this type follows.- Returns:
- the optional type
-
unionType
default <R extends @UnknownNullability Object> NetworkBuffer.Type<R> unionType(Function<? super T, NetworkBuffer.Type<? extends R>> serializers, Function<? super R, ? extends T> keyFunc) Creates a type for the subtypes ofR, such as the permitted types of a sealed interface. Each value is encoded as its key in this type, followed by the value in the serializer for that key. Reading and writing throwUnsupportedOperationExceptionwhenserializersreturnsnull.- Type Parameters:
R- the union type- Parameters:
serializers- the serializer for the values of each keykeyFunc- the key of a value- Returns:
- the union type
-
lengthPrefixed
Creates a type that encodes each value as aNetworkBuffer.BYTE_ARRAY, which is aNetworkBuffer.VAR_INTlength followed by the bytes of the value. Reading throwsIllegalArgumentExceptionwhen the length is greater thanmaxLengthor the readable bytes, or the value does not use exactly that many bytes.- Parameters:
maxLength- the largest length accepted when reading- Returns:
- the length prefixed type
-
maxLength
Creates a type that encodes values the same way as this type, but rejects values longer thanmaxLengthbytes. Both throwIllegalArgumentExceptionfor a value that is too long.- Parameters:
maxLength- the largest encoded size in bytes- Returns:
- the length limited type
-