Package org.apache.pdfbox.io
Class RandomAccessFile
java.lang.Object
org.apache.pdfbox.io.RandomAccessFile
- All Implemented Interfaces:
Closeable
,AutoCloseable
,RandomAccess
,RandomAccessRead
,RandomAccessWrite
A RandomAccess implementation which allows data to be stored in a scratch file on the disk to
reduce memory consumption.
- Author:
- Ben Litchfield
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Returns an estimate of the number of bytes that can be read.void
clear()
Clears all data of the buffer.void
close()
long
Returns offset of next byte to be returned by a read method.boolean
isClosed()
Returns true if this stream has been closed.boolean
isEOF()
A simple test to see if we are at the end of the data.long
length()
The total number of bytes that are available.int
peek()
This will peek at the next byte.int
read()
Read a single byte of data.int
read
(byte[] b) Read a buffer of data.int
read
(byte[] b, int offset, int length) Read a buffer of data.byte[]
readFully
(int length) Reads a given number of bytes.void
rewind
(int bytes) Seek backwards the given number of bytes.void
seek
(long position) Seek to a position in the data.void
write
(byte[] b) Write a buffer of data to the stream.void
write
(byte[] b, int offset, int length) Write a buffer of data to the stream.void
write
(int b) Write a byte to the stream.
-
Constructor Details
-
RandomAccessFile
Constructor.- Parameters:
file
- The file to write the data to.mode
- The writing mode.- Throws:
FileNotFoundException
- If the file cannot be created.
-
-
Method Details
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
clear
Description copied from interface:RandomAccessWrite
Clears all data of the buffer.- Specified by:
clear
in interfaceRandomAccessWrite
- Throws:
IOException
-
seek
Description copied from interface:RandomAccessRead
Seek to a position in the data.- Specified by:
seek
in interfaceRandomAccessRead
- Parameters:
position
- The position to seek to.- Throws:
IOException
- If there is an error while seeking.
-
getPosition
Description copied from interface:RandomAccessRead
Returns offset of next byte to be returned by a read method.- Specified by:
getPosition
in 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
Description copied from interface:RandomAccessRead
Read a single byte of data.- Specified by:
read
in interfaceRandomAccessRead
- Returns:
- The byte of data that is being read.
- Throws:
IOException
- If there is an error while reading the data.
-
read
Description copied from interface:RandomAccessRead
Read a buffer of data.- Specified by:
read
in 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
Description copied from interface:RandomAccessRead
Read a buffer of data.- Specified by:
read
in 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
Description copied from interface:RandomAccessRead
The total number of bytes that are available.- Specified by:
length
in 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:RandomAccessRead
Returns true if this stream has been closed.- Specified by:
isClosed
in interfaceRandomAccessRead
-
write
Description copied from interface:RandomAccessWrite
Write a buffer of data to the stream.- Specified by:
write
in 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
Description copied from interface:RandomAccessWrite
Write a buffer of data to the stream.- Specified by:
write
in interfaceRandomAccessWrite
- Parameters:
b
- The buffer to get the data from.- Throws:
IOException
- If there is an error while writing the data.
-
write
Description copied from interface:RandomAccessWrite
Write a byte to the stream.- Specified by:
write
in interfaceRandomAccessWrite
- Parameters:
b
- The byte to write.- Throws:
IOException
- If there is an IO error while writing.
-
peek
Description copied from interface:RandomAccessRead
This will peek at the next byte.- Specified by:
peek
in 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
Description copied from interface:RandomAccessRead
Seek backwards the given number of bytes.- Specified by:
rewind
in interfaceRandomAccessRead
- Parameters:
bytes
- the number of bytes to be seeked backwards- Throws:
IOException
- If there is an error while seeking
-
readFully
Description copied from interface:RandomAccessRead
Reads a given number of bytes.- Specified by:
readFully
in 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
Description copied from interface:RandomAccessRead
A simple test to see if we are at the end of the data.- Specified by:
isEOF
in interfaceRandomAccessRead
- Returns:
- true if we are at the end of the data.
- Throws:
IOException
- If there is an error reading the next byte.
-
available
Description copied from interface:RandomAccessRead
Returns an estimate of the number of bytes that can be read.- Specified by:
available
in interfaceRandomAccessRead
- Returns:
- the number of bytes that can be read
- Throws:
IOException
- if this random access has been closed
-