Class RandomAccessWriter

  • All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable

    public class RandomAccessWriter
    extends PrintWriter
    A PrintWriter that writes to a file channel, allowing it to do random access types of writes. This supports a stack of marks (and therefore resets). It also allows for jumping to offsets in the file, determining the current file offset, and truncating the file.
    Author:
    Jerry Vos
    • Method Detail

      • getTrueOffset

        public long getTrueOffset()
                           throws IOException
        Retrieves the actual offset of the file, disregarding any buffering. A result of this call is that all buffered data is flushed before this function returns.
        Returns:
        the current offset of the file
        Throws:
        IOException
      • mark

        public void mark()
                  throws IOException
        Adds a mark to the mark queue at the current location in the file. Since this used getTrueOffset(), this causes the underlying data stream to flush.
        Throws:
        IOException
      • popMark

        public Long popMark()
        Removes a mark from the mark queue and returns it.
        Returns:
        the top of the mark queue
      • reset

        public void reset()
                   throws IOException
        This jumps the file offset back to the previously marked position. This does not check if the mark stack is empty, so when calling this with an empty mark stack, an exception will be thrown. This does not pop the mark that was used off the stack.
        Throws:
        IOException
      • position

        public void position​(long position)
                      throws IOException
        Positions the file to the specified position.
        Parameters:
        position - the position to set the write mark to
        Throws:
        IOException
      • truncate

        public FileChannel truncate​(long size)
                             throws IOException
        Truncates the file to the specified size.
        Parameters:
        size - the size to truncate the file to
        Returns:
        the truncated channel
        Throws:
        IOException