LengthDelimitedEncoder 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() && this.remaining > 0) { if (this.buffer.hasData() || this.fragHint > 0) { final int chunk = nextChunk(src); if (chunk <= this.fragHint) { final int capacity = this.fragHint - this.buffer.length(); if (capacity > 0) { final int limit = Math.min(capacity, chunk); final int bytesWritten = writeToBuffer(src, limit); this.remaining -= bytesWritten; total += bytesWritten; } } } if (this.buffer.hasData()) { final int chunk = nextChunk(src); if (this.buffer.length() >= this.fragHint || chunk > 0) { final int bytesWritten = flushToChannel(); if (bytesWritten == 0) { break; } } } if (!this.buffer.hasData()) { final int chunk = nextChunk(src); if (chunk > this.fragHint) { final int bytesWritten = writeToChannel(src, chunk); this.remaining -= bytesWritten; total += bytesWritten; if (bytesWritten == 0) { break; } } } } if (this.remaining <= 0) { super.complete(null); } return total; } LengthDelimitedEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics,
            final long contentLength,
            final int chunkSizeHint);  LengthDelimitedEncoder(
            final WritableByteChannel channel,
            final SessionOutputBuffer buffer,
            final BasicHttpTransportMetrics metrics,
            final long contentLength); @Override int write(final ByteBuffer src); @Override long transfer(
            final FileChannel src,
            final long position,
            final long count); @Override String toString();  }
@Test public void testCodingBeyondContentLimit() throws Exception { final WritableByteChannelMock channel = new WritableByteChannelMock(64); final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128); final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics(); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder( channel, outbuf, metrics, 16); encoder.write(CodecTestUtils.wrap("stuff;")); encoder.write(CodecTestUtils.wrap("more stuff; and a lot more stuff")); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertTrue(encoder.isCompleted()); Assert.assertEquals("stuff;more stuff", s); }
@Test public void testCodingEmptyBuffer() throws Exception { final WritableByteChannelMock channel = new WritableByteChannelMock(64); final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128); final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics(); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder( channel, outbuf, metrics, 16); encoder.write(CodecTestUtils.wrap("stuff;")); final ByteBuffer empty = ByteBuffer.allocate(100); empty.flip(); encoder.write(empty); encoder.write(null); encoder.write(CodecTestUtils.wrap("more stuff")); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertTrue(encoder.isCompleted()); Assert.assertEquals("stuff;more stuff", s); }
@Test public void testCodingCompleted() throws Exception { final WritableByteChannelMock channel = new WritableByteChannelMock(64); final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128); final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics(); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder( channel, outbuf, metrics, 5); encoder.write(CodecTestUtils.wrap("stuff")); try { encoder.write(CodecTestUtils.wrap("more stuff")); Assert.fail("IllegalStateException should have been thrown"); } catch (final IllegalStateException ex) { } }
@Test public void testCodingNoFragmentBuffering() 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 CharArrayBuffer chbuffer = new CharArrayBuffer(16); chbuffer.append("header"); outbuf.writeLine(chbuffer); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 0); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).flush(channel); Assert.assertEquals(13, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("header\r\nstuff", s); }
@Test public void testCodingFragmentBuffering() 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 CharArrayBuffer chbuffer = new CharArrayBuffer(16); chbuffer.append("header"); outbuf.writeLine(chbuffer); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 32); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.never()).flush(channel); Assert.assertEquals(0, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("header\r\nstuff", s); }
@Test public void testCodingFragmentBufferingMultipleFragments() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 32); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff"))); Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.never()).flush(channel); Assert.assertEquals(0, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff-more stuff", s); }
@Test public void testCodingFragmentBufferingMultipleFragmentsBeyondContentLimit() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 16, 32); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff; and a lot more stuff"))); Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.never()).flush(channel); Assert.assertEquals(0, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff-more stuff", s); }
@Test public void testCodingFragmentBufferingLargeFragment() 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 CharArrayBuffer chbuffer = new CharArrayBuffer(16); chbuffer.append("header"); outbuf.writeLine(chbuffer); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 2); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).flush(channel); Assert.assertEquals(13, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("header\r\nstuff", s); }
@Test public void testCodingFragmentBufferingTinyFragments() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 1); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff"))); Mockito.verify(channel, Mockito.times(5)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).flush(channel); Assert.assertEquals(18, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff---more stuff", s); }
@Test public void testCodingFragmentBufferingTinyFragments2() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 2); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff"))); Mockito.verify(channel, Mockito.times(4)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(2)).flush(channel); Assert.assertEquals(18, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff---more stuff", s); }
@Test public void testCodingFragmentBufferingTinyFragments3() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 3); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(2, encoder.write(CodecTestUtils.wrap("--"))); Assert.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff"))); Mockito.verify(channel, Mockito.times(4)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(5)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(2)).flush(channel); Assert.assertEquals(21, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff------more stuff", s); }
@Test public void testCodingFragmentBufferingBufferFlush() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 8); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(6, encoder.write(CodecTestUtils.wrap("-stuff"))); Mockito.verify(channel, Mockito.times(1)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).flush(channel); Assert.assertEquals(8, metrics.getBytesTransferred()); Assert.assertEquals(3, outbuf.length()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff-stuff", s); }
@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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 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); }
@Test public void testCodingFragmentBufferingChannelSaturated() throws Exception { final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64, 8)); final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128)); final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics(); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 3); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(0, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(0, encoder.write(CodecTestUtils.wrap("more stuff"))); Mockito.verify(channel, Mockito.times(5)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(6)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(4)).flush(channel); Assert.assertEquals(8, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff---", s); Assert.assertEquals(3, outbuf.length()); }
@Test public void testCodingFragmentBufferingChannelSaturated2() throws Exception { final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64, 8)); final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128)); final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics(); final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 8); Assert.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("-"))); Assert.assertEquals(1, encoder.write(CodecTestUtils.wrap("much more stuff"))); Mockito.verify(channel, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any()); Mockito.verify(outbuf, Mockito.times(1)).flush(channel); Assert.assertEquals(8, metrics.getBytesTransferred()); outbuf.flush(channel); final String s = channel.dump(StandardCharsets.US_ASCII); Assert.assertEquals("stuff--m", s); Assert.assertEquals(0, outbuf.length()); }