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