NioLineCount: WIP. Non-blocking IO
Not working yet
This commit is contained in:
6
NioLineCount/.classpath
Normal file
6
NioLineCount/.classpath
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
17
NioLineCount/.project
Normal file
17
NioLineCount/.project
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>NioLineCount</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
11776
NioLineCount/input.txt
Normal file
11776
NioLineCount/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
74
NioLineCount/src/NioLineCount.java
Normal file
74
NioLineCount/src/NioLineCount.java
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.CharBuffer;
|
||||||
|
import java.nio.channels.AsynchronousFileChannel;
|
||||||
|
import java.nio.channels.CompletionHandler;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.OpenOption;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author KAJE
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class NioLineCount {
|
||||||
|
|
||||||
|
static int theCount = 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<Integer,ByteBuffer>() {
|
||||||
|
@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]);
|
||||||
|
}
|
||||||
|
attachment.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failed(Throwable exc, ByteBuffer attachment) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return theCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args){
|
||||||
|
long startTime = System.nanoTime();
|
||||||
|
lineCount("", false);
|
||||||
|
long dryRun = System.nanoTime() - startTime;
|
||||||
|
startTime = System.nanoTime();
|
||||||
|
int lines = lineCount("input.txt", true);
|
||||||
|
long stopTime = System.nanoTime();
|
||||||
|
System.out.println("Lines: " + String.valueOf(lines));
|
||||||
|
System.out.println("Time it took: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user