01: package org.bouncycastle.cms;
02:
03: import java.io.BufferedInputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06:
07: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
08:
09: public class CMSTypedStream {
10: private static final int BUF_SIZ = 32 * 1024;
11:
12: private final String _oid;
13: private final InputStream _in;
14: private final int _bufSize;
15:
16: public CMSTypedStream(InputStream in) {
17: this (PKCSObjectIdentifiers.data.getId(), in, BUF_SIZ);
18: }
19:
20: public CMSTypedStream(String oid, InputStream in) {
21: this (oid, in, BUF_SIZ);
22: }
23:
24: public CMSTypedStream(String oid, InputStream in, int bufSize) {
25: _oid = oid;
26: _bufSize = bufSize;
27: _in = new FullReaderStream(in, bufSize);
28: }
29:
30: public String getContentType() {
31: return _oid;
32: }
33:
34: public InputStream getContentStream() {
35: return _in;
36: }
37:
38: public void drain() throws IOException {
39: byte[] buf = new byte[_bufSize];
40:
41: while ((_in.read(buf, 0, buf.length) > 0)) {
42: // keep going...
43: }
44:
45: _in.close();
46: }
47:
48: private class FullReaderStream extends InputStream {
49: InputStream _stream;
50:
51: FullReaderStream(InputStream in, int bufSize) {
52: _stream = new BufferedInputStream(in, bufSize);
53: }
54:
55: public int read() throws IOException {
56: return _stream.read();
57: }
58:
59: public int read(byte[] buf, int off, int len)
60: throws IOException {
61: int rd = 0;
62: int total = 0;
63:
64: while (len != 0 && (rd = _stream.read(buf, off, len)) > 0) {
65: off += rd;
66: len -= rd;
67: total += rd;
68: }
69:
70: if (total > 0) {
71: return total;
72: } else {
73: return -1;
74: }
75: }
76:
77: public void close() throws IOException {
78: _stream.close();
79: }
80: }
81: }
|