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    /**
025     * A DER encoded set object
026     */
027    public class DERSet
028        extends ASN1Set
029    {
030        /**
031         * create an empty set
032         */
033        public DERSet()
034        {
035        }
036    
037        /**
038         * @param obj - a single object that makes up the set.
039         */
040        public DERSet(
041            DEREncodable   obj)
042        {
043            this.addObject(obj);
044        }
045    
046        /**
047         * @param v - a vector of objects making up the set.
048         */
049        public DERSet(
050            DEREncodableVector   v)
051        {
052            this(v, true);
053        }
054    
055        /**
056         * create a set from an array of objects.
057         */
058        public DERSet(
059            ASN1Encodable[]   a)
060        {
061            for (int i = 0; i != a.length; i++)
062            {
063                this.addObject(a[i]);
064            }
065    
066            this.sort();
067        }
068    
069        /**
070         * @param v - a vector of objects making up the set.
071         */
072        DERSet(
073            DEREncodableVector   v,
074            boolean              needsSorting)
075        {
076            for (int i = 0; i != v.size(); i++)
077            {
078                this.addObject(v.get(i));
079            }
080    
081            if (needsSorting)
082            {
083                this.sort();
084            }
085        }
086    
087        /*
088         * A note on the implementation:
089         * <p>
090         * As DER requires the constructed, definite-length model to
091         * be used for structured types, this varies slightly from the
092         * ASN.1 descriptions given. Rather than just outputing SET,
093         * we also have to specify CONSTRUCTED, and the objects length.
094         */
095        void encode(
096            DEROutputStream out)
097            throws IOException
098        {
099            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
100            DEROutputStream         dOut = new DEROutputStream(bOut);
101            Enumeration             e = this.getObjects();
102    
103            while (e.hasMoreElements())
104            {
105                Object    obj = e.nextElement();
106    
107                dOut.writeObject(obj);
108            }
109    
110            dOut.close();
111    
112            byte[]  bytes = bOut.toByteArray();
113    
114            out.writeEncoded(SET | CONSTRUCTED, bytes);
115        }
116    }