import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws Exception{
File aFile = new File("primes.txt");
FileInputStream inFile = new FileInputStream(aFile);
FileChannel inChannel = inFile.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(1024);
buf.position(buf.limit());
while (true) {
if (buf.remaining() < 8) {
if (inChannel.read(buf.compact()) == -1) {
break;
}
buf.flip();
}
int strLength = (int) buf.getDouble();
if (buf.remaining() < 2 * strLength) {
if (inChannel.read(buf.compact()) == -1) {
break;
}
buf.flip();
}
byte[] strChars = new byte[2 * strLength];
buf.get(strChars);
if (buf.remaining() < 8) {
if (inChannel.read(buf.compact()) == -1) {
break;
}
buf.flip();
}
System.out.println(strLength);
System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
System.out.println(buf.getLong());
}
inFile.close(); }
}
|