Interface SerializationSession
- All Superinterfaces:
AutoCloseable
,Broadcaster
,Closeable
,Flushable<Duration>
,Listener
,Named
,NamedObject
,Receiver
,Repeater
,SilentTryTrait
,Transceiver
,Transmitter
,TryTrait
,Versioned
- All Known Implementing Classes:
KryoSerializationSession
SerializableObject
s 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 SerializationSession
s. Alternatively a session can be created by constructing an
implementation instance.
Opening a Session
A serialization session is initiated by calling:
open(InputStream)
open(InputStream, SessionType)
open(OutputStream, Version)
open(OutputStream, SessionType, Version)
open(Socket, SessionType, Version, ProgressReporter)
open(InputStream, OutputStream, SessionType, Version)
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:
read()
- Read aSerializableObject
from inputread(Class)
- Read an object of the given type from inputwrite(Object)
- Write an object to outputwrite(SerializableObject)
- Write aSerializableObject
to output
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:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enum
The type of serialization session. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
close()
default boolean
isActive()
boolean
boolean
default Duration
void
onClose()
Ends a serialization session, flushing any pending output.default Version
open
(InputStream input) Opens this session for reading from a resourcedefault Version
open
(InputStream input, SerializationSession.SessionType sessionType) Opens this session for readingopen
(InputStream input, OutputStream output, SerializationSession.SessionType sessionType, Version version) Opens this session for reading and/or writing.default void
open
(OutputStream output, Version version) Opens this session for writing to a resourcedefault void
open
(OutputStream output, SerializationSession.SessionType sessionType, Version version) Opens this session for writingdefault Version
open
(Socket socket, SerializationSession.SessionType sessionType, Version version, ProgressReporter reporter) <T> SerializableObject<T>
read()
default <T> T
Reads an object of the given type from the input, discarding any version.default <Element> ObjectList<Element>
Reads a list of elements written by thewriteList(Collection)
methoddefault void
readResource
(Resource resource, UncheckedVoidCode code) Runs the given code while the given resource is open for reading<T> void
write
(SerializableObject<T> object) Writes the givenSerializableObject
to outputdefault <T> void
write
(T object) Writes the given object to output without version informationdefault <Element> void
writeList
(Collection<Element> list) Writes the given collection of elements as a listdefault void
writeResource
(WritableResource resource, Version version, UncheckedVoidCode code) Runs the given code while the given resource is open for writingMethods inherited from interface com.telenav.kivakit.core.messaging.Broadcaster
addListener, addListener, clearListeners, copyListeners, hasListeners, listeners, messageSource, messageSource, onReceive, onTransmit, onTransmitted, onTransmitting, removeListener, silence, transmit, transmit, transmitAll
Methods inherited from interface com.telenav.kivakit.core.messaging.Listener
isDeaf, listenTo, listenTo, onMessage
Methods inherited from interface com.telenav.kivakit.interfaces.naming.NamedObject
hasSyntheticName, objectName, objectName
Methods inherited from interface com.telenav.kivakit.interfaces.messaging.Receiver
isReceiving
Methods inherited from interface com.telenav.kivakit.core.messaging.Repeater
isRepeating, onReceive, receive
Methods inherited from interface com.telenav.kivakit.core.language.trait.SilentTryTrait
tryCatch, tryCatch, tryCatchDefault
Methods inherited from interface com.telenav.kivakit.core.messaging.Transceiver
announce, debug, debugClassContext, debugCodeContext, debugCodeContext, fatal, fatal, glitch, glitch, glitch, glitch, halted, halted, ifDebug, illegalArgument, illegalState, illegalState, information, isDebugOn, narrate, problem, problem, problem, problem, problemIfNull, quibble, quibble, quibble, quibble, trace, trace, trace, trace, warning, warning, warning, warning
Methods inherited from interface com.telenav.kivakit.interfaces.messaging.Transmitter
isTransmitting, withoutTransmitting
Methods inherited from interface com.telenav.kivakit.core.language.trait.TryTrait
tryCatch, tryCatch, tryCatchThrow, tryCatchThrow, tryFinally, tryFinally
Methods inherited from interface com.telenav.kivakit.core.version.Versioned
hasVersion, version
-
Method Details
-
close
default void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
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
- Specified by:
maximumWaitTime
in interfaceFlushable<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
Opens this session for reading from a resource- Parameters:
input
- The input stream,- Returns:
- The version of the stream, or a runtime exception
-
read
- Returns:
- A serializable object
-
read
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, anIllegalStateException
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
Reads a list of elements written by thewriteList(Collection)
method- Parameters:
type
- The element type- Returns:
- The list
-
write
default <T> void write(T object) Writes the given object to output without version information -
writeList
Writes the given collection of elements as a list- Parameters:
list
- The list to write
-