01: package org.bouncycastle.asn1;
02:
03: import java.io.FilterOutputStream;
04: import java.io.IOException;
05: import java.io.OutputStream;
06:
07: public class DEROutputStream extends FilterOutputStream implements
08: DERTags {
09: public DEROutputStream(OutputStream os) {
10: super (os);
11: }
12:
13: private void writeLength(int length) throws IOException {
14: if (length > 127) {
15: int size = 1;
16: int val = length;
17:
18: while ((val >>>= 8) != 0) {
19: size++;
20: }
21:
22: write((byte) (size | 0x80));
23:
24: for (int i = (size - 1) * 8; i >= 0; i -= 8) {
25: write((byte) (length >> i));
26: }
27: } else {
28: write((byte) length);
29: }
30: }
31:
32: void writeEncoded(int tag, byte[] bytes) throws IOException {
33: write(tag);
34: writeLength(bytes.length);
35: write(bytes);
36: }
37:
38: protected void writeNull() throws IOException {
39: write(NULL);
40: write(0x00);
41: }
42:
43: public void write(byte[] buf) throws IOException {
44: out.write(buf, 0, buf.length);
45: }
46:
47: public void write(byte[] buf, int offSet, int len)
48: throws IOException {
49: out.write(buf, offSet, len);
50: }
51:
52: public void writeObject(Object obj) throws IOException {
53: if (obj == null) {
54: writeNull();
55: } else if (obj instanceof DERObject) {
56: ((DERObject) obj).encode(this );
57: } else if (obj instanceof DEREncodable) {
58: ((DEREncodable) obj).getDERObject().encode(this );
59: } else {
60: throw new IOException("object not DEREncodable");
61: }
62: }
63: }
|