@Override public SocketChannel getChannel() { return sock.getChannel(); }
SelectableChannel getSelectableChannel() { return this.theSocket.getChannel(); }
/** * Same as SocketOutputStream(socket.getChannel(), timeout):<br><br> * * Create a new ouput stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketOutputStream#SocketOutputStream(WritableByteChannel, long) * * @param socket should have a channel associated with it. * @param timeout timeout timeout in milliseconds. must not be negative. * @throws IOException */ public SocketOutputStream(Socket socket, long timeout) throws IOException { this(socket.getChannel(), timeout); }
/** * Same as SocketInputStream(socket.getChannel(), timeout): <br><br> * * Create a new input stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long) * * @param socket should have a channel associated with it. * @param timeout timeout timeout in milliseconds. must not be negative. * @throws IOException */ public SocketInputStream(Socket socket, long timeout) throws IOException { this(socket.getChannel(), timeout); }
SocketInputWrapper(Socket s, InputStream is) { super(is); this.socket = s; this.hasChannel = s.getChannel() != null; if (hasChannel) { Preconditions.checkArgument(is instanceof SocketInputStream, "Expected a SocketInputStream when there is a channel. " + "Got: %s", is); } }
/** * Same as SocketInputStream(socket.getChannel(), socket.getSoTimeout()) * :<br><br> * * Create a new input stream with the given timeout. If the timeout * is zero, it will be treated as infinite timeout. The socket's * channel will be configured to be non-blocking. * @see SocketInputStream#SocketInputStream(ReadableByteChannel, long) * * @param socket should have a channel associated with it. * @throws IOException */ public SocketInputStream(Socket socket) throws IOException { this(socket.getChannel(), socket.getSoTimeout()); }
/** * See {@link Socket#getChannel()}. Calling this method does not trigger mode detection. */ @Override public SocketChannel getChannel() { return getSocketAllowUnknownMode().getChannel(); }
public RequestReader(Socket socket, Protocol protocol) { buffer = ByteBuffer.allocate(getBufferSize(socket.getChannel())); // set position to limit so that first read attempt // returns hasRemaining() false buffer.position(buffer.limit()); this.socket = socket; this.protocol = protocol; }
public void setComms(Socket socket, InputStream is, OutputStream os, ByteBuffer bb, MessageStats msgStats) { Assert.assertTrue(socket != null); this.socket = socket; this.socketChannel = socket.getChannel(); this.inputStream = is; this.outputStream = os; this.cachedCommBuffer = bb; this.messageStats = msgStats; }
/** * Switch this connection to blocking mode so we can use oldIO to read and write messages. */ void makeBlocking() throws IOException { SelectableChannel c = this.theSocket.getChannel(); c.configureBlocking(true); }
public TLSStreamWriter(TLSWrapper tlsWrapper, Socket socket) throws IOException { wrapper = tlsWrapper; // DANIELE: Add code to use directly the socket channel if (socket.getChannel() != null) { wbc = ServerTrafficCounter.wrapWritableChannel(socket.getChannel()); } else { wbc = Channels.newChannel( ServerTrafficCounter.wrapOutputStream(socket.getOutputStream())); } outAppData = ByteBuffer.allocate(tlsWrapper.getAppBuffSize()); }
public static ByteBuffer allocateCommBuffer(int size, Socket sock) { // I expect that size will almost always be the same value if (sock.getChannel() == null) { // The socket this commBuffer will be used for is old IO (it has no channel). // So the commBuffer should be heap based. return ByteBuffer.allocate(size); } LinkedBlockingQueue<ByteBuffer> q = commBufferMap.get(size); ByteBuffer result = null; if (q != null) { result = q.poll(); } if (result == null) { result = ByteBuffer.allocateDirect(size); } else { result.position(0); result.limit(result.capacity()); } return result; }
private Command readAsciiCommand() throws IOException { SocketChannel channel = this.socket.getChannel(); if (channel == null || !channel.isOpen()) { throw new IllegalStateException("cannot read from channel"); } buffer.clear(); int bytesRead = channel.read(buffer); if (bytesRead == -1) { throw new IOException("EOF"); } buffer.flip(); return Command.valueOf(readCommand(buffer)); }
void setComms(Socket socket, ByteBuffer bb, MessageStats msgStats) throws IOException { this.socketChannel = socket.getChannel(); if (this.socketChannel == null) { setComms(socket, socket.getInputStream(), socket.getOutputStream(), bb, msgStats); } else { setComms(socket, null, null, bb, msgStats); } }
public void sendException(Exception e) { SocketChannel channel = this.socket.getChannel(); if (channel == null || !channel.isOpen()) { throw new IllegalStateException("cannot write to channel"); } try { if (e instanceof ClientError) { channel.write(charsetASCII.encode(Reply.CLIENT_ERROR.toString())); } else { channel.write(charsetASCII.encode(Reply.ERROR.toString())); } } catch (IOException ex) { } } }
public TLSStreamReader(TLSWrapper tlsWrapper, Socket socket) throws IOException { wrapper = tlsWrapper; // DANIELE: Add code to use directly the socket channel if (socket.getChannel() != null) { rbc = ServerTrafficCounter.wrapReadableChannel(socket.getChannel()); } else { rbc = Channels.newChannel( ServerTrafficCounter.wrapInputStream(socket.getInputStream())); } inNetBB = ByteBuffer.allocate(wrapper.getNetBuffSize()); inAppBB = ByteBuffer.allocate(wrapper.getAppBuffSize()); }
/** * Returns OutputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketOutputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getOutputStream()} is returned. In the later * case, the timeout argument is ignored and the write will wait until * data is available.<br><br> * * Any socket created using socket factories returned by {@link NetUtils}, * must use this interface instead of {@link Socket#getOutputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return OutputStream for writing to the socket. * @throws IOException */ public static OutputStream getOutputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getOutputStream() : new SocketOutputStream(socket, timeout); }
@Override public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { if (!(socket instanceof UnixSocket)) { throw new AssertionError("Unexpected socket: " + socket); } socket.setSoTimeout(connectTimeout); try { socket.getChannel().connect(new UnixSocketAddress(socketFile)); } catch (SocketTimeoutException e) { throw new ConnectTimeoutException(e, null, remoteAddress.getAddress()); } return socket; } }
public void sendReply(ByteBuffer reply) throws IOException { // for binary set the response opCode if (this.protocol == Protocol.BINARY) { reply.rewind(); reply.put(POSITION_OPCODE, buffer.get(POSITION_OPCODE)); reply.putInt(POSITION_OPAQUE, buffer.getInt(POSITION_OPAQUE)); if (ConnectionHandler.getLogger().finerEnabled()) { ConnectionHandler.getLogger() .finer("sending reply:" + reply + " " + Command.buffertoString(reply)); } } SocketChannel channel = this.socket.getChannel(); if (channel == null || !channel.isOpen()) { throw new IllegalStateException("cannot write to channel"); } channel.write(reply); }
private ByteBuffer readAtLeast(int bytes) throws IOException { peerNetData = ioFilter.ensureWrappedCapacity(bytes, peerNetData, Buffers.BufferType.TRACKED_RECEIVER, getStats()); return ioFilter.readAtLeast(conn.getSocket().getChannel(), bytes, peerNetData, getStats()); }