Reputation: 357
In My java application (fuse file system) I need to read all types of files into ByteBuffer. I did it as below:
public int read(final String path, final ByteBuffer buffer, final long size, final long offset, final FileInfoWrapper info)
{
Path p = Paths.get(path);
try {
byte[] data = Files.readAllBytes(p);
buffer.put(ByteBuffer.wrap(data));
return data.length;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but only *.txt extensions files are read correctly (I think its because of less size, larger *.txt files are also not read correctly). The rest of file types are not correctly read.
These errors are shown by file type specific applications while opening the files
It throws these errors while reading files other than *.txt
SEVERE: Exception thrown: java.nio.BufferOverflowException
java.nio.DirectByteBuffer.put(DirectByteBuffer.java:357)
java.nio.DirectByteBuffer.put(DirectByteBuffer.java:336)
org.organization.upesh.FirstMaven.SFS_360.read(SFS_360.java:132)
net.fusejna.LoggedFuseFilesystem$27.invoke(LoggedFuseFilesystem.java:437)
net.fusejna.LoggedFuseFilesystem$27.invoke(LoggedFuseFilesystem.java:433)
net.fusejna.LoggedFuseFilesystem.log(LoggedFuseFilesystem.java:355)
net.fusejna.LoggedFuseFilesystem.read(LoggedFuseFilesystem.java:432)
net.fusejna.FuseFilesystem._read(FuseFilesystem.java:234)
net.fusejna.StructFuseOperations$23.callback(StructFuseOperations.java:260)
sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
com.sun.jna.CallbackReference$DefaultCallbackProxy.invokeCallback(CallbackReference.java:455)
com.sun.jna.CallbackReference$DefaultCallbackProxy.callback(CallbackReference.java:485)
And also reading process is very slow.
What is the correct way to read all types of file correctly and at good speed.
P.S. whatever solution you suggest, reading file into ByteBuffer is must and also must Returns the number of bytes transferred.
Upvotes: 1
Views: 3902
Reputation: 4882
Please read the function signature of what you're trying to implement.
public int read(final String path, final ByteBuffer buffer, final long size, final long offset, final FileInfoWrapper info)
As you can see, there's a size
and an offset
argument there. The function is supposed to read size
number of bytes from the file at most, and supposed to read them from the offset offset
from the file. The function is not supposed to read everything. The provided example filesystem shows how to do this.
Upvotes: 1
Reputation: 216
Try using FileChannel.map() http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#map(java.nio.channels.FileChannel.MapMode,%20long,%20long) This creates a ByteBuffer that is mapped with direct IO to a file. It will not attempt to load everything in one shot, but will give you data as it's needed.
Upvotes: 0