KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
21 import java.io.EOFException JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * @deprecated use ASN1InputStream
28  */

29 public class BERInputStream
30     extends DERInputStream
31 {
32     private DERObject END_OF_STREAM = new DERObject() {
33                                         void encode(
34                                             DEROutputStream out)
35                                         throws IOException JavaDoc
36                                         {
37                                             throw new IOException JavaDoc("Eeek!");
38                                         }
39                                         public int hashCode()
40                                         {
41                                             return 0;
42                                         }
43                                         public boolean equals(
44                                             Object JavaDoc o)
45                                         {
46                                             return o == this;
47                                         }
48                                     };
49     public BERInputStream(
50         InputStream JavaDoc is)
51     {
52         super(is);
53     }
54
55     /**
56      * read a string of bytes representing an indefinite length object.
57      */

58     private byte[] readIndefiniteLengthFully()
59         throws IOException JavaDoc
60     {
61         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
62         int b, b1;
63
64         b1 = read();
65
66         while ((b = read()) >= 0)
67         {
68             if (b1 == 0 && b == 0)
69             {
70                 break;
71             }
72
73             bOut.write(b1);
74             b1 = b;
75         }
76
77         return bOut.toByteArray();
78     }
79
80     private BERConstructedOctetString buildConstructedOctetString()
81         throws IOException JavaDoc
82     {
83         Vector JavaDoc octs = new Vector JavaDoc();
84
85         for (;;)
86         {
87             DERObject o = readObject();
88
89             if (o == END_OF_STREAM)
90             {
91                 break;
92             }
93
94             octs.addElement(o);
95         }
96
97         return new BERConstructedOctetString(octs);
98     }
99
100     public DERObject readObject()
101         throws IOException JavaDoc
102     {
103         int tag = read();
104         if (tag == -1)
105         {
106             throw new EOFException JavaDoc();
107         }
108
109         int length = readLength();
110
111         if (length < 0) // indefinite length method
112
{
113             switch (tag)
114             {
115             case NULL:
116                 return null;
117             case SEQUENCE | CONSTRUCTED:
118                 BERConstructedSequence seq = new BERConstructedSequence();
119
120                 for (;;)
121                 {
122                     DERObject obj = readObject();
123
124                     if (obj == END_OF_STREAM)
125                     {
126                         break;
127                     }
128
129                     seq.addObject(obj);
130                 }
131                 return seq;
132             case OCTET_STRING | CONSTRUCTED:
133                 return buildConstructedOctetString();
134             case SET | CONSTRUCTED:
135                 ASN1EncodableVector v = new ASN1EncodableVector();
136
137                 for (;;)
138                 {
139                     DERObject obj = readObject();
140
141                     if (obj == END_OF_STREAM)
142                     {
143                         break;
144                     }
145
146                     v.add(obj);
147                 }
148                 return new BERSet(v);
149             default:
150                 //
151
// with tagged object tag number is bottom 5 bits
152
//
153
if ((tag & TAGGED) != 0)
154                 {
155                     if ((tag & 0x1f) == 0x1f)
156                     {
157                         throw new IOException JavaDoc("unsupported high tag encountered");
158                     }
159
160                     //
161
// simple type - implicit... return an octet string
162
//
163
if ((tag & CONSTRUCTED) == 0)
164                     {
165                         byte[] bytes = readIndefiniteLengthFully();
166
167                         return new BERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes));
168                     }
169
170                     //
171
// either constructed or explicitly tagged
172
//
173
DERObject dObj = readObject();
174
175                     if (dObj == END_OF_STREAM) // empty tag!
176
{
177                         return new DERTaggedObject(tag & 0x1f);
178                     }
179
180                     DERObject next = readObject();
181
182                     //
183
// explicitly tagged (probably!) - if it isn't we'd have to
184
// tell from the context
185
//
186
if (next == END_OF_STREAM)
187                     {
188                         return new BERTaggedObject(tag & 0x1f, dObj);
189                     }
190
191                     //
192
// another implicit object, we'll create a sequence...
193
//
194
seq = new BERConstructedSequence();
195
196                     seq.addObject(dObj);
197
198                     do
199                     {
200                         seq.addObject(next);
201                         next = readObject();
202                     }
203                     while (next != END_OF_STREAM);
204
205                     return new BERTaggedObject(false, tag & 0x1f, seq);
206                 }
207
208                 throw new IOException JavaDoc("unknown BER object encountered");
209             }
210         }
211         else
212         {
213             if (tag == 0 && length == 0) // end of contents marker.
214
{
215                 return END_OF_STREAM;
216             }
217
218             byte[] bytes = new byte[length];
219
220             readFully(bytes);
221
222             return buildObject(tag, bytes);
223         }
224     }
225 }
226
Popular Tags