1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.unix;
17
18 import io.netty.util.internal.ThrowableUtil;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.channels.ClosedChannelException;
24 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
25
26 import static io.netty.channel.unix.Errors.ioResult;
27 import static io.netty.channel.unix.Errors.newIOException;
28 import static io.netty.util.internal.ObjectUtil.checkNotNull;
29
30
31
32
33
34 public class FileDescriptor {
35 private static final ClosedChannelException WRITE_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
36 new ClosedChannelException(), FileDescriptor.class, "write(..)");
37 private static final ClosedChannelException WRITE_ADDRESS_CLOSED_CHANNEL_EXCEPTION =
38 ThrowableUtil.unknownStackTrace(new ClosedChannelException(), FileDescriptor.class, "writeAddress(..)");
39 private static final ClosedChannelException WRITEV_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
40 new ClosedChannelException(), FileDescriptor.class, "writev(..)");
41 private static final ClosedChannelException WRITEV_ADDRESSES_CLOSED_CHANNEL_EXCEPTION =
42 ThrowableUtil.unknownStackTrace(new ClosedChannelException(), FileDescriptor.class, "writevAddresses(..)");
43 private static final ClosedChannelException READ_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
44 new ClosedChannelException(), FileDescriptor.class, "read(..)");
45 private static final ClosedChannelException READ_ADDRESS_CLOSED_CHANNEL_EXCEPTION = ThrowableUtil.unknownStackTrace(
46 new ClosedChannelException(), FileDescriptor.class, "readAddress(..)");
47 private static final Errors.NativeIoException WRITE_CONNECTION_RESET_EXCEPTION = ThrowableUtil.unknownStackTrace(
48 Errors.newConnectionResetException("syscall:write", Errors.ERRNO_EPIPE_NEGATIVE),
49 FileDescriptor.class, "write(..)");
50 private static final Errors.NativeIoException WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION =
51 ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:write",
52 Errors.ERRNO_EPIPE_NEGATIVE), FileDescriptor.class, "writeAddress(..)");
53 private static final Errors.NativeIoException WRITEV_CONNECTION_RESET_EXCEPTION = ThrowableUtil.unknownStackTrace(
54 Errors.newConnectionResetException("syscall:writev", Errors.ERRNO_EPIPE_NEGATIVE),
55 FileDescriptor.class, "writev(..)");
56 private static final Errors.NativeIoException WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION =
57 ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:writev",
58 Errors.ERRNO_EPIPE_NEGATIVE), FileDescriptor.class, "writeAddresses(..)");
59 private static final Errors.NativeIoException READ_CONNECTION_RESET_EXCEPTION = ThrowableUtil.unknownStackTrace(
60 Errors.newConnectionResetException("syscall:read", Errors.ERRNO_ECONNRESET_NEGATIVE),
61 FileDescriptor.class, "read(..)");
62 private static final Errors.NativeIoException READ_ADDRESS_CONNECTION_RESET_EXCEPTION =
63 ThrowableUtil.unknownStackTrace(Errors.newConnectionResetException("syscall:read",
64 Errors.ERRNO_ECONNRESET_NEGATIVE), FileDescriptor.class, "readAddress(..)");
65
66 private static final AtomicIntegerFieldUpdater<FileDescriptor> stateUpdater =
67 AtomicIntegerFieldUpdater.newUpdater(FileDescriptor.class, "state");
68
69 private static final int STATE_CLOSED_MASK = 1;
70 private static final int STATE_INPUT_SHUTDOWN_MASK = 1 << 1;
71 private static final int STATE_OUTPUT_SHUTDOWN_MASK = 1 << 2;
72 private static final int STATE_ALL_MASK = STATE_CLOSED_MASK |
73 STATE_INPUT_SHUTDOWN_MASK |
74 STATE_OUTPUT_SHUTDOWN_MASK;
75
76
77
78 volatile int state;
79 final int fd;
80
81 public FileDescriptor(int fd) {
82 if (fd < 0) {
83 throw new IllegalArgumentException("fd must be >= 0");
84 }
85 this.fd = fd;
86 }
87
88
89
90
91 public int intValue() {
92 return fd;
93 }
94
95
96
97
98 public void close() throws IOException {
99 for (;;) {
100 int state = this.state;
101 if (isClosed(state)) {
102 return;
103 }
104
105 if (casState(state, state | STATE_ALL_MASK)) {
106 break;
107 }
108 }
109 int res = close(fd);
110 if (res < 0) {
111 throw newIOException("close", res);
112 }
113 }
114
115
116
117
118 public boolean isOpen() {
119 return !isClosed(state);
120 }
121
122 public final int write(ByteBuffer buf, int pos, int limit) throws IOException {
123 int res = write(fd, buf, pos, limit);
124 if (res >= 0) {
125 return res;
126 }
127 return ioResult("write", res, WRITE_CONNECTION_RESET_EXCEPTION, WRITE_CLOSED_CHANNEL_EXCEPTION);
128 }
129
130 public final int writeAddress(long address, int pos, int limit) throws IOException {
131 int res = writeAddress(fd, address, pos, limit);
132 if (res >= 0) {
133 return res;
134 }
135 return ioResult("writeAddress", res,
136 WRITE_ADDRESS_CONNECTION_RESET_EXCEPTION, WRITE_ADDRESS_CLOSED_CHANNEL_EXCEPTION);
137 }
138
139 public final long writev(ByteBuffer[] buffers, int offset, int length) throws IOException {
140 long res = writev(fd, buffers, offset, length);
141 if (res >= 0) {
142 return res;
143 }
144 return ioResult("writev", (int) res, WRITEV_CONNECTION_RESET_EXCEPTION, WRITEV_CLOSED_CHANNEL_EXCEPTION);
145 }
146
147 public final long writevAddresses(long memoryAddress, int length) throws IOException {
148 long res = writevAddresses(fd, memoryAddress, length);
149 if (res >= 0) {
150 return res;
151 }
152 return ioResult("writevAddresses", (int) res,
153 WRITEV_ADDRESSES_CONNECTION_RESET_EXCEPTION, WRITEV_ADDRESSES_CLOSED_CHANNEL_EXCEPTION);
154 }
155
156 public final int read(ByteBuffer buf, int pos, int limit) throws IOException {
157 int res = read(fd, buf, pos, limit);
158 if (res > 0) {
159 return res;
160 }
161 if (res == 0) {
162 return -1;
163 }
164 return ioResult("read", res, READ_CONNECTION_RESET_EXCEPTION, READ_CLOSED_CHANNEL_EXCEPTION);
165 }
166
167 public final int readAddress(long address, int pos, int limit) throws IOException {
168 int res = readAddress(fd, address, pos, limit);
169 if (res > 0) {
170 return res;
171 }
172 if (res == 0) {
173 return -1;
174 }
175 return ioResult("readAddress", res,
176 READ_ADDRESS_CONNECTION_RESET_EXCEPTION, READ_ADDRESS_CLOSED_CHANNEL_EXCEPTION);
177 }
178
179 @Override
180 public String toString() {
181 return "FileDescriptor{" +
182 "fd=" + fd +
183 '}';
184 }
185
186 @Override
187 public boolean equals(Object o) {
188 if (this == o) {
189 return true;
190 }
191 if (!(o instanceof FileDescriptor)) {
192 return false;
193 }
194
195 return fd == ((FileDescriptor) o).fd;
196 }
197
198 @Override
199 public int hashCode() {
200 return fd;
201 }
202
203
204
205
206 public static FileDescriptor from(String path) throws IOException {
207 checkNotNull(path, "path");
208 int res = open(path);
209 if (res < 0) {
210 throw newIOException("open", res);
211 }
212 return new FileDescriptor(res);
213 }
214
215
216
217
218 public static FileDescriptor from(File file) throws IOException {
219 return from(checkNotNull(file, "file").getPath());
220 }
221
222
223
224
225 public static FileDescriptor[] pipe() throws IOException {
226 long res = newPipe();
227 if (res < 0) {
228 throw newIOException("newPipe", (int) res);
229 }
230 return new FileDescriptor[]{new FileDescriptor((int) (res >>> 32)), new FileDescriptor((int) res)};
231 }
232
233 final boolean casState(int expected, int update) {
234 return stateUpdater.compareAndSet(this, expected, update);
235 }
236
237 static boolean isClosed(int state) {
238 return (state & STATE_CLOSED_MASK) != 0;
239 }
240
241 static boolean isInputShutdown(int state) {
242 return (state & STATE_INPUT_SHUTDOWN_MASK) != 0;
243 }
244
245 static boolean isOutputShutdown(int state) {
246 return (state & STATE_OUTPUT_SHUTDOWN_MASK) != 0;
247 }
248
249 static int inputShutdown(int state) {
250 return state | STATE_INPUT_SHUTDOWN_MASK;
251 }
252
253 static int outputShutdown(int state) {
254 return state | STATE_OUTPUT_SHUTDOWN_MASK;
255 }
256
257 private static native int open(String path);
258 private static native int close(int fd);
259
260 private static native int write(int fd, ByteBuffer buf, int pos, int limit);
261 private static native int writeAddress(int fd, long address, int pos, int limit);
262 private static native long writev(int fd, ByteBuffer[] buffers, int offset, int length);
263 private static native long writevAddresses(int fd, long memoryAddress, int length);
264
265 private static native int read(int fd, ByteBuffer buf, int pos, int limit);
266 private static native int readAddress(int fd, long address, int pos, int limit);
267
268 private static native long newPipe();
269 }