1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 26 public abstract class ASN1Sequence 27 extends DERObject 28 { 29 private Vector seq = new Vector (); 30 31 37 public static ASN1Sequence getInstance( 38 Object obj) 39 { 40 if (obj == null || obj instanceof ASN1Sequence) 41 { 42 return (ASN1Sequence)obj; 43 } 44 45 throw new IllegalArgumentException ("unknown object in getInstance"); 46 } 47 48 64 public static ASN1Sequence getInstance( 65 ASN1TaggedObject obj, 66 boolean explicit) 67 { 68 if (explicit) 69 { 70 if (!obj.isExplicit()) 71 { 72 throw new IllegalArgumentException ("object implicit - explicit expected."); 73 } 74 75 return (ASN1Sequence)obj.getObject(); 76 } 77 else 78 { 79 if (obj.isExplicit()) 85 { 86 if (obj instanceof BERTaggedObject) 87 { 88 return new BERSequence(obj.getObject()); 89 } 90 else 91 { 92 return new DERSequence(obj.getObject()); 93 } 94 } 95 else 96 { 97 if (obj.getObject() instanceof ASN1Sequence) 98 { 99 return (ASN1Sequence)obj.getObject(); 100 } 101 } 102 } 103 104 throw new IllegalArgumentException ( 105 "unknown object in getInstanceFromTagged"); 106 } 107 108 public Enumeration getObjects() 109 { 110 return seq.elements(); 111 } 112 113 119 public DEREncodable getObjectAt( 120 int index) 121 { 122 return (DEREncodable)seq.elementAt(index); 123 } 124 125 130 public int size() 131 { 132 return seq.size(); 133 } 134 135 public int hashCode() 136 { 137 Enumeration e = this.getObjects(); 138 int hashCode = 0; 139 140 while (e.hasMoreElements()) 141 { 142 Object o = e.nextElement(); 143 144 if (o != null) 145 { 146 hashCode ^= o.hashCode(); 147 } 148 } 149 150 return hashCode; 151 } 152 153 public boolean equals( 154 Object o) 155 { 156 if (o == null || !(o instanceof ASN1Sequence)) 157 { 158 return false; 159 } 160 161 ASN1Sequence other = (ASN1Sequence)o; 162 163 if (this.size() != other.size()) 164 { 165 return false; 166 } 167 168 Enumeration s1 = this.getObjects(); 169 Enumeration s2 = other.getObjects(); 170 171 while (s1.hasMoreElements()) 172 { 173 Object o1 = s1.nextElement(); 174 Object o2 = s2.nextElement(); 175 176 if (o1 != null && o2 != null) 177 { 178 if (!o1.equals(o2)) 179 { 180 return false; 181 } 182 } 183 else if (o1 == null && o2 == null) 184 { 185 continue; 186 } 187 else 188 { 189 return false; 190 } 191 } 192 193 return true; 194 } 195 196 protected void addObject( 197 DEREncodable obj) 198 { 199 seq.addElement(obj); 200 } 201 202 abstract void encode(DEROutputStream out) 203 throws IOException ; 204 } 205 | Popular Tags |