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 Details

  • 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

      public FSTObjectOutput​(java.io.OutputStream out, FSTConfiguration conf)
      Creates a new FSTObjectOutput stream to write data to the specified underlying output stream. The counter written 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

      public 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(). 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.IOException
      Flushes this data output stream. This forces any buffered output bytes to be written out to the stream.

      The flush method of DataOutputStream calls the flush method of its underlying output stream.

      Specified by:
      flush in interface java.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:
      close in interface java.lang.AutoCloseable
      Specified by:
      close in interface java.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:
      writeObject in interface java.io.ObjectOutput
      Throws:
      java.io.IOException
    • write

      public void write​(int b) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Specified by:
      write in interface java.io.ObjectOutput
      Throws:
      java.io.IOException
    • write

      public void write​(byte[] b) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Specified by:
      write in interface java.io.ObjectOutput
      Throws:
      java.io.IOException
    • write

      public void write​(byte[] b, int off, int len) throws java.io.IOException
      Specified by:
      write in interface java.io.DataOutput
      Specified by:
      write in interface java.io.ObjectOutput
      Throws:
      java.io.IOException
    • writeBoolean

      public void writeBoolean​(boolean v) throws java.io.IOException
      Specified by:
      writeBoolean in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeByte

      public void writeByte​(int v) throws java.io.IOException
      Specified by:
      writeByte in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeShort

      public void writeShort​(int v) throws java.io.IOException
      Specified by:
      writeShort in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeChar

      public void writeChar​(int v) throws java.io.IOException
      Specified by:
      writeChar in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeInt

      public void writeInt​(int v) throws java.io.IOException
      Specified by:
      writeInt in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeLong

      public void writeLong​(long v) throws java.io.IOException
      Specified by:
      writeLong in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeFloat

      public void writeFloat​(float v) throws java.io.IOException
      Specified by:
      writeFloat in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeDouble

      public void writeDouble​(double v) throws java.io.IOException
      Specified by:
      writeDouble in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeBytes

      public void writeBytes​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeBytes in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeChars

      public void writeChars​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeChars in interface java.io.DataOutput
      Throws:
      java.io.IOException
    • writeUTF

      public void writeUTF​(java.lang.String s) throws java.io.IOException
      Specified by:
      writeUTF in interface java.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

      protected FSTClazzInfo.FSTFieldInfo getCachedFI​(java.lang.Class... possibles)
    • 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

      public FSTSerialisationListener getListener()
    • setListener

      public void setListener​(FSTSerialisationListener listener)
      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

      public FSTClazzInfoRegistry 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 only
      clinfo -
      referencee -
      toWrite -
      Returns:
      Throws:
      java.io.IOException
    • getObjectMap

      public FSTObjectRegistry 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

      public FSTConfiguration 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

      public FSTEncoder getCodec()
    • setCodec

      protected void setCodec​(FSTEncoder codec)