KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > SteerableInputStream


1 package org.columba.core.io;
2
3 import java.io.FilterInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 public class SteerableInputStream extends FilterInputStream JavaDoc {
8
9     private long lengthLeft;
10
11     private long position;
12
13     public SteerableInputStream(InputStream JavaDoc in) {
14         super(in);
15
16         try {
17             lengthLeft = in.available();
18         } catch (IOException JavaDoc e) {
19             lengthLeft = 0;
20         }
21     }
22
23     /**
24      * {@inheritDoc}
25      */

26     @Override JavaDoc
27     public int read() throws IOException JavaDoc {
28         if (lengthLeft > 0) {
29             int read = super.read();
30             if (read != -1) {
31                 lengthLeft--;
32             }
33             position++;
34             return read;
35         }
36         return -1;
37     }
38
39     /**
40      * {@inheritDoc}
41      */

42     @Override JavaDoc
43     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
44         if (len > lengthLeft) {
45             int correctedLen = (int) lengthLeft;
46             int read = super.read(b, off, correctedLen);
47             lengthLeft -= read;
48             position += read;
49             return read;
50         }
51         int read = super.read(b, off, len);
52         lengthLeft -= read;
53         position += read;
54         return read;
55
56     }
57
58     /**
59      * @return Returns the lengthLeft.
60      */

61     public long getLengthLeft() {
62         return lengthLeft;
63     }
64
65     /**
66      * @param lengthLeft
67      * The lengthLeft to set.
68      */

69     public void setLengthLeft(long lengthLeft) throws IOException JavaDoc {
70         this.lengthLeft = Math.min(lengthLeft, in.available());
71     }
72
73     /**
74      * @return Returns the position.
75      */

76     public long getPosition() {
77         return position;
78     }
79
80     /**
81      * @param position
82      * The position to set.
83      */

84     public void setPosition(long newposition) throws IOException JavaDoc {
85         long skipped = in.skip(newposition - position);
86         lengthLeft = Math.max(0, lengthLeft - skipped);
87         position = newposition;
88     }
89
90     
91     /**
92      * {@inheritDoc}
93      */

94     @Override JavaDoc
95     public int available() throws IOException JavaDoc {
96         return (int) lengthLeft;
97     }
98
99     
100     /**
101      * {@inheritDoc}
102      */

103     @Override JavaDoc
104     public void close() throws IOException JavaDoc {
105         // do nothinh here ... use finalClose
106
}
107
108     
109     /**
110      * @throws IOException
111      */

112     public void finalClose() throws IOException JavaDoc {
113         super.close();
114     }
115
116 }
117
Popular Tags