/** * Return a {@link SocketInputWrapper} for the socket and set the given * timeout. If the socket does not have an associated channel, then its socket * timeout will be set to the specified value. Otherwise, a * {@link SocketInputStream} will be created which reads with the configured * timeout. * * Any socket created using socket factories returned by {@link #NetUtils}, * must use this interface instead of {@link Socket#getInputStream()}. * * In general, this should be called only once on each socket: see the note * in {@link SocketInputWrapper#setTimeout(long)} for more information. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. zero for waiting as * long as necessary. * @return SocketInputWrapper for reading from the socket. * @throws IOException */ public static SocketInputWrapper getInputStream(Socket socket, long timeout) throws IOException { InputStream stm = (socket.getChannel() == null) ? socket.getInputStream() : new SocketInputStream(socket); SocketInputWrapper w = new SocketInputWrapper(socket, stm); w.setTimeout(timeout); return w; }
/** * Set the timeout for reads from this stream. * * Note: the behavior here can differ subtly depending on whether the * underlying socket has an associated Channel. In particular, if there is no * channel, then this call will affect the socket timeout for <em>all</em> * readers of this socket. If there is a channel, then this call will affect * the timeout only for <em>this</em> stream. As such, it is recommended to * only create one {@link SocketInputWrapper} instance per socket. * * @param timeoutMs * the new timeout, 0 for no timeout * @throws SocketException * if the timeout cannot be set */ public void setTimeout(long timeoutMs) throws SocketException { if (hasChannel) { ((SocketInputStream)in).setTimeout(timeoutMs); } else { socket.setSoTimeout((int)timeoutMs); } }
@Override public int read(byte[] b, int off, int len) throws IOException { return read(ByteBuffer.wrap(b, off, len)); }
final InputStream in = new SocketInputStream(source, TIMEOUT); OutputStream out = new SocketOutputStream(sink, TIMEOUT); ((SocketInputStream)in).setTimeout(TIMEOUT * 2); doIO(in, null, TIMEOUT * 2); ((SocketInputStream)in).setTimeout(0); TestingThread thread = new TestingThread(ctx) { @Override
@Override public void close() throws IOException { // We always close the outermost streams-- in this case, 'in' and 'out' // Closing either one of these will also close the Socket. try { in.close(); } finally { out.close(); } }
final InputStream in = new SocketInputStream(source, TIMEOUT); OutputStream out = new SocketOutputStream(sink, TIMEOUT); ((SocketInputStream)in).setTimeout(TIMEOUT * 2); doIO(in, null, TIMEOUT * 2); ((SocketInputStream)in).setTimeout(0); TestingThread thread = new TestingThread(ctx) { @Override
@Override public void close() throws IOException { // We always close the outermost streams-- in this case, 'in' and 'out' // Closing either one of these will also close the Socket. try { in.close(); } finally { out.close(); } }
public NioInetPeer(Socket socket) throws IOException { this.socket = socket; this.in = new SocketInputStream(socket.getChannel(), 0); this.out = new SocketOutputStream(socket.getChannel(), 0); this.isLocal = socket.getInetAddress().equals(socket.getLocalAddress()); }
@Override public int read() throws IOException { /* Allocation can be removed if required. * probably no need to optimize or encourage single byte read. */ byte[] buf = new byte[1]; int ret = read(buf, 0, 1); if (ret > 0) { return (int)(buf[0] & 0xff); } if (ret != -1) { // unexpected throw new IOException("Could not read from stream"); } return ret; }
@Override public void setReadTimeout(int timeoutMs) throws IOException { in.setTimeout(timeoutMs); }
@Override public void close() throws IOException { // We always close the outermost streams-- in this case, 'in' and 'out' // Closing either one of these will also close the Socket. try { in.close(); } finally { out.close(); } }
/** * Returns InputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketInputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getInputStream()} is returned. In the later * case, the timeout argument is ignored and the timeout set with * {@link Socket#setSoTimeout(int)} applies for reads.<br><br> * * Any socket created using socket factories returned by {@link #NetUtils}, * must use this interface instead of {@link Socket#getInputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return InputStream for reading from the socket. * @throws IOException */ public static InputStream getInputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getInputStream() : new SocketInputStream(socket, timeout); }
@Override public int read() throws IOException { /* Allocation can be removed if required. * probably no need to optimize or encourage single byte read. */ byte[] buf = new byte[1]; if (read(buf, 0, 1) > 0) { return (byte)buf[0]; } throw new IOException("Could not read from stream"); }
@Override public void setReadTimeout(int timeoutMs) throws IOException { in.setTimeout(timeoutMs); }
/** * Returns InputStream for the socket. If the socket has an associated * SocketChannel then it returns a * {@link SocketInputStream} with the given timeout. If the socket does not * have a channel, {@link Socket#getInputStream()} is returned. In the later * case, the timeout argument is ignored and the timeout set with * {@link Socket#setSoTimeout(int)} applies for reads.<br><br> * * Any socket created using socket factories returned by {@link #NetUtils}, * must use this interface instead of {@link Socket#getInputStream()}. * * @see Socket#getChannel() * * @param socket * @param timeout timeout in milliseconds. This may not always apply. zero * for waiting as long as necessary. * @return InputStream for reading from the socket. * @throws IOException */ public static InputStream getInputStream(Socket socket, long timeout) throws IOException { return (socket.getChannel() == null) ? socket.getInputStream() : new SocketInputStream(socket, timeout); }
@Override public int read(byte[] b, int off, int len) throws IOException { return read(ByteBuffer.wrap(b, off, len)); }
@Override public void setReadTimeout(int timeoutMs) throws IOException { in.setTimeout(timeoutMs); }
NioInetPeer(Socket socket) throws IOException { this.socket = socket; this.in = new SocketInputStream(socket.getChannel(), 0); this.out = new SocketOutputStream(socket.getChannel(), 0); this.isLocal = socket.getInetAddress().equals(socket.getLocalAddress()); }
@Override public int read(byte[] b, int off, int len) throws IOException { return read(ByteBuffer.wrap(b, off, len)); }
/** * Set the timeout for reads from this stream. * * Note: the behavior here can differ subtly depending on whether the * underlying socket has an associated Channel. In particular, if there is no * channel, then this call will affect the socket timeout for <em>all</em> * readers of this socket. If there is a channel, then this call will affect * the timeout only for <em>this</em> stream. As such, it is recommended to * only create one {@link SocketInputWrapper} instance per socket. * * @param timeoutMs * the new timeout, 0 for no timeout * @throws SocketException * if the timeout cannot be set */ public void setTimeout(long timeoutMs) throws SocketException { if (hasChannel) { ((SocketInputStream)in).setTimeout(timeoutMs); } else { socket.setSoTimeout((int)timeoutMs); } }