public void setTimeout(final int timeout) { socketOutStream.setTimeout(timeout); }
@Override public void close() throws IOException { socketChannelOutput.close(); }
@Override public void write(byte[] b) throws IOException { socketChannelOutput.write(b); }
socketChannelOutput = new SocketChannelOutputStream(channel); socketChannelOutput.setTimeout(timeout);
public StandardCommsSession(final String hostname, final int port, final int timeoutMillis) throws IOException { socketChannel = SocketChannel.open(); socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis); socketChannel.configureBlocking(false); in = new SocketChannelInputStream(socketChannel); bufferedIn = new InterruptableInputStream(new BufferedInputStream(in)); out = new SocketChannelOutputStream(socketChannel); bufferedOut = new InterruptableOutputStream(new BufferedOutputStream(out)); this.hostname = hostname; this.port = port; }
@Override public void flush() throws IOException { socketChannelOutput.flush(); updateLastUsed(); } };
public SocketChannelOutput(final SocketChannel socketChannel) throws IOException { socketOutStream = new SocketChannelOutputStream(socketChannel); countingOut = new ByteCountingOutputStream(socketOutStream); bufferedOut = new BufferedOutputStream(countingOut); interruptableOut = new InterruptableOutputStream(bufferedOut); }
public StandardCommsSession(final String hostname, final int port, final int timeoutMillis) throws IOException { socketChannel = SocketChannel.open(); socketChannel.socket().connect(new InetSocketAddress(hostname, port), timeoutMillis); socketChannel.configureBlocking(false); in = new SocketChannelInputStream(socketChannel); bufferedIn = new InterruptableInputStream(new BufferedInputStream(in)); out = new SocketChannelOutputStream(socketChannel); bufferedOut = new InterruptableOutputStream(new BufferedOutputStream(out)); this.hostname = hostname; this.port = port; }
@Override public void setTimeout(final long value, final TimeUnit timeUnit) { in.setTimeout((int) TimeUnit.MILLISECONDS.convert(value, timeUnit)); out.setTimeout((int) TimeUnit.MILLISECONDS.convert(value, timeUnit)); timeoutMillis = TimeUnit.MILLISECONDS.convert(value, timeUnit); }
@Override protected void write(byte[] data) throws IOException { socketChannelOutput.write(data); }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { final ByteBuffer buffer = ByteBuffer.wrap(b, off, len); final int timeoutMillis = this.timeout; long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesWritten; long sleepNanos = 1L; while (buffer.hasRemaining()) { bytesWritten = channel.write(buffer); if (bytesWritten == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out writing to socket"); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); // simulate an interrupted blocked write operation } sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS); } else { maxTime = System.currentTimeMillis() + timeoutMillis; } } }
@Override public void setTimeout(final long value, final TimeUnit timeUnit) { in.setTimeout((int) TimeUnit.MILLISECONDS.convert(value, timeUnit)); out.setTimeout((int) TimeUnit.MILLISECONDS.convert(value, timeUnit)); timeoutMillis = TimeUnit.MILLISECONDS.convert(value, timeUnit); }
@Override public void write(int b) throws IOException { socketChannelOutput.write(b); }
@Override public void write(final int b) throws IOException { oneByteBuffer.flip(); oneByteBuffer.clear(); oneByteBuffer.put((byte) b); oneByteBuffer.flip(); final int timeoutMillis = this.timeout; long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesWritten; long sleepNanos = 1L; while (oneByteBuffer.hasRemaining()) { bytesWritten = channel.write(oneByteBuffer); if (bytesWritten == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out writing to socket"); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); // simulate an interrupted blocked write operation } sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS); } else { return; } } }
@Override public void write(byte[] b, int off, int len) throws IOException { socketChannelOutput.write(b, off, len); }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { final ByteBuffer buffer = ByteBuffer.wrap(b, off, len); final int timeoutMillis = this.timeout; long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesWritten; long sleepNanos = 1L; while (buffer.hasRemaining()) { bytesWritten = channel.write(buffer); if (bytesWritten == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out writing to socket"); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); // simulate an interrupted blocked write operation } sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS); } else { maxTime = System.currentTimeMillis() + timeoutMillis; } } }
@Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }
@Override public void write(final int b) throws IOException { oneByteBuffer.flip(); oneByteBuffer.clear(); oneByteBuffer.put((byte) b); oneByteBuffer.flip(); final int timeoutMillis = this.timeout; long maxTime = System.currentTimeMillis() + timeoutMillis; int bytesWritten; long sleepNanos = 1L; while (oneByteBuffer.hasRemaining()) { bytesWritten = channel.write(oneByteBuffer); if (bytesWritten == 0) { if (System.currentTimeMillis() > maxTime) { throw new SocketTimeoutException("Timed out writing to socket"); } try { TimeUnit.NANOSECONDS.sleep(sleepNanos); } catch (InterruptedException e) { close(); Thread.currentThread().interrupt(); // set the interrupt status throw new ClosedByInterruptException(); // simulate an interrupted blocked write operation } sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS); } else { return; } } }
@Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }