NioLineCount: Working

This commit is contained in:
Karsten Jeppesen
2021-04-14 18:42:18 +02:00
parent 0ad4c474bc
commit b458d7067d

View File

@@ -8,6 +8,7 @@ import java.nio.file.OpenOption;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.concurrent.Semaphore;
/** /**
* *
@@ -20,36 +21,54 @@ import java.nio.file.StandardOpenOption;
public class NioLineCount { public class NioLineCount {
static int theCount = 0; static int theCount = 0;
// A 0 means that you will be waited until signalled
static Semaphore partialReadGate = new Semaphore(1);
static boolean doContinue = true;
static long readPosition = 0;
private static int lineCount(String thePath, boolean doDance){ private static int lineCount(String thePath, boolean doDance){
if ( ! doDance ) return -1; if ( ! doDance ) return -1;
ByteBuffer buffer = ByteBuffer.allocate(1024); ByteBuffer buffer = ByteBuffer.allocate(1024);
long myPosition = 0;
Path myPath = Paths.get( thePath ); Path myPath = Paths.get( thePath );
try { try {
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(myPath, StandardOpenOption.READ); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(myPath, StandardOpenOption.READ);
fileChannel.read(buffer, myPosition, buffer, new CompletionHandler<Integer,ByteBuffer>() { while ( doContinue ) {
@Override try {
public void completed(Integer result, ByteBuffer attachment) { // In the end we will be waiting here
System.out.println("Result = " + result + " Limit = " + attachment.limit()); partialReadGate.acquire();
attachment.flip(); if ( ! doContinue ) return theCount;
//System.out.println(Charset.defaultCharset().decode(buffer)); } catch (InterruptedException e) {
byte[] data = new byte[attachment.limit()]; // TODO Auto-generated catch block
attachment.get( data ); e.printStackTrace();
System.out.println(new String(data)); }
System.out.println(">> " + data.toString()); fileChannel.read(buffer, readPosition, buffer, new CompletionHandler<Integer,ByteBuffer>() {
for (int i = 0; i < data.length; i++ ) { @Override
if ( data[i] == '\n' ) public void completed(Integer result, ByteBuffer attachment) {
theCount++; // System.out.println("Result = " + result + " Limit = " + attachment.limit());
//System.out.println("data: " + data[i]); if ( result != -1 ) {
attachment.flip();
byte[] data = new byte[attachment.limit()];
attachment.get( data );
//System.out.println(new String(data));
for (int i = 0; i < data.length; i++ ) {
if ( data[i] == '\n' )
theCount++;
//System.out.println("data: " + data[i]);
}
// System.out.println("position: " + readPosition + " Count: " + theCount);
readPosition += data.length;
attachment.clear();
} else {
doContinue = false;
}
partialReadGate.release();
} }
attachment.clear();
} @Override
public void failed(Throwable exc, ByteBuffer attachment) {
@Override }
public void failed(Throwable exc, ByteBuffer attachment) { });
} }
});
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();