Home | History | Annotate | Download | only in asn1
      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 
     23 package org.apache.harmony.security.asn1;
     24 
     25 import java.io.IOException;
     26 import java.math.BigInteger;
     27 
     28 /**
     29  * This class represents ASN.1 Integer type.
     30  *
     31  * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
     32  */
     33 public final class ASN1Integer extends ASN1Primitive {
     34 
     35     /** default implementation */
     36     private static final ASN1Integer ASN1 = new ASN1Integer();
     37 
     38     /**
     39      * Constructs ASN.1 Integer type
     40      *
     41      * The constructor is provided for inheritance purposes
     42      * when there is a need to create a custom ASN.1 Integer type.
     43      * To get a default implementation it is recommended to use
     44      * getInstance() method.
     45      */
     46     public ASN1Integer() {
     47         super(TAG_INTEGER);
     48     }
     49 
     50     /**
     51      * Returns ASN.1 Integer type default implementation
     52      *
     53      * The default implementation works with encoding
     54      * that is represented as byte array in two's-complement notation.
     55      *
     56      * @return ASN.1 Integer type default implementation
     57      */
     58     public static ASN1Integer getInstance() {
     59         return ASN1;
     60     }
     61 
     62     public Object decode(BerInputStream in) throws IOException {
     63         in.readInteger();
     64 
     65         if (in.isVerify) {
     66             return null;
     67         }
     68         return getDecodedObject(in);
     69     }
     70 
     71     /**
     72      * Extracts array of bytes from BER input stream.
     73      *
     74      * @return array of bytes
     75      */
     76     public Object getDecodedObject(BerInputStream in) throws IOException {
     77         byte[] bytesEncoded = new byte[in.length];
     78         System.arraycopy(in.buffer, in.contentOffset, bytesEncoded, 0,
     79                 in.length);
     80         return bytesEncoded;
     81     }
     82 
     83     public void encodeContent(BerOutputStream out) {
     84         out.encodeInteger();
     85     }
     86 
     87     public void setEncodingContent(BerOutputStream out) {
     88         out.length = ((byte[]) out.content).length;
     89     }
     90 
     91     /**
     92      * Converts decoded ASN.1 Integer to int value.
     93      * If the object represents an integer value
     94      * larger than 32 bits, the high bits will be lost.
     95      *
     96      * @param decoded a decoded object corresponding to this type
     97      * @return decoded int value.
     98      */
     99     public static int toIntValue(Object decoded) {
    100         return new BigInteger((byte[]) decoded).intValue();
    101     }
    102 
    103     /**
    104      * Converts decoded ASN.1 Integer to a BigInteger.
    105      *
    106      * @param decoded a decoded object corresponding to this type
    107      * @return decoded BigInteger value.
    108      */
    109     public static BigInteger toBigIntegerValue(Object decoded) {
    110         return new BigInteger((byte[]) decoded);
    111     }
    112 
    113     /**
    114      * Converts primitive int value to a form most suitable for encoding.
    115      *
    116      * @param value primitive value to be encoded
    117      * @return object suitable for encoding
    118      */
    119     public static Object fromIntValue(int value) {
    120         return BigInteger.valueOf(value).toByteArray();
    121     }
    122 }
    123