public ContentChannelOutputStream(final ContentChannel endpoint) { this.endpoint = endpoint; buffer = new BufferChain(this); }
@Override public GenericWriter write(AbstractUtf8Array v) throws java.io.IOException { buffer.append(v); return this; }
/** * Return the number of bytes this writer will produce for the underlying * layer. That is, it sums the length of the raw bytes received and the * number of bytes in the written strings after encoding. * * @return the number of bytes appended to this writer */ public long appended() { return buffer.appended(); } }
public void append(final byte[] alreadyEncoded, final int offset, final int length) throws java.io.IOException { if (shouldCopy(length)) { makeRoom(length); current.put(alreadyEncoded, offset, length); } else { append(ByteBuffer.wrap(alreadyEncoded, offset, length)); } }
public void append(final ByteBuffer alreadyEncoded) throws java.io.IOException { if (alreadyEncoded.remaining() == 0) { return; } final int length = alreadyEncoded.limit() - alreadyEncoded.position(); if (shouldCopy(length)) { makeRoom(length); current.put(alreadyEncoded); } else { scratch(); add(alreadyEncoded); } } private final void add(final ByteBuffer buf) {
@Override public void flush() throws IOException { buffer.flush(); // Unit tests in prelude depends on the stream _not_ being flushed, it // is necessary for Jetty to write content length headers, it seems. // stream.flush(); } }
private void scratch() throws java.io.IOException { if (!possibleFlush() && current.position() != 0) { current.flip(); add(current); current = ByteBuffer.allocate(BUFFERSIZE); } }
private final void makeRoom(int length) throws IOException { if (current.remaining() < length) { scratch(); } } public void append(AbstractUtf8Array v) throws IOException {
public void append(final byte b) throws IOException { makeRoom(1); current.put(b); } private final boolean shouldCopy(int length) {
public void append(AbstractUtf8Array v) throws IOException { final int length = v.getByteLength(); if (shouldCopy(length)) { makeRoom(length); v.writeTo(current); } else { append(v.wrap()); } } public void append(final byte[] alreadyEncoded) throws java.io.IOException {
private boolean possibleFlush() throws java.io.IOException { if (buffers.size() > MAXBUFFERS) { flush(); return true; } return false; }
public void append(final CharBuffer toEncode, final CharsetEncoder encoder) throws java.io.IOException { CoderResult overflow; do { overflow = encoder.encode(toEncode, current, true); if (overflow.isOverflow()) { scratch(); } else if (overflow.isError()) { try { toEncode.get(); } catch (final BufferUnderflowException e) { // Give up if we can't discard some presumptively malformed // or unmappable data break; } } } while (!overflow.isUnderflow()); }
public void append(final byte[] alreadyEncoded) throws java.io.IOException { buffer.append(alreadyEncoded); }
@Override public void close() throws java.io.IOException { buffer.flush(); // Unit tests in prelude depends on the stream _not_ being flushed, it // is necessary for Jetty to write content length headers, it seems. // stream.flush(); stream.close(); }
/** * Give the number of bytes written. * * @return the number of bytes written to this stream */ public long written() { return buffer.appended() + byteBufferData; }
protected AbstractByteWriter(final CharsetEncoder encoder) { this.encoder = encoder; buffer = new BufferChain(this); }
public void append(final byte[] alreadyEncoded) throws java.io.IOException { if (alreadyEncoded.length > 0) { append(alreadyEncoded, 0, alreadyEncoded.length); } }
/** * Flush the internal buffers, does not touch the ContentChannel. */ @Override public void flush() throws IOException { try { buffer.flush(); } catch (RuntimeException e) { throw new IOException(Exceptions.toMessageString(e), e); } }
public void append(final byte[] alreadyEncoded, final int offset, final int length) throws java.io.IOException { buffer.append(alreadyEncoded, offset, length); }
/** * Flush the internal buffers, does not touch the ContentChannel. */ @Override public void close() throws IOException { // the endpoint is closed in a finally{} block inside AbstractHttpRequestHandler // this class should be possible to close willynilly as it is exposed to plug-ins try { buffer.flush(); } catch (RuntimeException e) { throw new IOException(Exceptions.toMessageString(e), e); } }