buffers[writeIndex] = newByteBuffer(); } else { buffers[writeIndex].clear();
int limit = src.limit(); src.limit(src.position() + bufferSize); inject[injectIndex++] = newByteBuffer().put(src); src.limit(limit); } else { inject[injectIndex++] = newByteBuffer().put(src);
/** * Flush the receive queue. * * @throws IOException if something goes wrong. */ private void flushRecvQueue() throws IOException { if (recvQueue == null) { return; } ByteBuffer tmp = recvQueue.newByteBuffer(); while (recvQueue.hasRemaining()) { tmp.clear(); recvQueue.get(tmp); tmp.flip(); ptr.onRecv(tmp); } recvQueue = null; }
/** * Flush the send queue. * * @throws IOException if something goes wrong. */ private void flushSendQueue() throws IOException { if (sendQueue == null) { return; } ByteBuffer tmp = sendQueue.newByteBuffer(); while (sendQueue.hasRemaining()) { tmp.clear(); sendQueue.get(tmp); tmp.flip(); while (tmp.hasRemaining()) { try { write(tmp); } catch (IOException e) { // store what ever we know was not written tmp.compact(); sendQueue.unget(tmp); throw e; } } } sendQueue = null; }
/** * Flushes the supplied {@link ByteBufferQueue} to {@link #next()}'s {@link ProtocolStack.Ptr#doSend(ByteBuffer)} * This method is especially helpful for {@link FilterLayer} implementations that are involved in initial * handshaking as they will need to queue up data until the handshake is completed and then flush the data to * the remainder of the stack. * * @param queue the data to send. * @throws IOException if there is an I/O error during the receive. */ protected final void flushSend(ByteBufferQueue queue) throws IOException { ProtocolStack<?>.Ptr ptr; synchronized (this) { ptr = this.ptr; } if (ptr == null) { throw new IllegalStateException(); } ByteBuffer tmp = queue.newByteBuffer(); while (queue.hasRemaining()) { tmp.clear(); queue.get(tmp); tmp.flip(); try { ptr.doSend(tmp); } catch (IOException e) { queue.unget(tmp); throw e; } } }
/** * Flushes the supplied {@link ByteBufferQueue} to {@link #next()}'s {@link ProtocolStack.Ptr#onRecv(ByteBuffer)}. * This method is especially helpful for {@link FilterLayer} implementations that are involved in initial * handshaking as they will need to queue up data until the handshake is completed and then flush the data to * the remainder of the stack. * * @param queue the data to receive. * @throws IOException if there is an I/O error during the receive. */ protected final void flushRecv(ByteBufferQueue queue) throws IOException { ProtocolStack<?>.Ptr ptr; synchronized (this) { ptr = this.ptr; } if (ptr == null) { throw new IllegalStateException(); } ByteBuffer tmp = queue.newByteBuffer(); while (queue.hasRemaining()) { tmp.clear(); queue.get(tmp); tmp.flip(); try { ptr.onRecv(tmp); } catch (IOException e) { queue.unget(tmp); throw e; } } }
@Override public void doSend(@NonNull ByteBuffer data) throws IOException { synchronized (holdLock) { if (holding) { synchronized (sendQueue) { sendQueue.put(data); } return; } } synchronized (sendQueue) { if (sendQueue.hasRemaining()) { sendQueue.put(data); ByteBuffer tempBuffer = sendQueue.newByteBuffer(); while (sendQueue.hasRemaining()) { tempBuffer.clear(); sendQueue.get(tempBuffer); next().doSend(tempBuffer); } } else if (data.hasRemaining()) { next().doSend(data); } } }
@Override public void onRecv(@NonNull ByteBuffer data) throws IOException { synchronized (holdLock) { if (holding) { synchronized (recvQueue) { recvQueue.put(data); } return; } } synchronized (recvQueue) { if (recvQueue.hasRemaining()) { recvQueue.put(data); ByteBuffer tempBuffer = recvQueue.newByteBuffer(); while (recvQueue.hasRemaining()) { tempBuffer.clear(); recvQueue.get(tempBuffer); next().onRecv(tempBuffer); } } else if (data.hasRemaining()) { next().onRecv(data); } } }