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