/** * @return [0] = read end, [1] = write end */ public static FileDescriptor[] pipe() throws IOException { long res = newPipe(); if (res < 0) { throw newIOException("newPipe", (int) res); } return new FileDescriptor[]{new FileDescriptor((int) (res >>> 32)), new FileDescriptor((int) res)}; }
/** * Close the file descriptor. */ public void close() throws IOException { for (;;) { int state = this.state; if (isClosed(state)) { return; } // Once a close operation happens, the channel is considered shutdown. if (casState(state, state | STATE_ALL_MASK)) { break; } } int res = close(fd); if (res < 0) { throw newIOException("close", res); } }
/** * Open a new {@link FileDescriptor} for the given {@link File}. */ public static FileDescriptor from(File file) throws IOException { return from(checkNotNull(file, "file").getPath()); }
/** * Open a new {@link FileDescriptor} for the given path. */ public static FileDescriptor from(String path) throws IOException { checkNotNull(path, "path"); int res = open(path); if (res < 0) { throw newIOException("open", res); } return new FileDescriptor(res); }
@Override public final void close() throws java.io.IOException { fd.close(); }
private void wakeup() { Native.keventTriggerUserEvent(kqueueFd.intValue(), KQUEUE_WAKE_UP_IDENT); // Note that the result may return an error (e.g. errno = EBADF after the event loop has been shutdown). // So it is not very practical to assert the return value is always >= 0. }
static FileDescriptor newKQueue() { return new FileDescriptor(kqueueCreate()); }
this.eventFd = eventFd = Native.newEventFd(); try { Native.epollCtlAdd(epollFd.intValue(), eventFd.intValue(), Native.EPOLLIN); } catch (IOException e) { throw new IllegalStateException("Unable to add eventFd filedescriptor to epoll", e); Native.epollCtlAdd(epollFd.intValue(), timerFd.intValue(), Native.EPOLLIN | Native.EPOLLET); } catch (IOException e) { throw new IllegalStateException("Unable to add timerFd filedescriptor to epoll", e); if (epollFd != null) { try { epollFd.close(); } catch (Exception e) { eventFd.close(); } catch (Exception e) { timerFd.close(); } catch (Exception e) {
public final int read(ByteBuffer buf, int pos, int limit) throws IOException { int res = read(fd, buf, pos, limit); if (res > 0) { return res; } if (res == 0) { return -1; } return ioResult("read", res, READ_CONNECTION_RESET_EXCEPTION, READ_CLOSED_CHANNEL_EXCEPTION); }
/** * Returns {@code true} if the file descriptor is open. */ public boolean isOpen() { return !isClosed(state); }
@Override public final boolean isOpen() { return fd.isOpen(); }
/** * Open a new {@link FileDescriptor} for the given path. */ public static FileDescriptor from(String path) throws IOException { checkNotNull(path, "path"); int res = open(path); if (res < 0) { throw newIOException("open", res); } return new FileDescriptor(res); }
@Override public final void close() throws java.io.IOException { fd.close(); }
@Override protected void wakeup(boolean inEventLoop) { if (!inEventLoop && WAKEN_UP_UPDATER.compareAndSet(this, 0, 1)) { // write to the evfd which will then wake-up epoll_wait(...) Native.eventFdWrite(eventFd.intValue(), 1L); } }
public static FileDescriptor newEventFd() { return new FileDescriptor(eventFd()); }
this.eventFd = eventFd = Native.newEventFd(); try { Native.epollCtlAdd(epollFd.intValue(), eventFd.intValue(), Native.EPOLLIN); } catch (IOException e) { throw new IllegalStateException("Unable to add eventFd filedescriptor to epoll", e); Native.epollCtlAdd(epollFd.intValue(), timerFd.intValue(), Native.EPOLLIN | Native.EPOLLET); } catch (IOException e) { throw new IllegalStateException("Unable to add timerFd filedescriptor to epoll", e); if (epollFd != null) { try { epollFd.close(); } catch (Exception e) { eventFd.close(); } catch (Exception e) { timerFd.close(); } catch (Exception e) {
public final int read(ByteBuffer buf, int pos, int limit) throws IOException { int res = read(fd, buf, pos, limit); if (res > 0) { return res; } if (res == 0) { return -1; } return ioResult("read", res, READ_CONNECTION_RESET_EXCEPTION, READ_CLOSED_CHANNEL_EXCEPTION); }
/** * Returns {@code true} if the file descriptor is open. */ public boolean isOpen() { return !isClosed(state); }
@Override public final boolean isOpen() { return fd.isOpen(); }
/** * Close the file descriptor. */ public void close() throws IOException { for (;;) { int state = this.state; if (isClosed(state)) { return; } // Once a close operation happens, the channel is considered shutdown. if (casState(state, state | STATE_ALL_MASK)) { break; } } int res = close(fd); if (res < 0) { throw newIOException("close", res); } }