1 package org.bouncycastle.asn1; 2 3 import java.io.IOException; 4 5 import org.bouncycastle.util.Arrays; 6 import org.bouncycastle.util.Strings; 7 8 /** 9 * Carrier class for a DER encoding GeneralString 10 */ 11 public class DERGeneralString 12 extends ASN1Primitive 13 implements ASN1String 14 { 15 private final byte[] string; 16 17 /** 18 * return a GeneralString from the given object. 19 * 20 * @param obj the object we want converted. 21 * @exception IllegalArgumentException if the object cannot be converted. 22 * @return a DERBMPString instance, or null. 23 */ getInstance( Object obj)24 public static DERGeneralString getInstance( 25 Object obj) 26 { 27 if (obj == null || obj instanceof DERGeneralString) 28 { 29 return (DERGeneralString) obj; 30 } 31 32 if (obj instanceof byte[]) 33 { 34 try 35 { 36 return (DERGeneralString)fromByteArray((byte[])obj); 37 } 38 catch (Exception e) 39 { 40 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString()); 41 } 42 } 43 44 throw new IllegalArgumentException("illegal object in getInstance: " 45 + obj.getClass().getName()); 46 } 47 48 /** 49 * return a GeneralString from a tagged object. 50 * 51 * @param obj the tagged object holding the object we want 52 * @param explicit true if the object is meant to be explicitly 53 * tagged false otherwise. 54 * @exception IllegalArgumentException if the tagged object cannot 55 * be converted. 56 * @return a DERGeneralString instance. 57 */ getInstance( ASN1TaggedObject obj, boolean explicit)58 public static DERGeneralString getInstance( 59 ASN1TaggedObject obj, 60 boolean explicit) 61 { 62 ASN1Primitive o = obj.getObject(); 63 64 if (explicit || o instanceof DERGeneralString) 65 { 66 return getInstance(o); 67 } 68 else 69 { 70 return new DERGeneralString(((ASN1OctetString)o).getOctets()); 71 } 72 } 73 DERGeneralString(byte[] string)74 DERGeneralString(byte[] string) 75 { 76 this.string = string; 77 } 78 79 /** 80 * Construct a GeneralString from the passed in String. 81 * 82 * @param string the string to be contained in this object. 83 */ DERGeneralString(String string)84 public DERGeneralString(String string) 85 { 86 this.string = Strings.toByteArray(string); 87 } 88 89 /** 90 * Return a Java String representation of our contained String. 91 * 92 * @return a Java String representing our contents. 93 */ getString()94 public String getString() 95 { 96 return Strings.fromByteArray(string); 97 } 98 toString()99 public String toString() 100 { 101 return getString(); 102 } 103 104 /** 105 * Return a byte array representation of our contained String. 106 * 107 * @return a byte array representing our contents. 108 */ getOctets()109 public byte[] getOctets() 110 { 111 return Arrays.clone(string); 112 } 113 isConstructed()114 boolean isConstructed() 115 { 116 return false; 117 } 118 encodedLength()119 int encodedLength() 120 { 121 return 1 + StreamUtil.calculateBodyLength(string.length) + string.length; 122 } 123 encode(ASN1OutputStream out)124 void encode(ASN1OutputStream out) 125 throws IOException 126 { 127 out.writeEncoded(BERTags.GENERAL_STRING, string); 128 } 129 hashCode()130 public int hashCode() 131 { 132 return Arrays.hashCode(string); 133 } 134 asn1Equals(ASN1Primitive o)135 boolean asn1Equals(ASN1Primitive o) 136 { 137 if (!(o instanceof DERGeneralString)) 138 { 139 return false; 140 } 141 DERGeneralString s = (DERGeneralString)o; 142 143 return Arrays.areEqual(string, s.string); 144 } 145 } 146