/** * Finishes writing to the underlying stream, but does NOT close the underlying stream. */ @Override public void close() throws IOException { if (!this.closed) { this.closed = true; finish(); this.buffer.flush(this.outputStream); } } }
/** * Finishes writing to the underlying stream, but does NOT close the underlying stream. */ @Override public void close() throws IOException { if (!this.closed) { this.closed = true; finish(); this.buffer.flush(this.outputStream); } } }
@Test public void testChunkedOutputStreamSmallChunk() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2); out.write('1'); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("1\r\n1\r\n0\r\n\r\n", content); }
@Test public void testChunkedOutputStreamLargeChunk() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2); out.write(new byte[] {'1', '2', '3', '4'}); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("4\r\n1234\r\n0\r\n\r\n", content); }
@Test public void testChunkedOutputStreamWithTrailers() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2, new Supplier<List<? extends Header>>() { @Override public List<? extends Header> get() { return Arrays.asList( new BasicHeader("E", ""), new BasicHeader("Y", "Z")); } } ); out.write('x'); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("1\r\nx\r\n0\r\nE: \r\nY: Z\r\n\r\n", content); }
@Test public void testChunkedOutputStream() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2); out.write('1'); out.write('2'); out.write('3'); out.write('4'); out.finish(); out.close(); final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII); Assert.assertEquals("2\r\n12\r\n2\r\n34\r\n0\r\n\r\n", content); }