1 package com.lowagie.bc.asn1; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.util.Enumeration; 6 import java.util.Vector; 7 8 public class BERConstructedOctetString 9 extends DEROctetString 10 { 11 14 static private byte[] toBytes( 15 Vector octs) 16 { 17 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 18 19 for (int i = 0; i != octs.size(); i++) 20 { 21 DEROctetString o = (DEROctetString)octs.elementAt(i); 22 23 try 24 { 25 bOut.write(o.getOctets()); 26 } 27 catch (IOException e) 28 { 29 throw new RuntimeException("exception converting octets " + e.toString()); 30 } 31 } 32 33 return bOut.toByteArray(); 34 } 35 36 private Vector octs; 37 38 41 public BERConstructedOctetString( 42 byte[] string) 43 { 44 super(string); 45 } 46 47 public BERConstructedOctetString( 48 Vector octs) 49 { 50 super(toBytes(octs)); 51 52 this.octs = octs; 53 } 54 55 public BERConstructedOctetString( 56 DERObject obj) 57 { 58 super(obj); 59 } 60 61 public BERConstructedOctetString( 62 DEREncodable obj) 63 { 64 super(obj.getDERObject()); 65 } 66 67 public byte[] getOctets() 68 { 69 return string; 70 } 71 72 75 public Enumeration getObjects() 76 { 77 if (octs == null) 78 { 79 return generateOcts().elements(); 80 } 81 82 return octs.elements(); 83 } 84 85 private Vector generateOcts() 86 { 87 int start = 0; 88 int end = 0; 89 Vector vec = new Vector(); 90 91 while ((end + 1) < string.length) 92 { 93 if (string[end] == 0 && string[end + 1] == 0) 94 { 95 byte[] nStr = new byte[end - start + 1]; 96 97 System.arraycopy(string, start, nStr, 0, nStr.length); 98 99 vec.addElement(new DEROctetString(nStr)); 100 start = end + 1; 101 } 102 end++; 103 } 104 105 byte[] nStr = new byte[string.length - start]; 106 107 System.arraycopy(string, start, nStr, 0, nStr.length); 108 109 vec.addElement(new DEROctetString(nStr)); 110 111 return vec; 112 } 113 114 public void encode( 115 DEROutputStream out) 116 throws IOException 117 { 118 if (out instanceof ASN1OutputStream || out instanceof BEROutputStream) 119 { 120 out.write(CONSTRUCTED | OCTET_STRING); 121 122 out.write(0x80); 123 124 if (octs != null) 128 { 129 for (int i = 0; i != octs.size(); i++) 130 { 131 out.writeObject(octs.elementAt(i)); 132 } 133 } 134 else 135 { 136 int start = 0; 137 int end = 0; 138 139 while ((end + 1) < string.length) 140 { 141 if (string[end] == 0 && string[end + 1] == 0) 142 { 143 byte[] nStr = new byte[end - start + 1]; 144 145 System.arraycopy(string, start, nStr, 0, nStr.length); 146 147 out.writeObject(new DEROctetString(nStr)); 148 start = end + 1; 149 } 150 end++; 151 } 152 153 byte[] nStr = new byte[string.length - start]; 154 155 System.arraycopy(string, start, nStr, 0, nStr.length); 156 157 out.writeObject(new DEROctetString(nStr)); 158 } 159 160 out.write(0x00); 161 out.write(0x00); 162 } 163 else 164 { 165 super.encode(out); 166 } 167 } 168 } 169 | Popular Tags |