KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 public class BERConstructedOctetString
26     extends DEROctetString
27 {
28     /**
29      * convert a vector of octet strings into a single byte string
30      */

31     static private byte[] toBytes(
32         Vector JavaDoc octs)
33     {
34         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
35
36         for (int i = 0; i != octs.size(); i++)
37         {
38             try
39             {
40                 DEROctetString o = (DEROctetString)octs.elementAt(i);
41
42                 bOut.write(o.getOctets());
43             }
44             catch (ClassCastException JavaDoc e)
45             {
46                 throw new IllegalArgumentException JavaDoc(octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString");
47             }
48             catch (IOException JavaDoc e)
49             {
50                 throw new IllegalArgumentException JavaDoc("exception converting octets " + e.toString());
51             }
52         }
53
54         return bOut.toByteArray();
55     }
56
57     private Vector JavaDoc octs;
58
59     /**
60      * @param string the octets making up the octet string.
61      */

62     public BERConstructedOctetString(
63         byte[] string)
64     {
65         super(string);
66     }
67
68     public BERConstructedOctetString(
69         Vector JavaDoc octs)
70     {
71         super(toBytes(octs));
72
73         this.octs = octs;
74     }
75
76     public BERConstructedOctetString(
77         DERObject obj)
78     {
79         super(obj);
80     }
81
82     public BERConstructedOctetString(
83         DEREncodable obj)
84     {
85         super(obj.getDERObject());
86     }
87
88     public byte[] getOctets()
89     {
90         return string;
91     }
92
93     /**
94      * return the DER octets that make up this string.
95      */

96     public Enumeration JavaDoc getObjects()
97     {
98         if (octs == null)
99         {
100             return generateOcts().elements();
101         }
102
103         return octs.elements();
104     }
105
106     private Vector JavaDoc generateOcts()
107     {
108         int start = 0;
109         int end = 0;
110         Vector JavaDoc vec = new Vector JavaDoc();
111
112         while ((end + 1) < string.length)
113         {
114             if (string[end] == 0 && string[end + 1] == 0)
115             {
116                 byte[] nStr = new byte[end - start + 1];
117
118                 System.arraycopy(string, start, nStr, 0, nStr.length);
119
120                 vec.addElement(new DEROctetString(nStr));
121                 start = end + 1;
122             }
123             end++;
124         }
125
126         byte[] nStr = new byte[string.length - start];
127
128         System.arraycopy(string, start, nStr, 0, nStr.length);
129
130         vec.addElement(new DEROctetString(nStr));
131
132         return vec;
133     }
134
135     public void encode(
136         DEROutputStream out)
137         throws IOException JavaDoc
138     {
139         if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
140         {
141             out.write(CONSTRUCTED | OCTET_STRING);
142
143             out.write(0x80);
144
145             //
146
// write out the octet array
147
//
148
if (octs != null)
149             {
150                 for (int i = 0; i != octs.size(); i++)
151                 {
152                     out.writeObject(octs.elementAt(i));
153                 }
154             }
155             else
156             {
157                 int start = 0;
158                 int end = 0;
159
160                 while ((end + 1) < string.length)
161                 {
162                     if (string[end] == 0 && string[end + 1] == 0)
163                     {
164                         byte[] nStr = new byte[end - start + 1];
165
166                         System.arraycopy(string, start, nStr, 0, nStr.length);
167
168                         out.writeObject(new DEROctetString(nStr));
169                         start = end + 1;
170                     }
171                     end++;
172                 }
173
174                 byte[] nStr = new byte[string.length - start];
175
176                 System.arraycopy(string, start, nStr, 0, nStr.length);
177
178                 out.writeObject(new DEROctetString(nStr));
179             }
180
181             out.write(0x00);
182             out.write(0x00);
183         }
184         else
185         {
186             super.encode(out);
187         }
188     }
189 }
190
Popular Tags