1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 import java.util.Vector ; 24 25 public class BERConstructedOctetString 26 extends DEROctetString 27 { 28 31 static private byte[] toBytes( 32 Vector octs) 33 { 34 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 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 e) 45 { 46 throw new IllegalArgumentException (octs.elementAt(i).getClass().getName() + " found in input should only contain DEROctetString"); 47 } 48 catch (IOException e) 49 { 50 throw new IllegalArgumentException ("exception converting octets " + e.toString()); 51 } 52 } 53 54 return bOut.toByteArray(); 55 } 56 57 private Vector octs; 58 59 62 public BERConstructedOctetString( 63 byte[] string) 64 { 65 super(string); 66 } 67 68 public BERConstructedOctetString( 69 Vector 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 96 public Enumeration getObjects() 97 { 98 if (octs == null) 99 { 100 return generateOcts().elements(); 101 } 102 103 return octs.elements(); 104 } 105 106 private Vector generateOcts() 107 { 108 int start = 0; 109 int end = 0; 110 Vector vec = new Vector (); 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 138 { 139 if (out instanceof ASN1OutputStream || out instanceof BEROutputStream) 140 { 141 out.write(CONSTRUCTED | OCTET_STRING); 142 143 out.write(0x80); 144 145 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 |