01: package org.bouncycastle.sasn1;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.io.OutputStream;
07:
08: /**
09: * @deprecated use corresponsding classes in org.bouncycastle.asn1.
10: */
11: public abstract class DerGenerator extends Asn1Generator {
12: private boolean _tagged = false;
13: private boolean _isExplicit;
14: private int _tagNo;
15:
16: protected DerGenerator(OutputStream out) {
17: super (out);
18: }
19:
20: public DerGenerator(OutputStream out, int tagNo, boolean isExplicit) {
21: super (out);
22:
23: _tagged = true;
24: _isExplicit = isExplicit;
25: _tagNo = tagNo;
26: }
27:
28: private void writeLength(OutputStream out, int length)
29: throws IOException {
30: if (length > 127) {
31: int size = 1;
32: int val = length;
33:
34: while ((val >>>= 8) != 0) {
35: size++;
36: }
37:
38: out.write((byte) (size | 0x80));
39:
40: for (int i = (size - 1) * 8; i >= 0; i -= 8) {
41: out.write((byte) (length >> i));
42: }
43: } else {
44: out.write((byte) length);
45: }
46: }
47:
48: void writeDerEncoded(OutputStream out, int tag, byte[] bytes)
49: throws IOException {
50: out.write(tag);
51: writeLength(out, bytes.length);
52: out.write(bytes);
53: }
54:
55: void writeDerEncoded(int tag, byte[] bytes) throws IOException {
56: if (_tagged) {
57: int tagNum = _tagNo | BerTag.TAGGED;
58:
59: if (_isExplicit) {
60: int newTag = _tagNo | BerTag.CONSTRUCTED
61: | BerTag.TAGGED;
62:
63: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
64:
65: writeDerEncoded(bOut, tag, bytes);
66:
67: writeDerEncoded(_out, newTag, bOut.toByteArray());
68: } else {
69: if ((tag & BerTag.CONSTRUCTED) != 0) {
70: writeDerEncoded(_out, tagNum | BerTag.CONSTRUCTED,
71: bytes);
72: } else {
73: writeDerEncoded(_out, tagNum, bytes);
74: }
75: }
76: } else {
77: writeDerEncoded(_out, tag, bytes);
78: }
79: }
80:
81: void writeDerEncoded(OutputStream out, int tag, InputStream in)
82: throws IOException {
83: out.write(tag);
84:
85: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
86:
87: int b = 0;
88: while ((b = in.read()) >= 0) {
89: bOut.write(b);
90: }
91:
92: byte[] bytes = bOut.toByteArray();
93:
94: writeLength(out, bytes.length);
95: out.write(bytes);
96: }
97: }
|