/** * Writes the array. If the array does not fit within the buffer, it is * not split, but rather written out as one large chunk. */ @Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }
/** * Writes the array. If the array does not fit within the buffer, it is * not split, but rather written out as one large chunk. */ @Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }
@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); }
@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 testChunkedOutputStreamClose() throws IOException { final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048); out.close(); out.close(); try { out.write(new byte[] {1,2,3}); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } try { out.write(1); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } }
@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 testChunkedConsistence() throws IOException { final String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb"; final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048); out.write(input.getBytes(StandardCharsets.ISO_8859_1)); out.flush(); out.close(); out.close(); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream); final byte[] d = new byte[10]; final ByteArrayOutputStream result = new ByteArrayOutputStream(); int len = 0; while ((len = in.read(d)) > 0) { result.write(d, 0, len); } final String output = new String(result.toByteArray(), StandardCharsets.ISO_8859_1); Assert.assertEquals(input, output); in.close(); }