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: import java.math.BigInteger;
025:
026: /**
027: * This class represents ASN.1 Integer type.
028: *
029: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
030: */
031:
032: public class ASN1Integer extends ASN1Primitive {
033:
034: // default implementation
035: private static final ASN1Integer ASN1 = new ASN1Integer();
036:
037: /**
038: * Constructs ASN.1 Integer type
039: *
040: * The constructor is provided for inheritance purposes
041: * when there is a need to create a custom ASN.1 Integer type.
042: * To get a default implementation it is recommended to use
043: * getInstance() method.
044: */
045: public ASN1Integer() {
046: super (TAG_INTEGER);
047: }
048:
049: /**
050: * Returns ASN.1 Integer type default implementation
051: *
052: * The default implementation works with encoding
053: * that is represented as byte array in two's-complement notation.
054: *
055: * @return ASN.1 Integer type default implementation
056: */
057: public static ASN1Integer getInstance() {
058: return ASN1;
059: }
060:
061: //
062: //
063: // Decode
064: //
065: //
066:
067: public Object decode(BerInputStream in) throws IOException {
068: in.readInteger();
069:
070: if (in.isVerify) {
071: return null;
072: }
073: return getDecodedObject(in);
074: }
075:
076: /**
077: * Extracts array of bytes from BER input stream.
078: *
079: * @param in - BER input stream
080: * @return array of bytes
081: */
082: public Object getDecodedObject(BerInputStream in)
083: throws IOException {
084: byte[] bytesEncoded = new byte[in.length];
085: System.arraycopy(in.buffer, in.contentOffset, bytesEncoded, 0,
086: in.length);
087: return bytesEncoded;
088: }
089:
090: //
091: //
092: // Encode
093: //
094: //
095:
096: public void encodeContent(BerOutputStream out) {
097: out.encodeInteger();
098: }
099:
100: public void setEncodingContent(BerOutputStream out) {
101: out.length = ((byte[]) out.content).length;
102: }
103:
104: /**
105: * Converts decoded ASN.1 Integer to int value.
106: *
107: * @param decoded a decoded object corresponding to {@link #asn1 this type}
108: * @return decoded int value.
109: */
110: public static int toIntValue(Object decoded) {
111: return new BigInteger((byte[]) decoded).intValue();//FIXME optimize
112: }
113:
114: /**
115: * Converts primitive int value to a form most suitable for encoding.
116: *
117: * @param value primitive value to be encoded
118: * @return object suitable for encoding
119: */
120: public static Object fromIntValue(int value) {
121: //FIXME optimize
122: return BigInteger.valueOf(value).toByteArray();
123: }
124: }
|