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 * Return this very same {@link BufferedInputStream}, but keep a counter of
110 * how many streams were open this way. When calling
111 * {@link BufferedInputStream#close()}, decrease this counter if it is not
112 * already zero instead of actually closing the stream.
114 * You are now responsible for it — you <b>must</b> close it.
116 * This method allows you to use a wrapping stream around this one and still
117 * close the wrapping stream.
119 * @return the same stream, but you are now responsible for closing it
121 * @throws IOException
122 * in case of I/O error or if the stream is closed
124 public synchronized InputStream
open() throws IOException
{
131 * Check if the current content (what will be read next) starts with the
134 * Note: the search term size <b>must</b> be smaller or equal the internal
138 * the term to search for
140 * @return TRUE if the content that will be read starts with it
142 * @throws IOException
143 * in case of I/O error or if the size of the search term is
144 * greater than the internal buffer
146 public boolean startsWiths(String search
) throws IOException
{
147 return startsWith(StringUtils
.getBytes(search
));
151 * Check if the current content (what will be read next) starts with the
154 * Note: the search term size <b>must</b> be smaller or equal the internal
158 * the term to search for
160 * @return TRUE if the content that will be read starts with it
162 * @throws IOException
163 * in case of I/O error or if the size of the search term is
164 * greater than the internal buffer
166 public boolean startsWith(byte[] search
) throws IOException
{
167 if (search
.length
> originalBuffer
.length
) {
168 throw new IOException(
169 "This stream does not support searching for more than "
170 + buffer
.length
+ " bytes");
175 if (available() < search
.length
) {
179 if (available() >= search
.length
) {
181 return StreamUtils
.startsWith(search
, buffer
, start
, stop
);
184 if (buffer2
== null && buffer
.length
== originalBuffer
.length
) {
185 buffer2
= Arrays
.copyOf(buffer
, buffer
.length
* 2);
187 pos2
= buffer
.length
;
188 len2
= read(in
, buffer2
, pos2
, buffer
.length
);
193 // Note: here, len/len2 = INDEX of last good byte
197 return StreamUtils
.startsWith(search
, buffer2
, pos2
, len2
);
204 * The number of bytes read from the under-laying {@link InputStream}.
206 * @return the number of bytes
208 public long getBytesRead() {
213 * Check if this stream is totally spent (no more data to read or to
216 * @return TRUE if it is
218 * @throws IOException
219 * in case of I/O error
221 public boolean eof() throws IOException
{
227 return !hasMoreData();
231 public int read() throws IOException
{
239 return buffer
[start
++];
243 public int read(byte[] b
) throws IOException
{
244 return read(b
, 0, b
.length
);
248 public int read(byte[] b
, int boff
, int blen
) throws IOException
{
252 throw new NullPointerException();
253 } else if (boff
< 0 || blen
< 0 || blen
> b
.length
- boff
) {
254 throw new IndexOutOfBoundsException();
255 } else if (blen
== 0) {
260 while (hasMoreData() && done
< blen
) {
263 int now
= Math
.min(blen
- done
, stop
- start
);
265 System
.arraycopy(buffer
, start
, b
, boff
+ done
, now
);
272 return done
> 0 ? done
: -1;
276 public long skip(long n
) throws IOException
{
282 while (hasMoreData() && n
> 0) {
285 long inBuffer
= Math
.min(n
, available());
295 public int available() {
300 return Math
.max(0, stop
- start
);
304 * Closes this stream and releases any system resources associated with the
307 * Including the under-laying {@link InputStream}.
309 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
310 * prior to this one, it will just decrease the internal count of how many
311 * open streams it held and do nothing else. The stream will actually be
312 * closed when you have called {@link BufferedInputStream#close()} once more
313 * than {@link BufferedInputStream#open()}.
315 * @exception IOException
316 * in case of I/O error
319 public synchronized void close() throws IOException
{
324 * Closes this stream and releases any system resources associated with the
327 * Including the under-laying {@link InputStream} if
328 * <tt>incudingSubStream</tt> is true.
330 * You can call this method multiple times, it will not cause an
331 * {@link IOException} for subsequent calls.
333 * <b>Note:</b> if you called the {@link BufferedInputStream#open()} method
334 * prior to this one, it will just decrease the internal count of how many
335 * open streams it held and do nothing else. The stream will actually be
336 * closed when you have called {@link BufferedInputStream#close()} once more
337 * than {@link BufferedInputStream#open()}.
339 * @param includingSubStream
340 * also close the under-laying stream
342 * @exception IOException
343 * in case of I/O error
345 public synchronized void close(boolean includingSubStream
)
348 if (openCounter
> 0) {
352 if (includingSubStream
&& in
!= null) {
360 * Check if we still have some data in the buffer and, if not, fetch some.
362 * @return TRUE if we fetched some data, FALSE if there are still some in
365 * @throws IOException
366 * in case of I/O error
368 protected boolean preRead() throws IOException
{
369 boolean hasRead
= false;
370 if (in
!= null && !eof
&& start
>= stop
) {
372 if (buffer2
!= null) {
381 buffer
= originalBuffer
;
383 stop
= read(in
, buffer
, 0, buffer
.length
);
400 * Read the under-laying stream into the local buffer.
403 * the under-laying {@link InputStream}
405 * the buffer we use in this {@link BufferedInputStream}
409 * the length in bytes
411 * @return the number of bytes read
413 * @throws IOException
414 * in case of I/O error
416 protected int read(InputStream in
, byte[] buffer
, int off
, int len
)
418 return in
.read(buffer
, off
, len
);
422 * We have more data available in the buffer <b>or</b> we can, maybe, fetch
425 * @return TRUE if it is the case, FALSE if not
427 protected boolean hasMoreData() {
432 return (start
< stop
) || !eof
;
436 * Check that the stream was not closed, and throw an {@link IOException} if
439 * @throws IOException
442 protected void checkClose() throws IOException
{
444 throw new IOException(
445 "This BufferedInputStream was closed, you cannot use it anymore.");