Package ghidra.app.util.bin
Class GhidraRandomAccessFile
- java.lang.Object
-
- ghidra.app.util.bin.GhidraRandomAccessFile
-
public class GhidraRandomAccessFile extends java.lang.Object
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 tolen
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.IOException
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.
- 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 filejava.io.IOException
-
-
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.
- 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
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 tolen
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.off
- the start offset of the data.len
- 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.
-
-