01: package org.bouncycastle.sasn1;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05:
06: class IndefiniteLengthInputStream extends LimitedInputStream {
07: private int _b1;
08: private int _b2;
09: private boolean _eofReached = false;
10: private boolean _eofOn00 = true;
11:
12: IndefiniteLengthInputStream(InputStream in) throws IOException {
13: super (in);
14:
15: _b1 = in.read();
16: _b2 = in.read();
17: _eofReached = (_b2 < 0);
18: }
19:
20: void setEofOn00(boolean eofOn00) {
21: _eofOn00 = eofOn00;
22: }
23:
24: void checkForEof() throws IOException {
25: if (_eofOn00 && (_b1 == 0x00 && _b2 == 0x00)) {
26: _eofReached = true;
27: setParentEofDetect(true);
28: }
29: }
30:
31: public int read() throws IOException {
32: checkForEof();
33:
34: if (_eofReached) {
35: return -1;
36: }
37:
38: int b = _in.read();
39:
40: //
41: // strictly speaking we should return b1 and b2, but if this happens the stream
42: // is corrupted so we are already in trouble.
43: //
44: if (b < 0) {
45: _eofReached = true;
46:
47: return -1;
48: }
49:
50: int v = _b1;
51:
52: _b1 = _b2;
53: _b2 = b;
54:
55: return v;
56: }
57: }
|