@Override public void write(final byte[] b, final OutputStream outputStream) throws IOException { if (b == null) { return; } write(b, 0, b.length, outputStream); }
@Override public void flush() throws IOException { final SocketHolder socketHolder = ensureOpen(); this.outbuffer.flush(socketHolder.getOutputStream()); }
@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); }
flushBuffer(outputStream); writeEncoded(cbuf, outputStream); write(CRLF, outputStream);
@Override public void flush(final OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); flushBuffer(outputStream); outputStream.flush(); }
private void writeEncoded(final CharBuffer cbuf, final OutputStream outputStream) throws IOException { if (!cbuf.hasRemaining()) { return; } if (this.bbuf == null) { this.bbuf = ByteBuffer.allocate(1024); } this.encoder.reset(); while (cbuf.hasRemaining()) { final CoderResult result = this.encoder.encode(cbuf, this.bbuf, true); handleEncodingResult(result, outputStream); } final CoderResult result = this.encoder.flush(this.bbuf); handleEncodingResult(result, outputStream); this.bbuf.clear(); }
@Test public void testBasics() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L); final byte[] tmp = new byte[10]; out.write(tmp, 0, 10); out.write(1); out.write(tmp, 0, 10); out.write(tmp, 0, 10); out.write(tmp); out.write(1); out.write(2); out.flush(); out.close(); final byte[] data = outputStream.toByteArray(); Assert.assertEquals(15, data.length); }
flushBuffer(outputStream); writeEncoded(cbuf, outputStream); write(CRLF, outputStream);
@Override public void flush(final OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); flushBuffer(outputStream); outputStream.flush(); }
private void writeEncoded(final CharBuffer cbuf, final OutputStream outputStream) throws IOException { if (!cbuf.hasRemaining()) { return; } if (this.bbuf == null) { this.bbuf = ByteBuffer.allocate(1024); } this.encoder.reset(); while (cbuf.hasRemaining()) { final CoderResult result = this.encoder.encode(cbuf, this.bbuf, true); handleEncodingResult(result, outputStream); } final CoderResult result = this.encoder.flush(this.bbuf); handleEncodingResult(result, outputStream); this.bbuf.clear(); }
@Test(expected=CharacterCodingException.class) public void testUnmappableInputActionReport() throws Exception { final String s = "This text contains a circumflex \u0302 !!!"; final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder(); encoder.onMalformedInput(CodingErrorAction.IGNORE); encoder.onUnmappableCharacter(CodingErrorAction.REPORT); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final CharArrayBuffer chbuffer = new CharArrayBuffer(32); chbuffer.append(s); outbuffer.writeLine(chbuffer, outputStream); }
@Override public void write(final byte[] b, final OutputStream outputStream) throws IOException { if (b == null) { return; } write(b, 0, b.length, outputStream); }
@Override public void write(final int b, final OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); if (this.fragementSizeHint > 0) { if (this.buffer.isFull()) { flushBuffer(outputStream); } this.buffer.append(b); } else { flushBuffer(outputStream); outputStream.write(b); } }
@Override public void flush() throws IOException { final SocketHolder socketHolder = ensureOpen(); this.outbuffer.flush(socketHolder.getOutputStream()); }
@Test public void testClose() throws Exception { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16); final OutputStream out = new ContentLengthOutputStream(outbuffer, outputStream, 15L); out.close(); out.close(); final byte[] tmp = new byte[10]; try { out.write(tmp); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } try { out.write(1); Assert.fail("StreamClosedException expected"); } catch (final StreamClosedException expected) { } }
private void handleEncodingResult(final CoderResult result, final OutputStream outputStream) throws IOException { if (result.isError()) { result.throwException(); } this.bbuf.flip(); while (this.bbuf.hasRemaining()) { write(this.bbuf.get(), outputStream); } this.bbuf.compact(); }
@Override public void write(final int b, final OutputStream outputStream) throws IOException { Args.notNull(outputStream, "Output stream"); if (this.fragementSizeHint > 0) { if (this.buffer.isFull()) { flushBuffer(outputStream); } this.buffer.append(b); } else { flushBuffer(outputStream); outputStream.write(b); } }
@Override public void close() throws IOException { final SocketHolder socketHolder = this.socketHolderRef.getAndSet(null); if (socketHolder != null) { try (final Socket socket = socketHolder.getSocket()) { this.inBuffer.clear(); this.outbuffer.flush(socketHolder.getOutputStream()); try { try { socket.shutdownOutput(); } catch (final IOException ignore) { } try { socket.shutdownInput(); } catch (final IOException ignore) { } } catch (final UnsupportedOperationException ignore) { // if one isn't supported, the other one isn't either } } } }