001    /*
002     * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003     * 
004     * Permission is hereby granted, free of charge, to any person obtaining a copy of this
005     * software and associated documentation files (the "Software"), to deal in the Software
006     * without restriction, including without limitation the rights to use, copy, modify,
007     * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
008     * permit persons to whom the Software is furnished to do so, subject to the following
009     * conditions:
010     * 
011     * The above copyright notice and this permission notice shall be included in all copies
012     * or substantial portions of the Software.
013     * 
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
015     * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
016     * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
017     * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018     * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
019     * DEALINGS IN THE SOFTWARE.
020     * 
021     */
022    
023    package org.apache.directory.shared.asn1.der;
024    
025    
026    import java.io.ByteArrayOutputStream;
027    import java.io.IOException;
028    
029    
030    /**
031     * DER TaggedObject
032     */
033    public class DERTaggedObject implements DEREncodable
034    {
035        protected int tag;
036    
037        protected boolean empty = false;
038    
039        protected boolean explicit = true;
040    
041        protected DEREncodable obj;
042    
043        private byte[] bytes;
044    
045    
046        /**
047         * create an implicitly tagged object that contains a zero length sequence.
048         */
049        public DERTaggedObject(int tag)
050        {
051            this( false, tag, new DERSequence() );
052        }
053    
054    
055        /**
056         * @param tag
057         *            the tag number for this object.
058         * @param obj
059         *            the tagged object.
060         */
061        public DERTaggedObject(int tag, DEREncodable obj)
062        {
063            this.explicit = true;
064            this.tag = tag;
065            this.obj = obj;
066        }
067    
068    
069        /**
070         * @param explicit
071         *            true if an explicitly tagged object.
072         * @param tag
073         *            the tag number for this object.
074         * @param obj
075         *            the tagged object.
076         */
077        public DERTaggedObject(boolean explicit, int tag, DEREncodable obj)
078        {
079            this.explicit = explicit;
080            this.tag = tag;
081            this.obj = obj;
082        }
083    
084    
085        public DERTaggedObject(boolean explicit, int tag, DEREncodable obj, byte[] bytes)
086        {
087            this.explicit = explicit;
088            this.tag = tag;
089            this.obj = obj;
090    
091            // Copy the byte array
092            this.bytes = new byte[bytes.length];
093            System.arraycopy( bytes, 0, this.bytes, 0, bytes.length );
094        }
095    
096    
097        public byte[] getOctets()
098        {
099            return bytes;
100        }
101    
102    
103        public int getTagNo()
104        {
105            return tag;
106        }
107    
108    
109        /**
110         * return whatever was following the tag.
111         * <p>
112         * Note: tagged objects are generally context dependent if you're trying to
113         * extract a tagged object you should be going via the appropriate
114         * getInstance method.
115         */
116        public DEREncodable getObject()
117        {
118            if ( obj != null )
119            {
120                return obj;
121            }
122    
123            return null;
124        }
125    
126    
127        public void encode( ASN1OutputStream out ) throws IOException
128        {
129            if ( !empty )
130            {
131                ByteArrayOutputStream baos = new ByteArrayOutputStream();
132                ASN1OutputStream aos = new ASN1OutputStream( baos );
133    
134                aos.writeObject( obj );
135                aos.close();
136    
137                byte[] bytes = baos.toByteArray();
138    
139                if ( explicit )
140                {
141                    out.writeEncoded( DERObject.CONSTRUCTED | DERObject.TAGGED | tag, bytes );
142                }
143                else
144                {
145                    // need to mark constructed types
146                    if ( ( bytes[0] & DERObject.CONSTRUCTED ) != 0 )
147                    {
148                        bytes[0] = ( byte ) ( DERObject.CONSTRUCTED | DERObject.TAGGED | tag );
149                    }
150                    else
151                    {
152                        bytes[0] = ( byte ) ( DERObject.TAGGED | tag );
153                    }
154    
155                    out.write( bytes );
156                }
157            }
158            else
159            {
160                out.writeEncoded( DERObject.CONSTRUCTED | DERObject.TAGGED | tag, new byte[0] );
161            }
162        }
163    }