Package org.nustaq.serialization
Class FSTObjectOutput
java.lang.Object
org.nustaq.serialization.FSTObjectOutput
- All Implemented Interfaces:
java.io.DataOutput,java.io.ObjectOutput,java.lang.AutoCloseable
- Direct Known Subclasses:
FSTObjectOutputNoShared
public class FSTObjectOutput
extends java.lang.Object
implements java.io.ObjectOutput
replacement of ObjectOutputStream
-
Field Summary
Fields Modifier and Type Field Description static byteARRAYstatic byteBIG_BOOLEAN_FALSEstatic byteBIG_BOOLEAN_TRUEstatic byteBIG_INTstatic byteBIG_LONGprotected booleanclosedprotected FSTEncodercodecprotected FSTConfigurationconfprotected intcurDepthstatic byteDIRECT_ARRAY_OBJECTstatic byteDIRECT_OBJECTprotected booleandontShareprotected static java.io.ByteArrayOutputStreamemptystatic byteENUMstatic byteHANDLEprotected booleanisCrossPlatformprotected FSTSerialisationListenerlistenerstatic byteNULLstatic java.lang.ObjectNULL_PLACEHOLDERstatic byteOBJECTprotected FSTObjectRegistryobjectsstatic byteONE_OFprotected java.lang.ThreadLocal<FSTClazzInfo.FSTFieldInfo[]>refsLocalstatic byteSPECIAL_COMPATIBILITY_OBJECT_TAGstatic byteSTRINGprotected FSTClazzInfostringInfoprotected int[]tmpstatic byteTYPEDprotected intwriteExternalWriteAhead -
Constructor Summary
Constructors Constructor Description FSTObjectOutput()serialize without an underlying stream, the resulting byte array of writing to this FSTObjectOutput can be accessed using getBuffer(), the size using getWritten().FSTObjectOutput(java.io.OutputStream out)Creates a new FSTObjectOutput stream to write data to the specified underlying output stream.FSTObjectOutput(java.io.OutputStream out, FSTConfiguration conf)Creates a new FSTObjectOutput stream to write data to the specified underlying output stream.FSTObjectOutput(FSTConfiguration conf)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 Type Method Description voidclose()voiddefaultWriteObject(java.lang.Object toWrite, FSTClazzInfo serializationInfo)voidensureFree(int bytes)voidflush()Flushes this data output stream.byte[]getBuffer()protected FSTClazzInfo.FSTFieldInfogetCachedFI(java.lang.Class... possibles)FSTClazzInfoRegistrygetClassInfoRegistry()FSTEncodergetCodec()FSTConfigurationgetConf()byte[]getCopyOfWrittenBuffer()protected FSTClazzInfogetFstClazzInfo(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Class clazz)if class is same as last referenced, returned cached clzinfo, else do a lookupFSTSerialisationListenergetListener()FSTObjectRegistrygetObjectMap()java.io.ObjectOutputStreamgetObjectOutputStream(java.lang.Class cl, FSTClazzInfo clinfo, FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite)intgetWriteExternalWriteAhead()since the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal.intgetWritten()protected voidobjectHasBeenWritten(java.lang.Object obj, int oldStreamPosition, int streamPosition)hook for debugging profiling. empty impl, you need to subclass to make use of this hookprotected voidobjectWillBeWritten(java.lang.Object obj, int streamPosition)hook for debugging profiling. register a FSTSerialisationListener to useprotected voidresetAndClearRefs()voidresetForReUse()reset keeping the last used byte[] buffervoidresetForReUse(byte[] out)voidresetForReUse(java.io.OutputStream out)if out == null => automatically create/reuse a bytebufferprotected voidsetCodec(FSTEncoder codec)voidsetListener(FSTSerialisationListener listener)note this might slow down serialization significantly * @param listenervoidsetWriteExternalWriteAhead(int writeExternalWriteAhead)since the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal.voidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)protected voidwriteArray(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object array)voidwriteBoolean(boolean v)voidwriteByte(int v)voidwriteBytes(java.lang.String s)voidwriteChar(int v)voidwriteChars(java.lang.String s)voidwriteClassTag(java.lang.Class aClass)protected voidwriteCompatibleObjectFields(java.lang.Object toWrite, java.util.Map fields, FSTClazzInfo.FSTFieldInfo[] fieldInfo)voidwriteDouble(double v)protected FSTClazzInfowriteEnum(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite)voidwriteFloat(float v)protected booleanwriteHandleIfApplicable(java.lang.Object toWrite, FSTClazzInfo serializationInfo)voidwriteInt(int v)voidwriteLong(long v)voidwriteObject(java.lang.Object obj)voidwriteObject(java.lang.Object obj, java.lang.Class... possibles)protected voidwriteObjectCompatible(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite, FSTClazzInfo serializationInfo)protected voidwriteObjectCompatibleRecursive(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite, FSTClazzInfo serializationInfo, java.lang.Class cl)protected voidwriteObjectFields(java.lang.Object toWrite, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, int startIndex, int version)protected booleanwriteObjectHeader(FSTClazzInfo clsInfo, FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite)FSTClazzInfowriteObjectInternal(java.lang.Object obj, FSTClazzInfo ci, java.lang.Class... possibles)protected FSTClazzInfowriteObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite)protected FSTClazzInfowriteObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite, FSTClazzInfo ci)voidwriteShort(int v)voidwriteStringUTF(java.lang.String str)voidwriteUTF(java.lang.String s)
-
Field Details
-
NULL_PLACEHOLDER
public static java.lang.Object NULL_PLACEHOLDER -
SPECIAL_COMPATIBILITY_OBJECT_TAG
public static final byte SPECIAL_COMPATIBILITY_OBJECT_TAG- See Also:
- Constant Field Values
-
ONE_OF
public static final byte ONE_OF- See Also:
- Constant Field Values
-
BIG_BOOLEAN_FALSE
public static final byte BIG_BOOLEAN_FALSE- See Also:
- Constant Field Values
-
BIG_BOOLEAN_TRUE
public static final byte BIG_BOOLEAN_TRUE- See Also:
- Constant Field Values
-
BIG_LONG
public static final byte BIG_LONG- See Also:
- Constant Field Values
-
BIG_INT
public static final byte BIG_INT- See Also:
- Constant Field Values
-
DIRECT_ARRAY_OBJECT
public static final byte DIRECT_ARRAY_OBJECT- See Also:
- Constant Field Values
-
HANDLE
public static final byte HANDLE- See Also:
- Constant Field Values
-
ENUM
public static final byte ENUM- See Also:
- Constant Field Values
-
ARRAY
public static final byte ARRAY- See Also:
- Constant Field Values
-
STRING
public static final byte STRING- See Also:
- Constant Field Values
-
TYPED
public static final byte TYPED- See Also:
- Constant Field Values
-
DIRECT_OBJECT
public static final byte DIRECT_OBJECT- See Also:
- Constant Field Values
-
NULL
public static final byte NULL- See Also:
- Constant Field Values
-
OBJECT
public static final byte OBJECT- See Also:
- Constant Field Values
-
codec
-
conf
-
objects
-
curDepth
protected int curDepth -
writeExternalWriteAhead
protected int writeExternalWriteAhead -
listener
-
dontShare
protected boolean dontShare -
stringInfo
-
isCrossPlatform
protected boolean isCrossPlatform -
refsLocal
-
empty
protected static java.io.ByteArrayOutputStream empty -
closed
protected boolean closed -
tmp
protected int[] tmp
-
-
Constructor Details
-
FSTObjectOutput
public FSTObjectOutput(java.io.OutputStream out)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 counterwrittenis 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:
java.io.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:
java.io.IOException
-
-
Method Details
-
flush
public void flush() throws java.io.IOExceptionFlushes this data output stream. This forces any buffered output bytes to be written out to the stream. Theflushmethod ofDataOutputStreamcalls theflushmethod of its underlying output stream.- Specified by:
flushin interfacejava.io.ObjectOutput- Throws:
java.io.IOException- if an I/O error occurs.- See Also:
FilterOutputStream.out,OutputStream.flush()
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.ObjectOutput- Throws:
java.io.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
public void ensureFree(int bytes) throws java.io.IOException- Throws:
java.io.IOException
-
writeObject
public void writeObject(java.lang.Object obj) throws java.io.IOException- Specified by:
writeObjectin interfacejava.io.ObjectOutput- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Specified by:
writein interfacejava.io.ObjectOutput- Throws:
java.io.IOException
-
write
public void write(byte[] b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Specified by:
writein interfacejava.io.ObjectOutput- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Specified by:
writein interfacejava.io.ObjectOutput- Throws:
java.io.IOException
-
writeBoolean
public void writeBoolean(boolean v) throws java.io.IOException- Specified by:
writeBooleanin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeByte
public void writeByte(int v) throws java.io.IOException- Specified by:
writeBytein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeShort
public void writeShort(int v) throws java.io.IOException- Specified by:
writeShortin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChar
public void writeChar(int v) throws java.io.IOException- Specified by:
writeCharin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeInt
public void writeInt(int v) throws java.io.IOException- Specified by:
writeIntin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeLong
public void writeLong(long v) throws java.io.IOException- Specified by:
writeLongin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeFloat
public void writeFloat(float v) throws java.io.IOException- Specified by:
writeFloatin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeDouble
public void writeDouble(double v) throws java.io.IOException- Specified by:
writeDoublein interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeBytes
public void writeBytes(java.lang.String s) throws java.io.IOException- Specified by:
writeBytesin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeChars
public void writeChars(java.lang.String s) throws java.io.IOException- Specified by:
writeCharsin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeUTF
public void writeUTF(java.lang.String s) throws java.io.IOException- Specified by:
writeUTFin interfacejava.io.DataOutput- Throws:
java.io.IOException
-
writeObject
public void writeObject(java.lang.Object obj, java.lang.Class... possibles) throws java.io.IOException- Throws:
java.io.IOException
-
getCachedFI
-
writeObjectInternal
public FSTClazzInfo writeObjectInternal(java.lang.Object obj, FSTClazzInfo ci, java.lang.Class... possibles) throws java.io.IOException- Parameters:
obj-ci-possibles-- Returns:
- last FSTClazzInfo if class is plain reusable (not replaceable, needs compatible mode)
- Throws:
java.io.IOException
-
getListener
-
setListener
note this might slow down serialization significantly * @param listener -
objectWillBeWritten
protected void objectWillBeWritten(java.lang.Object obj, int streamPosition)hook for debugging profiling. register a FSTSerialisationListener to use- Parameters:
obj-streamPosition-
-
objectHasBeenWritten
protected void objectHasBeenWritten(java.lang.Object obj, int oldStreamPosition, int streamPosition)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, java.lang.Object toWrite) throws java.io.IOException- Throws:
java.io.IOException
-
writeObjectWithContext
protected FSTClazzInfo writeObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite, FSTClazzInfo ci) throws java.io.IOException- Throws:
java.io.IOException
-
writeEnum
protected FSTClazzInfo writeEnum(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite) throws java.io.IOException- Throws:
java.io.IOException
-
writeHandleIfApplicable
protected boolean writeHandleIfApplicable(java.lang.Object toWrite, FSTClazzInfo serializationInfo) throws java.io.IOException- Throws:
java.io.IOException
-
getFstClazzInfo
protected FSTClazzInfo getFstClazzInfo(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Class clazz)if class is same as last referenced, returned cached clzinfo, else do a lookup -
defaultWriteObject
public void defaultWriteObject(java.lang.Object toWrite, FSTClazzInfo serializationInfo) throws java.io.IOException- Throws:
java.io.IOException
-
writeObjectCompatible
protected void writeObjectCompatible(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite, FSTClazzInfo serializationInfo) throws java.io.IOException- Throws:
java.io.IOException
-
writeObjectCompatibleRecursive
protected void writeObjectCompatibleRecursive(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite, FSTClazzInfo serializationInfo, java.lang.Class cl) throws java.io.IOException- Throws:
java.io.IOException
-
writeObjectFields
protected void writeObjectFields(java.lang.Object toWrite, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, int startIndex, int version) throws java.io.IOException- Throws:
java.io.IOException
-
writeCompatibleObjectFields
protected void writeCompatibleObjectFields(java.lang.Object toWrite, java.util.Map fields, FSTClazzInfo.FSTFieldInfo[] fieldInfo) throws java.io.IOException- Throws:
java.io.IOException
-
writeObjectHeader
protected boolean writeObjectHeader(FSTClazzInfo clsInfo, FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite) throws java.io.IOException- Parameters:
clsInfo-referencee-toWrite-- Returns:
- true if header already wrote object
- Throws:
java.io.IOException
-
writeArray
protected void writeArray(FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object array) throws java.io.IOException- Throws:
java.io.IOException
-
writeStringUTF
public void writeStringUTF(java.lang.String str) throws java.io.IOException- Throws:
java.io.IOException
-
resetAndClearRefs
protected void resetAndClearRefs() -
resetForReUse
public void resetForReUse(java.io.OutputStream out)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 java.io.ObjectOutputStream getObjectOutputStream(java.lang.Class cl, FSTClazzInfo clinfo, FSTClazzInfo.FSTFieldInfo referencee, java.lang.Object toWrite) throws java.io.IOException- Parameters:
cl- - class or superclass of currently serialized obj, write declared fields of this class onlyclinfo-referencee-toWrite-- Returns:
- Throws:
java.io.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
public void writeClassTag(java.lang.Class aClass) -
getCodec
-
setCodec
-