| /* This Source Code Form is subject to the terms of the Mozilla Public |
| * License, v. 2.0. If a copy of the MPL was not distributed with this |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| package org.onosproject.xran.codecs.ber.types; |
| |
| import com.fasterxml.jackson.annotation.JsonIgnore; |
| import com.fasterxml.jackson.annotation.JsonValue; |
| import org.onosproject.xran.codecs.ber.BerByteArrayOutputStream; |
| import org.onosproject.xran.codecs.ber.BerLength; |
| import org.onosproject.xran.codecs.ber.BerTag; |
| import org.onosproject.xran.codecs.ber.internal.Util; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.Serializable; |
| import java.math.BigInteger; |
| |
| public class BerInteger implements Serializable { |
| |
| public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.INTEGER_TAG); |
| private static final long serialVersionUID = 1L; |
| @JsonIgnore |
| public byte[] code = null; |
| |
| public BigInteger value; |
| |
| public BerInteger() { |
| } |
| |
| public BerInteger(byte[] code) { |
| this.code = code; |
| } |
| |
| public BerInteger(BigInteger val) { |
| this.value = val; |
| } |
| |
| public BerInteger(long val) { |
| this.value = BigInteger.valueOf(val); |
| } |
| |
| public int encode(BerByteArrayOutputStream os) throws IOException { |
| return encode(os, true); |
| } |
| |
| public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| |
| if (code != null) { |
| for (int i = code.length - 1; i >= 0; i--) { |
| os.write(code[i]); |
| } |
| if (withTag) { |
| return tag.encode(os) + code.length; |
| } |
| return code.length; |
| } |
| |
| byte[] encoded = value.toByteArray(); |
| int codeLength = encoded.length; |
| os.write(encoded); |
| |
| codeLength += BerLength.encodeLength(os, codeLength); |
| |
| if (withTag) { |
| codeLength += tag.encode(os); |
| } |
| |
| return codeLength; |
| } |
| |
| public int decode(InputStream is) throws IOException { |
| return decode(is, true); |
| } |
| |
| public int decode(InputStream is, boolean withTag) throws IOException { |
| |
| int codeLength = 0; |
| |
| if (withTag) { |
| codeLength += tag.decodeAndCheck(is); |
| } |
| |
| BerLength length = new BerLength(); |
| codeLength += length.decode(is); |
| |
| if (length.val < 1) { |
| throw new IOException("Decoded length of BerInteger is not correct"); |
| } |
| |
| byte[] byteCode = new byte[length.val]; |
| Util.readFully(is, byteCode); |
| |
| codeLength += length.val; |
| |
| value = new BigInteger(byteCode); |
| |
| return codeLength; |
| } |
| |
| public void encodeAndSave(int encodingSizeGuess) throws IOException { |
| BerByteArrayOutputStream os = new BerByteArrayOutputStream(encodingSizeGuess); |
| encode(os, false); |
| code = os.getArray(); |
| } |
| |
| @JsonValue |
| @Override |
| public String toString() { |
| return "" + value; |
| } |
| |
| public byte byteValue() { |
| return value.byteValue(); |
| } |
| |
| public short shortValue() { |
| return value.shortValue(); |
| } |
| |
| public int intValue() { |
| return value.intValue(); |
| } |
| |
| public long longValue() { |
| return value.longValue(); |
| } |
| |
| } |