Class RandomAccessWriter

java.lang.Object
java.io.Writer
java.io.PrintWriter
repast.simphony.integration.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
  • Constructor Details

  • Method Details

    • 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
    • truncateToPosition

      public FileChannel truncateToPosition() throws IOException
      Truncates the file to the current offset. This is the same as using truncate(long) with the value from getTrueOffset().
      Returns:
      the truncated channel
      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
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class PrintWriter