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 abstract public class ASN1Set 26 extends DERObject 27 { 28 protected Vector set = new Vector (); 29 30 36 public static ASN1Set getInstance( 37 Object obj) 38 { 39 if (obj == null || obj instanceof ASN1Set) 40 { 41 return (ASN1Set)obj; 42 } 43 44 throw new IllegalArgumentException ("unknown object in getInstance"); 45 } 46 47 63 public static ASN1Set getInstance( 64 ASN1TaggedObject obj, 65 boolean explicit) 66 { 67 if (explicit) 68 { 69 if (!obj.isExplicit()) 70 { 71 throw new IllegalArgumentException ("object implicit - explicit expected."); 72 } 73 74 return (ASN1Set)obj.getObject(); 75 } 76 else 77 { 78 if (obj.isExplicit()) 84 { 85 ASN1Set set = new DERSet(obj.getObject()); 86 87 return set; 88 } 89 else 90 { 91 if (obj.getObject() instanceof ASN1Set) 92 { 93 return (ASN1Set)obj.getObject(); 94 } 95 96 ASN1EncodableVector v = new ASN1EncodableVector(); 101 102 if (obj.getObject() instanceof ASN1Sequence) 103 { 104 ASN1Sequence s = (ASN1Sequence)obj.getObject(); 105 Enumeration e = s.getObjects(); 106 107 while (e.hasMoreElements()) 108 { 109 v.add((DEREncodable)e.nextElement()); 110 } 111 112 return new DERSet(v, false); 113 } 114 } 115 } 116 117 throw new IllegalArgumentException ( 118 "unknown object in getInstanceFromTagged"); 119 } 120 121 public ASN1Set() 122 { 123 } 124 125 public Enumeration getObjects() 126 { 127 return set.elements(); 128 } 129 130 136 public DEREncodable getObjectAt( 137 int index) 138 { 139 return (DEREncodable)set.elementAt(index); 140 } 141 142 147 public int size() 148 { 149 return set.size(); 150 } 151 152 public int hashCode() 153 { 154 Enumeration e = this.getObjects(); 155 int hashCode = 0; 156 157 while (e.hasMoreElements()) 158 { 159 hashCode ^= e.nextElement().hashCode(); 160 } 161 162 return hashCode; 163 } 164 165 public boolean equals( 166 Object o) 167 { 168 if (o == null || !(o instanceof ASN1Set)) 169 { 170 return false; 171 } 172 173 ASN1Set other = (ASN1Set)o; 174 175 if (this.size() != other.size()) 176 { 177 return false; 178 } 179 180 Enumeration s1 = this.getObjects(); 181 Enumeration s2 = other.getObjects(); 182 183 while (s1.hasMoreElements()) 184 { 185 if (!s1.nextElement().equals(s2.nextElement())) 186 { 187 return false; 188 } 189 } 190 191 return true; 192 } 193 194 197 private boolean lessThanOrEqual( 198 byte[] a, 199 byte[] b) 200 { 201 if (a.length <= b.length) 202 { 203 for (int i = 0; i != a.length; i++) 204 { 205 int l = a[i] & 0xff; 206 int r = b[i] & 0xff; 207 208 if (r > l) 209 { 210 return true; 211 } 212 else if (l > r) 213 { 214 return false; 215 } 216 } 217 218 return true; 219 } 220 else 221 { 222 for (int i = 0; i != b.length; i++) 223 { 224 int l = a[i] & 0xff; 225 int r = b[i] & 0xff; 226 227 if (r > l) 228 { 229 return true; 230 } 231 else if (l > r) 232 { 233 return false; 234 } 235 } 236 237 return false; 238 } 239 } 240 241 private byte[] getEncoded( 242 DEREncodable obj) 243 { 244 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 245 ASN1OutputStream aOut = new ASN1OutputStream(bOut); 246 247 try 248 { 249 aOut.writeObject(obj); 250 } 251 catch (IOException e) 252 { 253 throw new IllegalArgumentException ("cannot encode object added to SET"); 254 } 255 256 return bOut.toByteArray(); 257 } 258 259 protected void sort() 260 { 261 if (set.size() > 1) 262 { 263 boolean swapped = true; 264 265 while (swapped) 266 { 267 int index = 0; 268 byte[] a = getEncoded((DEREncodable)set.elementAt(0)); 269 270 swapped = false; 271 272 while (index != set.size() - 1) 273 { 274 byte[] b = getEncoded((DEREncodable)set.elementAt(index + 1)); 275 276 if (lessThanOrEqual(a, b)) 277 { 278 a = b; 279 } 280 else 281 { 282 Object o = set.elementAt(index); 283 284 set.setElementAt(set.elementAt(index + 1), index); 285 set.setElementAt(o, index + 1); 286 287 swapped = true; 288 } 289 290 index++; 291 } 292 } 293 } 294 } 295 296 protected void addObject( 297 DEREncodable obj) 298 { 299 set.addElement(obj); 300 } 301 302 abstract void encode(DEROutputStream out) 303 throws IOException ; 304 } 305 | Popular Tags |