1 /*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 package org.apache.asn1.der;
19
20 import java.io.IOException;
21 import java.util.Enumeration;
22
23 /**
24 * BER TaggedObject
25 */
26 public class BERTaggedObject extends DERTaggedObject
27 {
28 /**
29 * @param tag the tag number for this object.
30 * @param obj the tagged object.
31 */
32 public BERTaggedObject( int tag, DEREncodable obj )
33 {
34 super( tag, obj );
35 }
36
37 /**
38 * @param true if an explicitly tagged object.
39 * @param tag the tag number for this object.
40 * @param obj the tagged object.
41 */
42 public BERTaggedObject( boolean explicit, int tag, DEREncodable obj )
43 {
44 super( explicit, tag, obj );
45 }
46
47 public void encode( ASN1OutputStream out )
48 throws IOException
49 {
50 out.write( DERObject.CONSTRUCTED | DERObject.TAGGED | tag );
51 out.write( DERObject.TAGGED );
52
53 if ( !empty )
54 {
55 if ( !explicit )
56 {
57 if ( obj instanceof DEROctetString )
58 {
59 Enumeration e;
60
61 if ( obj instanceof BERConstructedOctetString )
62 {
63 e = ( (BERConstructedOctetString)obj ).getObjects();
64 }
65 else
66 {
67 DEROctetString octs = (DEROctetString)obj;
68 BERConstructedOctetString berO = new BERConstructedOctetString( octs.getOctets() );
69
70 e = berO.getObjects();
71 }
72
73 while ( e.hasMoreElements() )
74 {
75 out.writeObject( e.nextElement() );
76 }
77 }
78 else if ( obj instanceof DERSequence )
79 {
80 Enumeration e = ( (DERSequence)obj ).getObjects();
81
82 while ( e.hasMoreElements() )
83 {
84 out.writeObject( e.nextElement() );
85 }
86 }
87 else
88 {
89 throw new RuntimeException( "Not implemented: " + obj.getClass().getName() );
90 }
91 }
92 else
93 {
94 out.writeObject( obj );
95 }
96 }
97
98 out.write( DERObject.TERMINATOR );
99 out.write( DERObject.TERMINATOR );
100 }
101 }
102