Package ghidra.app.util.bin
Class FileByteProvider
- java.lang.Object
-
- ghidra.app.util.bin.FileByteProvider
-
- All Implemented Interfaces:
ByteProvider
,MutableByteProvider
,java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
ObfuscatedFileByteProvider
public class FileByteProvider extends java.lang.Object implements ByteProvider, MutableByteProvider
AByteProvider
that reads its bytes from a file.
-
-
Field Summary
-
Fields inherited from interface ghidra.app.util.bin.ByteProvider
EMPTY_BYTEPROVIDER
-
-
Constructor Summary
Constructors Constructor Description FileByteProvider(java.io.File file, FSRL fsrl, java.nio.file.AccessMode accessMode)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Releases any resources theByteProvider
may have occupiedprotected int
doReadBytes(long index, byte[] buffer)
Reads bytes from the file.protected void
doWriteBytes(long index, byte[] buffer, int offset, int length)
Writes the specified bytes to the file.protected void
finalize()
java.lang.String
getAbsolutePath()
Returns the absolute path (similar to, but not a, URI) to theByteProvider
.java.nio.file.AccessMode
getAccessMode()
Returns the access mode the file was opened with.java.io.File
getFile()
Returns the underlyingFile
for thisByteProvider
, or null if thisByteProvider
is not associated with aFile
.FSRL
getFSRL()
Returns theFSRL
of the underlying file for this byte provider, or null if this byte provider is not associated with a file.java.lang.String
getName()
Returns the name of theByteProvider
.boolean
isValidIndex(long index)
Returns true if the specified index is valid.long
length()
Returns the length of theByteProvider
byte
readByte(long index)
Reads a byte at the specified indexint
readBytes(long index, byte[] buffer, int offset, int length)
Read bytes at the specified index into the given byte array.byte[]
readBytes(long index, long length)
Reads a byte array at the specified indexvoid
writeByte(long index, byte value)
Writes a byte at the specified index.void
writeBytes(long index, byte[] values)
Writes a byte array at the specified index.void
writeBytes(long index, byte[] buffer, int offset, int length)
Writes bytes to the specified offset in the file.-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface ghidra.app.util.bin.ByteProvider
getInputStream
-
-
-
-
Constructor Detail
-
FileByteProvider
public FileByteProvider(java.io.File file, FSRL fsrl, java.nio.file.AccessMode accessMode) throws java.io.IOException
Creates a new instance.- Parameters:
file
-File
to openfsrl
-FSRL
identity of the fileaccessMode
-AccessMode.READ
orAccessMode.WRITE
- Throws:
java.io.IOException
- if error
-
-
Method Detail
-
getAccessMode
public java.nio.file.AccessMode getAccessMode()
Returns the access mode the file was opened with.- Returns:
AccessMode
used to open file
-
close
public void close() throws java.io.IOException
Description copied from interface:ByteProvider
Releases any resources theByteProvider
may have occupied- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceByteProvider
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
- if an I/O error occurs
-
getFile
public java.io.File getFile()
Description copied from interface:ByteProvider
Returns the underlyingFile
for thisByteProvider
, or null if thisByteProvider
is not associated with aFile
.- Specified by:
getFile
in interfaceByteProvider
- Returns:
- the underlying file for this byte provider
-
getName
public java.lang.String getName()
Description copied from interface:ByteProvider
Returns the name of theByteProvider
. For example, the underlying file name.- Specified by:
getName
in interfaceByteProvider
- Returns:
- the name of the
ByteProvider
or null if there is no name
-
getAbsolutePath
public java.lang.String getAbsolutePath()
Description copied from interface:ByteProvider
Returns the absolute path (similar to, but not a, URI) to theByteProvider
. For example, the complete path to the file.- Specified by:
getAbsolutePath
in interfaceByteProvider
- Returns:
- the absolute path to the
ByteProvider
or null if not associated with aFile
.
-
getFSRL
public FSRL getFSRL()
Description copied from interface:ByteProvider
Returns theFSRL
of the underlying file for this byte provider, or null if this byte provider is not associated with a file.- Specified by:
getFSRL
in interfaceByteProvider
- Returns:
- The
FSRL
of the underlyingFile
, or null if no associatedFile
.
-
length
public long length() throws java.io.IOException
Description copied from interface:ByteProvider
Returns the length of theByteProvider
- Specified by:
length
in interfaceByteProvider
- Returns:
- the length of the
ByteProvider
- Throws:
java.io.IOException
- if an I/O error occurs
-
isValidIndex
public boolean isValidIndex(long index)
Description copied from interface:ByteProvider
Returns true if the specified index is valid.- Specified by:
isValidIndex
in interfaceByteProvider
- Parameters:
index
- the index in the byte provider to check- Returns:
- true if the specified index is valid
-
readByte
public byte readByte(long index) throws java.io.IOException
Description copied from interface:ByteProvider
Reads a byte at the specified index- Specified by:
readByte
in interfaceByteProvider
- Parameters:
index
- the index of the byte to read- Returns:
- the byte read from the specified index
- Throws:
java.io.IOException
- if an I/O error occurs
-
readBytes
public byte[] readBytes(long index, long length) throws java.io.IOException
Description copied from interface:ByteProvider
Reads a byte array at the specified index- Specified by:
readBytes
in interfaceByteProvider
- Parameters:
index
- the index of the byte to readlength
- the number of bytes to read- Returns:
- the byte array read from the specified index
- Throws:
java.io.IOException
- if an I/O error occurs
-
readBytes
public int readBytes(long index, byte[] buffer, int offset, int length) throws java.io.IOException
Read bytes at the specified index into the given byte array.See
InputStream.read(byte[], int, int)
.- Parameters:
index
- file offset to start readingbuffer
- byte array that will receive the bytesoffset
- offset inside the byte array to place the byteslength
- number of bytes to read- Returns:
- number of actual bytes read
- Throws:
java.io.IOException
- if error
-
finalize
protected void finalize()
- Overrides:
finalize
in classjava.lang.Object
-
writeBytes
public void writeBytes(long index, byte[] buffer, int offset, int length) throws java.io.IOException
Writes bytes to the specified offset in the file.- Parameters:
index
- the location in the file to starting writingbuffer
- bytes to writeoffset
- offset in the buffer byte array to startlength
- number of bytes to write- Throws:
java.io.IOException
- if badAccessMode
or other io error
-
writeByte
public void writeByte(long index, byte value) throws java.io.IOException
Description copied from interface:MutableByteProvider
Writes a byte at the specified index.- Specified by:
writeByte
in interfaceMutableByteProvider
- Parameters:
index
- the index to write the bytevalue
- the value to write at the specified index- Throws:
java.io.IOException
- if an I/O error occurs
-
writeBytes
public void writeBytes(long index, byte[] values) throws java.io.IOException
Description copied from interface:MutableByteProvider
Writes a byte array at the specified index.- Specified by:
writeBytes
in interfaceMutableByteProvider
- Parameters:
index
- the index to write the byte arrayvalues
- the values to write at the specified index- Throws:
java.io.IOException
- if an I/O error occurs
-
doReadBytes
protected int doReadBytes(long index, byte[] buffer) throws java.io.IOException
Reads bytes from the file.Protected by synchronized lock. (See
getBufferFor(long)
).- Parameters:
index
- file position of where to readbuffer
- byte array that will receive bytes- Returns:
- actual number of byte read
- Throws:
java.io.IOException
- if error
-
doWriteBytes
protected void doWriteBytes(long index, byte[] buffer, int offset, int length) throws java.io.IOException
Writes the specified bytes to the file.Protected by synchronized lock (See
writeBytes(long, byte[], int, int)
)- Parameters:
index
- file position of where to writebuffer
- byte array containing bytes to writeoffset
- offset inside of byte array to startlength
- number of bytes from buffer to write- Throws:
java.io.IOException
- if error
-
-