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
FieldsModifier and TypeFieldDescriptionstatic final bytestatic final bytestatic final bytestatic final bytestatic final byteprotected booleanprotected FSTEncoderprotected FSTConfigurationprotected intstatic final bytestatic final byteprotected booleanprotected static ByteArrayOutputStreamstatic final bytestatic final byteprotected booleanprotected FSTSerialisationListenerstatic final bytestatic Objectstatic final byteprotected FSTObjectRegistrystatic final byteprotected ThreadLocal<FSTClazzInfo.FSTFieldInfo[]>static final bytestatic final byteprotected final FSTClazzInfoprotected int[]static final byteprotected int -
Constructor Summary
ConstructorsConstructorDescriptionserialize 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 TypeMethodDescriptionvoidclose()voiddefaultWriteObject(Object toWrite, FSTClazzInfo serializationInfo) voidensureFree(int bytes) voidflush()Flushes this data output stream.byte[]protected FSTClazzInfo.FSTFieldInfogetCachedFI(Class... possibles) getCodec()getConf()byte[]protected FSTClazzInfogetFstClazzInfo(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) intsince the stock writeXX methods on InputStream are final, i can't ensure sufficient bufferSize on the output buffer before calling writeExternal.intprotected voidobjectHasBeenWritten(Object obj, int oldStreamPosition, int streamPosition) hook for debugging profiling. empty impl, you need to subclass to make use of this hookprotected voidobjectWillBeWritten(Object obj, int streamPosition) hook for debugging profiling. register a FSTSerialisationListener to useprotected voidvoidreset keeping the last used byte[] buffervoidresetForReUse(byte[] out) voidif 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, Object array) voidwriteBoolean(boolean v) voidwriteByte(int v) voidwriteBytes(String s) voidwriteChar(int v) voidwriteChars(String s) voidwriteClassTag(Class aClass) protected voidwriteCompatibleObjectFields(Object toWrite, Map fields, FSTClazzInfo.FSTFieldInfo[] fieldInfo) voidwriteDouble(double v) protected FSTClazzInfowriteEnum(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) voidwriteFloat(float v) protected booleanwriteHandleIfApplicable(Object toWrite, FSTClazzInfo serializationInfo) voidwriteInt(int v) voidwriteLong(long v) voidwriteObject(Object obj) voidwriteObject(Object obj, Class... possibles) protected voidwriteObjectCompatible(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo serializationInfo) protected voidwriteObjectCompatibleRecursive(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo serializationInfo, Class cl) protected voidwriteObjectFields(Object toWrite, FSTClazzInfo serializationInfo, FSTClazzInfo.FSTFieldInfo[] fieldInfo, int startIndex, int version) protected booleanwriteObjectHeader(FSTClazzInfo clsInfo, FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) writeObjectInternal(Object obj, FSTClazzInfo ci, Class... possibles) protected FSTClazzInfowriteObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite) protected FSTClazzInfowriteObjectWithContext(FSTClazzInfo.FSTFieldInfo referencee, Object toWrite, FSTClazzInfo ci) voidwriteShort(int v) voidwriteStringUTF(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 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:
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. Theflushmethod ofDataOutputStreamcalls theflushmethod of its underlying output stream.- Specified by:
flushin interfaceObjectOutput- Throws:
IOException- if an I/O error occurs.- See Also:
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein 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:
writeObjectin interfaceObjectOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceObjectOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceObjectOutput- Throws:
IOException
-
write
- Specified by:
writein interfaceDataOutput- Specified by:
writein interfaceObjectOutput- Throws:
IOException
-
writeBoolean
- Specified by:
writeBooleanin interfaceDataOutput- Throws:
IOException
-
writeByte
- Specified by:
writeBytein interfaceDataOutput- Throws:
IOException
-
writeShort
- Specified by:
writeShortin interfaceDataOutput- Throws:
IOException
-
writeChar
- Specified by:
writeCharin interfaceDataOutput- Throws:
IOException
-
writeInt
- Specified by:
writeIntin interfaceDataOutput- Throws:
IOException
-
writeLong
- Specified by:
writeLongin interfaceDataOutput- Throws:
IOException
-
writeFloat
- Specified by:
writeFloatin interfaceDataOutput- Throws:
IOException
-
writeDouble
- Specified by:
writeDoublein interfaceDataOutput- Throws:
IOException
-
writeBytes
- Specified by:
writeBytesin interfaceDataOutput- Throws:
IOException
-
writeChars
- Specified by:
writeCharsin interfaceDataOutput- Throws:
IOException
-
writeUTF
- Specified by:
writeUTFin 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
-