001    package org.apache.commons.ssl.asn1;
002    
003    import java.io.IOException;
004    
005    /** DER VisibleString object. */
006    public class DERVisibleString
007        extends ASN1Object
008        implements DERString {
009        String string;
010    
011        /**
012         * return a Visible String from the passed in object.
013         *
014         * @throws IllegalArgumentException if the object cannot be converted.
015         */
016        public static DERVisibleString getInstance(
017            Object obj) {
018            if (obj == null || obj instanceof DERVisibleString) {
019                return (DERVisibleString) obj;
020            }
021    
022            if (obj instanceof ASN1OctetString) {
023                return new DERVisibleString(((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 a Visible 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 DERVisibleString getInstance(
043            ASN1TaggedObject obj,
044            boolean explicit) {
045            return getInstance(obj.getObject());
046        }
047    
048        /** basic constructor - byte encoded string. */
049        public DERVisibleString(
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 */
061        public DERVisibleString(
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        public byte[] getOctets() {
075            char[] cs = string.toCharArray();
076            byte[] bs = new byte[cs.length];
077    
078            for (int i = 0; i != cs.length; i++) {
079                bs[i] = (byte) cs[i];
080            }
081    
082            return bs;
083        }
084    
085        void encode(
086            DEROutputStream out)
087            throws IOException {
088            out.writeEncoded(VISIBLE_STRING, this.getOctets());
089        }
090    
091        boolean asn1Equals(
092            DERObject o) {
093            if (!(o instanceof DERVisibleString)) {
094                return false;
095            }
096    
097            return this.getString().equals(((DERVisibleString) o).getString());
098        }
099    
100        public int hashCode() {
101            return this.getString().hashCode();
102        }
103    }