1 package be
.nikiroo
.utils
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.ArrayList
;
8 public class NextableInputStream
extends InputStream
{
9 private List
<NextableInputStreamStep
> steps
= new ArrayList
<NextableInputStreamStep
>();
10 private NextableInputStreamStep step
= null;
12 private InputStream in
;
16 private byte[] buffer
= new byte[4096];
18 public NextableInputStream(InputStream in
) {
22 public void addStep(NextableInputStreamStep step
) {
26 public boolean next() {
27 if (!hasMoreData() && step
!= null) {
28 len
= step
.getResumeLen();
29 pos
+= step
.getSkip();
42 public int read() throws IOException
{
52 public int read(byte[] b
) throws IOException
{
53 return read(b
, 0, b
.length
);
57 public int read(byte[] b
, int boff
, int blen
) throws IOException
{
59 throw new NullPointerException();
60 } else if (boff
< 0 || blen
< 0 || blen
> b
.length
- boff
) {
61 throw new IndexOutOfBoundsException();
62 } else if (blen
== 0) {
67 while (hasMoreData() && done
< blen
) {
70 for (int i
= pos
; i
< blen
&& i
< len
; i
++) {
71 b
[boff
+ done
] = buffer
[i
];
78 return done
> 0 ? done
: -1;
82 public int available() throws IOException
{
83 return Math
.max(0, len
- pos
);
86 private void preRead() throws IOException
{
87 if (!eof
&& in
!= null && pos
>= len
&& step
== null) {
89 len
= in
.read(buffer
);
98 private boolean hasMoreData() {
99 return !(eof
&& pos
>= len
);
102 private void checkNexts(boolean newBuffer
) {
104 for (NextableInputStreamStep step
: steps
) {
109 int stopAt
= step
.stop(buffer
, pos
, len
);