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