private PipeSourceChannel(SelectorProvider selectorProvider, FileDescriptor fd) throws IOException { super(selectorProvider); this.fd = fd; this.channel = new SocketChannelImpl(selectorProvider, fd); }
@Override public boolean connect(SocketAddress socketAddress) throws IOException { checkUnconnected(); InetSocketAddress inetSocketAddress = validateAddress(socketAddress); InetAddress normalAddr = inetSocketAddress.getAddress(); int port = inetSocketAddress.getPort(); if (isBlocking()) { begin(); if (isEINPROGRESS(e)) { status = SOCKET_STATUS_PENDING; } else { if (isOpen()) { close(); finished = true; if (isBlocking()) { end(finished); initLocalAddressAndPort(); connectAddress = inetSocketAddress; if (socket != null) { if (isBlocking()) { status = (finished ? SOCKET_STATUS_CONNECTED : SOCKET_STATUS_UNCONNECTED); } else {
@Override public int read(ByteBuffer dst) throws IOException { dst.checkWritable(); checkOpenConnected(); if (!dst.hasRemaining()) { return 0; } return readImpl(dst); }
@Override public int write(ByteBuffer src) throws IOException { if (src == null) { throw new NullPointerException("src == null"); } checkOpenConnected(); if (!src.hasRemaining()) { return 0; } return writeImpl(src); }
private int readImpl(ByteBuffer dst) throws IOException { synchronized (readLock) { int readCount = 0; try { if (isBlocking()) { begin(); } readCount = IoBridge.recvfrom(true, fd, dst, 0, null, false); if (readCount > 0) { dst.position(dst.position() + readCount); } } finally { if (isBlocking()) { end(readCount > 0); } } return readCount; } }
public Socket implAccept(SocketChannelImpl clientSocketChannel) throws IOException { Socket clientSocket = clientSocketChannel.socket(); boolean connectOK = false; try { synchronized (this) { super.implAccept(clientSocket); clientSocketChannel.setConnected(); clientSocketChannel.setBound(true); clientSocketChannel.finishAccept(); } connectOK = true; } finally { if (!connectOK) { clientSocket.close(); } } return clientSocket; }
@Override public boolean finishConnect() throws IOException { synchronized (this) { if (!isOpen()) { throw new ClosedChannelException(); begin(); InetAddress inetAddress = connectAddress.getAddress(); int port = connectAddress.getPort(); isBound = finished; } catch (ConnectException e) { if (isOpen()) { close(); finished = true; end(finished);
@Override public SocketChannel accept() throws IOException { if (!isOpen()) { throw new ClosedChannelException(); } if (!isBound) { throw new NotYetBoundException(); } // Create an empty socket channel. This will be populated by ServerSocketAdapter.accept. SocketChannelImpl result = new SocketChannelImpl(provider(), false); try { begin(); synchronized (acceptLock) { try { socket.implAccept(result); } catch (SocketTimeoutException e) { if (shouldThrowSocketTimeoutExceptionFromAccept(e)) { throw e; } // Otherwise, this is a non-blocking socket and there's nothing ready, so we'll // fall through and return null. } } } finally { end(result.socket().isConnected()); } return result.socket().isConnected() ? result : null; }
@Override public InetAddress getLocalAddress() { try { return channel.getLocalAddress(); } catch (UnknownHostException e) { return null; } }
void finishAccept() { initLocalAddressAndPort(); }
FileDescriptor outFd = ((SocketChannelImpl) target).getFD(); try { begin();
private int writeImpl(ByteBuffer src) throws IOException { synchronized (writeLock) { if (!src.hasRemaining()) { return 0; } int writeCount = 0; try { if (isBlocking()) { begin(); } writeCount = IoBridge.sendto(fd, src, 0, null, 0); if (writeCount > 0) { src.position(src.position() + writeCount); } } finally { if (isBlocking()) { end(writeCount >= 0); } } return writeCount; } }
public Socket implAccept(SocketChannelImpl clientSocketChannel) throws IOException { Socket clientSocket = clientSocketChannel.socket(); boolean connectOK = false; try { synchronized (this) { super.implAccept(clientSocket); clientSocketChannel.setConnected(); clientSocketChannel.setBound(true); clientSocketChannel.finishAccept(); } connectOK = true; } finally { if (!connectOK) { clientSocket.close(); } } return clientSocket; }
@Override public boolean finishConnect() throws IOException { synchronized (this) { if (!isOpen()) { throw new ClosedChannelException(); begin(); InetAddress inetAddress = connectAddress.getAddress(); int port = connectAddress.getPort(); isBound = finished; } catch (ConnectException e) { if (isOpen()) { close(); finished = true; end(finished);
@Override public long write(ByteBuffer[] sources, int offset, int length) throws IOException { Arrays.checkOffsetAndCount(sources.length, offset, length); checkOpenConnected(); int count = FileChannelImpl.calculateTotalRemaining(sources, offset, length, false); if (count == 0) { return 0; } ByteBuffer writeBuf = ByteBuffer.allocate(count); for (int val = offset; val < length + offset; val++) { ByteBuffer source = sources[val]; int oldPosition = source.position(); writeBuf.put(source); source.position(oldPosition); } writeBuf.flip(); int result = writeImpl(writeBuf); int val = offset; int written = result; while (result > 0) { ByteBuffer source = sources[val]; int gap = Math.min(result, source.remaining()); source.position(source.position() + gap); val++; result -= gap; } return written; }
@Override public InetAddress getLocalAddress() { try { return channel.getLocalAddress(); } catch (UnknownHostException e) { return null; } }
void finishAccept() { initLocalAddressAndPort(); }
FileDescriptor outFd = ((SocketChannelImpl) target).getFD(); try { begin();