Package org.nustaq.serialization
Class FSTObjectOutput
java.lang.Object
org.nustaq.serialization.FSTObjectOutput
- All Implemented Interfaces:
DataOutput
,ObjectOutput
,AutoCloseable
- Direct Known Subclasses:
FSTObjectOutputNoShared
replacement of ObjectOutputStream
-
Field Summary
Modifier and TypeFieldDescriptionstatic final byte
static final byte
static final byte
static final byte
static final byte
protected boolean
protected FSTEncoder
protected FSTConfiguration
protected int
static final byte
static final byte
protected boolean
protected static ByteArrayOutputStream
static final byte
static final byte
protected boolean
protected FSTSerialisationListener
static final byte
static Object
static final byte
protected FSTObjectRegistry
static final byte
protected ThreadLocal<FSTClazzInfo.FSTFieldInfo[]>
static final byte
static final byte
protected final FSTClazzInfo
protected int[]
static final byte
protected int
-
Constructor Summary
ConstructorDescriptionserialize without an underlying stream, the resulting byte array of writing to this FSTObjectOutput can be accessed using getBuffer(), the size using getWritten().Creates a new FSTObjectOutput stream to write data to the specified underlying output stream.FSTObjectOutput
(OutputStream out, FSTConfiguration conf) Creates a new FSTObjectOutput stream to write data to the specified underlying output stream.serialize without an underlying stream, the resulting byte array of writing to this FSTObjectOutput can be accessed using getBuffer(), the size using getWritten(). -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
defaultWriteObject
(Object toWrite, FSTClazzInfo serializationInfo) void
ensureFree
(int bytes) void
flush()
Flushes this data output stream.byte[]
protected FSTClazzInfo.FSTFieldInfo
getCachedFI
(Class... possibles) getCodec()
getConf()
byte[]
protected FSTClazzInfo
getFstClazzInfo
(FSTClazzInfo.FSTFieldInfo referencee, Class clazz) if class is same as last referenced, returned cached clzinfo, else do a lookupgetObjectOutputStream
(Class cl, FSTClazzInfo clinfo, FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) int
since the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal.int
protected void
objectHasBeenWritten
(Object obj, int oldStreamPosition, int streamPosition) hook for debugging profiling. empty impl, you need to subclass to make use of this hookprotected void
objectWillBeWritten
(Object obj, int streamPosition) hook for debugging profiling. register a FSTSerialisationListener to useprotected void
void
reset keeping the last used byte[] buffervoid
resetForReUse
(byte[] out) void
if out == null => automatically create/reuse a bytebufferprotected void
setCodec
(FSTEncoder codec) void
setListener
(FSTSerialisationListener listener) note this might slow down serialization significantly * @param listenervoid
setWriteExternalWriteAhead
(int writeExternalWriteAhead) since the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal.void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(int b) protected void
writeArray
(FSTClazzInfo.FSTFieldInfo referencee, Object array) void
writeBoolean
(boolean v) void
writeByte
(int v) void
writeBytes
(String s) void
writeChar
(int v) void
writeChars
(String s) void
writeClassTag
(Class aClass) protected void
writeCompatibleObjectFields
(Object toWrite, Map fields, FSTClazzInfo.FSTFieldInfo[] fieldInfo) void
writeDouble
(double v) protected FSTClazzInfo
writeEnum
(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) void
writeFloat
(float v) protected boolean
writeHandleIfApplicable
(Object toWrite, FSTClazzInfo serializationInfo) void
writeInt
(int v) void
writeLong
(long v) void
writeObject
(Object obj) void
writeObject
(Object obj, Class... possibles) protected void
writeObjectCompatible
(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo serializationInfo) protected void
writeObjectCompatibleRecursive
(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo serializationInfo, Class cl) protected void
writeObjectFields
(Object toWrite, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, int startIndex, int version) protected boolean
writeObjectHeader
(FSTClazzInfo clsInfo, FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) writeObjectInternal
(Object obj, FSTClazzInfo ci, Class... possibles) protected FSTClazzInfo
writeObjectWithContext
(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) protected FSTClazzInfo
writeObjectWithContext
(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo ci) void
writeShort
(int v) void
writeStringUTF
(String str) void
-
Field Details
-
NULL_PLACEHOLDER
-
SPECIAL_COMPATIBILITY_OBJECT_TAG
public static final byte SPECIAL_COMPATIBILITY_OBJECT_TAG- See Also:
-
ONE_OF
public static final byte ONE_OF- See Also:
-
BIG_BOOLEAN_FALSE
public static final byte BIG_BOOLEAN_FALSE- See Also:
-
BIG_BOOLEAN_TRUE
public static final byte BIG_BOOLEAN_TRUE- See Also:
-
BIG_LONG
public static final byte BIG_LONG- See Also:
-
BIG_INT
public static final byte BIG_INT- See Also:
-
DIRECT_ARRAY_OBJECT
public static final byte DIRECT_ARRAY_OBJECT- See Also:
-
HANDLE
public static final byte HANDLE- See Also:
-
ENUM
public static final byte ENUM- See Also:
-
ARRAY
public static final byte ARRAY- See Also:
-
STRING
public static final byte STRING- See Also:
-
TYPED
public static final byte TYPED- See Also:
-
DIRECT_OBJECT
public static final byte DIRECT_OBJECT- See Also:
-
NULL
public static final byte NULL- See Also:
-
OBJECT
public static final byte OBJECT- See Also:
-
codec
-
conf
-
objects
-
curDepth
protected int curDepth -
writeExternalWriteAhead
protected int writeExternalWriteAhead -
listener
-
stringInfo
-
isCrossPlatform
protected boolean isCrossPlatform -
refsLocal
-
empty
-
closed
protected boolean closed -
tmp
protected int[] tmp
-
-
Constructor Details
-
FSTObjectOutput
Creates a new FSTObjectOutput stream to write data to the specified underlying output stream. uses Default Configuration singleton -
FSTObjectOutput
Creates a new FSTObjectOutput stream to write data to the specified underlying output stream. The counterwritten
is set to zero. Don't create a FSTConfiguration with each stream, just create one global static configuration and reuse it. FSTConfiguration is threadsafe.- Parameters:
out
- the underlying output stream, to be saved for later use.
-
FSTObjectOutput
serialize without an underlying stream, the resulting byte array of writing to this FSTObjectOutput can be accessed using getBuffer(), the size using getWritten(). Don't create a FSTConfiguration with each stream, just create one global static configuration and reuse it. FSTConfiguration is threadsafe.- Parameters:
conf
-- Throws:
IOException
-
FSTObjectOutput
public FSTObjectOutput()serialize without an underlying stream, the resulting byte array of writing to this FSTObjectOutput can be accessed using getBuffer(), the size using getWritten(). Note once you call close or flush, the tmp byte array is lost. (grab array before flushing/closing) uses default configuration singleton- Throws:
IOException
-
-
Method Details
-
flush
Flushes this data output stream. This forces any buffered output bytes to be written out to the stream. Theflush
method ofDataOutputStream
calls theflush
method of its underlying output stream.- Specified by:
flush
in interfaceObjectOutput
- Throws:
IOException
- if an I/O error occurs.- See Also:
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceObjectOutput
- Throws:
IOException
-
getWriteExternalWriteAhead
public int getWriteExternalWriteAhead()since the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal. Default value is 5000 bytes. If you make use of the externalizable interface and write larger Objects a) cast the ObjectOutput in readExternal to FSTObjectOutput and call ensureFree on this in your writeExternal method or b) statically set a sufficient maximum using this method. -
setWriteExternalWriteAhead
public void setWriteExternalWriteAhead(int writeExternalWriteAhead) since the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal. Default value is 5000 bytes. If you make use of the externalizable interface and write larger Objects a) cast the ObjectOutput in readExternal to FSTObjectOutput and call ensureFree on this in your writeExternal method or b) statically set a sufficient maximum using this method.- Parameters:
writeExternalWriteAhead
-
-
ensureFree
- Throws:
IOException
-
writeObject
- Specified by:
writeObject
in interfaceObjectOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in interfaceObjectOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in interfaceObjectOutput
- Throws:
IOException
-
write
- Specified by:
write
in interfaceDataOutput
- Specified by:
write
in interfaceObjectOutput
- Throws:
IOException
-
writeBoolean
- Specified by:
writeBoolean
in interfaceDataOutput
- Throws:
IOException
-
writeByte
- Specified by:
writeByte
in interfaceDataOutput
- Throws:
IOException
-
writeShort
- Specified by:
writeShort
in interfaceDataOutput
- Throws:
IOException
-
writeChar
- Specified by:
writeChar
in interfaceDataOutput
- Throws:
IOException
-
writeInt
- Specified by:
writeInt
in interfaceDataOutput
- Throws:
IOException
-
writeLong
- Specified by:
writeLong
in interfaceDataOutput
- Throws:
IOException
-
writeFloat
- Specified by:
writeFloat
in interfaceDataOutput
- Throws:
IOException
-
writeDouble
- Specified by:
writeDouble
in interfaceDataOutput
- Throws:
IOException
-
writeBytes
- Specified by:
writeBytes
in interfaceDataOutput
- Throws:
IOException
-
writeChars
- Specified by:
writeChars
in interfaceDataOutput
- Throws:
IOException
-
writeUTF
- Specified by:
writeUTF
in interfaceDataOutput
- Throws:
IOException
-
writeObject
- Throws:
IOException
-
getCachedFI
-
writeObjectInternal
public FSTClazzInfo writeObjectInternal(Object obj, FSTClazzInfo ci, Class... possibles) throws IOException - Parameters:
obj
-ci
-possibles
-- Returns:
- last FSTClazzInfo if class is plain reusable (not replaceable, needs compatible mode)
- Throws:
IOException
-
getListener
-
setListener
note this might slow down serialization significantly * @param listener -
objectWillBeWritten
hook for debugging profiling. register a FSTSerialisationListener to use- Parameters:
obj
-streamPosition
-
-
objectHasBeenWritten
hook for debugging profiling. empty impl, you need to subclass to make use of this hook- Parameters:
obj
-oldStreamPosition
-streamPosition
-
-
writeObjectWithContext
protected FSTClazzInfo writeObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) throws IOException - Throws:
IOException
-
writeObjectWithContext
protected FSTClazzInfo writeObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo ci) throws IOException - Throws:
IOException
-
writeEnum
protected FSTClazzInfo writeEnum(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) throws IOException - Throws:
IOException
-
writeHandleIfApplicable
protected boolean writeHandleIfApplicable(Object toWrite, FSTClazzInfo serializationInfo) throws IOException - Throws:
IOException
-
getFstClazzInfo
if class is same as last referenced, returned cached clzinfo, else do a lookup -
defaultWriteObject
- Throws:
IOException
-
writeObjectCompatible
protected void writeObjectCompatible(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo serializationInfo) throws IOException - Throws:
IOException
-
writeObjectCompatibleRecursive
protected void writeObjectCompatibleRecursive(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo serializationInfo, Class cl) throws IOException - Throws:
IOException
-
writeObjectFields
protected void writeObjectFields(Object toWrite, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, int startIndex, int version) throws IOException - Throws:
IOException
-
writeCompatibleObjectFields
protected void writeCompatibleObjectFields(Object toWrite, Map fields, FSTClazzInfo.FSTFieldInfo[] fieldInfo) throws IOException - Throws:
IOException
-
writeObjectHeader
protected boolean writeObjectHeader(FSTClazzInfo clsInfo, FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) throws IOException - Parameters:
clsInfo
-referencee
-toWrite
-- Returns:
- true if header already wrote object
- Throws:
IOException
-
writeArray
- Throws:
IOException
-
writeStringUTF
- Throws:
IOException
-
resetAndClearRefs
protected void resetAndClearRefs() -
resetForReUse
if out == null => automatically create/reuse a bytebuffer- Parameters:
out
-
-
resetForReUse
public void resetForReUse()reset keeping the last used byte[] buffer -
resetForReUse
public void resetForReUse(byte[] out) -
getClassInfoRegistry
-
getObjectOutputStream
public ObjectOutputStream getObjectOutputStream(Class cl, FSTClazzInfo clinfo, FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) throws IOException - Parameters:
cl
- - class or superclass of currently serialized obj, write declared fields of this class onlyclinfo
-referencee
-toWrite
-- Returns:
- Throws:
IOException
-
getObjectMap
-
getBuffer
public byte[] getBuffer()- Returns:
- the written buffer reference. use getWritten() to obtain the length of written bytes. WARNING: if more than one objects have been written, an implicit flush is triggered, so the buffer only contains the last written object. getWritten() then has a larger size than the buffer length. only usable if one single object is written to the stream (e.g. messaging) note: in case of non-standard underlyings (e.g. serializing to direct offheap or DirectBuffer, this method might cause creation of a byte array and a copy.
-
getCopyOfWrittenBuffer
public byte[] getCopyOfWrittenBuffer()- Returns:
- a copy of written bytes. Warning: if the stream has been flushed, this will fail with an exception. a flush is triggered after each 1st level writeObject. note: in case of non-stream based serialization (directbuffer, offheap mem) getBuffer will return a copy anyways.
-
getConf
-
getWritten
public int getWritten()- Returns:
- the number of bytes written to this stream. This also is the number of valid bytes in the buffer one obtains from the various getBuffer, getCopyOfBuffer methods. Warning: if the stream has been flushed (done after each 1st level object write), the buffer will be smaller than the value given here or contain invalid bytes.
-
writeClassTag
-
getCodec
-
setCodec
-