1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.util.Enumeration;
24 import java.util.Vector;
25
26 public class BERConstructedOctetString
27 extends DEROctetString
28 {
29 /**
30 * convert a vector of octet strings into a single byte string
31 */
32 static private byte[] toBytes(
33 Vector octs)
34 {
35 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
36
37 for (int i = 0; i != octs.size(); i++)
38 {
39 try
40 {
41 DEROctetString o = (DEROctetString)octs.elementAt(i);
42
43 bOut.write(o.getOctets());
44 }
45 catch (ClassCastException e)
46 {
47 throw new IllegalArgumentException(octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString");
48 }
49 catch (IOException e)
50 {
51 throw new IllegalArgumentException("exception converting octets " + e.toString());
52 }
53 }
54
55 return bOut.toByteArray();
56 }
57
58 private Vector octs;
59
60 /**
61 * @param string the octets making up the octet string.
62 */
63 public BERConstructedOctetString(
64 byte[] string)
65 {
66 super(string);
67 }
68
69 public BERConstructedOctetString(
70 Vector octs)
71 {
72 super(toBytes(octs));
73
74 this.octs = octs;
75 }
76
77 public BERConstructedOctetString(
78 DERObject obj)
79 {
80 super(obj);
81 }
82
83 public BERConstructedOctetString(
84 DEREncodable obj)
85 {
86 super(obj.getDERObject());
87 }
88
89 public byte[] getOctets()
90 {
91 return string;
92 }
93
94 /**
95 * return the DER octets that make up this string.
96 */
97 public Enumeration getObjects()
98 {
99 if (octs == null)
100 {
101 return generateOcts().elements();
102 }
103
104 return octs.elements();
105 }
106
107 private Vector generateOcts()
108 {
109 int start = 0;
110 int end = 0;
111 Vector vec = new Vector();
112
113 while ((end + 1) < string.length)
114 {
115 if (string[end] == 0 && string[end + 1] == 0)
116 {
117 byte[] nStr = new byte[end - start + 1];
118
119 System.arraycopy(string, start, nStr, 0, nStr.length);
120
121 vec.addElement(new DEROctetString(nStr));
122 start = end + 1;
123 }
124 end++;
125 }
126
127 byte[] nStr = new byte[string.length - start];
128
129 System.arraycopy(string, start, nStr, 0, nStr.length);
130
131 vec.addElement(new DEROctetString(nStr));
132
133 return vec;
134 }
135
136 public void encode(
137 DEROutputStream out)
138 throws IOException
139 {
140 if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
141 {
142 out.write(CONSTRUCTED | OCTET_STRING);
143
144 out.write(0x80);
145
146
147
148
149 if (octs != null)
150 {
151 for (int i = 0; i != octs.size(); i++)
152 {
153 out.writeObject(octs.elementAt(i));
154 }
155 }
156 else
157 {
158 int start = 0;
159 int end = 0;
160
161 while ((end + 1) < string.length)
162 {
163 if (string[end] == 0 && string[end + 1] == 0)
164 {
165 byte[] nStr = new byte[end - start + 1];
166
167 System.arraycopy(string, start, nStr, 0, nStr.length);
168
169 out.writeObject(new DEROctetString(nStr));
170 start = end + 1;
171 }
172 end++;
173 }
174
175 byte[] nStr = new byte[string.length - start];
176
177 System.arraycopy(string, start, nStr, 0, nStr.length);
178
179 out.writeObject(new DEROctetString(nStr));
180 }
181
182 out.write(0x00);
183 out.write(0x00);
184 }
185 else
186 {
187 super.encode(out);
188 }
189 }
190 }