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
= in
.read(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 public boolean eof() {
208 return closed
|| (stop
< 0 && !hasMoreData());
212 public int read() throws IOException
{
220 return buffer
[start
++];
224 public int read(byte[] b
) throws IOException
{
225 return read(b
, 0, b
.length
);
229 public int read(byte[] b
, int boff
, int blen
) throws IOException
{
233 throw new NullPointerException();
234 } else if (boff
< 0 || blen
< 0 || blen
> b
.length
- boff
) {
235 throw new IndexOutOfBoundsException();
236 } else if (blen
== 0) {
241 while (hasMoreData() && done
< blen
) {
244 int now
= Math
.min(blen
, stop
) - start
;
246 System
.arraycopy(buffer
, start
, b
, boff
+ done
, now
);
253 return done
> 0 ? done
: -1;
257 public long skip(long n
) throws IOException
{
263 while (hasMoreData() && n
> 0) {
266 long inBuffer
= Math
.min(n
, available());
276 public int available() {
281 return Math
.max(0, stop
- start
);
285 * Closes this stream and releases any system resources associated with the
288 * Including the under-laying {@link InputStream}.
290 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
291 * prior to this one, it will just decrease the internal count of how many
292 * open streams it held and do nothing else. The stream will actually be
293 * closed when you have called {@link BufferedInputStream#close()} once more
294 * than {@link BufferedInputStream#open()}.
296 * @exception IOException
297 * in case of I/O error
300 public synchronized void close() throws IOException
{
305 * Closes this stream and releases any system resources associated with the
308 * Including the under-laying {@link InputStream} if
309 * <tt>incudingSubStream</tt> is true.
311 * You can call this method multiple times, it will not cause an
312 * {@link IOException} for subsequent calls.
314 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
315 * prior to this one, it will just decrease the internal count of how many
316 * open streams it held and do nothing else. The stream will actually be
317 * closed when you have called {@link BufferedInputStream#close()} once more
318 * than {@link BufferedInputStream#open()}.
320 * @param includingSubStream
321 * also close the under-laying stream
323 * @exception IOException
324 * in case of I/O error
326 public synchronized void close(boolean includingSubStream
)
329 if (openCounter
> 0) {
333 if (includingSubStream
&& in
!= null) {
341 * Check if we still have some data in the buffer and, if not, fetch some.
343 * @return TRUE if we fetched some data, FALSE if there are still some in
346 * @throws IOException
347 * in case of I/O error
349 protected boolean preRead() throws IOException
{
350 boolean hasRead
= false;
351 if (!eof
&& in
!= null && start
>= stop
) {
353 if (buffer2
!= null) {
362 buffer
= originalBuffer
;
364 stop
= read(in
, buffer
);
381 * Read the under-laying stream into the local buffer.
384 * the under-laying {@link InputStream}
386 * the buffer we use in this {@link BufferedInputStream}
388 * @return the number of bytes read
390 * @throws IOException
391 * in case of I/O error
393 protected int read(InputStream in
, byte[] buffer
) throws IOException
{
394 return in
.read(buffer
);
398 * We have more data available in the buffer or we can fetch more.
400 * @return TRUE if it is the case, FALSE if not
402 protected boolean hasMoreData() {
403 return !closed
&& !(eof
&& start
>= stop
);
407 * Check that the stream was not closed, and throw an {@link IOException} if
410 * @throws IOException
413 protected void checkClose() throws IOException
{
415 throw new IOException(
416 "This BufferedInputStream was closed, you cannot use it anymore.");