001    package org.apache.commons.ssl.asn1;
002    
003    import java.io.ByteArrayOutputStream;
004    import java.io.IOException;
005    
006    /** DER UniversalString object. */
007    public class DERUniversalString
008        extends ASN1Object
009        implements DERString {
010        private static final char[] table = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
011        private byte[] string;
012    
013        /**
014         * return a Universal String from the passed in object.
015         *
016         * @throws IllegalArgumentException if the object cannot be converted.
017         */
018        public static DERUniversalString getInstance(
019            Object obj) {
020            if (obj == null || obj instanceof DERUniversalString) {
021                return (DERUniversalString) obj;
022            }
023    
024            if (obj instanceof ASN1OctetString) {
025                return new DERUniversalString(((ASN1OctetString) obj).getOctets());
026            }
027    
028            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
029        }
030    
031        /**
032         * return a Universal String from a tagged object.
033         *
034         * @param obj      the tagged object holding the object we want
035         * @param explicit true if the object is meant to be explicitly
036         *                 tagged false otherwise.
037         * @throws IllegalArgumentException if the tagged object cannot
038         *                                  be converted.
039         */
040        public static DERUniversalString getInstance(
041            ASN1TaggedObject obj,
042            boolean explicit) {
043            return getInstance(obj.getObject());
044        }
045    
046        /** basic constructor - byte encoded string. */
047        public DERUniversalString(
048            byte[] string) {
049            this.string = string;
050        }
051    
052        public String getString() {
053            StringBuffer buf = new StringBuffer("#");
054            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
055            ASN1OutputStream aOut = new ASN1OutputStream(bOut);
056    
057            try {
058                aOut.writeObject(this);
059            }
060            catch (IOException e) {
061                throw new RuntimeException("internal error encoding BitString");
062            }
063    
064            byte[] string = bOut.toByteArray();
065    
066            for (int i = 0; i != string.length; i++) {
067                buf.append(table[(string[i] >>> 4) & 0xf]);
068                buf.append(table[string[i] & 0xf]);
069            }
070    
071            return buf.toString();
072        }
073    
074        public String toString() {
075            return getString();
076        }
077    
078        public byte[] getOctets() {
079            return string;
080        }
081    
082        void encode(
083            DEROutputStream out)
084            throws IOException {
085            out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
086        }
087    
088        boolean asn1Equals(
089            DERObject o) {
090            if (!(o instanceof DERUniversalString)) {
091                return false;
092            }
093    
094            return this.getString().equals(((DERUniversalString) o).getString());
095        }
096    
097        public int hashCode() {
098            return this.getString().hashCode();
099        }
100    }