01: package org.bouncycastle.asn1;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.OutputStream;
06:
07: public class BERGenerator extends ASN1Generator {
08: private boolean _tagged = false;
09: private boolean _isExplicit;
10: private int _tagNo;
11:
12: protected BERGenerator(OutputStream out) {
13: super (out);
14: }
15:
16: public BERGenerator(OutputStream out, int tagNo, boolean isExplicit) {
17: super (out);
18:
19: _tagged = true;
20: _isExplicit = isExplicit;
21: _tagNo = tagNo;
22: }
23:
24: public OutputStream getRawOutputStream() {
25: return _out;
26: }
27:
28: private void writeHdr(int tag) throws IOException {
29: _out.write(tag);
30: _out.write(0x80);
31: }
32:
33: protected void writeBERHeader(int tag) throws IOException {
34: if (_tagged) {
35: int tagNum = _tagNo | DERTags.TAGGED;
36:
37: if (_isExplicit) {
38: writeHdr(tagNum | DERTags.CONSTRUCTED);
39: writeHdr(tag);
40: } else {
41: if ((tag & DERTags.CONSTRUCTED) != 0) {
42: writeHdr(tagNum | DERTags.CONSTRUCTED);
43: } else {
44: writeHdr(tagNum);
45: }
46: }
47: } else {
48: writeHdr(tag);
49: }
50: }
51:
52: protected void writeBERBody(InputStream contentStream)
53: throws IOException {
54: int ch;
55:
56: while ((ch = contentStream.read()) >= 0) {
57: _out.write(ch);
58: }
59: }
60:
61: protected void writeBEREnd() throws IOException {
62: _out.write(0x00);
63: _out.write(0x00);
64:
65: if (_tagged && _isExplicit) // write extra end for tag header
66: {
67: _out.write(0x00);
68: _out.write(0x00);
69: }
70: }
71: }
|