@Override public void close() throws IOException { socketChannelOutput.close(); }
@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, 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, 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 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; } } }