1 package org.bouncycastle.asn1; 2 3 import java.io.IOException; 4 5 /** 6 * DER IA5String object - this is an ascii string. 7 */ 8 public class DERIA5String 9 extends ASN1Object 10 implements DERString 11 { 12 String string; 13 14 /** 15 * return a IA5 string from the passed in object 16 * 17 * @exception IllegalArgumentException if the object cannot be converted. 18 */ 19 public static DERIA5String getInstance( 20 Object obj) 21 { 22 if (obj == null || obj instanceof DERIA5String) 23 { 24 return (DERIA5String)obj; 25 } 26 27 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 28 } 29 30 /** 31 * return an IA5 String from a tagged object. 32 * 33 * @param obj the tagged object holding the object we want 34 * @param explicit true if the object is meant to be explicitly 35 * tagged false otherwise. 36 * @exception IllegalArgumentException if the tagged object cannot 37 * be converted. 38 */ 39 public static DERIA5String getInstance( 40 ASN1TaggedObject obj, 41 boolean explicit) 42 { 43 DERObject o = obj.getObject(); 44 45 if (explicit || o instanceof DERIA5String) 46 { 47 return getInstance(o); 48 } 49 else 50 { 51 return new DERIA5String(((ASN1OctetString)o).getOctets()); 52 } 53 } 54 55 /** 56 * basic constructor - with bytes. 57 */ 58 public DERIA5String( 59 byte[] string) 60 { 61 char[] cs = new char[string.length]; 62 63 for (int i = 0; i != cs.length; i++) 64 { 65 cs[i] = (char)(string[i] & 0xff); 66 } 67 68 this.string = new String(cs); 69 } 70 71 /** 72 * basic constructor - without validation. 73 */ 74 public DERIA5String( 75 String string) 76 { 77 this(string, false); 78 } 79 80 /** 81 * Constructor with optional validation. 82 * 83 * @param string the base string to wrap. 84 * @param validate whether or not to check the string. 85 * @throws IllegalArgumentException if validate is true and the string 86 * contains characters that should not be in an IA5String. 87 */ 88 public DERIA5String( 89 String string, 90 boolean validate) 91 { 92 if (string == null) 93 { 94 throw new NullPointerException("string cannot be null"); 95 } 96 if (validate && !isIA5String(string)) 97 { 98 throw new IllegalArgumentException("string contains illegal characters"); 99 } 100 101 this.string = string; 102 } 103 104 public String getString() 105 { 106 return string; 107 } 108 109 public String toString() 110 { 111 return string; 112 } 113 114 public byte[] getOctets() 115 { 116 char[] cs = string.toCharArray(); 117 byte[] bs = new byte[cs.length]; 118 119 for (int i = 0; i != cs.length; i++) 120 { 121 bs[i] = (byte)cs[i]; 122 } 123 124 return bs; 125 } 126 127 void encode( 128 DEROutputStream out) 129 throws IOException 130 { 131 out.writeEncoded(IA5_STRING, this.getOctets()); 132 } 133 134 public int hashCode() 135 { 136 return this.getString().hashCode(); 137 } 138 139 boolean asn1Equals( 140 DERObject o) 141 { 142 if (!(o instanceof DERIA5String)) 143 { 144 return false; 145 } 146 147 DERIA5String s = (DERIA5String)o; 148 149 return this.getString().equals(s.getString()); 150 } 151 152 /** 153 * return true if the passed in String can be represented without 154 * loss as an IA5String, false otherwise. 155 * 156 * @return true if in printable set, false otherwise. 157 */ 158 public static boolean isIA5String( 159 String str) 160 { 161 for (int i = str.length() - 1; i >= 0; i--) 162 { 163 char ch = str.charAt(i); 164 165 if (ch > 0x007f) 166 { 167 return false; 168 } 169 } 170 171 return true; 172 } 173 } 174