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