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