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.ByteArrayInputStream;
022    import java.io.ByteArrayOutputStream;
023    import java.io.IOException;
024    
025    /**
026     * Base class for an application specific object
027     */
028    public class DERApplicationSpecific
029        extends DERObject
030    {
031        private int       tag;
032        private byte[]    octets;
033    
034        public DERApplicationSpecific(
035            int        tag,
036            byte[]    octets)
037        {
038            this.tag = tag;
039            this.octets = octets;
040        }
041    
042        public DERApplicationSpecific(
043            int                  tag,
044            DEREncodable         object)
045            throws IOException
046        {
047            this.tag = tag | DERTags.CONSTRUCTED;
048    
049            ByteArrayOutputStream baos = new ByteArrayOutputStream();
050            DEROutputStream dos = new DEROutputStream(baos);
051    
052            dos.writeObject(object);
053    
054            this.octets = baos.toByteArray();
055        }
056    
057        public boolean isConstructed()
058        {
059            return (tag & DERTags.CONSTRUCTED) != 0;
060        }
061    
062        public byte[] getContents()
063        {
064            return octets;
065        }
066    
067        public int getApplicationTag()
068        {
069            return tag & 0x1F;
070        }
071    
072        public DERObject getObject()
073            throws IOException
074        {
075            return new ASN1InputStream(new ByteArrayInputStream(getContents())).readObject();
076        }
077    
078        /* (non-Javadoc)
079         * @see org.apache.geronimo.util.asn1.DERObject#encode(org.apache.geronimo.util.asn1.DEROutputStream)
080         */
081        void encode(DEROutputStream out) throws IOException
082        {
083            out.writeEncoded(DERTags.APPLICATION | tag, octets);
084        }
085    
086        public boolean equals(
087                Object o)
088        {
089            if ((o == null) || !(o instanceof DERApplicationSpecific))
090            {
091                return false;
092            }
093    
094            DERApplicationSpecific other = (DERApplicationSpecific)o;
095    
096            if (tag != other.tag)
097            {
098                return false;
099            }
100    
101            if (octets.length != other.octets.length)
102            {
103                return false;
104            }
105    
106            for (int i = 0; i < octets.length; i++)
107            {
108                if (octets[i] != other.octets[i])
109                {
110                    return false;
111                }
112            }
113    
114            return true;
115        }
116    
117        public int hashCode()
118        {
119            byte[]  b = this.getContents();
120            int     value = 0;
121    
122            for (int i = 0; i != b.length; i++)
123            {
124                value ^= (b[i] & 0xff) << (i % 4);
125            }
126    
127            return value ^ this.getApplicationTag();
128        }
129    }