KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > BERConstructedOctetString


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 public class BERConstructedOctetString
28     extends DEROctetString
29 {
30     /**
31      * convert a vector of octet strings into a single byte string
32      */

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

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

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