@Deprecated public static int getFilenoFromChannel(Channel channel) { return FilenoUtil.filenoFrom(channel); } }
private static int getFilenoUsingReflection(Channel channel) { if (ReflectiveAccess.FILE_DESCRIPTOR_FD != null) { return filenoFrom(getDescriptorFromChannel(channel)); } return -1; }
public static int filenoFrom(Channel channel) { if (channel instanceof NativeSelectableChannel) { return ((NativeSelectableChannel)channel).getFD(); } return getFilenoUsingReflection(channel); }
private void initFileno() { realFileno = FilenoUtil.filenoFrom(ch); if (realFileno == -1) { fakeFileno = filenoUtil.getNewFileno(); } else { fakeFileno = -1; } }
/** * Build a set of mode flags using the specified channel's actual capabilities. * * @param channel the channel to examine for capabilities * @return the mode flags */ public static int oflagsFrom(POSIX posix, Channel channel) { int mode; int fileno = FilenoUtil.filenoFrom(channel); if (FilenoUtil.isFake(fileno) || !posix.isNative() || Platform.IS_WINDOWS) { // channel doesn't have a real fileno; best we can do is go off the Java type if (channel instanceof ReadableByteChannel) { if (channel instanceof WritableByteChannel) { mode = RDWR; } else { mode = RDONLY; } } else if (channel instanceof WritableByteChannel) { mode = WRONLY; } else { // FIXME: I don't like this mode = RDWR; } } else { // real fileno, we can use fcntl mode = posix.fcntl(fileno, Fcntl.F_GETFL); } return mode; }
if (!FilenoUtil.isFake(fileno)) { fd = runtime.getFilenoUtil().getWrapperFromFileno(fileno); ChannelFD descriptor = runtime.getFilenoUtil().getWrapperFromFileno(fileno);
@Deprecated public static FileDescriptor getDescriptorFromChannel(Channel channel) { return FilenoUtil.getDescriptorFromChannel(channel); }
@JRubyMethod(meta = true) public static IRubyObject for_fd(ThreadContext context, IRubyObject _klass, IRubyObject _fileno) { Ruby runtime = context.runtime; int fileno = (int)_fileno.convertToInteger().getLongValue(); RubyClass klass = (RubyClass)_klass; ChannelFD fd = runtime.getFilenoUtil().getWrapperFromFileno(fileno); RubyBasicSocket basicSocket = (RubyBasicSocket)klass.getAllocator().allocate(runtime, klass); basicSocket.initSocket(fd); return basicSocket; }
public ChannelFD getWrapperFromFileno(int fileno) { ChannelFD fd = filenoMap.get(fileno); // This is a hack to get around stale ChannelFD that are closed when a descriptor is reused. // It appears to happen for openpty, and in theory could happen for any IO call that produces // a new descriptor. if (fd != null && !fd.ch.isOpen() && !isFake(fileno)) { FileStat stat = posix.allocateStat(); if (posix.fstat(fileno, stat) >= 0) { // found ChannelFD is closed, but actual fileno is open; clear it. filenoMap.remove(fileno); fd = null; } } return fd; }
public ChannelFD(Channel fd, POSIX posix, FilenoUtil filenoUtil) { assert fd != null; this.ch = fd; this.posix = posix; this.filenoUtil = filenoUtil; initFileno(); initChannelTypes(); refs = new AtomicInteger(1); filenoUtil.registerWrapper(realFileno, this); filenoUtil.registerWrapper(fakeFileno, this); }
private void finish() throws IOException { synchronized (refs) { // if refcount is at or below zero, we're no longer valid if (refs.get() <= 0) { throw new ClosedChannelException(); } // if channel is already closed, we're no longer valid if (!ch.isOpen()) { throw new ClosedChannelException(); } // otherwise decrement and possibly close as normal int count = refs.decrementAndGet(); if (count <= 0) { // if we're the last referrer, close the channel try { ch.close(); } finally { filenoUtil.unregisterWrapper(realFileno); filenoUtil.unregisterWrapper(fakeFileno); } } } }
filenoUtil = new FilenoUtil(posix);
private void initFileno() { realFileno = FilenoUtil.filenoFrom(ch); if (realFileno == -1) { fakeFileno = filenoUtil.getNewFileno(); } else { fakeFileno = -1; } }
/** * Build a set of mode flags using the specified channel's actual capabilities. * * @param channel the channel to examine for capabilities * @return the mode flags */ public static int oflagsFrom(POSIX posix, Channel channel) { int mode; int fileno = FilenoUtil.filenoFrom(channel); if (FilenoUtil.isFake(fileno) || !posix.isNative() || Platform.IS_WINDOWS) { // channel doesn't have a real fileno; best we can do is go off the Java type if (channel instanceof ReadableByteChannel) { if (channel instanceof WritableByteChannel) { mode = RDWR; } else { mode = RDONLY; } } else if (channel instanceof WritableByteChannel) { mode = WRONLY; } else { // FIXME: I don't like this mode = RDWR; } } else { // real fileno, we can use fcntl mode = posix.fcntl(fileno, Fcntl.F_GETFL); } return mode; }
if (!FilenoUtil.isFake(fileno)) { fd = runtime.getFilenoUtil().getWrapperFromFileno(fileno); ChannelFD descriptor = runtime.getFilenoUtil().getWrapperFromFileno(fileno);
@Deprecated public static FileDescriptor getDescriptorFromChannel(Channel channel) { return FilenoUtil.getDescriptorFromChannel(channel); }
@JRubyMethod(meta = true) public static IRubyObject for_fd(ThreadContext context, IRubyObject _klass, IRubyObject _fileno) { Ruby runtime = context.runtime; int fileno = (int)_fileno.convertToInteger().getLongValue(); RubyClass klass = (RubyClass)_klass; ChannelFD fd = runtime.getFilenoUtil().getWrapperFromFileno(fileno); RubyBasicSocket basicSocket = (RubyBasicSocket)klass.getAllocator().allocate(runtime, klass); basicSocket.initSocket(fd); return basicSocket; }
public ChannelFD getWrapperFromFileno(int fileno) { ChannelFD fd = filenoMap.get(fileno); // This is a hack to get around stale ChannelFD that are closed when a descriptor is reused. // It appears to happen for openpty, and in theory could happen for any IO call that produces // a new descriptor. if (fd != null && !fd.ch.isOpen() && !isFake(fileno)) { FileStat stat = posix.allocateStat(); if (posix.fstat(fileno, stat) >= 0) { // found ChannelFD is closed, but actual fileno is open; clear it. filenoMap.remove(fileno); fd = null; } } return fd; }
public ChannelFD(Channel fd, POSIX posix, FilenoUtil filenoUtil) { assert fd != null; this.ch = fd; this.posix = posix; this.filenoUtil = filenoUtil; initFileno(); initChannelTypes(); refs = new AtomicInteger(1); filenoUtil.registerWrapper(realFileno, this); filenoUtil.registerWrapper(fakeFileno, this); }
private void finish() throws IOException { synchronized (refs) { // if refcount is at or below zero, we're no longer valid if (refs.get() <= 0) { throw new ClosedChannelException(); } // if channel is already closed, we're no longer valid if (!ch.isOpen()) { throw new ClosedChannelException(); } // otherwise decrement and possibly close as normal int count = refs.decrementAndGet(); if (count <= 0) { // if we're the last referrer, close the channel try { ch.close(); } finally { filenoUtil.unregisterWrapper(realFileno); filenoUtil.unregisterWrapper(fakeFileno); } } } }