1 package be
.nikiroo
.utils
.streams
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.Arrays
;
8 * A simple {@link InputStream} that is buffered with a bytes array.
10 * It is mostly intended to be used as a base class to create new
11 * {@link InputStream}s with special operation modes, and to give some default
16 public class BufferedInputStream
extends InputStream
{
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 InputStream in
;
28 private int openCounter
;
30 // special use, prefetched next buffer
31 private byte[] buffer2
;
34 private byte[] originalBuffer
;
36 private long bytesRead
;
39 * Create a new {@link BufferedInputStream} that wraps the given
40 * {@link InputStream}.
43 * the {@link InputStream} to wrap
45 public BufferedInputStream(InputStream in
) {
48 this.buffer
= new byte[4096];
49 this.originalBuffer
= this.buffer
;
55 * Create a new {@link BufferedInputStream} that wraps the given bytes array
59 * the array to wrap, cannot be NULL
61 public BufferedInputStream(byte[] in
) {
62 this(in
, 0, in
.length
);
66 * Create a new {@link BufferedInputStream} that wraps the given bytes array
70 * the array to wrap, cannot be NULL
72 * the offset to start the reading at
74 * the number of bytes to take into account in the array,
75 * starting from the offset
77 * @throws NullPointerException
78 * if the array is NULL
79 * @throws IndexOutOfBoundsException
80 * if the offset and length do not correspond to the given array
82 public BufferedInputStream(byte[] in
, int offset
, int length
) {
84 throw new NullPointerException();
85 } else if (offset
< 0 || length
< 0 || length
> in
.length
- offset
) {
86 throw new IndexOutOfBoundsException();
92 this.originalBuffer
= this.buffer
;
98 * Return this very same {@link BufferedInputStream}, but keep a counter of
99 * how many streams were open this way. When calling
100 * {@link BufferedInputStream#close()}, decrease this counter if it is not
101 * already zero instead of actually closing the stream.
103 * You are now responsible for it — you <b>must</b> close it.
105 * This method allows you to use a wrapping stream around this one and still
106 * close the wrapping stream.
108 * @return the same stream, but you are now responsible for closing it
110 * @throws IOException
111 * in case of I/O error or if the stream is closed
113 public synchronized InputStream
open() throws IOException
{
120 * Check if the current content (what will be read next) starts with the
123 * Note: the search term size <b>must</b> be smaller or equal the internal
127 * the term to search for
129 * @return TRUE if the content that will be read starts with it
131 * @throws IOException
132 * in case of I/O error or if the size of the search term is
133 * greater than the internal buffer
135 public boolean startsWiths(String search
) throws IOException
{
136 return startsWith(search
.getBytes("UTF-8"));
140 * Check if the current content (what will be read next) starts with the
143 * Note: the search term size <b>must</b> be smaller or equal the internal
147 * the term to search for
149 * @return TRUE if the content that will be read starts with it
151 * @throws IOException
152 * in case of I/O error or if the size of the search term is
153 * greater than the internal buffer
155 public boolean startsWith(byte[] search
) throws IOException
{
156 if (search
.length
> originalBuffer
.length
) {
157 throw new IOException(
158 "This stream does not support searching for more than "
159 + buffer
.length
+ " bytes");
164 if (available() < search
.length
) {
168 if (available() >= search
.length
) {
170 return StreamUtils
.startsWith(search
, buffer
, start
, stop
);
173 if (buffer2
== null && buffer
.length
== originalBuffer
.length
) {
174 buffer2
= Arrays
.copyOf(buffer
, buffer
.length
* 2);
176 pos2
= buffer
.length
;
177 len2
= read(in
, buffer2
, pos2
, buffer
.length
);
182 // Note: here, len/len2 = INDEX of last good byte
186 return StreamUtils
.startsWith(search
, buffer2
, pos2
, len2
);
193 * The number of bytes read from the under-laying {@link InputStream}.
195 * @return the number of bytes
197 public long getBytesRead() {
202 * Check if this stream is totally spent (no more data to read or to
205 * @return TRUE if it is
207 * @throws IOException
208 * in case of I/O error
210 public boolean eof() throws IOException
{
216 return !hasMoreData();
220 public int read() throws IOException
{
228 return buffer
[start
++];
232 public int read(byte[] b
) throws IOException
{
233 return read(b
, 0, b
.length
);
237 public int read(byte[] b
, int boff
, int blen
) throws IOException
{
241 throw new NullPointerException();
242 } else if (boff
< 0 || blen
< 0 || blen
> b
.length
- boff
) {
243 throw new IndexOutOfBoundsException();
244 } else if (blen
== 0) {
249 while (hasMoreData() && done
< blen
) {
252 int now
= Math
.min(blen
, stop
) - start
;
254 System
.arraycopy(buffer
, start
, b
, boff
+ done
, now
);
261 return done
> 0 ? done
: -1;
265 public long skip(long n
) throws IOException
{
271 while (hasMoreData() && n
> 0) {
274 long inBuffer
= Math
.min(n
, available());
284 public int available() {
289 return Math
.max(0, stop
- start
);
293 * Closes this stream and releases any system resources associated with the
296 * Including the under-laying {@link InputStream}.
298 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
299 * prior to this one, it will just decrease the internal count of how many
300 * open streams it held and do nothing else. The stream will actually be
301 * closed when you have called {@link BufferedInputStream#close()} once more
302 * than {@link BufferedInputStream#open()}.
304 * @exception IOException
305 * in case of I/O error
308 public synchronized void close() throws IOException
{
313 * Closes this stream and releases any system resources associated with the
316 * Including the under-laying {@link InputStream} if
317 * <tt>incudingSubStream</tt> is true.
319 * You can call this method multiple times, it will not cause an
320 * {@link IOException} for subsequent calls.
322 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
323 * prior to this one, it will just decrease the internal count of how many
324 * open streams it held and do nothing else. The stream will actually be
325 * closed when you have called {@link BufferedInputStream#close()} once more
326 * than {@link BufferedInputStream#open()}.
328 * @param includingSubStream
329 * also close the under-laying stream
331 * @exception IOException
332 * in case of I/O error
334 public synchronized void close(boolean includingSubStream
)
337 if (openCounter
> 0) {
341 if (includingSubStream
&& in
!= null) {
349 * Check if we still have some data in the buffer and, if not, fetch some.
351 * @return TRUE if we fetched some data, FALSE if there are still some in
354 * @throws IOException
355 * in case of I/O error
357 protected boolean preRead() throws IOException
{
358 boolean hasRead
= false;
359 if (in
!= null && !eof
&& start
>= stop
) {
361 if (buffer2
!= null) {
370 buffer
= originalBuffer
;
372 stop
= read(in
, buffer
, 0, buffer
.length
);
389 * Read the under-laying stream into the local buffer.
392 * the under-laying {@link InputStream}
394 * the buffer we use in this {@link BufferedInputStream}
398 * the length in bytes
400 * @return the number of bytes read
402 * @throws IOException
403 * in case of I/O error
405 protected int read(InputStream in
, byte[] buffer
, int off
, int len
)
407 return in
.read(buffer
, off
, len
);
411 * We have more data available in the buffer <b>or</b> we can, maybe, fetch
414 * @return TRUE if it is the case, FALSE if not
416 protected boolean hasMoreData() {
421 return (start
< stop
) || !eof
;
425 * Check that the stream was not closed, and throw an {@link IOException} if
428 * @throws IOException
431 protected void checkClose() throws IOException
{
433 throw new IOException(
434 "This BufferedInputStream was closed, you cannot use it anymore.");