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    import java.util.Enumeration;
023    
024    public class DERSequence
025        extends ASN1Sequence
026    {
027        /**
028         * create an empty sequence
029         */
030        public DERSequence()
031        {
032        }
033    
034        /**
035         * create a sequence containing one object
036         */
037        public DERSequence(
038            DEREncodable    obj)
039        {
040            this.addObject(obj);
041        }
042    
043        /**
044         * create a sequence containing a vector of objects.
045         */
046        public DERSequence(
047            DEREncodableVector   v)
048        {
049            for (int i = 0; i != v.size(); i++)
050            {
051                this.addObject(v.get(i));
052            }
053        }
054    
055        /**
056         * create a sequence containing an array of objects.
057         */
058        public DERSequence(
059            ASN1Encodable[]   a)
060        {
061            for (int i = 0; i != a.length; i++)
062            {
063                this.addObject(a[i]);
064            }
065        }
066    
067        /*
068         * A note on the implementation:
069         * <p>
070         * As DER requires the constructed, definite-length model to
071         * be used for structured types, this varies slightly from the
072         * ASN.1 descriptions given. Rather than just outputing SEQUENCE,
073         * we also have to specify CONSTRUCTED, and the objects length.
074         */
075        void encode(
076            DEROutputStream out)
077            throws IOException
078        {
079            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
080            DEROutputStream         dOut = new DEROutputStream(bOut);
081            Enumeration             e = this.getObjects();
082    
083            while (e.hasMoreElements())
084            {
085                Object    obj = e.nextElement();
086    
087                dOut.writeObject(obj);
088            }
089    
090            dOut.close();
091    
092            byte[]  bytes = bOut.toByteArray();
093    
094            out.writeEncoded(SEQUENCE | CONSTRUCTED, bytes);
095        }
096    }