From 91942fcafe79e292e73ca0f09c0225614e7ed2b4 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Mon, 29 Mar 2021 17:07:46 +0200 Subject: BigInteger without ValueExact function supported --- .../java/org/uic/barcode/asn1/uper/IntCoder.java | 42 ++++++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/uic/barcode/asn1/uper/IntCoder.java b/src/main/java/org/uic/barcode/asn1/uper/IntCoder.java index 87b561b..5964a64 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/IntCoder.java +++ b/src/main/java/org/uic/barcode/asn1/uper/IntCoder.java @@ -223,11 +223,11 @@ class IntCoder implements Encoder, Decoder { } else if (classOfT == BigInteger.class) { return (T) resultValue; } else if (classOfT == Long.class) { - return (T) Long.valueOf(resultValue.longValueExact()); + return (T) bigToLong(resultValue); } else if (classOfT == Integer.class) { - return (T) Integer.valueOf(resultValue.intValueExact()); + return (T) bigToInteger(resultValue); } else if (classOfT == Short.class) { - return (T) Short.valueOf(resultValue.shortValueExact()); + return (T) bigToShort(resultValue); } } catch (Exception e){ UperEncoder.logger.debug(String.format("INTEGER Decoded as %s is too big for data type", resultValue)); @@ -235,6 +235,42 @@ class IntCoder implements Encoder, Decoder { return null; } + private Long bigToLong(BigInteger b) { + + Long l = b.longValue(); + + if (BigInteger.valueOf(l).equals(b)) { + return l; + } else { + throw new ArithmeticException("BigInteger To Long failed"); + } + + } + + private Integer bigToInteger(BigInteger b) { + + Integer l = b.intValue(); + + if (BigInteger.valueOf(l).equals(b)) { + return l; + } else { + throw new ArithmeticException("BigInteger To Integer failed"); + } + + } + + private Short bigToShort(BigInteger b) { + + Short l = b.shortValue(); + + if (BigInteger.valueOf(l).equals(b)) { + return l; + } else { + throw new ArithmeticException("BigInteger To Short failed"); + } + + } + @SuppressWarnings("unchecked") @Override public T getDefault(Class classOfT, Annotation[] extraAnnotations) { -- cgit v1.2.3