Class BufferedChannel

All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
DefaultEntryLogger.BufferedLogChannel, SlowBufferedChannel

public class BufferedChannel extends BufferedReadChannel implements Closeable
Provides a buffering layer in front of a FileChannel.
  • Field Details

    • writeCapacity

      protected final int writeCapacity
      The capacity of the write buffer.
    • writeBufferStartPosition

      protected AtomicLong writeBufferStartPosition
      The position of the file channel's write pointer.
    • writeBuffer

      protected final io.netty.buffer.ByteBuf writeBuffer
      The buffer used to write operations.
    • position

      protected volatile long position
      The absolute position of the next write operation.
    • unpersistedBytesBound

      protected final long unpersistedBytesBound
    • unpersistedBytes

      protected final AtomicLong unpersistedBytes
  • Constructor Details

    • BufferedChannel

      public BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, FileChannel fc, int capacity) throws IOException
      Throws:
      IOException
    • BufferedChannel

      public BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, FileChannel fc, int capacity, long unpersistedBytesBound) throws IOException
      Throws:
      IOException
    • BufferedChannel

      public BufferedChannel(io.netty.buffer.ByteBufAllocator allocator, FileChannel fc, int writeCapacity, int readCapacity, long unpersistedBytesBound) throws IOException
      Throws:
      IOException
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • write

      public void write(io.netty.buffer.ByteBuf src) throws IOException
      Write all the data in src to the FileChannel. Note that this function can buffer or re-order writes based on the implementation. These writes will be flushed to the disk only when flush() is invoked.
      Parameters:
      src - The source ByteBuffer which contains the data to be written.
      Throws:
      IOException - if a write operation fails.
    • position

      public long position()
      Get the position where the next write operation will begin writing from.
      Returns:
    • getFileChannelPosition

      public long getFileChannelPosition()
      Get the position of the file channel's write pointer.
      Returns:
    • flushAndForceWrite

      public void flushAndForceWrite(boolean forceMetadata) throws IOException
      calls both flush and forceWrite methods.
      Parameters:
      forceMetadata - - If true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written
      Throws:
      IOException
    • flushAndForceWriteIfRegularFlush

      public void flushAndForceWriteIfRegularFlush(boolean forceMetadata) throws IOException
      calls both flush and forceWrite methods if regular flush is enabled.
      Parameters:
      forceMetadata - - If true then this method is required to force changes to both the file's content and metadata to be written to storage; otherwise, it need only force content changes to be written
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Write any data in the buffer to the file and advance the writeBufferPosition. Callers are expected to synchronize appropriately
      Throws:
      IOException - if the write fails.
    • forceWrite

      public long forceWrite(boolean forceMetadata) throws IOException
      force a sync operation so that data is persisted to the disk.
      Parameters:
      forceMetadata -
      Returns:
      Throws:
      IOException
    • read

      public int read(io.netty.buffer.ByteBuf dest, long pos, int length) throws IOException
      Overrides:
      read in class BufferedReadChannel
      Throws:
      IOException
    • clear

      public void clear()
      Overrides:
      clear in class BufferedReadChannel
    • getNumOfBytesInWriteBuffer

      public int getNumOfBytesInWriteBuffer()
    • getUnpersistedBytes

      long getUnpersistedBytes()