001: package org.bouncycastle.asn1;
002:
003: import java.io.IOException;
004:
005: /**
006: * DER IA5String object - this is an ascii string.
007: */
008: public class DERIA5String extends ASN1Object implements DERString {
009: String string;
010:
011: /**
012: * return a IA5 string from the passed in object
013: *
014: * @exception IllegalArgumentException if the object cannot be converted.
015: */
016: public static DERIA5String getInstance(Object obj) {
017: if (obj == null || obj instanceof DERIA5String) {
018: return (DERIA5String) obj;
019: }
020:
021: if (obj instanceof ASN1OctetString) {
022: return new DERIA5String(((ASN1OctetString) obj).getOctets());
023: }
024:
025: if (obj instanceof ASN1TaggedObject) {
026: return getInstance(((ASN1TaggedObject) obj).getObject());
027: }
028:
029: throw new IllegalArgumentException(
030: "illegal object in getInstance: "
031: + obj.getClass().getName());
032: }
033:
034: /**
035: * return an IA5 String from a tagged object.
036: *
037: * @param obj the tagged object holding the object we want
038: * @param explicit true if the object is meant to be explicitly
039: * tagged false otherwise.
040: * @exception IllegalArgumentException if the tagged object cannot
041: * be converted.
042: */
043: public static DERIA5String getInstance(ASN1TaggedObject obj,
044: boolean explicit) {
045: return getInstance(obj.getObject());
046: }
047:
048: /**
049: * basic constructor - with bytes.
050: */
051: public DERIA5String(byte[] string) {
052: char[] cs = new char[string.length];
053:
054: for (int i = 0; i != cs.length; i++) {
055: cs[i] = (char) (string[i] & 0xff);
056: }
057:
058: this .string = new String(cs);
059: }
060:
061: /**
062: * basic constructor - without validation.
063: */
064: public DERIA5String(String string) {
065: this (string, false);
066: }
067:
068: /**
069: * Constructor with optional validation.
070: *
071: * @param string the base string to wrap.
072: * @param validate whether or not to check the string.
073: * @throws IllegalArgumentException if validate is true and the string
074: * contains characters that should not be in an IA5String.
075: */
076: public DERIA5String(String string, boolean validate) {
077: if (validate && !isIA5String(string)) {
078: throw new IllegalArgumentException(
079: "string contains illegal characters");
080: }
081:
082: this .string = string;
083: }
084:
085: public String getString() {
086: return string;
087: }
088:
089: public String toString() {
090: return string;
091: }
092:
093: public byte[] getOctets() {
094: char[] cs = string.toCharArray();
095: byte[] bs = new byte[cs.length];
096:
097: for (int i = 0; i != cs.length; i++) {
098: bs[i] = (byte) cs[i];
099: }
100:
101: return bs;
102: }
103:
104: void encode(DEROutputStream out) throws IOException {
105: out.writeEncoded(IA5_STRING, this .getOctets());
106: }
107:
108: public int hashCode() {
109: return this .getString().hashCode();
110: }
111:
112: boolean asn1Equals(DERObject o) {
113: if (!(o instanceof DERIA5String)) {
114: return false;
115: }
116:
117: DERIA5String s = (DERIA5String) o;
118:
119: return this .getString().equals(s.getString());
120: }
121:
122: /**
123: * return true if the passed in String can be represented without
124: * loss as an IA5String, false otherwise.
125: *
126: * @return true if in printable set, false otherwise.
127: */
128: public static boolean isIA5String(String str) {
129: for (int i = str.length() - 1; i >= 0; i--) {
130: char ch = str.charAt(i);
131:
132: if (ch > 0x007f) {
133: return false;
134: }
135: }
136:
137: return true;
138: }
139: }
|