Class ByteBufResponseWriter

java.lang.Object
org.infinispan.server.resp.serialization.bytebuf.ByteBufResponseWriter
All Implemented Interfaces:
ResponseWriter

public final class ByteBufResponseWriter extends Object implements ResponseWriter
Entrypoint to serialize a response in RESP3 format.

The class provides specific methods for the RESP3 types. In cases where the response object type is unknown or heterogeneous during runtime, there is a generic method.

During serialization, the class introspects the registry for a serializer capable of handling the object before proceeding. Utilizing a specific method reduces the number of candidates to check. The generic method verifies all serializers in the registry and short circuits after the first match. However, this could cause many checks until the correct serializer. Give preference to specific methods.

The method throws an exception when an object does not have a serializer.

Since:
15.0
Author:
José Bolina
  • Constructor Details

    • ByteBufResponseWriter

      public ByteBufResponseWriter(ByteBufPool alloc)
  • Method Details

    • nulls

      public void nulls()
      Writes the null value.
      Specified by:
      nulls in interface ResponseWriter
    • ok

      public void ok()
      Serializes a simple string with the "OK" content.
      Specified by:
      ok in interface ResponseWriter
    • queued

      public void queued(Object ignore)
      Serializes a simple string with the "QUEUED" content.
      Specified by:
      queued in interface ResponseWriter
      Parameters:
      ignore - Content to ignore.
    • simpleString

      public void simpleString(CharSequence value)
      Serializes a char sequence in a simple string format.
      Specified by:
      simpleString in interface ResponseWriter
      Parameters:
      value - The ASCII string to serialize.
      See Also:
      • ByteBufPrimitiveSerializer.SimpleStringSerializer
    • string

      public void string(CharSequence value)
      Serializes a char sequence in a bulk string format.
      Specified by:
      string in interface ResponseWriter
      Parameters:
      value - The string to serialize.
      See Also:
      • ByteBufPrimitiveSerializer.BulkStringSerializer2
    • string

      public void string(byte[] value)
      Serializes the binary blob in a bulk string format.
      Specified by:
      string in interface ResponseWriter
      Parameters:
      value - The binary blob to serialize.
      See Also:
      • ByteBufPrimitiveSerializer.BulkStringSerializer
    • integers

      public void integers(Number value)
      Serializes a 64-bit number in the integer format.
      Specified by:
      integers in interface ResponseWriter
      Parameters:
      value - Number to serialize.
      See Also:
      • ByteBufPrimitiveSerializer.IntegerSerializer
    • doubles

      public void doubles(Number value)
      Serializes a double-precision floating point into the doubles format.
      Specified by:
      doubles in interface ResponseWriter
      Parameters:
      value - The floating point to serialize.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufDoubleSerializer
    • booleans

      public void booleans(boolean value)
      Serializes a boolean value in the RESP3 format.
      Specified by:
      booleans in interface ResponseWriter
      Parameters:
      value - Boolean value to serialize.
      See Also:
      • ByteBufPrimitiveSerializer.BooleanSerializer
    • arrayEmpty

      public void arrayEmpty()
      Serializes a collection in the array format.
      Specified by:
      arrayEmpty in interface ResponseWriter
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufCollectionSerializer.ArraySerializer
    • array

      public <T> void array(Collection<T> collection, JavaObjectSerializer<T> serializer)
      Serializes the collection by delegating the serialization of the elements to the provided serializer.
      Specified by:
      array in interface ResponseWriter
      Type Parameters:
      T - The type of the elements in the collection and the serializer handles.
      Parameters:
      collection - Collection of elements to serialize.
      serializer - Serializer for a single element in the collection.
      See Also:
      • ByteBufCollectionSerializer.ArraySerializer
    • array

      public void array(Collection<?> collection, Resp3Type contentType)
      Serializes a collection in the array format with elements of a specified type.
      Specified by:
      array in interface ResponseWriter
      Parameters:
      collection - Collection of heterogeneous values to serialize.
      contentType - The type of elements contained by the sequence.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufCollectionSerializer.ArraySerializer
    • emptySet

      public void emptySet()
      Serializes a set in the RESP3 set format.
      Specified by:
      emptySet in interface ResponseWriter
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufCollectionSerializer.SetSerializer
    • set

      public void set(Set<?> set, Resp3Type contentType)
      Serializes a set in the RESP3 set format with elements of a specified type.
      Specified by:
      set in interface ResponseWriter
      Parameters:
      set - Set of heterogeneous values to serialize.
      contentType - The type of elements contained by the set.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufCollectionSerializer.SetSerializer
    • map

      public void map(Map<?,?> value)
      Serializes a map in the RESP3 map format.
      Specified by:
      map in interface ResponseWriter
      Parameters:
      value - A map with heterogeneous key-value tuples to serialize.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufMapSerializer
    • map

      public void map(Map<?,?> value, Resp3Type contentType)
      Serializes a map in the RESP3 map format with key and values of the same specified type.
      Specified by:
      map in interface ResponseWriter
      Parameters:
      value - A map with heterogeneous key-value tuples to serialize.
      contentType - The type of key and value elements.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufMapSerializer
    • map

      public void map(Map<?,?> value, Resp3Type keyType, Resp3Type valueType)
      Serializes a map in the RESP3 map format with keys and values of a specified format.
      Specified by:
      map in interface ResponseWriter
      Parameters:
      value - A map with heterogeneous key-value tuples to serialize.
      keyType - The type of keys in the map.
      valueType - The type of values in the map.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufMapSerializer
    • error

      public void error(CharSequence value)
      Serializes an error message in the RESP3 format.

      The first character in the error message must be the '-' symbol.

      Specified by:
      error in interface ResponseWriter
      Parameters:
      value - An ASCII char sequence with the error message.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
      See Also:
      • ByteBufPrimitiveSerializer.SimpleErrorSerializer
    • error

      public void error(Throwable t)
      Serializes the exception message in the RESP3 error format.
      Specified by:
      error in interface ResponseWriter
      Parameters:
      t - The throwable to serialize.
      See Also:
      • ByteBufThrowableSerializer
    • write

      public <T> void write(T object, JavaObjectSerializer<T> serializer)
      Writes an object utilizing the specific serializer.

      Implementors do not need to check for nullability in the implementation. Null response values are handled by native RESP3 serializers before passing the serialization ahead.

      Specified by:
      write in interface ResponseWriter
      Type Parameters:
      T - The type of the object.
      Parameters:
      object - The element to serialize.
      serializer - The serializer to utilize.
    • write

      public <T> void write(JavaObjectSerializer<T> serializer)
      Invokes the callback to write a response with the serializer.
      Specified by:
      write in interface ResponseWriter
      Parameters:
      serializer - Serializer to write.
    • serialize

      public void serialize(Object object)
      Generic method to serialize an object of unknown type.

      This method searches all serializers available in the registry.

      Specified by:
      serialize in interface ResponseWriter
      Parameters:
      object - The object to serialize in RESP3 format.
      Throws:
      IllegalStateException - in case no serializer is found for the object.
    • writeNumericPrefix

      public void writeNumericPrefix(byte symbol, long number, int additionalWidth)
      Specified by:
      writeNumericPrefix in interface ResponseWriter