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.FilterOutputStream;
022    import java.io.IOException;
023    import java.io.OutputStream;
024    
025    public class DEROutputStream
026        extends FilterOutputStream implements DERTags
027    {
028        public DEROutputStream(
029            OutputStream    os)
030        {
031            super(os);
032        }
033    
034        private void writeLength(
035            int length)
036            throws IOException
037        {
038            if (length > 127)
039            {
040                int size = 1;
041                int val = length;
042    
043                while ((val >>>= 8) != 0)
044                {
045                    size++;
046                }
047    
048                write((byte)(size | 0x80));
049    
050                for (int i = (size - 1) * 8; i >= 0; i -= 8)
051                {
052                    write((byte)(length >> i));
053                }
054            }
055            else
056            {
057                write((byte)length);
058            }
059        }
060    
061        void writeEncoded(
062            int     tag,
063            byte[]  bytes)
064            throws IOException
065        {
066            write(tag);
067            writeLength(bytes.length);
068            write(bytes);
069        }
070    
071        protected void writeNull()
072            throws IOException
073        {
074            write(NULL);
075            write(0x00);
076        }
077    
078        public void writeObject(
079            Object    obj)
080            throws IOException
081        {
082            if (obj == null)
083            {
084                writeNull();
085            }
086            else if (obj instanceof DERObject)
087            {
088                ((DERObject)obj).encode(this);
089            }
090            else if (obj instanceof DEREncodable)
091            {
092                ((DEREncodable)obj).getDERObject().encode(this);
093            }
094            else
095            {
096                throw new IOException("object not DEREncodable");
097            }
098        }
099    }