private boolean closed;
private int openCounter;
+ private byte[] b1;
/**
* Create a new {@link BufferedInputStream} that wraps the given
this.out = out;
this.buffer = new byte[4096];
+ this.b1 = new byte[1];
this.start = 0;
this.stop = 0;
}
@Override
public void write(int b) throws IOException {
- checkClose();
-
- if (available() <= 0) {
- flush(false);
- }
-
- buffer[start++] = (byte) b;
+ b1[0] = (byte) b;
+ write(b1, 0, 1);
}
@Override
return;
}
- if (sourceLength >= buffer.length) {
+ if (bypassFlush && sourceLength >= buffer.length) {
/*
* If the request length exceeds the size of the output buffer,
* flush the output buffer and then write the data directly. In this
*/
flush(false);
out.write(source, sourceOffset, sourceLength);
+ bytesWritten += (sourceLength - sourceOffset);
return;
}
flush(false);
}
- int now = Math.min(sourceLength, available());
+ int now = Math.min(sourceLength - done, available());
System.arraycopy(source, sourceOffset + done, buffer, stop, now);
stop += now;
done += now;
* <p>
* If {@link BufferedOutputStream#bypassFlush} is false, all writes to the
* under-laying stream are done in this method.
+ * <p>
+ * This can be used if you want to write some data in the under-laying
+ * stream yourself (in that case, flush this {@link BufferedOutputStream}
+ * with or without flushing the under-laying stream, then you can write to
+ * the under-laying stream).
*
* @param includingSubStream
* also flush the under-laying stream
* @throws IOException
* in case of I/O error
*/
- protected void flush(boolean includingSubStream) throws IOException {
- out.write(buffer, start, stop - start);
- bytesWritten += (stop - start);
+ public void flush(boolean includingSubStream) throws IOException {
+ if (stop > start) {
+ out.write(buffer, start, stop - start);
+ bytesWritten += (stop - start);
+ }
start = 0;
stop = 0;
openCounter--;
} else {
closed = true;
- flush(true);
+ flush(includingSubStream);
if (includingSubStream && out != null) {
out.close();
}