From b458d7067d77530afa33ce2fdd311ab861bde49b Mon Sep 17 00:00:00 2001 From: Karsten Jeppesen Date: Wed, 14 Apr 2021 18:42:18 +0200 Subject: [PATCH] NioLineCount: Working --- NioLineCount/src/NioLineCount.java | 63 +++++++++++++++++++----------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/NioLineCount/src/NioLineCount.java b/NioLineCount/src/NioLineCount.java index 267dde3..16cd84a 100644 --- a/NioLineCount/src/NioLineCount.java +++ b/NioLineCount/src/NioLineCount.java @@ -8,6 +8,7 @@ import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.concurrent.Semaphore; /** * @@ -20,36 +21,54 @@ import java.nio.file.StandardOpenOption; public class NioLineCount { 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){ if ( ! doDance ) return -1; ByteBuffer buffer = ByteBuffer.allocate(1024); - long myPosition = 0; Path myPath = Paths.get( thePath ); try { AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(myPath, StandardOpenOption.READ); - fileChannel.read(buffer, myPosition, buffer, new CompletionHandler() { - @Override - public void completed(Integer result, ByteBuffer attachment) { - System.out.println("Result = " + result + " Limit = " + attachment.limit()); - attachment.flip(); - //System.out.println(Charset.defaultCharset().decode(buffer)); - byte[] data = new byte[attachment.limit()]; - attachment.get( data ); - System.out.println(new String(data)); - System.out.println(">> " + data.toString()); - for (int i = 0; i < data.length; i++ ) { - if ( data[i] == '\n' ) - theCount++; - //System.out.println("data: " + data[i]); + while ( doContinue ) { + try { + // In the end we will be waiting here + partialReadGate.acquire(); + if ( ! doContinue ) return theCount; + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + fileChannel.read(buffer, readPosition, buffer, new CompletionHandler() { + @Override + public void completed(Integer result, ByteBuffer attachment) { + // System.out.println("Result = " + result + " Limit = " + attachment.limit()); + 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) { // TODO Auto-generated catch block e.printStackTrace();