-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
FileAsyncReader.java
62 lines (56 loc) · 1.99 KB
/
FileAsyncReader.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
58
59
60
61
62
/**
* Copyright (c) KMG. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.sbk.driver.File;
import io.sbk.api.AsyncReader;
import io.sbk.params.ParameterOptions;
import io.sbk.data.DataType;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Class for File Asynchronous Reader.
*/
public class FileAsyncReader implements AsyncReader<ByteBuffer> {
final private FileChannel in;
final private DataType<ByteBuffer> dType;
final private ExecutorService executor;
public FileAsyncReader(int id, ParameterOptions params, DataType<ByteBuffer> dType, FileConfig config) throws IOException {
this.in = FileChannel.open(Paths.get(config.fileName), StandardOpenOption.READ);
this.dType = dType;
this.executor = Executors.newFixedThreadPool(config.asyncThreads);
}
@Override
public CompletableFuture<ByteBuffer> readAsync(int size) {
final CompletableFuture<ByteBuffer> ret = new CompletableFuture<>();
CompletableFuture.runAsync(() -> {
final ByteBuffer readBuffer = dType.create(size);
try {
final int readSize = in.read(readBuffer);
if (readSize <= 0) {
ret.completeExceptionally(new EOFException());
}
ret.complete(readBuffer);
} catch (IOException ex) {
ret.completeExceptionally(ex);
}
}, executor);
return ret;
}
@Override
public void close() throws IOException {
in.close();
}
}