001: package org.bouncycastle.asn1;
002:
003: import java.io.IOException;
004:
005: /**
006: * DER T61String (also the teletex string)
007: */
008: public class DERT61String extends ASN1Object implements DERString {
009: String string;
010:
011: /**
012: * return a T61 string from the passed in object.
013: *
014: * @exception IllegalArgumentException if the object cannot be converted.
015: */
016: public static DERT61String getInstance(Object obj) {
017: if (obj == null || obj instanceof DERT61String) {
018: return (DERT61String) obj;
019: }
020:
021: if (obj instanceof ASN1OctetString) {
022: return new DERT61String(((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 T61 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 DERT61String getInstance(ASN1TaggedObject obj,
044: boolean explicit) {
045: return getInstance(obj.getObject());
046: }
047:
048: /**
049: * basic constructor - with bytes.
050: */
051: public DERT61String(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 - with string.
063: */
064: public DERT61String(String string) {
065: this .string = string;
066: }
067:
068: public String getString() {
069: return string;
070: }
071:
072: public String toString() {
073: return string;
074: }
075:
076: void encode(DEROutputStream out) throws IOException {
077: out.writeEncoded(T61_STRING, this .getOctets());
078: }
079:
080: public byte[] getOctets() {
081: char[] cs = string.toCharArray();
082: byte[] bs = new byte[cs.length];
083:
084: for (int i = 0; i != cs.length; i++) {
085: bs[i] = (byte) cs[i];
086: }
087:
088: return bs;
089: }
090:
091: boolean asn1Equals(DERObject o) {
092: if (!(o instanceof DERT61String)) {
093: return false;
094: }
095:
096: return this .getString().equals(((DERT61String) o).getString());
097: }
098:
099: public int hashCode() {
100: return this.getString().hashCode();
101: }
102: }
|