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
;
26 private boolean closed
;
27 private OutputStream out
;
28 private int openCounter
;
30 private long bytesWritten
;
33 * Create a new {@link BufferedInputStream} that wraps the given
34 * {@link InputStream}.
37 * the {@link OutputStream} to wrap
39 public BufferedOutputStream(OutputStream out
) {
42 this.buffer
= new byte[4096];
48 public void write(int b
) throws IOException
{
51 if (available() <= 0) {
55 buffer
[start
++] = (byte) b
;
59 public void write(byte[] b
) throws IOException
{
60 write(b
, 0, b
.length
);
64 public void write(byte[] source
, int sourceOffset
, int sourceLength
)
70 throw new NullPointerException();
71 } else if ((sourceOffset
< 0) || (sourceOffset
> source
.length
)
73 || ((sourceOffset
+ sourceLength
) > source
.length
)
74 || ((sourceOffset
+ sourceLength
) < 0)) {
75 throw new IndexOutOfBoundsException();
76 } else if (sourceLength
== 0) {
80 if (sourceLength
>= buffer
.length
) {
82 * If the request length exceeds the size of the output buffer,
83 * flush the output buffer and then write the data directly. In this
84 * way buffered streams will cascade harmlessly.
87 out
.write(source
, sourceOffset
, sourceLength
);
92 while (done
< sourceLength
) {
93 if (available() <= 0) {
97 int now
= Math
.min(sourceLength
, available());
98 System
.arraycopy(source
, sourceOffset
+ done
, buffer
, stop
, now
);
105 * The available space in the buffer.
107 * @return the space in bytes
109 private int available() {
114 return Math
.max(0, buffer
.length
- stop
- 1);
118 * The number of bytes written to the under-laying {@link OutputStream}.
120 * @return the number of bytes
122 public long getBytesWritten() {
127 * Return this very same {@link BufferedInputStream}, but keep a counter of
128 * how many streams were open this way. When calling
129 * {@link BufferedInputStream#close()}, decrease this counter if it is not
130 * already zero instead of actually closing the stream.
132 * You are now responsible for it — you <b>must</b> close it.
134 * This method allows you to use a wrapping stream around this one and still
135 * close the wrapping stream.
137 * @return the same stream, but you are now responsible for closing it
139 * @throws IOException
140 * in case of I/O error or if the stream is closed
142 public synchronized OutputStream
open() throws IOException
{
149 * Check that the stream was not closed, and throw an {@link IOException} if
152 * @throws IOException
155 protected void checkClose() throws IOException
{
157 throw new IOException(
158 "This BufferedInputStream was closed, you cannot use it anymore.");
163 public void flush() throws IOException
{
168 * Flush the {@link BufferedOutputStream}, and optionally the under-laying
171 * @param includingSubStream
172 * also flush the under-laying stream
173 * @throws IOException
174 * in case of I/O error
176 private void flush(boolean includingSubStream
) throws IOException
{
177 out
.write(buffer
, start
, stop
- start
);
178 bytesWritten
+= (stop
- start
);
181 if (includingSubStream
) {
187 * Closes this stream and releases any system resources associated with the
190 * Including the under-laying {@link InputStream}.
192 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
193 * prior to this one, it will just decrease the internal count of how many
194 * open streams it held and do nothing else. The stream will actually be
195 * closed when you have called {@link BufferedInputStream#close()} once more
196 * than {@link BufferedInputStream#open()}.
198 * @exception IOException
199 * in case of I/O error
202 public synchronized void close() throws IOException
{
207 * Closes this stream and releases any system resources associated with the
210 * Including the under-laying {@link InputStream} if
211 * <tt>incudingSubStream</tt> is true.
213 * You can call this method multiple times, it will not cause an
214 * {@link IOException} for subsequent calls.
216 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
217 * prior to this one, it will just decrease the internal count of how many
218 * open streams it held and do nothing else. The stream will actually be
219 * closed when you have called {@link BufferedInputStream#close()} once more
220 * than {@link BufferedInputStream#open()}.
222 * @param includingSubStream
223 * also close the under-laying stream
225 * @exception IOException
226 * in case of I/O error
228 public synchronized void close(boolean includingSubStream
)
231 if (openCounter
> 0) {
236 if (includingSubStream
&& out
!= null) {