01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.crypto.asn1;
17:
18: import java.io.FilterOutputStream;
19: import java.io.IOException;
20: import java.io.OutputStream;
21:
22: public class DEROutputStream extends FilterOutputStream implements
23: DERTags {
24: public DEROutputStream(OutputStream os) {
25: super (os);
26: }
27:
28: private void writeLength(int length) throws IOException {
29: if (length > 127) {
30: int size = 1;
31: int val = length;
32:
33: while ((val >>>= 8) != 0) {
34: size++;
35: }
36:
37: write((byte) (size | 0x80));
38:
39: for (int i = (size - 1) * 8; i >= 0; i -= 8) {
40: write((byte) (length >> i));
41: }
42: } else {
43: write((byte) length);
44: }
45: }
46:
47: void writeEncoded(int tag, byte[] bytes) throws IOException {
48: write(tag);
49: writeLength(bytes.length);
50: write(bytes);
51: }
52:
53: protected void writeNull() throws IOException {
54: write(NULL);
55: write(0x00);
56: }
57:
58: public void writeObject(Object obj) throws IOException {
59: if (obj == null) {
60: writeNull();
61: } else if (obj instanceof DERObject) {
62: ((DERObject) obj).encode(this );
63: } else if (obj instanceof DEREncodable) {
64: ((DEREncodable) obj).getDERObject().encode(this );
65: } else {
66: throw new IOException("object not DEREncodable");
67: }
68: }
69: }
|