001    package org.apache.commons.ssl.asn1;
002    
003    import java.io.IOException;
004    
005    /** DER T61String (also the teletex string) */
006    public class DERT61String
007        extends ASN1Object
008        implements DERString {
009        String string;
010    
011        /**
012         * return a T61 string from the passed in object.
013         *
014         * @throws IllegalArgumentException if the object cannot be converted.
015         */
016        public static DERT61String getInstance(
017            Object obj) {
018            if (obj == null || obj instanceof DERT61String) {
019                return (DERT61String) obj;
020            }
021    
022            if (obj instanceof ASN1OctetString) {
023                return new DERT61String(((ASN1OctetString) obj).getOctets());
024            }
025    
026            if (obj instanceof ASN1TaggedObject) {
027                return getInstance(((ASN1TaggedObject) obj).getObject());
028            }
029    
030            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
031        }
032    
033        /**
034         * return an T61 String from a tagged object.
035         *
036         * @param obj      the tagged object holding the object we want
037         * @param explicit true if the object is meant to be explicitly
038         *                 tagged false otherwise.
039         * @throws IllegalArgumentException if the tagged object cannot
040         *                                  be converted.
041         */
042        public static DERT61String getInstance(
043            ASN1TaggedObject obj,
044            boolean explicit) {
045            return getInstance(obj.getObject());
046        }
047    
048        /** basic constructor - with bytes. */
049        public DERT61String(
050            byte[] string) {
051            char[] cs = new char[string.length];
052    
053            for (int i = 0; i != cs.length; i++) {
054                cs[i] = (char) (string[i] & 0xff);
055            }
056    
057            this.string = new String(cs);
058        }
059    
060        /** basic constructor - with string. */
061        public DERT61String(
062            String string) {
063            this.string = string;
064        }
065    
066        public String getString() {
067            return string;
068        }
069    
070        public String toString() {
071            return string;
072        }
073    
074        void encode(
075            DEROutputStream out)
076            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(
092            DERObject o) {
093            if (!(o instanceof DERT61String)) {
094                return false;
095            }
096    
097            return this.getString().equals(((DERT61String) o).getString());
098        }
099    
100        public int hashCode() {
101            return this.getString().hashCode();
102        }
103    }