001package org.apache.commons.ssl.org.bouncycastle.asn1; 002 003import java.io.IOException; 004 005import org.bouncycastle.util.Arrays; 006import org.bouncycastle.util.Strings; 007 008/** 009 * DER T61String (also the teletex string), try not to use this if you don't need to. The standard support the encoding for 010 * this has been withdrawn. 011 */ 012public class DERT61String 013 extends ASN1Primitive 014 implements ASN1String 015{ 016 private byte[] string; 017 018 /** 019 * return a T61 string from the passed in object. 020 * 021 * @param obj a DERT61String or an object that can be converted into one. 022 * @exception IllegalArgumentException if the object cannot be converted. 023 * @return a DERT61String instance, or null 024 */ 025 public static DERT61String getInstance( 026 Object obj) 027 { 028 if (obj == null || obj instanceof DERT61String) 029 { 030 return (DERT61String)obj; 031 } 032 033 if (obj instanceof byte[]) 034 { 035 try 036 { 037 return (DERT61String)fromByteArray((byte[])obj); 038 } 039 catch (Exception e) 040 { 041 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString()); 042 } 043 } 044 045 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 046 } 047 048 /** 049 * return an T61 String from a tagged object. 050 * 051 * @param obj the tagged object holding the object we want 052 * @param explicit true if the object is meant to be explicitly 053 * tagged false otherwise. 054 * @exception IllegalArgumentException if the tagged object cannot 055 * be converted. 056 * @return a DERT61String instance, or null 057 */ 058 public static DERT61String getInstance( 059 ASN1TaggedObject obj, 060 boolean explicit) 061 { 062 ASN1Primitive o = obj.getObject(); 063 064 if (explicit || o instanceof DERT61String) 065 { 066 return getInstance(o); 067 } 068 else 069 { 070 return new DERT61String(ASN1OctetString.getInstance(o).getOctets()); 071 } 072 } 073 074 /** 075 * basic constructor - string encoded as a sequence of bytes. 076 * 077 * @param string the byte encoding of the string to be wrapped. 078 */ 079 public DERT61String( 080 byte[] string) 081 { 082 this.string = string; 083 } 084 085 /** 086 * basic constructor - with string 8 bit assumed. 087 * 088 * @param string the string to be wrapped. 089 */ 090 public DERT61String( 091 String string) 092 { 093 this(Strings.toByteArray(string)); 094 } 095 096 /** 097 * Decode the encoded string and return it, 8 bit encoding assumed. 098 * @return the decoded String 099 */ 100 public String getString() 101 { 102 return Strings.fromByteArray(string); 103 } 104 105 public String toString() 106 { 107 return getString(); 108 } 109 110 boolean isConstructed() 111 { 112 return false; 113 } 114 115 int encodedLength() 116 { 117 return 1 + StreamUtil.calculateBodyLength(string.length) + string.length; 118 } 119 120 void encode( 121 ASN1OutputStream out) 122 throws IOException 123 { 124 out.writeEncoded(BERTags.T61_STRING, string); 125 } 126 127 /** 128 * Return the encoded string as a byte array. 129 * @return the actual bytes making up the encoded body of the T61 string. 130 */ 131 public byte[] getOctets() 132 { 133 return Arrays.clone(string); 134 } 135 136 boolean asn1Equals( 137 ASN1Primitive o) 138 { 139 if (!(o instanceof DERT61String)) 140 { 141 return false; 142 } 143 144 return Arrays.areEqual(string, ((DERT61String)o).string); 145 } 146 147 public int hashCode() 148 { 149 return Arrays.hashCode(string); 150 } 151}