final Pipe topPipe = Pipe.open(); try { topPipe.source().configureBlocking(false); topPipe.sink().configureBlocking(false); final Pipe bottomPipe = Pipe.open(); try { bottomPipe.source().configureBlocking(false); bottomPipe.sink().configureBlocking(false); final WorkerThread peerThread = getPeerThread(peer); final SelectionKey topSourceKey = registerChannel(topPipe.source()); final SelectionKey topSinkKey = peerThread.registerChannel(topPipe.sink()); final SelectionKey bottomSourceKey = peerThread.registerChannel(bottomPipe.source()); final SelectionKey bottomSinkKey = registerChannel(bottomPipe.sink()); final NioPipeStreamConnection leftConnection = new NioPipeStreamConnection(this, bottomSourceKey, topSinkKey); if (! ok) { safeClose(bottomPipe.sink()); safeClose(bottomPipe.source()); if (! ok) { safeClose(topPipe.sink()); safeClose(topPipe.source());
public ChannelPipe<StreamSourceChannel, StreamSinkChannel> createHalfDuplexPipe(final XnioIoFactory peer) throws IOException { getWorker().checkShutdown(); final Pipe pipe = Pipe.open(); boolean ok = false; try { pipe.source().configureBlocking(false); pipe.sink().configureBlocking(false); final WorkerThread peerThread = getPeerThread(peer); final SelectionKey readKey = registerChannel(pipe.source()); final SelectionKey writeKey = peerThread.registerChannel(pipe.sink()); final NioPipeStreamConnection leftConnection = new NioPipeStreamConnection(this, readKey, null); final NioPipeStreamConnection rightConnection = new NioPipeStreamConnection(this, null, writeKey); leftConnection.writeClosed(); rightConnection.readClosed(); final ChannelPipe<StreamSourceChannel,StreamSinkChannel> result = new ChannelPipe<StreamSourceChannel, StreamSinkChannel>(leftConnection.getSourceChannel(), rightConnection.getSinkChannel()); ok = true; return result; } finally { if (! ok) { safeClose(pipe.sink()); safeClose(pipe.source()); } } }
/** * @return a channel from which stderr data produced by the spawn can be read, or * null if there is no channel to stderr. */ Pipe.SourceChannel getStderrChannel() { if (systemErr == null) { return null; } return systemErr.source(); }
/** * Returns the SelectableChannel of the queue that can be registered * with a selector. * * @return SelectableChannel of the queue */ public SelectableChannel readChannel() { return _readPipe.source(); }
@Override public void run(Selectable selectable) { try { wakeup.source().read(ByteBuffer.allocate(64)); } catch (IOException e) { throw new RuntimeException(e); } expireSelectable(selectable); }
@Override public void run(Selectable selectable) { try { wakeup.source().read(ByteBuffer.allocate(64)); } catch (IOException e) { throw new RuntimeException(e); } expireSelectable(selectable); }
void pipeRead() throws IOException { if (_pipeWriteCount.decrementAndGet() == 0) { _pipeReadByte.clear(); _pipe.source().read(_pipeReadByte); } }
/** * Read from the pipe. */ public int read(byte[] dst, int offset, int length) throws IOException { checkNotFlipped(); return pipe.source().read(ByteBuffer.wrap(dst, offset, length)); }
/** * Get a CodepointIterator that can iterate over unicode codepoints in this pipe. The pipe must be readable */ public CodepointIterator getIterator() { checkNotFlipped(); return CodepointIterator.forReadableByteChannel(pipe.source(), charset); }
/** * Get a CodepointIterator that can iterate over unicode codepoints in this pipe. The pipe must be readable */ public CodepointIterator getIterator(String charset) { checkNotFlipped(); return CodepointIterator.forReadableByteChannel(pipe.source(), charset); }
/** * Read from the pipe. */ public int read(ByteBuffer dst) throws IOException { checkNotFlipped(); return pipe.source().read(dst); }
public PipeSocket(Socket socket) throws IOException { this.socket = socket; this.inputPipe = Pipe.open(); this.outputPipe = Pipe.open(); this.outputPipe.source().configureBlocking(false); }
/** * Get an inputstream that can read from this pipe. The Pipe must be readable */ public InputStream getInputStream() { checkNotFlipped(); return new PipeChannelInputStream(this, Channels.newInputStream(pipe.source())); }
/** * Get a reader that can reader from this pipe. The pipe must be readable */ public Reader getReader(String charset) { checkNotFlipped(); return new PipeChannelReader(this, Channels.newReader(pipe.source(), charset)); }
/** * Get a reader that can reader from this pipe. The pipe must be readable */ public Reader getReader() { checkNotFlipped(); return new PipeChannelReader(this, Channels.newReader(pipe.source(), charset)); }
/** * True if the pipe is open */ public boolean isOpen() { return pipe.sink().isOpen() || pipe.source().isOpen(); }
public void cleanup() throws IOException { pipe.sink().close(); pipe.source().close(); } }
public PipeSocket(Socket socket) throws IOException { this.socket = socket; this.inputPipe = Pipe.open(); this.outputPipe = Pipe.open(); this.outputPipe.source().configureBlocking(false); }
@JRubyMethod(name = "pipe", meta = true) public static IRubyObject pipe(ThreadContext context, IRubyObject recv) throws Exception { // TODO: This isn't an exact port of MRI's pipe behavior, so revisit Ruby runtime = context.getRuntime(); Pipe pipe = Pipe.open(); RubyIO source = new RubyIO(runtime, pipe.source()); RubyIO sink = new RubyIO(runtime, pipe.sink()); sink.openFile.getMainStream().setSync(true); return runtime.newArrayNoCopy(new IRubyObject[] { source, sink }); }
private Selectable timerSelectable() { Selectable sel = selectable(); sel.setChannel(wakeup.source()); sel.onReadable(new TimerReadable()); sel.onExpired(new TimerExpired()); sel.onFree(new TimerFree()); sel.setReading(true); sel.setDeadline(timer.deadline()); update(sel); return sel; }