Package ghidra.app.util.bin
Class GhidraRandomAccessFile
java.lang.Object
ghidra.app.util.bin.GhidraRandomAccessFile
- All Implemented Interfaces:
AutoCloseable
Instances of this class support both reading and writing to a
random access file. A random access file behaves like a large
array of bytes stored in the file system. There is a kind of cursor,
or index into the implied array, called the file pointer.
This implementation relies on java.net.RandomAccessFile,
but adds buffering to limit the amount.
-
Constructor Summary
ConstructorDescriptionGhidraRandomAccessFile
(File file, String mode) Creates a random access file stream to read from, and optionally to write to, the file specified by theFile
argument. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this random access file stream and releases any system resources associated with the stream.protected void
finalize()
long
length()
Returns the length of this file.int
read
(byte[] b) Reads up tob.length
bytes of data from this file into an array of bytes.int
read
(byte[] b, int offset, int length) Reads up tolength
bytes of data from this file into an array of bytes.byte
readByte()
This method reads a byte from the file, starting from the current file pointer.void
seek
(long pos) Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.void
write
(byte b) Writes a byte to this file, starting at the current file pointer.void
write
(byte[] b) Writesb.length
bytes from the specified byte array to this file, starting at the current file pointer.void
write
(byte[] b, int offset, int length) Writes a sub array as a sequence of bytes.
-
Constructor Details
-
GhidraRandomAccessFile
Creates a random access file stream to read from, and optionally to write to, the file specified by theFile
argument. A newFileDescriptor
object is created to represent this file connection.This implementation relies on java.net.RandomAccessFile, but adds buffering to limit the amount.
The
mode
argument specifies the access mode in which the file is to be opened. The permitted values and their meanings are:Value
Meaning
"r"
Open for reading only. Invoking any of the write
methods of the resulting object will cause anIOException
to be thrown."rw"
Open for reading and writing. If the file does not already exist then an attempt will be made to create it. "rws"
Open for reading and writing, as with "rw"
, and also require that every update to the file's content or metadata be written synchronously to the underlying storage device."rwd"
Open for reading and writing, as with "rw"
, and also require that every update to the file's content be written synchronously to the underlying storage device.- Parameters:
file
- the file objectmode
- the access mode, as described above- Throws:
IllegalArgumentException
- if the mode argument is not equal to one of"r"
,"rw"
,"rws"
, or"rwd"
FileNotFoundException
- that name cannot be created, or if some other error occurs while opening or creating the file
-
-
Method Details
-
finalize
protected void finalize() -
close
Closes this random access file stream and releases any system resources associated with the stream. A closed random access file cannot perform input or output operations and cannot be reopened.If this file has an associated channel then the channel is closed as well.
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
- if an I/O error occurs.
-
length
Returns the length of this file.- Returns:
- the length of this file, measured in bytes.
- Throws:
IOException
- if an I/O error occurs.
-
seek
Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs. The offset may be set beyond the end of the file. Setting the offset beyond the end of the file does not change the file length. The file length will change only by writing after the offset has been set beyond the end of the file.- Parameters:
pos
- the offset position, measured in bytes from the beginning of the file, at which to set the file pointer.- Throws:
IOException
- ifpos
is less than0
or if an I/O error occurs.
-
readByte
This method reads a byte from the file, starting from the current file pointer.This method blocks until the byte is read, the end of the stream is detected, or an exception is thrown.
- Returns:
- the next byte of this file as a signed eight-bit
byte
. - Throws:
EOFException
- if this file has reached the end.IOException
- if an I/O error occurs.
-
read
Reads up tob.length
bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.- Parameters:
b
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of this file has been reached. - Throws:
IOException
- if an I/O error occurs.
-
read
Reads up tolength
bytes of data from this file into an array of bytes. This method blocks until at least one byte of input is available.- Parameters:
b
- the buffer into which the data is read.offset
- the start offset of the data.length
- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the file has been reached. - Throws:
IOException
- if an I/O error occurs.
-
write
Writes a byte to this file, starting at the current file pointer.- Parameters:
b
- the data.- Throws:
IOException
- if an I/O error occurs.
-
write
Writesb.length
bytes from the specified byte array to this file, starting at the current file pointer.- Parameters:
b
- the data.- Throws:
IOException
- if an I/O error occurs.
-
write
Writes a sub array as a sequence of bytes.- Parameters:
b
- the data to be writtenoffset
- the start offset in the datalength
- the number of bytes that are written- Throws:
IOException
- If an I/O error has occurred.
-