Class AppendableMemory

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class AppendableMemory
    extends Object
    implements Closeable
    A class that allows writing to a series of Memory blocks as if they are one big coherent chunk of memory. Memory is allocated along the way using a MemoryAllocator. Useful for situations where you don't know ahead of time exactly how much memory you'll need.
    • Method Detail

      • create

        public static AppendableMemory create​(MemoryAllocator allocator)
        Creates an appendable memory instance with a default initial allocation size. This default size can accept is a multiple of 4, 5, 8, and 9, meaning that reserveAdditional(int) can accept allocations of that size and remain fully-packed.
      • create

        public static AppendableMemory create​(MemoryAllocator allocator,
                                              int initialAllocationSize)
        Creates an appendable memory instance using a particular initial allocation size.
      • cursor

        public MemoryRange<org.apache.datasketches.memory.WritableMemory> cursor()
        Return a pointer to the current cursor location, which is where the next elements should be written. The returned object is updated on calls to advanceCursor(int) and rewindCursor(int). The start of the returned range is the cursor location; the end is the end of the current Memory block. The returned Memory object is in little-endian order.
      • reserveAdditional

        public boolean reserveAdditional​(int bytes)
        Ensure that at least "bytes" amount of space is available after the cursor. Allocates a new block if needed. Note: the amount of bytes is guaranteed to be in a *single* block. Does not move the cursor forward. Multiple calls to this method without moving the cursor forward are not additive: the reservations overlap on top of each other. Typical usage is to call reserveAdditional, then write to the memory, then call advanceCursor(int) with the amount of memory written.
        Returns:
        true if reservation was successful, false otherwise.
      • advanceCursor

        public void advanceCursor​(int bytes)
        Advances the cursor a certain number of bytes. This number of bytes must not exceed the space available in the current block. Typical usage is to call reserveAdditional(int), then write to the memory, then call advanceCursor with the amount of memory written.
      • rewindCursor

        public void rewindCursor​(int bytes)
        Rewinds the cursor a certain number of bytes, effectively erasing them. This provides a way to undo the most-recently-done write. The number of bytes rewound must not rewind prior to the current block. It is safe, and typical, to call rewindCursor with values up to, and including, the most recent call to advanceCursor(int).
      • size

        public long size()
      • writeTo

        public long writeTo​(org.apache.datasketches.memory.WritableMemory memory,
                            long startPosition)
        Write current memory to a WritableMemory buffer.
      • clear

        public void clear()