1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 23 26 public class DERUniversalString 27 extends DERObject 28 implements DERString 29 { 30 private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 31 private byte[] string; 32 33 38 public static DERUniversalString getInstance( 39 Object obj) 40 { 41 if (obj == null || obj instanceof DERUniversalString) 42 { 43 return (DERUniversalString)obj; 44 } 45 46 if (obj instanceof ASN1OctetString) 47 { 48 return new DERUniversalString(((ASN1OctetString)obj).getOctets()); 49 } 50 51 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 52 } 53 54 63 public static DERUniversalString getInstance( 64 ASN1TaggedObject obj, 65 boolean explicit) 66 { 67 return getInstance(obj.getObject()); 68 } 69 70 73 public DERUniversalString( 74 byte[] string) 75 { 76 this.string = string; 77 } 78 79 public String getString() 80 { 81 StringBuffer buf = new StringBuffer ("#"); 82 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 83 ASN1OutputStream aOut = new ASN1OutputStream(bOut); 84 85 try 86 { 87 aOut.writeObject(this); 88 } 89 catch (IOException e) 90 { 91 throw new RuntimeException ("internal error encoding BitString"); 92 } 93 94 byte[] string = bOut.toByteArray(); 95 96 for (int i = 0; i != string.length; i++) 97 { 98 buf.append(table[(string[i] >>> 4) % 0xf]); 99 buf.append(table[string[i] & 0xf]); 100 } 101 102 return buf.toString(); 103 } 104 105 public byte[] getOctets() 106 { 107 return string; 108 } 109 110 void encode( 111 DEROutputStream out) 112 throws IOException 113 { 114 out.writeEncoded(UNIVERSAL_STRING, this.getOctets()); 115 } 116 117 public boolean equals( 118 Object o) 119 { 120 if ((o == null) || !(o instanceof DERUniversalString)) 121 { 122 return false; 123 } 124 125 return this.getString().equals(((DERUniversalString)o).getString()); 126 } 127 128 public int hashCode() 129 { 130 return this.getString().hashCode(); 131 } 132 } 133 | Popular Tags |