001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vladimir N. Molotkov, Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.asn1;
022:
023: import java.io.IOException;
024:
025: /**
026: * This class represents ASN.1 ANY type.
027: *
028: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
029: */
030:
031: public class ASN1Any extends ASN1Type {
032:
033: // default implementation
034: private static final ASN1Any ASN1 = new ASN1Any();
035:
036: /**
037: * Constructs ASN.1 ANY type
038: *
039: * The constructor is provided for inheritance purposes
040: * when there is a need to create a custom ASN.1 ANY type.
041: * To get a default implementation it is recommended to use
042: * getInstance() method.
043: */
044: public ASN1Any() {
045: super (TAG_ANY); // has not tag number
046: }
047:
048: /**
049: * Returns ASN.1 ANY type default implementation
050: *
051: * The default implementation works with full encoding
052: * that is represented as raw byte array.
053: *
054: * @return ASN.1 ANY type default implementation
055: */
056: public static ASN1Any getInstance() {
057: return ASN1;
058: }
059:
060: //
061: //
062: // Decode
063: //
064: //
065:
066: /**
067: * Tests provided identifier.
068: *
069: * @param identifier - identifier to be verified
070: * @return - true
071: */
072: public final boolean checkTag(int identifier) {
073: return true; //all tags are OK
074: }
075:
076: public Object decode(BerInputStream in) throws IOException {
077:
078: // only read content, doesn't check it
079: in.readContent();
080:
081: if (in.isVerify) {
082: return null;
083: }
084: return getDecodedObject(in);
085: }
086:
087: /**
088: * Extracts array of bytes that represents full encoding from BER input
089: * stream.
090: *
091: * @param in -
092: * BER input stream
093: * @return array of bytes
094: */
095: public Object getDecodedObject(BerInputStream in)
096: throws IOException {
097: byte[] bytesEncoded = new byte[in.offset - in.tagOffset];
098: System.arraycopy(in.buffer, in.tagOffset, bytesEncoded, 0,
099: bytesEncoded.length);
100: return bytesEncoded;
101: }
102:
103: //
104: //
105: // Encode
106: //
107: //
108:
109: public void encodeASN(BerOutputStream out) {
110: out.encodeANY();
111: }
112:
113: public void encodeContent(BerOutputStream out) {
114: out.encodeANY();
115: }
116:
117: public void setEncodingContent(BerOutputStream out) {
118: out.length = ((byte[]) out.content).length;
119: }
120:
121: public int getEncodedLength(BerOutputStream out) {
122: return out.length;
123: }
124: }
|