/** * Writes directly to the network, bypassing the socket write buffer. * * @param from The ByteBuffer containing the data to be written * * @throws IOException If an IO error occurs during the write */ protected void writeBlockingDirect(ByteBuffer from) throws IOException { // The socket write buffer capacity is socket.appWriteBufSize // TODO This only matters when using TLS. For non-TLS connections it // should be possible to write the ByteBuffer in a single write int limit = socketBufferHandler.getWriteBuffer().capacity(); int fromLimit = from.limit(); while (from.remaining() >= limit) { from.limit(from.position() + limit); doWrite(true, from); from.limit(fromLimit); } if (from.remaining() > 0) { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, socketBufferHandler.getWriteBuffer()); } }
@Override protected void writeBlockingDirect(ByteBuffer from) throws IOException { if (from.isDirect()) { super.writeBlockingDirect(from); } else { // The socket write buffer capacity is socket.appWriteBufSize ByteBuffer writeBuffer = socketBufferHandler.getWriteBuffer(); int limit = writeBuffer.capacity(); while (from.remaining() >= limit) { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, writeBuffer); doWrite(true); } if (from.remaining() > 0) { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, writeBuffer); } } }
protected void writeNonBlockingDirect(ByteBuffer from) throws IOException { // The socket write buffer capacity is socket.appWriteBufSize // TODO This only matters when using TLS. For non-TLS connections it // should be possible to write the ByteBuffer in a single write int limit = socketBufferHandler.getWriteBuffer().capacity(); int fromLimit = from.limit(); while (from.remaining() >= limit) { int newLimit = from.position() + limit; from.limit(newLimit); doWrite(false, from); from.limit(fromLimit); if (from.position() != newLimit) { // Didn't write the whole amount of data in the last // non-blocking write. // Exit the loop. return; } } if (from.remaining() > 0) { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, socketBufferHandler.getWriteBuffer()); } }
@Override protected void writeNonBlockingDirect(ByteBuffer from) throws IOException { if (from.isDirect()) { super.writeNonBlockingDirect(from); } else { // The socket write buffer capacity is socket.appWriteBufSize ByteBuffer writeBuffer = socketBufferHandler.getWriteBuffer(); int limit = writeBuffer.capacity(); while (from.remaining() >= limit) { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, writeBuffer); int newPosition = writeBuffer.position() + limit; doWrite(false); if (writeBuffer.position() != newPosition) { // Didn't write the whole amount of data in the last // non-blocking write. // Exit the loop. return; } } if (from.remaining() > 0) { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, writeBuffer); } } }
public void expand(int newSize) { configureReadBufferForWrite(); readBuffer = ByteBufferUtils.expand(readBuffer, newSize); configureWriteBufferForWrite(); writeBuffer = ByteBufferUtils.expand(writeBuffer, newSize); }
/** * Writes the provided data to the socket write buffer. If the socket write * buffer fills during the write, the content of the socket write buffer is * written to the network using a blocking write. Once that blocking write * is complete, this method starts to fill the socket write buffer again. * Depending on the size of the data to write, there may be multiple writes * to the network. On completion of this method there will always be space * remaining in the socket write buffer. * * @param buf The byte array containing the data to be written * @param off The offset within the byte array of the data to be written * @param len The length of the data to be written * * @throws IOException If an IO error occurs during the write */ protected void writeBlocking(byte[] buf, int off, int len) throws IOException { socketBufferHandler.configureWriteBufferForWrite(); int thisTime = transfer(buf, off, len, socketBufferHandler.getWriteBuffer()); while (socketBufferHandler.getWriteBuffer().remaining() == 0) { len = len - thisTime; off = off + thisTime; doWrite(true); socketBufferHandler.configureWriteBufferForWrite(); thisTime = transfer(buf, off, len, socketBufferHandler.getWriteBuffer()); } }
socketBufferHandler.configureWriteBufferForWrite(); transfer(from, socketBufferHandler.getWriteBuffer()); if (from.remaining() > 0) {
private class VectoredIOCompletionHandler<A> implements CompletionHandler<Long, OperationState<A>> { @Override public void completed(Long nBytes, OperationState<A> state) { if (nBytes.longValue() < 0) { failed(new EOFException(), state); } else { state.nBytes += nBytes.longValue(); CompletionState currentState = Nio2Endpoint.isInline() ? CompletionState.INLINE : CompletionState.DONE; boolean complete = true; boolean completion = true; if (state.check != null) { switch (state.check.callHandler(currentState, state.buffers, state.offset, state.length)) { case CONTINUE: complete = false; break; case DONE: break; case NONE: completion = false; break; } } if (complete) { boolean notify = false; state.semaphore.release(); if (state.block == BlockingMode.BLOCK && currentState != CompletionState.INLINE) { notify = true; } else { state.state = currentState; }
getSocket().getBufHandler().configureWriteBufferForWrite(); ByteBuffer buffer = getSocket().getBufHandler().getWriteBuffer(); int nRead = -1;
socketBufferHandler.configureWriteBufferForWrite(); int thisTime = transfer(buf, off, len, socketBufferHandler.getWriteBuffer()); len = len - thisTime; doWrite(false); if (len > 0 && socketBufferHandler.isWriteBufferWritable()) { socketBufferHandler.configureWriteBufferForWrite(); thisTime = transfer(buf, off, len, socketBufferHandler.getWriteBuffer()); } else {
getSocket().getBufHandler().configureWriteBufferForWrite(); int nRead = -1; try {
/** * Separate method so it can be re-used by the socket write buffer to write * data to the network * * @param from The ByteBuffer containing the data to be written * * @throws IOException If an IO error occurs during the write */ protected void writeNonBlockingInternal(ByteBuffer from) throws IOException { if (socketBufferHandler.isWriteBufferEmpty()) { writeNonBlockingDirect(from); } else { socketBufferHandler.configureWriteBufferForWrite(); transfer(from, socketBufferHandler.getWriteBuffer()); if (!socketBufferHandler.isWriteBufferWritable()) { doWrite(false); if (socketBufferHandler.isWriteBufferWritable()) { writeNonBlockingDirect(from); } } } }
} else { socketBufferHandler.configureWriteBufferForWrite();