KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DEROutputStream


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.FilterOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 public class DEROutputStream
25     extends FilterOutputStream JavaDoc implements DERTags
26 {
27     public DEROutputStream(
28         OutputStream JavaDoc os)
29     {
30         super(os);
31     }
32
33     private void writeLength(
34         int length)
35         throws IOException JavaDoc
36     {
37         if (length > 127)
38         {
39             int size = 1;
40             int val = length;
41
42             while ((val >>>= 8) != 0)
43             {
44                 size++;
45             }
46
47             write((byte)(size | 0x80));
48
49             for (int i = (size - 1) * 8; i >= 0; i -= 8)
50             {
51                 write((byte)(length >> i));
52             }
53         }
54         else
55         {
56             write((byte)length);
57         }
58     }
59
60     void writeEncoded(
61         int tag,
62         byte[] bytes)
63         throws IOException JavaDoc
64     {
65         write(tag);
66         writeLength(bytes.length);
67         write(bytes);
68     }
69
70     protected void writeNull()
71         throws IOException JavaDoc
72     {
73         write(NULL);
74         write(0x00);
75     }
76
77     public void writeObject(
78         Object JavaDoc obj)
79         throws IOException JavaDoc
80     {
81         if (obj == null)
82         {
83             writeNull();
84         }
85         else if (obj instanceof DERObject)
86         {
87             ((DERObject)obj).encode(this);
88         }
89         else if (obj instanceof DEREncodable)
90         {
91             ((DEREncodable)obj).getDERObject().encode(this);
92         }
93         else
94         {
95             throw new IOException JavaDoc("object not DEREncodable");
96         }
97     }
98 }
99
Popular Tags