/
FileUtils.java
57 lines (50 loc) · 1.96 KB
/
FileUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.stoyanr.wordcounter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import com.stoyanr.wordcounter.TextProcessor;
public class FileUtils {
private static final int BUF_SIZE = 256 * 1024;
public static String readFileToString(Path file) throws IOException {
final StringBuilder sb = new StringBuilder();
readFileAsync(file, new TextProcessor<Void>() {
@Override
public Void process(String text, Void x) throws InterruptedException {
sb.append(text);
return x;
}
});
return sb.toString();
}
public static <T> void readFileAsync(Path file, TextProcessor<T> processor) throws IOException {
try (AsynchronousFileChannel ac = AsynchronousFileChannel.open(file)) {
ByteBuffer buffer = ByteBuffer.allocate(BUF_SIZE);
T rem = null;
int pos = 0, read = 0;
do {
read = readBuffer(buffer, ac, pos);
pos += read;
String text = Charset.defaultCharset().decode(buffer).toString();
rem = processor.process(text, rem);
} while (read == buffer.capacity());
processor.process("", rem);
} catch (IOException ex) {
throw ex;
} catch (ExecutionException | InterruptedException ex) {
}
}
private static int readBuffer(ByteBuffer buffer, AsynchronousFileChannel ac, int pos)
throws InterruptedException, ExecutionException {
buffer.rewind();
Future<Integer> future = ac.read(buffer, pos);
while (!future.isDone()) {
Thread.yield();
}
buffer.flip();
return future.get();
}
}