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 abstract class ASN1OctetString 26 extends DERObject 27 { 28 byte[] string; 29 30 39 public static ASN1OctetString getInstance( 40 ASN1TaggedObject obj, 41 boolean explicit) 42 { 43 return getInstance(obj.getObject()); 44 } 45 46 52 public static ASN1OctetString getInstance( 53 Object obj) 54 { 55 if (obj == null || obj instanceof ASN1OctetString) 56 { 57 return (ASN1OctetString)obj; 58 } 59 60 if (obj instanceof ASN1TaggedObject) 61 { 62 return getInstance(((ASN1TaggedObject)obj).getObject()); 63 } 64 65 if (obj instanceof ASN1Sequence) 66 { 67 Vector v = new Vector (); 68 Enumeration e = ((ASN1Sequence)obj).getObjects(); 69 70 while (e.hasMoreElements()) 71 { 72 v.addElement(e.nextElement()); 73 } 74 75 return new BERConstructedOctetString(v); 76 } 77 78 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 79 } 80 81 84 public ASN1OctetString( 85 byte[] string) 86 { 87 this.string = string; 88 } 89 90 public ASN1OctetString( 91 DEREncodable obj) 92 { 93 try 94 { 95 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 96 DEROutputStream dOut = new DEROutputStream(bOut); 97 98 dOut.writeObject(obj); 99 dOut.close(); 100 101 this.string = bOut.toByteArray(); 102 } 103 catch (IOException e) 104 { 105 throw new IllegalArgumentException ("Error processing object : " + e.toString()); 106 } 107 } 108 109 public byte[] getOctets() 110 { 111 return string; 112 } 113 114 public int hashCode() 115 { 116 byte[] b = this.getOctets(); 117 int value = 0; 118 119 for (int i = 0; i != b.length; i++) 120 { 121 value ^= (b[i] & 0xff) << (i % 4); 122 } 123 124 return value; 125 } 126 127 public boolean equals( 128 Object o) 129 { 130 if (o == null || !(o instanceof DEROctetString)) 131 { 132 return false; 133 } 134 135 DEROctetString other = (DEROctetString)o; 136 137 byte[] b1 = other.getOctets(); 138 byte[] b2 = this.getOctets(); 139 140 if (b1.length != b2.length) 141 { 142 return false; 143 } 144 145 for (int i = 0; i != b1.length; i++) 146 { 147 if (b1[i] != b2[i]) 148 { 149 return false; 150 } 151 } 152 153 return true; 154 } 155 156 abstract void encode(DEROutputStream out) 157 throws IOException ; 158 } 159 | Popular Tags |