/** * {@inheritDoc} */ @Override public synchronized void reset() throws IOException { if (mark == null) { throw new IOException(); } mark.flip(); pos -= mark.remaining(); queue.unget(mark); mark.clear(); }
/** * 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#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; } } }
/** * 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; } } }
if (send.hasRemaining()) { synchronized (sendQueue) { this.sendQueue.unget(send); sendHasRemaining = true;
dst.put((byte) 39); dst.flip(); queue.unget(dst); src.clear(); src.put((byte) 20);
assertThat(dst.get(), is((byte) 2)); dst.reset(); queue.unget(dst); src.clear(); src.put((byte) 20);
assertThat(dst.get(), is((byte) 12)); dst.reset(); queue.unget(dst); src.clear(); src.put((byte) 20);