1 package org.bouncycastle.asn1;
2 
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.util.Enumeration;
6 import java.util.Vector;
7 
8 /**
9  * @deprecated use BEROctetString
10  */
11 public class BERConstructedOctetString
12     extends BEROctetString
13 {
14     private static final int MAX_LENGTH = 1000;
15 
16     /**
17      * convert a vector of octet strings into a single byte string
18      */
toBytes( Vector octs)19     static private byte[] toBytes(
20         Vector  octs)
21     {
22         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
23 
24         for (int i = 0; i != octs.size(); i++)
25         {
26             try
27             {
28                 DEROctetString  o = (DEROctetString)octs.elementAt(i);
29 
30                 bOut.write(o.getOctets());
31             }
32             catch (ClassCastException e)
33             {
34                 throw new IllegalArgumentException(octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString");
35             }
36             catch (IOException e)
37             {
38                 throw new IllegalArgumentException("exception converting octets " + e.toString());
39             }
40         }
41 
42         return bOut.toByteArray();
43     }
44 
45     private Vector  octs;
46 
47     /**
48      * @param string the octets making up the octet string.
49      */
BERConstructedOctetString( byte[] string)50     public BERConstructedOctetString(
51         byte[]  string)
52     {
53         super(string);
54     }
55 
BERConstructedOctetString( Vector octs)56     public BERConstructedOctetString(
57         Vector  octs)
58     {
59         super(toBytes(octs));
60 
61         this.octs = octs;
62     }
63 
BERConstructedOctetString( ASN1Primitive obj)64     public BERConstructedOctetString(
65         ASN1Primitive  obj)
66     {
67         super(toByteArray(obj));
68     }
69 
toByteArray(ASN1Primitive obj)70     private static byte[] toByteArray(ASN1Primitive obj)
71     {
72         try
73         {
74             return obj.getEncoded();
75         }
76         catch (IOException e)
77         {
78             throw new IllegalArgumentException("Unable to encode object");
79         }
80     }
81 
BERConstructedOctetString( ASN1Encodable obj)82     public BERConstructedOctetString(
83         ASN1Encodable  obj)
84     {
85         this(obj.toASN1Primitive());
86     }
87 
getOctets()88     public byte[] getOctets()
89     {
90         return string;
91     }
92 
93     /**
94      * return the DER octets that make up this string.
95      */
getObjects()96     public Enumeration getObjects()
97     {
98         if (octs == null)
99         {
100             return generateOcts().elements();
101         }
102 
103         return octs.elements();
104     }
105 
generateOcts()106     private Vector generateOcts()
107     {
108         Vector vec = new Vector();
109         for (int i = 0; i < string.length; i += MAX_LENGTH)
110         {
111             int end;
112 
113             if (i + MAX_LENGTH > string.length)
114             {
115                 end = string.length;
116             }
117             else
118             {
119                 end = i + MAX_LENGTH;
120             }
121 
122             byte[] nStr = new byte[end - i];
123 
124             System.arraycopy(string, i, nStr, 0, nStr.length);
125 
126             vec.addElement(new DEROctetString(nStr));
127          }
128 
129          return vec;
130     }
131 
fromSequence(ASN1Sequence seq)132     public static BEROctetString fromSequence(ASN1Sequence seq)
133     {
134         Vector      v = new Vector();
135         Enumeration e = seq.getObjects();
136 
137         while (e.hasMoreElements())
138         {
139             v.addElement(e.nextElement());
140         }
141 
142         return new BERConstructedOctetString(v);
143     }
144 }
145