@Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }
@Test public void testBasics() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); final byte[] tmp = new byte[10]; out.write(tmp, 0, 10); out.write(tmp); out.write(1); out.flush(); out.close(); final byte[] data = outputStream.toByteArray(); Assert.assertEquals(21, data.length); }
protected OutputStream createContentOutputStream( final long len, final SessionOutputBuffer buffer, final OutputStream outputStream, final Supplier<List<? extends Header>> trailers) { if (len >= 0) { return new ContentLengthOutputStream(buffer, outputStream, len); } else if (len == ContentLengthStrategy.CHUNKED) { final int chunkSizeHint = h1Config.getChunkSizeHint() >= 0 ? h1Config.getChunkSizeHint() : 2048; return new ChunkedOutputStream(buffer, outputStream, chunkSizeHint, trailers); } else { return new IdentityOutputStream(buffer, outputStream); } }
protected OutputStream createContentOutputStream( final long len, final SessionOutputBuffer buffer, final OutputStream outputStream, final Supplier<List<? extends Header>> trailers) { if (len >= 0) { return new ContentLengthOutputStream(buffer, outputStream, len); } else if (len == ContentLengthStrategy.CHUNKED) { final int chunkSizeHint = h1Config.getChunkSizeHint() >= 0 ? h1Config.getChunkSizeHint() : 2048; return new ChunkedOutputStream(buffer, outputStream, chunkSizeHint, trailers); } else { return new IdentityOutputStream(buffer, outputStream); } }
@Override public void write(final byte[] b) throws IOException { write(b, 0, b.length); }
@Test public void testBasicWrite() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); out.write(new byte[] {'a', 'b'}, 0, 2); out.write('c'); out.flush(); final byte[] input = outputStream.toByteArray(); Assert.assertNotNull(input); final byte[] expected = new byte[] {'a', 'b', 'c'}; Assert.assertEquals(expected.length, input.length); for (int i = 0; i < expected.length; i++) { Assert.assertEquals(expected[i], input[i]); } out.close(); }
@Test public void testClosedCondition() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); out.close(); out.close(); try { final byte[] tmp = new byte[2]; out.write(tmp, 0, tmp.length); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } try { out.write('a'); Assert.fail("StreamClosedException expected"); } catch (final IOException expected) { } }
@Test public void testClose() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new IdentityOutputStream(outbuffer, outputStream); out.close(); out.close(); final byte[] tmp = new byte[10]; try { out.write(tmp); 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 } }