Package ghidra.app.util.bin
Class GhidraRandomAccessFile
- java.lang.Object
-
- ghidra.app.util.bin.GhidraRandomAccessFile
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class GhidraRandomAccessFile extends java.lang.Object implements java.lang.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
Constructors Constructor Description GhidraRandomAccessFile(java.io.File file, java.lang.String mode)
Creates a random access file stream to read from, and optionally to write to, the file specified by theFile
argument.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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 Detail
-
GhidraRandomAccessFile
public GhidraRandomAccessFile(java.io.File file, java.lang.String mode) throws java.io.FileNotFoundException
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:
java.lang.IllegalArgumentException
- if the mode argument is not equal to one of"r"
,"rw"
,"rws"
, or"rwd"
java.io.FileNotFoundException
- that name cannot be created, or if some other error occurs while opening or creating the file
-
-
Method Detail
-
finalize
protected void finalize()
- Overrides:
finalize
in classjava.lang.Object
-
close
public void close() throws java.io.IOException
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 interfacejava.lang.AutoCloseable
- Throws:
java.io.IOException
- if an I/O error occurs.
-
length
public long length() throws java.io.IOException
Returns the length of this file.- Returns:
- the length of this file, measured in bytes.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
seek
public void seek(long pos) throws java.io.IOException
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:
java.io.IOException
- ifpos
is less than0
or if an I/O error occurs.
-
readByte
public byte readByte() throws java.io.IOException
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:
java.io.EOFException
- if this file has reached the end.java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b, int offset, int length) throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurs.
-
write
public void write(byte b) throws java.io.IOException
Writes a byte to this file, starting at the current file pointer.- Parameters:
b
- the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
write
public void write(byte[] b) throws java.io.IOException
Writesb.length
bytes from the specified byte array to this file, starting at the current file pointer.- Parameters:
b
- the data.- Throws:
java.io.IOException
- if an I/O error occurs.
-
write
public void write(byte[] b, int offset, int length) throws java.io.IOException
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:
java.io.IOException
- If an I/O error has occurred.
-
-