Class DataOutputBuffer

All Implemented Interfaces:
Closeable, DataOutput, Flushable, AutoCloseable, DataOutputPlus
Direct Known Subclasses:
DataOutputBufferFixed, SafeMemoryWriter

public class DataOutputBuffer extends BufferedDataOutputStreamPlus
An implementation of the DataOutputStream interface using a FastByteArrayOutputStream and exposing its buffer so copies can be avoided. This class is completely thread unsafe.
  • Field Details

    • scratchBuffer

      public static final io.netty.util.concurrent.FastThreadLocal<DataOutputBuffer> scratchBuffer
      Scratch buffers used mostly for serializing in memory. It's important to call #close() when finished to keep the memory overhead from being too large in the system.
  • Constructor Details

    • DataOutputBuffer

      public DataOutputBuffer()
    • DataOutputBuffer

      public DataOutputBuffer(int size)
    • DataOutputBuffer

      public DataOutputBuffer(ByteBuffer buffer)
  • Method Details

    • flush

      public void flush()
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class BufferedDataOutputStreamPlus
    • doFlush

      protected void doFlush(int count) throws IOException
      Overrides:
      doFlush in class BufferedDataOutputStreamPlus
      Throws:
      IOException
    • expandToFit

      protected void expandToFit(long count)
    • setBuffer

      protected void setBuffer(ByteBuffer newBuffer)
    • newDefaultChannel

      protected WritableByteChannel newDefaultChannel()
      Overrides:
      newDefaultChannel in class DataOutputStreamPlus
    • clear

      public void clear()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class BufferedDataOutputStreamPlus
    • buffer

      public ByteBuffer buffer()
    • buffer

      public ByteBuffer buffer(boolean duplicate)
    • unsafeGetBufferAndFlip

      public ByteBuffer unsafeGetBufferAndFlip()
      Gets the underlying ByteBuffer and calls ByteBuffer.flip(). This method is "unsafe" in the sense that it returns the underlying buffer, which may be modified by other methods after calling this method (or cleared on close()). If the calling logic knows that no new calls to this object will happen after calling this method, then this method can avoid the copying done in asNewBuffer(), and buffer().
    • getData

      public byte[] getData()
    • getLength

      public int getLength()
    • hasPosition

      public boolean hasPosition()
      Description copied from interface: DataOutputPlus
      If the implementation supports providing a position, this method returns true, otherwise false.
    • position

      public long position()
      Description copied from interface: DataOutputPlus
      Returns the current position of the underlying target like a file-pointer or the position withing a buffer. Not every implementation may support this functionality. Whether or not this functionality is supported can be checked via the DataOutputPlus.hasPosition().
    • asNewBuffer

      public ByteBuffer asNewBuffer()
    • toByteArray

      public byte[] toByteArray()
    • asString

      public String asString()