Package repast.simphony.integration
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
APrintWriter
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
-
-
Field Summary
-
Fields inherited from class java.io.PrintWriter
out
-
-
Constructor Summary
Constructors Constructor Description RandomAccessWriter(File file)
Opens this writer on the specified file.RandomAccessWriter(String fileName)
Opens this writer on the specified file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
long
getTrueOffset()
Retrieves the actual offset of the file, disregarding any buffering.void
mark()
Adds a mark to the mark queue at the current location in the file.Long
popMark()
Removes a mark from the mark queue and returns it.void
position(long position)
Positions the file to the specified position.void
reset()
This jumps the file offset back to the previously marked position.FileChannel
truncate(long size)
Truncates the file to the specified size.FileChannel
truncateToPosition()
Truncates the file to the current offset.-
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write
-
Methods inherited from class java.io.Writer
nullWriter
-
-
-
-
Constructor Detail
-
RandomAccessWriter
public RandomAccessWriter(File file) throws FileNotFoundException
Opens this writer on the specified file.- Parameters:
file
- the file to write to- Throws:
FileNotFoundException
-
RandomAccessWriter
public RandomAccessWriter(String fileName) throws FileNotFoundException
Opens this writer on the specified file.- Parameters:
fileName
- the name of the file to open- Throws:
FileNotFoundException
-
-
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 usedgetTrueOffset()
, 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 usingtruncate(long)
with the value fromgetTrueOffset()
.- 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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classPrintWriter
-
-