LongBuffer: remaining()
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.nio.BufferUnderflowException;
import java.nio.channels.FileChannel;
public class MainClass {
public static void main(String[] args) {
File aFile = new File("data.dat");
FileInputStream inFile = null;
try {
inFile = new FileInputStream(aFile);
} catch (FileNotFoundException e) {
e.printStackTrace(System.err);
System.exit(1);
}
FileChannel inChannel = inFile.getChannel();
final int COUNT = 6;
ByteBuffer buf = ByteBuffer.allocate(8 * COUNT);
long[] data = new long[COUNT];
try {
int pos = 0;
while (inChannel.read(buf) != -1) {
try {
((ByteBuffer) (buf.flip())).asLongBuffer().get(data);
pos = data.length;
} catch (BufferUnderflowException e) {
LongBuffer longBuf = buf.asLongBuffer();
pos = longBuf.remaining();
longBuf.get(data, 0, pos);
}
System.out.println();
for (int i = 0; i < pos; i++) {
System.out.printf("%10d", data[i]);
}
buf.clear();
}
System.out.println("\nEOF reached.");
inFile.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.exit(1);
}
}
}
Related examples in the same category