001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1;
019    
020    import java.io.ByteArrayOutputStream;
021    import java.io.IOException;
022    
023    /**
024     * DER TaggedObject - in ASN.1 nottation this is any object proceeded by
025     * a [n] where n is some number - these are assume to follow the construction
026     * rules (as with sequences).
027     */
028    public class DERTaggedObject
029        extends ASN1TaggedObject
030    {
031        /**
032         * @param tagNo the tag number for this object.
033         * @param obj the tagged object.
034         */
035        public DERTaggedObject(
036            int             tagNo,
037            DEREncodable    obj)
038        {
039            super(tagNo, obj);
040        }
041    
042        /**
043         * @param explicit true if an explicitly tagged object.
044         * @param tagNo the tag number for this object.
045         * @param obj the tagged object.
046         */
047        public DERTaggedObject(
048            boolean         explicit,
049            int             tagNo,
050            DEREncodable    obj)
051        {
052            super(explicit, tagNo, obj);
053        }
054    
055        /**
056         * create an implicitly tagged object that contains a zero
057         * length sequence.
058         */
059        public DERTaggedObject(
060            int             tagNo)
061        {
062            super(false, tagNo, new DERSequence());
063        }
064    
065        void encode(
066            DEROutputStream  out)
067            throws IOException
068        {
069            if (!empty)
070            {
071                ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
072                DEROutputStream         dOut = new DEROutputStream(bOut);
073    
074                dOut.writeObject(obj);
075                dOut.close();
076    
077                byte[]  bytes = bOut.toByteArray();
078    
079                if (explicit)
080                {
081                    out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, bytes);
082                }
083                else
084                {
085                    //
086                    // need to mark constructed types...
087                    //
088                    if ((bytes[0] & CONSTRUCTED) != 0)
089                    {
090                        bytes[0] = (byte)(CONSTRUCTED | TAGGED | tagNo);
091                    }
092                    else
093                    {
094                        bytes[0] = (byte)(TAGGED | tagNo);
095                    }
096    
097                    out.write(bytes);
098                }
099            }
100            else
101            {
102                out.writeEncoded(CONSTRUCTED | TAGGED | tagNo, new byte[0]);
103            }
104        }
105    }