forked from jnr/jnr-enxio
/
NativeDeviceChannel.java
49 lines (40 loc) · 1.32 KB
/
NativeDeviceChannel.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
package enxio.nio.channels;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.nio.channels.spi.SelectorProvider;
public class NativeDeviceChannel extends AbstractSelectableChannel implements ByteChannel, NativeSelectableChannel {
private final int fd;
private final int validOps;
public NativeDeviceChannel(int fd) {
this(NativeSelectorProvider.getInstance(), fd, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}
public NativeDeviceChannel(SelectorProvider provider, int fd, int ops) {
super(provider);
this.fd = fd;
this.validOps = ops;
}
@Override
protected void implCloseSelectableChannel() throws IOException {
Native.close(fd);
}
@Override
protected void implConfigureBlocking(boolean block) throws IOException {
Native.setBlocking(fd, block);
}
@Override
public final int validOps() {
return validOps;
}
public final int getFD() {
return fd;
}
public int read(ByteBuffer dst) throws IOException {
return Native.read(fd, dst);
}
public int write(ByteBuffer src) throws IOException {
return Native.write(fd, src);
}
}