Home | History | Annotate | Download | only in asn1
      1 package com.android.hotspot2.asn1;
      2 
      3 import java.math.BigInteger;
      4 import java.nio.ByteBuffer;
      5 import java.util.Collection;
      6 
      7 public class Asn1Integer extends Asn1Object {
      8     private static final int SignBit = 0x80;
      9 
     10     private final long mValue;
     11     private final BigInteger mBigValue;
     12 
     13     public Asn1Integer(int tag, Asn1Class asn1Class, int length, ByteBuffer data) {
     14         super(tag, asn1Class, false, length);
     15 
     16         if (length <= 8) {
     17             long value = (data.get(data.position()) & SignBit) != 0 ? -1 : 0;
     18             for (int n = 0; n < length; n++) {
     19                 value = (value << Byte.SIZE) | data.get();
     20             }
     21             mValue = value;
     22             mBigValue = null;
     23         } else {
     24             byte[] payload = new byte[length];
     25             data.get(payload);
     26             mValue = 0;
     27             mBigValue = new BigInteger(payload);
     28         }
     29     }
     30 
     31     public boolean isBigValue() {
     32         return mBigValue != null;
     33     }
     34 
     35     public long getValue() {
     36         return mValue;
     37     }
     38 
     39     public BigInteger getBigValue() {
     40         return mBigValue;
     41     }
     42 
     43     @Override
     44     public Collection<Asn1Object> getChildren() {
     45         throw new UnsupportedOperationException();
     46     }
     47 
     48     @Override
     49     public String toString() {
     50         if (isBigValue()) {
     51             return super.toString() + '=' + mBigValue.toString(16);
     52         } else {
     53             return super.toString() + '=' + mValue;
     54         }
     55     }
     56 }
     57