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 DERUTF8String 27 extends DERObject 28 implements DERString 29 { 30 String string; 31 32 37 public static DERUTF8String getInstance( 38 Object obj) 39 { 40 if (obj == null || obj instanceof DERUTF8String) 41 { 42 return (DERUTF8String)obj; 43 } 44 45 if (obj instanceof ASN1OctetString) 46 { 47 return new DERUTF8String(((ASN1OctetString)obj).getOctets()); 48 } 49 50 if (obj instanceof ASN1TaggedObject) 51 { 52 return getInstance(((ASN1TaggedObject)obj).getObject()); 53 } 54 55 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 56 } 57 58 67 public static DERUTF8String getInstance( 68 ASN1TaggedObject obj, 69 boolean explicit) 70 { 71 return getInstance(obj.getObject()); 72 } 73 74 77 DERUTF8String( 78 byte[] string) 79 { 80 int i = 0; 81 int length = 0; 82 83 while (i < string.length) 84 { 85 length++; 86 if ((string[i] & 0xe0) == 0xe0) 87 { 88 i += 3; 89 } 90 else if ((string[i] & 0xc0) == 0xc0) 91 { 92 i += 2; 93 } 94 else 95 { 96 i += 1; 97 } 98 } 99 100 char[] cs = new char[length]; 101 102 i = 0; 103 length = 0; 104 105 while (i < string.length) 106 { 107 char ch; 108 109 if ((string[i] & 0xe0) == 0xe0) 110 { 111 ch = (char)(((string[i] & 0x1f) << 12) 112 | ((string[i + 1] & 0x3f) << 6) | (string[i + 2] & 0x3f)); 113 i += 3; 114 } 115 else if ((string[i] & 0xc0) == 0xc0) 116 { 117 ch = (char)(((string[i] & 0x3f) << 6) | (string[i + 1] & 0x3f)); 118 i += 2; 119 } 120 else 121 { 122 ch = (char)(string[i] & 0xff); 123 i += 1; 124 } 125 126 cs[length++] = ch; 127 } 128 129 this.string = new String (cs); 130 } 131 132 135 public DERUTF8String( 136 String string) 137 { 138 this.string = string; 139 } 140 141 public String getString() 142 { 143 return string; 144 } 145 146 public int hashCode() 147 { 148 return this.getString().hashCode(); 149 } 150 151 public boolean equals( 152 Object o) 153 { 154 if (!(o instanceof DERUTF8String)) 155 { 156 return false; 157 } 158 159 DERUTF8String s = (DERUTF8String)o; 160 161 return this.getString().equals(s.getString()); 162 } 163 164 void encode( 165 DEROutputStream out) 166 throws IOException 167 { 168 char[] c = string.toCharArray(); 169 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 170 171 for (int i = 0; i != c.length; i++) 172 { 173 char ch = c[i]; 174 175 if (ch < 0x0080) 176 { 177 bOut.write(ch); 178 } 179 else if (ch < 0x0800) 180 { 181 bOut.write(0xc0 | (ch >> 6)); 182 bOut.write(0x80 | (ch & 0x3f)); 183 } 184 else 185 { 186 bOut.write(0xe0 | (ch >> 12)); 187 bOut.write(0x80 | ((ch >> 6) & 0x3F)); 188 bOut.write(0x80 | (ch & 0x3F)); 189 } 190 } 191 192 out.writeEncoded(UTF8_STRING, bOut.toByteArray()); 193 } 194 } 195 | Popular Tags |