2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / BERConstructedOctetString.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4 import java.util.*;
5
6 public class BERConstructedOctetString
7     extends DEROctetString
8 {
9     /**
10      * convert a vector of octet strings into a single byte string
11      */
12     static private byte[] toBytes(
13         Vector  octs)
14     {
15         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
16
17         for (int i = 0; i != octs.size(); i++)
18         {
19             DEROctetString  o = (DEROctetString)octs.elementAt(i);
20
21             try
22             {
23                 bOut.write(o.getOctets());
24             }
25             catch (IOException e)
26             {
27                 throw new RuntimeException("exception converting octets " + e.toString());
28             }
29         }
30
31         return bOut.toByteArray();
32     }
33
34     private Vector  octs;
35
36     /**
37      * @param string the octets making up the octet string.
38      */
39     public BERConstructedOctetString(
40         byte[]  string)
41     {
42                 super(string);
43     }
44
45     public BERConstructedOctetString(
46         Vector  octs)
47     {
48                 super(toBytes(octs));
49
50         this.octs = octs;
51     }
52
53     public BERConstructedOctetString(
54         DERObject  obj)
55     {
56                 super(obj);
57     }
58
59     public BERConstructedOctetString(
60         DEREncodable  obj)
61     {
62         super(obj.getDERObject());
63     }
64
65     public byte[] getOctets()
66     {
67         return string;
68     }
69
70     public Vector getDEROctets()
71     {
72         if (octs == null)
73         {
74             octs = generateOcts();
75         }
76
77         return octs;
78     }
79
80     private Vector generateOcts()
81     {
82         int     start = 0;
83         int     end = 0;
84         Vector  vec = new Vector();
85
86         while (end < string.length)
87         {
88             if ((end + 1) < string.length)
89             {
90                 if (string[end] == 0 && string[end + 1] == 0)
91                 {
92                     byte[]  nStr = new byte[end - start + 1];
93
94                     for (int i = 0; i != nStr.length; i++)
95                     {
96                         nStr[i] = string[start + i];
97                     }
98
99                     vec.addElement(new DEROctetString(nStr));
100                     start = end + 1;
101                 }
102             }
103             end++;
104         }
105
106         byte[]  nStr = new byte[end - start];
107         for (int i = 0; i != nStr.length; i++)
108         {
109             nStr[i] = string[start + i];
110         }
111
112         vec.addElement(new DEROctetString(nStr));
113
114         return vec;
115     }
116
117     public void encode(
118         DEROutputStream out)
119         throws IOException
120     {
121                 if (out instanceof BEROutputStream)
122                 {
123             out.write(CONSTRUCTED | OCTET_STRING);
124
125             out.write(0x80);
126
127             if (octs == null)
128             {
129                 octs = generateOcts();
130             }
131
132             for (int i = 0; i != octs.size(); i++)
133             {
134                 out.writeObject(octs.elementAt(i));
135             }
136
137             out.write(0x00);
138             out.write(0x00);
139                 }
140                 else
141                 {
142                         super.encode(out);
143                 }
144     }
145 }