@Test public void testCodingFragmentBufferingBufferFlush2() throws Exception { final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64)); final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128)); final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics(); final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 8); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(16, encoder.write(CodecTestUtils.wrap("-much more stuff"))); Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).flush(channel); Assert.assertEquals(21, metrics.getBytesTransferred()); Assert.assertEquals(0, outbuf.length()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff-much more stuff", s); }
@Override public int write(final ByteBuffer src) throws IOException { if (src == null) { return 0; } assertNotCompleted(); int total = 0; while (src.hasRemaining()) { if (this.buffer.hasData() || this.fragHint > 0) { if (src.remaining() <= this.fragHint) { final int capacity = this.fragHint - this.buffer.length(); if (capacity > 0) { final int limit = Math.min(capacity, src.remaining()); final int bytesWritten = writeToBuffer(src, limit); total += bytesWritten; } } } if (this.buffer.hasData()) { if (this.buffer.length() >= this.fragHint || src.hasRemaining()) { final int bytesWritten = flushToChannel(); if (bytesWritten == 0) { break; } } } if (!this.buffer.hasData() && src.remaining() > this.fragHint) { final int bytesWritten = writeToChannel(src); total += bytesWritten; if (bytesWritten == 0) { break; } } } return total; }
IdentityEncoder extends AbstractContentEncoder implements FileContentEncoder { @Override public int write(final ByteBuffer src) throws IOException { if (src == null) { return 0; } assertNotCompleted(); int total = 0; while (src.hasRemaining()) { if (this.buffer.hasData() || this.fragHint > 0) { if (src.remaining() <= this.fragHint) { final int capacity = this.fragHint - this.buffer.length(); if (capacity > 0) { final int limit = Math.min(capacity, src.remaining()); final int bytesWritten = writeToBuffer(src, limit); total += bytesWritten; } } } if (this.buffer.hasData()) { if (this.buffer.length() >= this.fragHint || src.hasRemaining()) { final int bytesWritten = flushToChannel(); if (bytesWritten == 0) { break; } } } if (!this.buffer.hasData() && src.remaining() > this.fragHint) { final int bytesWritten = writeToChannel(src); total += bytesWritten; if (bytesWritten == 0) { break; } } } return total; } }
IdentityEncoder extends AbstractContentEncoder implements FileContentEncoder { @Override public int write(final ByteBuffer src) throws IOException { if (src == null) { return 0; } assertNotCompleted(); int total = 0; while (src.hasRemaining()) { if (this.buffer.hasData() || this.fragHint > 0) { if (src.remaining() <= this.fragHint) { final int capacity = this.fragHint - this.buffer.length(); if (capacity > 0) { final int limit = Math.min(capacity, src.remaining()); final int bytesWritten = writeToBuffer(src, limit); total += bytesWritten; } } } if (this.buffer.hasData()) { if (this.buffer.length() >= this.fragHint || src.hasRemaining()) { final int bytesWritten = flushToChannel(); if (bytesWritten == 0) { break; } } } if (!this.buffer.hasData() && src.remaining() > this.fragHint) { final int bytesWritten = writeToChannel(src); total += bytesWritten; if (bytesWritten == 0) { break; } } } return total; } IdentityEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics,
            final int chunkSizeHint);  IdentityEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics); }
IdentityEncoder extends AbstractContentEncoder implements FileContentEncoder { @Override public int write(final ByteBuffer src) throws IOException { if (src == null) { return 0; } assertNotCompleted(); int total = 0; while (src.hasRemaining()) { if (this.buffer.hasData() || this.fragHint > 0) { if (src.remaining() <= this.fragHint) { final int capacity = this.fragHint - this.buffer.length(); if (capacity > 0) { final int limit = Math.min(capacity, src.remaining()); final int bytesWritten = writeToBuffer(src, limit); total += bytesWritten; } } } if (this.buffer.hasData()) { if (this.buffer.length() >= this.fragHint || src.hasRemaining()) { final int bytesWritten = flushToChannel(); if (bytesWritten == 0) { break; } } } if (!this.buffer.hasData() && src.remaining() > this.fragHint) { final int bytesWritten = writeToChannel(src); total += bytesWritten; if (bytesWritten == 0) { break; } } } return total; } IdentityEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics,
            final int chunkSizeHint);  IdentityEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics); @Override int write(final ByteBuffer src); @Override long transfer(
            final FileChannel src,
            final long position,
            final long count); @Override String toString(); }
IdentityEncoder extends AbstractContentEncoder implements FileContentEncoder { @Override public int write(final ByteBuffer src) throws IOException { if (src == null) { return 0; } assertNotCompleted(); int total = 0; while (src.hasRemaining()) { if (this.buffer.hasData() || this.fragHint > 0) { if (src.remaining() <= this.fragHint) { final int capacity = this.fragHint - this.buffer.length(); if (capacity > 0) { final int limit = Math.min(capacity, src.remaining()); final int bytesWritten = writeToBuffer(src, limit); total += bytesWritten; } } } if (this.buffer.hasData()) { if (this.buffer.length() >= this.fragHint || src.hasRemaining()) { final int bytesWritten = flushToChannel(); if (bytesWritten == 0) { break; } } } if (!this.buffer.hasData() && src.remaining() > this.fragHint) { final int bytesWritten = writeToChannel(src); total += bytesWritten; if (bytesWritten == 0) { break; } } } return total; } IdentityEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics,
            final int chunkSizeHint);  IdentityEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics); @Override int write(final ByteBuffer src); @Override long transfer(
            final FileChannel src,
            final long position,
            final long count); @Override String toString();  }