1 package be
.nikiroo
.utils
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 public class NextableInputStream
extends InputStream
{
7 private InputStream in
;
11 private byte[] buffer
= new byte[4096];
13 public NextableInputStream(InputStream in
) {
18 public int read() throws IOException
{
28 public int read(byte[] b
) throws IOException
{
29 return read(b
, 0, b
.length
);
33 public int read(byte[] b
, int boff
, int blen
) throws IOException
{
35 throw new NullPointerException();
36 } else if (boff
< 0 || blen
< 0 || blen
> b
.length
- boff
) {
37 throw new IndexOutOfBoundsException();
38 } else if (blen
== 0) {
43 while (!eof
&& done
< blen
) {
45 for (int i
= pos
; i
< blen
&& i
< len
; i
++) {
46 b
[boff
+ done
] = buffer
[i
];
52 return done
> 0 ? done
: -1;
55 private void preRead() throws IOException
{
56 if (in
!= null && !eof
&& pos
>= len
) {
58 len
= in
.read(buffer
);