public class NativeFile
extends java.lang.Object
implements java.io.Closeable, java.lang.AutoCloseable
This class provides access to Linux files using native I/O operations. It is implemented using the JNA package. The class is required because certain operations (like ioctl) that are required by the Lego kernel module interface are not support by standard Java methods. In addition standard Java memory mapped files do not seem to function correctly when used with Linux character devices.
Only JNA is used, the original interface used combination of Java and JNA.
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_PRIVS |
protected int |
fd |
private ILibc |
libc |
Modifier | Constructor and Description |
---|---|
protected |
NativeFile()
Basic constructor.
|
protected |
NativeFile(ILibc libc)
Basic constructor.
|
|
NativeFile(java.lang.String fname,
int flags)
Create a NativeFile object and open the associated file/device
for native access.
|
|
NativeFile(java.lang.String fname,
int flags,
ILibc libc)
Create a NativeFile object and open the associated file/device
for native access.
|
|
NativeFile(java.lang.String fname,
int flags,
int mode)
Create a NativeFile object and open the associated file/device
for native access.
|
|
NativeFile(java.lang.String fname,
int flags,
int mode,
ILibc libc)
Create a NativeFile object and open the associated file/device
for native access.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the associated file
|
int |
ioctl(int req,
IntByReference info)
Perform a Linux style ioctl operation on the associated file.
|
int |
ioctl(int req,
Pointer buf)
Perform a Linux style ioctl operation on the associated file.
|
boolean |
isOpen()
Check whether this file has been open()en.
|
Pointer |
mmap(long len,
int prot,
int flags,
long off)
Map a portion of the associated file into memory and return a pointer
that can be used to access that memory.
|
void |
msync(Pointer addr,
long len,
int flags)
Synchronize mapped memory region.
|
void |
munmap(Pointer addr,
long len)
Unmap mapped memory region.
|
void |
open(java.lang.String fname,
int flags)
Open the specified file/device for native access.
|
void |
open(java.lang.String fname,
int flags,
int mode)
Open the specified file/device for native access.
|
int |
read(byte[] buf,
int len)
Attempt to read the requested number of bytes from the associated file.
|
int |
read(byte[] buf,
int offset,
int len)
Attempt to read the requested number of byte from the associated file.
|
int |
write(byte[] buf,
int len)
Attempt to write the requested number of bytes to the associated file.
|
int |
write(byte[] buf,
int offset,
int len)
Attempt to write the requested number of bytes to the associated file.
|
private static int DEFAULT_PRIVS
protected int fd
private ILibc libc
protected NativeFile()
protected NativeFile(ILibc libc)
libc
- standard C library interface to be used.public NativeFile(java.lang.String fname, int flags) throws LastErrorException
fname
- the name of the file to openflags
- Linux style file access flagsLastErrorException
- when operations failspublic NativeFile(java.lang.String fname, int flags, int mode) throws LastErrorException
fname
- the name of the file to openflags
- Linux style file access flagsmode
- Linux style file access modeLastErrorException
- when operations failspublic NativeFile(java.lang.String fname, int flags, ILibc libc) throws LastErrorException
fname
- the name of the file to openflags
- Linux style file access flagslibc
- standard C library interface to be used.LastErrorException
- when operations failspublic NativeFile(java.lang.String fname, int flags, int mode, ILibc libc) throws LastErrorException
fname
- the name of the file to openflags
- Linux style file access flagsmode
- Linux style file access modelibc
- standard C library interface to be used.LastErrorException
- when operations failspublic boolean isOpen()
public void open(java.lang.String fname, int flags) throws LastErrorException
fname
- the name of the file to openflags
- Linux style file access flagsLastErrorException
- when operations failspublic void open(java.lang.String fname, int flags, int mode) throws LastErrorException
fname
- the name of the file to openflags
- Linux style file access flagsmode
- Linux style file access modeLastErrorException
- when operations failspublic int read(byte[] buf, int len) throws LastErrorException
buf
- location to store the read byteslen
- number of bytes to attempt to readLastErrorException
- when operations failspublic int write(byte[] buf, int offset, int len) throws LastErrorException
buf
- location to store the read bytesoffset
- the offset within buf to take data from for the writelen
- number of bytes to attempt to readLastErrorException
- when operations failspublic int read(byte[] buf, int offset, int len) throws LastErrorException
buf
- location to store the read bytesoffset
- offset with buf to start storing the read byteslen
- number of bytes to attempt to readLastErrorException
- when operations failspublic int write(byte[] buf, int len) throws LastErrorException
buf
- location to store the read byteslen
- number of bytes to attempt to readLastErrorException
- when operations failspublic int ioctl(int req, IntByReference info) throws LastErrorException
req
- ioctl operation to be performedinfo
- output as integerLastErrorException
- when operations failspublic int ioctl(int req, Pointer buf) throws LastErrorException
req
- ioctl operation to be performedbuf
- pointer to ioctl parametersLastErrorException
- when operations failspublic void close() throws LastErrorException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
LastErrorException
- when operations failspublic Pointer mmap(long len, int prot, int flags, long off) throws LastErrorException
len
- size of the region to mapprot
- protection for the memory regionflags
- Linux mmap flagsoff
- offset within the file for the start of the regionLastErrorException
- when operations failspublic void munmap(Pointer addr, long len) throws LastErrorException
addr
- Mapped address.len
- Region length.LastErrorException
- when operations failspublic void msync(Pointer addr, long len, int flags) throws LastErrorException
addr
- Mapped address.len
- Region length.flags
- Synchronization flagsLastErrorException
- when operations fails