8b74ae162d2b58bf90040650bf7d02312134fece
1 package be
.nikiroo
.utils
.streams
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.io
.OutputStream
;
8 * A simple {@link OutputStream} that is buffered with a bytes array.
10 * It is mostly intended to be used as a base class to create new
11 * {@link OutputStream}s with special operation modes, and to give some default
16 public class BufferedOutputStream
extends OutputStream
{
17 /** The current position in the buffer. */
19 /** The index of the last usable position of the buffer. */
21 /** The buffer itself. */
22 protected byte[] buffer
;
23 /** An End-Of-File (or buffer, here) marker. */
24 protected boolean eof
;
25 /** The actual under-laying stream. */
26 protected OutputStream out
;
27 /** The number of bytes written to the under-laying stream. */
28 protected long bytesWritten
;
30 * Can bypass the flush process for big writes (will directly write to the
31 * under-laying buffer if the array to write is > the internal buffer
34 * By default, this is true.
36 protected boolean bypassFlush
= true;
38 private boolean closed
;
39 private int openCounter
;
42 * Create a new {@link BufferedInputStream} that wraps the given
43 * {@link InputStream}.
46 * the {@link OutputStream} to wrap
48 public BufferedOutputStream(OutputStream out
) {
51 this.buffer
= new byte[4096];
57 public void write(int b
) throws IOException
{
60 if (available() <= 0) {
64 buffer
[start
++] = (byte) b
;
68 public void write(byte[] b
) throws IOException
{
69 write(b
, 0, b
.length
);
73 public void write(byte[] source
, int sourceOffset
, int sourceLength
)
79 throw new NullPointerException();
80 } else if ((sourceOffset
< 0) || (sourceOffset
> source
.length
)
82 || ((sourceOffset
+ sourceLength
) > source
.length
)
83 || ((sourceOffset
+ sourceLength
) < 0)) {
84 throw new IndexOutOfBoundsException();
85 } else if (sourceLength
== 0) {
89 if (sourceLength
>= buffer
.length
) {
91 * If the request length exceeds the size of the output buffer,
92 * flush the output buffer and then write the data directly. In this
93 * way buffered streams will cascade harmlessly.
96 out
.write(source
, sourceOffset
, sourceLength
);
101 while (done
< sourceLength
) {
102 if (available() <= 0) {
106 int now
= Math
.min(sourceLength
, available());
107 System
.arraycopy(source
, sourceOffset
+ done
, buffer
, stop
, now
);
114 * The available space in the buffer.
116 * @return the space in bytes
118 private int available() {
123 return Math
.max(0, buffer
.length
- stop
- 1);
127 * The number of bytes written to the under-laying {@link OutputStream}.
129 * @return the number of bytes
131 public long getBytesWritten() {
136 * Return this very same {@link BufferedInputStream}, but keep a counter of
137 * how many streams were open this way. When calling
138 * {@link BufferedInputStream#close()}, decrease this counter if it is not
139 * already zero instead of actually closing the stream.
141 * You are now responsible for it — you <b>must</b> close it.
143 * This method allows you to use a wrapping stream around this one and still
144 * close the wrapping stream.
146 * @return the same stream, but you are now responsible for closing it
148 * @throws IOException
149 * in case of I/O error or if the stream is closed
151 public synchronized OutputStream
open() throws IOException
{
158 * Check that the stream was not closed, and throw an {@link IOException} if
161 * @throws IOException
164 protected void checkClose() throws IOException
{
166 throw new IOException(
167 "This BufferedInputStream was closed, you cannot use it anymore.");
172 public void flush() throws IOException
{
177 * Flush the {@link BufferedOutputStream}, write the current buffered data
178 * to (and optionally also flush) the under-laying stream.
180 * If {@link BufferedOutputStream#bypassFlush} is false, all writes to the
181 * under-laying stream are done in this method.
183 * @param includingSubStream
184 * also flush the under-laying stream
185 * @throws IOException
186 * in case of I/O error
188 protected void flush(boolean includingSubStream
) throws IOException
{
189 out
.write(buffer
, start
, stop
- start
);
190 bytesWritten
+= (stop
- start
);
194 if (includingSubStream
) {
200 * Closes this stream and releases any system resources associated with the
203 * Including the under-laying {@link InputStream}.
205 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
206 * prior to this one, it will just decrease the internal count of how many
207 * open streams it held and do nothing else. The stream will actually be
208 * closed when you have called {@link BufferedInputStream#close()} once more
209 * than {@link BufferedInputStream#open()}.
211 * @exception IOException
212 * in case of I/O error
215 public synchronized void close() throws IOException
{
220 * Closes this stream and releases any system resources associated with the
223 * Including the under-laying {@link InputStream} if
224 * <tt>incudingSubStream</tt> is true.
226 * You can call this method multiple times, it will not cause an
227 * {@link IOException} for subsequent calls.
229 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
230 * prior to this one, it will just decrease the internal count of how many
231 * open streams it held and do nothing else. The stream will actually be
232 * closed when you have called {@link BufferedInputStream#close()} once more
233 * than {@link BufferedInputStream#open()}.
235 * @param includingSubStream
236 * also close the under-laying stream
238 * @exception IOException
239 * in case of I/O error
241 public synchronized void close(boolean includingSubStream
)
244 if (openCounter
> 0) {
249 if (includingSubStream
&& out
!= null) {