1package org.bouncycastle.asn1; 2 3import java.io.IOException; 4 5import org.bouncycastle.util.Arrays; 6import org.bouncycastle.util.Strings; 7 8public class DERGeneralString 9 extends ASN1Primitive 10 implements ASN1String 11{ 12 private byte[] string; 13 14 public static DERGeneralString getInstance( 15 Object obj) 16 { 17 if (obj == null || obj instanceof DERGeneralString) 18 { 19 return (DERGeneralString) obj; 20 } 21 22 if (obj instanceof byte[]) 23 { 24 try 25 { 26 return (DERGeneralString)fromByteArray((byte[])obj); 27 } 28 catch (Exception e) 29 { 30 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString()); 31 } 32 } 33 34 throw new IllegalArgumentException("illegal object in getInstance: " 35 + obj.getClass().getName()); 36 } 37 38 public static DERGeneralString getInstance( 39 ASN1TaggedObject obj, 40 boolean explicit) 41 { 42 ASN1Primitive o = obj.getObject(); 43 44 if (explicit || o instanceof DERGeneralString) 45 { 46 return getInstance(o); 47 } 48 else 49 { 50 return new DERGeneralString(((ASN1OctetString)o).getOctets()); 51 } 52 } 53 54 DERGeneralString(byte[] string) 55 { 56 this.string = string; 57 } 58 59 public DERGeneralString(String string) 60 { 61 this.string = Strings.toByteArray(string); 62 } 63 64 public String getString() 65 { 66 return Strings.fromByteArray(string); 67 } 68 69 public String toString() 70 { 71 return getString(); 72 } 73 74 public byte[] getOctets() 75 { 76 return Arrays.clone(string); 77 } 78 79 boolean isConstructed() 80 { 81 return false; 82 } 83 84 int encodedLength() 85 { 86 return 1 + StreamUtil.calculateBodyLength(string.length) + string.length; 87 } 88 89 void encode(ASN1OutputStream out) 90 throws IOException 91 { 92 out.writeEncoded(BERTags.GENERAL_STRING, string); 93 } 94 95 public int hashCode() 96 { 97 return Arrays.hashCode(string); 98 } 99 100 boolean asn1Equals(ASN1Primitive o) 101 { 102 if (!(o instanceof DERGeneralString)) 103 { 104 return false; 105 } 106 DERGeneralString s = (DERGeneralString)o; 107 108 return Arrays.areEqual(string, s.string); 109 } 110} 111