01: package org.bouncycastle.asn1;
02:
03: import java.io.InputStream;
04: import java.io.IOException;
05:
06: class ConstructedOctetStream extends InputStream {
07: private final ASN1ObjectParser _parser;
08:
09: private boolean _first = true;
10: private InputStream _currentStream;
11:
12: ConstructedOctetStream(ASN1ObjectParser parser) {
13: _parser = parser;
14: }
15:
16: public int read(byte[] b, int off, int len) throws IOException {
17: if (_currentStream == null) {
18: if (!_first) {
19: return -1;
20: }
21:
22: ASN1OctetStringParser s = (ASN1OctetStringParser) _parser
23: .readObject();
24:
25: if (s == null) {
26: return -1;
27: }
28:
29: _first = false;
30: _currentStream = s.getOctetStream();
31: }
32:
33: int totalRead = 0;
34:
35: for (;;) {
36: int numRead = _currentStream.read(b, off + totalRead, len
37: - totalRead);
38:
39: if (numRead >= 0) {
40: totalRead += numRead;
41:
42: if (totalRead == len) {
43: return totalRead;
44: }
45: } else {
46: ASN1OctetStringParser aos = (ASN1OctetStringParser) _parser
47: .readObject();
48:
49: if (aos == null) {
50: _currentStream = null;
51: return totalRead < 1 ? -1 : totalRead;
52: }
53:
54: _currentStream = aos.getOctetStream();
55: }
56: }
57: }
58:
59: public int read() throws IOException {
60: if (_currentStream == null) {
61: if (!_first) {
62: return -1;
63: }
64:
65: ASN1OctetStringParser s = (ASN1OctetStringParser) _parser
66: .readObject();
67:
68: if (s == null) {
69: return -1;
70: }
71:
72: _first = false;
73: _currentStream = s.getOctetStream();
74: }
75:
76: for (;;) {
77: int b = _currentStream.read();
78:
79: if (b >= 0) {
80: return b;
81: }
82:
83: ASN1OctetStringParser s = (ASN1OctetStringParser) _parser
84: .readObject();
85:
86: if (s == null) {
87: _currentStream = null;
88: return -1;
89: }
90:
91: _currentStream = s.getOctetStream();
92: }
93: }
94: }
|