01: package org.bouncycastle.sasn1;
02:
03: import org.bouncycastle.asn1.DEROctetString;
04:
05: import java.io.IOException;
06: import java.io.OutputStream;
07:
08: /**
09: * @deprecated use corresponsding classes in org.bouncycastle.asn1.
10: */
11: public class BerOctetStringGenerator extends BerGenerator {
12: public BerOctetStringGenerator(OutputStream out) throws IOException {
13: super (out);
14:
15: writeBerHeader(BerTag.CONSTRUCTED | BerTag.OCTET_STRING);
16: }
17:
18: public BerOctetStringGenerator(OutputStream out, int tagNo,
19: boolean isExplicit) throws IOException {
20: super (out, tagNo, isExplicit);
21:
22: writeBerHeader(BerTag.CONSTRUCTED | BerTag.OCTET_STRING);
23: }
24:
25: public OutputStream getOctetOutputStream() {
26: return new BerOctetStream();
27: }
28:
29: public OutputStream getOctetOutputStream(byte[] buf) {
30: return new BufferedBerOctetStream(buf);
31: }
32:
33: private class BerOctetStream extends OutputStream {
34: private byte[] _buf = new byte[1];
35:
36: public void write(int b) throws IOException {
37: _buf[0] = (byte) b;
38:
39: _out.write(new DEROctetString(_buf).getEncoded());
40: }
41:
42: public void write(byte[] buf) throws IOException {
43: _out.write(new DEROctetString(buf).getEncoded());
44: }
45:
46: public void write(byte[] buf, int offSet, int len)
47: throws IOException {
48: byte[] bytes = new byte[len];
49:
50: System.arraycopy(buf, offSet, bytes, 0, len);
51:
52: _out.write(new DEROctetString(bytes).getEncoded());
53: }
54:
55: public void close() throws IOException {
56: writeBerEnd();
57: }
58: }
59:
60: private class BufferedBerOctetStream extends OutputStream {
61: private byte[] _buf;
62: private int _off;
63:
64: BufferedBerOctetStream(byte[] buf) {
65: _buf = buf;
66: _off = 0;
67: }
68:
69: public void write(int b) throws IOException {
70: _buf[_off++] = (byte) b;
71:
72: if (_off == _buf.length) {
73: _out.write(new DEROctetString(_buf).getEncoded());
74: _off = 0;
75: }
76: }
77:
78: public void close() throws IOException {
79: if (_off != 0) {
80: byte[] bytes = new byte[_off];
81: System.arraycopy(_buf, 0, bytes, 0, _off);
82:
83: _out.write(new DEROctetString(bytes).getEncoded());
84: }
85:
86: writeBerEnd();
87: }
88: }
89: }
|