private IRubyObject shutdownInternal(ThreadContext context, int how) throws BadDescriptorException { Ruby runtime = context.runtime; Channel channel; switch (how) { case 0: channel = getOpenChannel(); try { SocketType.forChannel(channel).shutdownInput(channel); } catch (IOException e) { throw runtime.newIOError(e.getMessage()); } if (openFile.getPipeStream() != null) { openFile.setMainStream(openFile.getPipeStream()); openFile.setPipeStream(null); } openFile.setMode(openFile.getMode() & ~OpenFile.READABLE); return RubyFixnum.zero(runtime); case 1: channel = getOpenChannel(); try { SocketType.forChannel(channel).shutdownOutput(channel); } catch (IOException e) { throw runtime.newIOError(e.getMessage()); } openFile.setPipeStream(null); openFile.setMode(openFile.getMode() & ~OpenFile.WRITABLE); return RubyFixnum.zero(runtime); case 2: shutdownInternal(context, 0); shutdownInternal(context, 1); return RubyFixnum.zero(runtime); default: throw runtime.newArgumentError("`how' should be either :SHUT_RD, :SHUT_WR, :SHUT_RDWR"); } }
@Override @JRubyMethod public IRubyObject close_write(ThreadContext context) { Ruby runtime = context.runtime; if (!openFile.isWritable()) { return runtime.getNil(); } if (openFile.getPipeStream() == null && openFile.isReadable()) { throw runtime.newIOError("closing non-duplex IO for writing"); } if (!openFile.isReadable()) { close(); } else { // shutdown write try { shutdownInternal(context, 1); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } } return context.nil; }
private IRubyObject shutdownInternal(ThreadContext context, int how) { Channel socketChannel; switch (how) { case 0: socketChannel = openFile.getMainStream().getDescriptor().getChannel(); try { if (socketChannel instanceof SocketChannel || socketChannel instanceof DatagramChannel) { asSocket().shutdownInput(); } else if (socketChannel instanceof Shutdownable) { ((Shutdownable) socketChannel).shutdownInput(); } } catch (IOException e) { throw context.getRuntime().newIOError(e.getMessage()); } if (openFile.getPipeStream() != null) { openFile.setMainStream(openFile.getPipeStream()); openFile.setPipeStream(null); } openFile.setMode(openFile.getMode() & ~OpenFile.READABLE); return RubyFixnum.zero(context.getRuntime()); case 1: socketChannel = openFile.getMainStream().getDescriptor().getChannel(); try { if (socketChannel instanceof SocketChannel || socketChannel instanceof DatagramChannel) { asSocket().shutdownOutput(); } else if (socketChannel instanceof Shutdownable) { ((Shutdownable) socketChannel).shutdownOutput(); } } catch (IOException e) { throw context.getRuntime().newIOError(e.getMessage()); } openFile.setPipeStream(null); openFile.setMode(openFile.getMode() & ~OpenFile.WRITABLE); return RubyFixnum.zero(context.getRuntime()); case 2: shutdownInternal(context, 0); shutdownInternal(context, 1); return RubyFixnum.zero(context.getRuntime()); default: throw context.getRuntime().newArgumentError("`how' should be either 0, 1, 2"); } }
@Override public IRubyObject close_read(ThreadContext context) { Ruby runtime = context.getRuntime(); if (runtime.getSafeLevel() >= 4 && isTaint()) { throw runtime.newSecurityError("Insecure: can't close"); } if (!openFile.isOpen()) { throw context.getRuntime().newIOError("not opened for reading"); } if (!openFile.isWritable()) { close(); } else { // shutdown read shutdownInternal(context, 0); } return runtime.getNil(); }
@Override public IRubyObject close_write(ThreadContext context) { if (context.getRuntime().getSafeLevel() >= 4 && isTaint()) { throw context.getRuntime().newSecurityError("Insecure: can't close"); } if (!openFile.isWritable()) { return context.getRuntime().getNil(); } if (openFile.getPipeStream() == null && openFile.isReadable()) { throw context.getRuntime().newIOError("closing non-duplex IO for writing"); } if (!openFile.isReadable()) { close(); } else { // shutdown write shutdownInternal(context, 1); } return context.getRuntime().getNil(); }
@Override @JRubyMethod public IRubyObject close_read(ThreadContext context) { Ruby runtime = context.runtime; if (!openFile.isOpen()) { throw context.runtime.newIOError("not opened for reading"); } if (!openFile.isWritable()) { close(); } else { // shutdown read try { shutdownInternal(context, 0); } catch (BadDescriptorException e) { throw runtime.newErrnoEBADFError(); } } return context.nil; }