1 19 20 package com.maverick.crypto.asn1; 21 22 import java.io.IOException ; 23 24 27 public class DERBMPString 28 extends DERObject 29 implements DERString 30 { 31 String string; 32 33 39 public static DERBMPString getInstance( 40 Object obj) 41 { 42 if (obj == null || obj instanceof DERBMPString) 43 { 44 return (DERBMPString)obj; 45 } 46 47 if (obj instanceof ASN1OctetString) 48 { 49 return new DERBMPString(((ASN1OctetString)obj).getOctets()); 50 } 51 52 if (obj instanceof ASN1TaggedObject) 53 { 54 return getInstance(((ASN1TaggedObject)obj).getObject()); 55 } 56 57 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 58 } 59 60 69 public static DERBMPString getInstance( 70 ASN1TaggedObject obj, 71 boolean explicit) 72 { 73 return getInstance(obj.getObject()); 74 } 75 76 77 80 public DERBMPString( 81 byte[] string) 82 { 83 char[] cs = new char[string.length / 2]; 84 85 for (int i = 0; i != cs.length; i++) 86 { 87 cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff)); 88 } 89 90 this.string = new String (cs); 91 } 92 93 96 public DERBMPString( 97 String string) 98 { 99 this.string = string; 100 } 101 102 public String getString() 103 { 104 return string; 105 } 106 107 public int hashCode() 108 { 109 return this.getString().hashCode(); 110 } 111 112 public boolean equals( 113 Object o) 114 { 115 if (!(o instanceof DERBMPString)) 116 { 117 return false; 118 } 119 120 DERBMPString s = (DERBMPString)o; 121 122 return this.getString().equals(s.getString()); 123 } 124 125 void encode( 126 DEROutputStream out) 127 throws IOException 128 { 129 char[] c = string.toCharArray(); 130 byte[] b = new byte[c.length * 2]; 131 132 for (int i = 0; i != c.length; i++) 133 { 134 b[2 * i] = (byte)(c[i] >> 8); 135 b[2 * i + 1] = (byte)c[i]; 136 } 137 138 out.writeEncoded(BMP_STRING, b); 139 } 140 } 141 | Popular Tags |