Package com.tom_roush.pdfbox.io
Class RandomAccessFile
- java.lang.Object
-
- com.tom_roush.pdfbox.io.RandomAccessFile
-
- All Implemented Interfaces:
RandomAccess,RandomAccessRead,RandomAccessWrite,Closeable,AutoCloseable
public class RandomAccessFile extends Object implements RandomAccess
A RandomAccess implementation which allows data to be stored in a scratch file on the disk to reduce memory consumption.
-
-
Constructor Summary
Constructors Constructor Description RandomAccessFile(File file, String mode)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns an estimate of the number of bytes that can be read.voidclear()Clears all data of the buffer.voidclose()longgetPosition()Returns offset of next byte to be returned by a read method.booleanisClosed()Returns true if this stream has been closed.booleanisEOF()A simple test to see if we are at the end of the data.longlength()The total number of bytes that are available.intpeek()This will peek at the next byte.intread()Read a single byte of data.intread(byte[] b)Read a buffer of data.intread(byte[] b, int offset, int length)Read a buffer of data.byte[]readFully(int length)Reads a given number of bytes.voidrewind(int bytes)Seek backwards the given number of bytes.voidseek(long position)Seek to a position in the data.voidwrite(byte[] b)Write a buffer of data to the stream.voidwrite(byte[] b, int offset, int length)Write a buffer of data to the stream.voidwrite(int b)Write a byte to the stream.
-
-
-
Constructor Detail
-
RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException
Constructor.- Parameters:
file- The file to write the data to.mode- The writing mode.- Throws:
FileNotFoundException- If the file cannot be created.
-
-
Method Detail
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
clear
public void clear() throws IOExceptionDescription copied from interface:RandomAccessWriteClears all data of the buffer.- Specified by:
clearin interfaceRandomAccessWrite- Throws:
IOException
-
seek
public void seek(long position) throws IOExceptionDescription copied from interface:RandomAccessReadSeek to a position in the data.- Specified by:
seekin interfaceRandomAccessRead- Parameters:
position- The position to seek to.- Throws:
IOException- If there is an error while seeking.
-
getPosition
public long getPosition() throws IOExceptionDescription copied from interface:RandomAccessReadReturns offset of next byte to be returned by a read method.- Specified by:
getPositionin interfaceRandomAccessRead- Returns:
- offset of next byte which will be returned with next
RandomAccessRead.read()(if no more bytes are left it returns a value >= length of source) - Throws:
IOException
-
read
public int read() throws IOExceptionDescription copied from interface:RandomAccessReadRead a single byte of data.- Specified by:
readin interfaceRandomAccessRead- Returns:
- The byte of data that is being read.
- Throws:
IOException- If there is an error while reading the data.
-
read
public int read(byte[] b) throws IOExceptionDescription copied from interface:RandomAccessReadRead a buffer of data.- Specified by:
readin interfaceRandomAccessRead- Parameters:
b- The buffer to write the data to.- Returns:
- The number of bytes that were actually read.
- Throws:
IOException- If there was an error while reading the data.
-
read
public int read(byte[] b, int offset, int length) throws IOExceptionDescription copied from interface:RandomAccessReadRead a buffer of data.- Specified by:
readin interfaceRandomAccessRead- Parameters:
b- The buffer to write the data to.offset- Offset into the buffer to start writing.length- The amount of data to attempt to read.- Returns:
- The number of bytes that were actually read.
- Throws:
IOException- If there was an error while reading the data.
-
length
public long length() throws IOExceptionDescription copied from interface:RandomAccessReadThe total number of bytes that are available.- Specified by:
lengthin interfaceRandomAccessRead- Returns:
- The number of bytes available.
- Throws:
IOException- If there is an IO error while determining the length of the data stream.
-
isClosed
public boolean isClosed()
Description copied from interface:RandomAccessReadReturns true if this stream has been closed.- Specified by:
isClosedin interfaceRandomAccessRead
-
write
public void write(byte[] b, int offset, int length) throws IOExceptionDescription copied from interface:RandomAccessWriteWrite a buffer of data to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The buffer to get the data from.offset- An offset into the buffer to get the data from.length- The length of data to write.- Throws:
IOException- If there is an error while writing the data.
-
write
public void write(byte[] b) throws IOExceptionDescription copied from interface:RandomAccessWriteWrite a buffer of data to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The buffer to get the data from.- Throws:
IOException- If there is an error while writing the data.
-
write
public void write(int b) throws IOExceptionDescription copied from interface:RandomAccessWriteWrite a byte to the stream.- Specified by:
writein interfaceRandomAccessWrite- Parameters:
b- The byte to write.- Throws:
IOException- If there is an IO error while writing.
-
peek
public int peek() throws IOExceptionDescription copied from interface:RandomAccessReadThis will peek at the next byte.- Specified by:
peekin interfaceRandomAccessRead- Returns:
- The next byte on the stream, leaving it as available to read.
- Throws:
IOException- If there is an error reading the next byte.
-
rewind
public void rewind(int bytes) throws IOExceptionDescription copied from interface:RandomAccessReadSeek backwards the given number of bytes.- Specified by:
rewindin interfaceRandomAccessRead- Parameters:
bytes- the number of bytes to be seeked backwards- Throws:
IOException- If there is an error while seeking
-
readFully
public byte[] readFully(int length) throws IOExceptionDescription copied from interface:RandomAccessReadReads a given number of bytes.- Specified by:
readFullyin interfaceRandomAccessRead- Parameters:
length- the number of bytes to be read- Returns:
- a byte array containing the bytes just read
- Throws:
IOException- if an I/O error occurs while reading data
-
isEOF
public boolean isEOF() throws IOExceptionDescription copied from interface:RandomAccessReadA simple test to see if we are at the end of the data.- Specified by:
isEOFin interfaceRandomAccessRead- Returns:
- true if we are at the end of the data.
- Throws:
IOException- If there is an error reading the next byte.
-
available
public int available() throws IOExceptionDescription copied from interface:RandomAccessReadReturns an estimate of the number of bytes that can be read.- Specified by:
availablein interfaceRandomAccessRead- Returns:
- the number of bytes that can be read
- Throws:
IOException- if this random access has been closed
-
-