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;
26import java.util.Arrays;
27
28
29/**
30 * This class represents ASN.1 octet string type.
31 *
32 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
33 */
34public class ASN1OctetString extends ASN1StringType {
35
36    /** default implementation */
37    private static final ASN1OctetString ASN1 = new ASN1OctetString();
38
39    /**
40     * Constructs ASN.1 octet string type
41     *
42     * The constructor is provided for inheritance purposes
43     * when there is a need to create a custom ASN.1 octet string type.
44     * To get a default implementation it is recommended to use
45     * getInstance() method.
46     */
47    public ASN1OctetString() {
48        super(TAG_OCTETSTRING);
49    }
50
51    /**
52     * Returns ASN.1 octet string type default implementation
53     *
54     * The default implementation works with encoding
55     * that is represented as byte array.
56     */
57    public static ASN1OctetString getInstance() {
58        return ASN1;
59    }
60
61    @Override public Object decode(BerInputStream in) throws IOException {
62        in.readOctetString();
63
64        if (in.isVerify) {
65            return null;
66        }
67        return getDecodedObject(in);
68    }
69
70    /**
71     * Extracts array of bytes from BER input stream.
72     *
73     * @return array of bytes
74     */
75    @Override public Object getDecodedObject(BerInputStream in) throws IOException {
76        return Arrays.copyOfRange(in.buffer, in.contentOffset, in.contentOffset + in.length);
77    }
78
79    @Override public void encodeContent(BerOutputStream out) {
80        out.encodeOctetString();
81    }
82
83    @Override public void setEncodingContent(BerOutputStream out) {
84        out.length = ((byte[]) out.content).length;
85    }
86}
87