001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.asn1;
020    
021    import java.io.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.util.Enumeration;
024    import java.util.Vector;
025    
026    public abstract class ASN1OctetString
027        extends DERObject
028    {
029        byte[]  string;
030    
031        /**
032         * return an Octet 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         * @exception IllegalArgumentException if the tagged object cannot
038         *              be converted.
039         */
040        public static ASN1OctetString getInstance(
041            ASN1TaggedObject    obj,
042            boolean             explicit)
043        {
044            return getInstance(obj.getObject());
045        }
046    
047        /**
048         * return an Octet String from the given object.
049         *
050         * @param obj the object we want converted.
051         * @exception IllegalArgumentException if the object cannot be converted.
052         */
053        public static ASN1OctetString getInstance(
054            Object  obj)
055        {
056            if (obj == null || obj instanceof ASN1OctetString)
057            {
058                return (ASN1OctetString)obj;
059            }
060    
061            if (obj instanceof ASN1TaggedObject)
062            {
063                return getInstance(((ASN1TaggedObject)obj).getObject());
064            }
065    
066            if (obj instanceof ASN1Sequence)
067            {
068                Vector      v = new Vector();
069                Enumeration e = ((ASN1Sequence)obj).getObjects();
070    
071                while (e.hasMoreElements())
072                {
073                    v.addElement(e.nextElement());
074                }
075    
076                return new BERConstructedOctetString(v);
077            }
078    
079            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
080        }
081    
082        /**
083         * @param string the octets making up the octet string.
084         */
085        public ASN1OctetString(
086            byte[]  string)
087        {
088            this.string = string;
089        }
090    
091        public ASN1OctetString(
092            DEREncodable obj)
093        {
094            try
095            {
096                ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
097                DEROutputStream         dOut = new DEROutputStream(bOut);
098    
099                dOut.writeObject(obj);
100                dOut.close();
101    
102                this.string = bOut.toByteArray();
103            }
104            catch (IOException e)
105            {
106                throw new IllegalArgumentException("Error processing object : " + e.toString());
107            }
108        }
109    
110        public byte[] getOctets()
111        {
112            return string;
113        }
114    
115        public int hashCode()
116        {
117            byte[]  b = this.getOctets();
118            int     value = 0;
119    
120            for (int i = 0; i != b.length; i++)
121            {
122                value ^= (b[i] & 0xff) << (i % 4);
123            }
124    
125            return value;
126        }
127    
128        public boolean equals(
129            Object  o)
130        {
131            if (o == null || !(o instanceof DEROctetString))
132            {
133                return false;
134            }
135    
136            DEROctetString  other = (DEROctetString)o;
137    
138            byte[] b1 = other.getOctets();
139            byte[] b2 = this.getOctets();
140    
141            if (b1.length != b2.length)
142            {
143                return false;
144            }
145    
146            for (int i = 0; i != b1.length; i++)
147            {
148                if (b1[i] != b2[i])
149                {
150                    return false;
151                }
152            }
153    
154            return true;
155        }
156    
157        abstract void encode(DEROutputStream out)
158            throws IOException;
159    }