1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Vladimir N. Molotkov, Stepan M. Mishura
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.asn1;
24
25import java.io.IOException;
26
27import org.apache.harmony.security.internal.nls.Messages;
28
29
30/**
31 * This class represents explicitly tagged ASN.1 type.
32 *
33 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
34 */
35
36public final class ASN1Explicit extends ASN1Constructured {
37
38    /**
39     * Tagged type
40     */
41    public final ASN1Type type;
42
43    /**
44     * Constructs explicitly tagged ASN.1 type
45     * with context-specific tag class and specified tag number.
46     *
47     * @param tagNumber - ASN.1 tag number
48     * @param type - ASN.1 type to be tagged
49     * @throws IllegalArgumentException - if tagNumber is invalid
50     */
51    public ASN1Explicit(int tagNumber, ASN1Type type) {
52        this(CLASS_CONTEXTSPECIFIC, tagNumber, type);
53    }
54
55    /**
56     * Constructs explicitly tagged ASN.1 type.
57     *
58     * @param tagClass - ASN.1 tag class.
59     * @param tagNumber - ASN.1 tag number
60     * @param type - ASN.1 type to be tagged
61     * @throws IllegalArgumentException - if tagClass or tagNumber is invalid
62     */
63    public ASN1Explicit(int tagClass, int tagNumber, ASN1Type type) {
64        super(tagClass, tagNumber);
65
66        this.type = type;
67    }
68
69    //
70    //
71    // Decode
72    //
73    //
74
75    public Object decode(BerInputStream in) throws IOException {
76        if (constrId != in.tag) {
77            throw new ASN1Exception(
78                    Messages.getString("security.13F", //$NON-NLS-1$
79                    new Object[] { in.tagOffset, Integer.toHexString(constrId),
80                            Integer.toHexString(in.tag) }));
81        }
82        in.next();
83
84        in.content = type.decode(in);
85
86        if (in.isVerify) {
87            return null;
88        }
89        return getDecodedObject(in);
90    }
91
92    //
93    //
94    // Encode
95    //
96    //
97
98    public void encodeContent(BerOutputStream out) {
99        out.encodeExplicit(this);
100    }
101
102    public void setEncodingContent(BerOutputStream out) {
103        out.getExplicitLength(this);
104    }
105
106    public String toString() {
107        //FIXME fix performance
108        return super.toString() + " for type " + type; //$NON-NLS-1$
109    }
110}
111