1 package be
.nikiroo
.utils
.streams
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.Arrays
;
7 import be
.nikiroo
.utils
.StringUtils
;
10 * A simple {@link InputStream} that is buffered with a bytes array.
12 * It is mostly intended to be used as a base class to create new
13 * {@link InputStream}s with special operation modes, and to give some default
18 public class BufferedInputStream
extends InputStream
{
20 * The size of the internal buffer (can be different if you pass your own
23 * A second buffer of twice the size can sometimes be created as needed for
24 * the {@link BufferedInputStream#startsWith(byte[])} search operation.
26 static private final int BUFFER_SIZE
= 4096;
28 /** The current position in the buffer. */
30 /** The index of the last usable position of the buffer. */
32 /** The buffer itself. */
33 protected byte[] buffer
;
34 /** An End-Of-File (or {@link InputStream}, here) marker. */
35 protected boolean eof
;
37 private boolean closed
;
38 private InputStream in
;
39 private int openCounter
;
41 // special use, prefetched next buffer
42 private byte[] buffer2
;
45 private byte[] originalBuffer
;
47 private long bytesRead
;
50 * Create a new {@link BufferedInputStream} that wraps the given
51 * {@link InputStream}.
54 * the {@link InputStream} to wrap
56 public BufferedInputStream(InputStream in
) {
59 this.buffer
= new byte[BUFFER_SIZE
];
60 this.originalBuffer
= this.buffer
;
66 * Create a new {@link BufferedInputStream} that wraps the given bytes array
70 * the array to wrap, cannot be NULL
72 public BufferedInputStream(byte[] in
) {
73 this(in
, 0, in
.length
);
77 * Create a new {@link BufferedInputStream} that wraps the given bytes array
81 * the array to wrap, cannot be NULL
83 * the offset to start the reading at
85 * the number of bytes to take into account in the array,
86 * starting from the offset
88 * @throws NullPointerException
89 * if the array is NULL
90 * @throws IndexOutOfBoundsException
91 * if the offset and length do not correspond to the given array
93 public BufferedInputStream(byte[] in
, int offset
, int length
) {
95 throw new NullPointerException();
96 } else if (offset
< 0 || length
< 0 || length
> in
.length
- offset
) {
97 throw new IndexOutOfBoundsException();
103 this.originalBuffer
= this.buffer
;
109 * The internal buffer size (can be useful to know for search methods).
111 * @return the size of the internal buffer, in bytes.
113 public int getInternalBufferSize() {
114 return originalBuffer
.length
;
118 * Return this very same {@link BufferedInputStream}, but keep a counter of
119 * how many streams were open this way. When calling
120 * {@link BufferedInputStream#close()}, decrease this counter if it is not
121 * already zero instead of actually closing the stream.
123 * You are now responsible for it — you <b>must</b> close it.
125 * This method allows you to use a wrapping stream around this one and still
126 * close the wrapping stream.
128 * @return the same stream, but you are now responsible for closing it
130 * @throws IOException
131 * in case of I/O error or if the stream is closed
133 public synchronized InputStream
open() throws IOException
{
140 * Check if the current content (until eof) is equal to the given search
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 is(String search
) throws IOException
{
156 return is(StringUtils
.getBytes(search
));
160 * Check if the current content (until eof) is equal to the given search
163 * Note: the search term size <b>must</b> be smaller or equal the internal
167 * the term to search for
169 * @return TRUE if the content that will be read starts with it
171 * @throws IOException
172 * in case of I/O error or if the size of the search term is
173 * greater than the internal buffer
175 public boolean is(byte[] search
) throws IOException
{
176 if (startsWith(search
)) {
177 return stop
== search
.length
;
184 * Check if the current content (what will be read next) starts with the
187 * Note: the search term size <b>must</b> be smaller or equal the internal
191 * the term to search for
193 * @return TRUE if the content that will be read starts with it
195 * @throws IOException
196 * in case of I/O error or if the size of the search term is
197 * greater than the internal buffer
199 public boolean startsWiths(String search
) throws IOException
{
200 return startsWith(StringUtils
.getBytes(search
));
204 * Check if the current content (what will be read next) starts with the
207 * Note: the search term size <b>must</b> be smaller or equal the internal
211 * the term to search for
213 * @return TRUE if the content that will be read starts with it
215 * @throws IOException
216 * in case of I/O error or if the size of the search term is
217 * greater than the internal buffer
219 public boolean startsWith(byte[] search
) throws IOException
{
220 if (search
.length
> originalBuffer
.length
) {
221 throw new IOException(
222 "This stream does not support searching for more than "
223 + buffer
.length
+ " bytes");
228 if (available() < search
.length
) {
232 if (available() >= search
.length
) {
234 return StreamUtils
.startsWith(search
, buffer
, start
, stop
);
237 if (buffer2
== null && buffer
.length
== originalBuffer
.length
) {
238 buffer2
= Arrays
.copyOf(buffer
, buffer
.length
* 2);
240 pos2
= buffer
.length
;
241 len2
= read(in
, buffer2
, pos2
, buffer
.length
);
246 // Note: here, len/len2 = INDEX of last good byte
250 return StreamUtils
.startsWith(search
, buffer2
, pos2
, len2
);
257 * The number of bytes read from the under-laying {@link InputStream}.
259 * @return the number of bytes
261 public long getBytesRead() {
266 * Check if this stream is totally spent (no more data to read or to
269 * @return TRUE if it is
271 * @throws IOException
272 * in case of I/O error
274 public boolean eof() throws IOException
{
280 return !hasMoreData();
284 public int read() throws IOException
{
292 return buffer
[start
++];
296 public int read(byte[] b
) throws IOException
{
297 return read(b
, 0, b
.length
);
301 public int read(byte[] b
, int boff
, int blen
) throws IOException
{
305 throw new NullPointerException();
306 } else if (boff
< 0 || blen
< 0 || blen
> b
.length
- boff
) {
307 throw new IndexOutOfBoundsException();
308 } else if (blen
== 0) {
313 while (hasMoreData() && done
< blen
) {
316 int now
= Math
.min(blen
- done
, stop
- start
);
318 System
.arraycopy(buffer
, start
, b
, boff
+ done
, now
);
325 return done
> 0 ? done
: -1;
329 public long skip(long n
) throws IOException
{
335 while (hasMoreData() && n
> 0) {
338 long inBuffer
= Math
.min(n
, available());
348 public int available() {
353 return Math
.max(0, stop
- start
);
357 * Closes this stream and releases any system resources associated with the
360 * Including the under-laying {@link InputStream}.
362 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
363 * prior to this one, it will just decrease the internal count of how many
364 * open streams it held and do nothing else. The stream will actually be
365 * closed when you have called {@link BufferedInputStream#close()} once more
366 * than {@link BufferedInputStream#open()}.
368 * @exception IOException
369 * in case of I/O error
372 public synchronized void close() throws IOException
{
377 * Closes this stream and releases any system resources associated with the
380 * Including the under-laying {@link InputStream} if
381 * <tt>incudingSubStream</tt> is true.
383 * You can call this method multiple times, it will not cause an
384 * {@link IOException} for subsequent calls.
386 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
387 * prior to this one, it will just decrease the internal count of how many
388 * open streams it held and do nothing else. The stream will actually be
389 * closed when you have called {@link BufferedInputStream#close()} once more
390 * than {@link BufferedInputStream#open()}.
392 * @param includingSubStream
393 * also close the under-laying stream
395 * @exception IOException
396 * in case of I/O error
398 public synchronized void close(boolean includingSubStream
)
401 if (openCounter
> 0) {
405 if (includingSubStream
&& in
!= null) {
413 * Check if we still have some data in the buffer and, if not, fetch some.
415 * @return TRUE if we fetched some data, FALSE if there are still some in
418 * @throws IOException
419 * in case of I/O error
421 protected boolean preRead() throws IOException
{
422 boolean hasRead
= false;
423 if (in
!= null && !eof
&& start
>= stop
) {
425 if (buffer2
!= null) {
434 buffer
= originalBuffer
;
436 stop
= read(in
, buffer
, 0, buffer
.length
);
453 * Read the under-laying stream into the local buffer.
456 * the under-laying {@link InputStream}
458 * the buffer we use in this {@link BufferedInputStream}
462 * the length in bytes
464 * @return the number of bytes read
466 * @throws IOException
467 * in case of I/O error
469 protected int read(InputStream in
, byte[] buffer
, int off
, int len
)
471 return in
.read(buffer
, off
, len
);
475 * We have more data available in the buffer <b>or</b> we can, maybe, fetch
478 * @return TRUE if it is the case, FALSE if not
480 protected boolean hasMoreData() {
485 return (start
< stop
) || !eof
;
489 * Check that the stream was not closed, and throw an {@link IOException} if
492 * @throws IOException
495 protected void checkClose() throws IOException
{
497 throw new IOException(
498 "This BufferedInputStream was closed, you cannot use it anymore.");