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
27/**
28 * This class represents explicitly tagged ASN.1 type.
29 *
30 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
31 */
32public final class ASN1Explicit extends ASN1Constructed {
33
34    /** Tagged type */
35    public final ASN1Type type;
36
37    /**
38     * Constructs explicitly tagged ASN.1 type
39     * with context-specific tag class and specified tag number.
40     *
41     * @param tagNumber - ASN.1 tag number
42     * @param type - ASN.1 type to be tagged
43     * @throws IllegalArgumentException - if tagNumber is invalid
44     */
45    public ASN1Explicit(int tagNumber, ASN1Type type) {
46        this(CLASS_CONTEXTSPECIFIC, tagNumber, type);
47    }
48
49    /**
50     * Constructs explicitly tagged ASN.1 type.
51     *
52     * @param tagClass - ASN.1 tag class.
53     * @param tagNumber - ASN.1 tag number
54     * @param type - ASN.1 type to be tagged
55     * @throws IllegalArgumentException - if tagClass or tagNumber is invalid
56     */
57    public ASN1Explicit(int tagClass, int tagNumber, ASN1Type type) {
58        super(tagClass, tagNumber);
59        this.type = type;
60    }
61
62    public Object decode(BerInputStream in) throws IOException {
63        if (constrId != in.tag) {
64            throw new ASN1Exception("ASN.1 explicitly tagged type is expected at [" +
65                    in.tagOffset + "]. Expected tag: " + Integer.toHexString(constrId) + ", " +
66                    "but encountered tag " + Integer.toHexString(in.tag));
67        }
68        in.next();
69
70        in.content = type.decode(in);
71
72        if (in.isVerify) {
73            return null;
74        }
75        return getDecodedObject(in);
76    }
77
78    public void encodeContent(BerOutputStream out) {
79        out.encodeExplicit(this);
80    }
81
82    public void setEncodingContent(BerOutputStream out) {
83        out.getExplicitLength(this);
84    }
85
86    @Override public String toString() {
87        return super.toString() + " for type " + type;
88    }
89}
90