Interface SerializationSession

All Superinterfaces:
AutoCloseable, Broadcaster, Closeable, Flushable<Duration>, Listener, Named, NamedObject, Receiver, Repeater, SilentTryTrait, Transceiver, Transmitter, TryTrait, Versioned
All Known Implementing Classes:
KryoSerializationSession

public interface SerializationSession extends Named, Closeable, Flushable<Duration>, Versioned, Repeater, TryTrait
A high-level abstraction for serialization. This interface allows the serialization of a sequence of SerializableObjects using an ObjectSerializer during a bracketed session associated with an InputStream or OutputStream. This design provides ease-of-use while ensuring that the stream is always assigned a version and the input or output stream is managed correctly.

Creating a Session

The method SerializationSessionFactory.newSession(Listener) can be called to obtain a SerializationSession instance. Providers of serialization will provide a SerializationSessionFactory that produces thread-safe SerializationSessions. Alternatively a session can be created by constructing an implementation instance.

Opening a Session

A serialization session is initiated by calling:

When a SerializationSession.SessionType.CLIENT or SerializationSession.SessionType.SERVER session is opened, handshaking and exchange for version information will take place.

Reading and Writing

A session will remember any input or output stream that was given to it when the session was opened, so that when read and write methods are called:

objects will be read and written to the streams passed to open().

Example

 var session = new KryoSerializationSession();
 var version = session.open(SessionType.RESOURCE, input, output);
 session.write(new SerializedObject<>("hello"));
 session.close();
 
Author:
jonathanl (shibo)
See Also:
  • Method Details

    • close

      default void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • isActive

      default boolean isActive()
    • isReading

      boolean isReading()
      Returns:
      True if data is being read
    • isWriting

      boolean isWriting()
      Returns:
      True if data is being written
    • maximumWaitTime

      default Duration maximumWaitTime()
      Specified by:
      maximumWaitTime in interface Flushable<Duration>
      Returns:
      The maximum time to wait when Flushable.flush() is called. Normally this is the largest duration possible, which for all practical purposes is "forever".
    • onClose

      void onClose()
      Ends a serialization session, flushing any pending output.
    • open

      default Version open(Socket socket, SerializationSession.SessionType sessionType, Version version, ProgressReporter reporter)
      Returns:
      Opens the given socket for reading and writing. Version handshaking is performed automatically for SerializationSession.SessionType.SERVERs and SerializationSession.SessionType.CLIENTs with the version of the connected endpoint returned to the caller.
    • open

      default Version open(InputStream input, SerializationSession.SessionType sessionType)
      Opens this session for reading
      Returns:
      The version or an exception is thrown
    • open

      default Version open(InputStream input)
      Opens this session for reading from a resource
      Parameters:
      input - The input stream,
      Returns:
      The version of the stream, or a runtime exception
    • open

      default void open(OutputStream output, SerializationSession.SessionType sessionType, Version version)
      Opens this session for writing
    • open

      default void open(OutputStream output, Version version)
      Opens this session for writing to a resource
    • open

      Version open(InputStream input, OutputStream output, SerializationSession.SessionType sessionType, Version version)
      Opens this session for reading and/or writing. Retains the given input and output streams for future use, and performs version any socket handshaking per the SerializationSession.SessionType parameter.
      Returns:
      The resource, client or server version, or an exception
    • read

      <T> SerializableObject<T> read()
      Returns:
      A serializable object
    • read

      default <T> T read(Class<T> type)
      Reads an object of the given type from the input, discarding any version. If the object read from input is not of the given type, an IllegalStateException will be thrown. This method can be used to read primitives, for example: read(Integer.class).
      Parameters:
      type - The type to read
      Returns:
      The object
    • readList

      default <Element> ObjectList<Element> readList(Class<Element> type)
      Reads a list of elements written by the writeList(Collection) method
      Parameters:
      type - The element type
      Returns:
      The list
    • readResource

      default void readResource(Resource resource, UncheckedVoidCode code)
      Runs the given code while the given resource is open for reading
      Parameters:
      resource - The resource to read from
      code - The code to run
    • write

      default <T> void write(T object)
      Writes the given object to output without version information
    • write

      <T> void write(SerializableObject<T> object)
      Writes the given SerializableObject to output
    • writeList

      default <Element> void writeList(Collection<Element> list)
      Writes the given collection of elements as a list
      Parameters:
      list - The list to write
    • writeResource

      default void writeResource(WritableResource resource, Version version, UncheckedVoidCode code)
      Runs the given code while the given resource is open for writing
      Parameters:
      resource - The resource to write to
      version - The output version
      code - The code to run