From 1a41e8a915699c31677e94d702ea2bd400e743da Mon Sep 17 00:00:00 2001 From: Matthew Pope Date: Fri, 17 Nov 2023 09:39:36 -0800 Subject: [PATCH] Adds suggested changes --- .../com/amazon/ion/impl/bin/IonEncoder_1_1.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/bin/IonEncoder_1_1.java b/src/main/java/com/amazon/ion/impl/bin/IonEncoder_1_1.java index 4a7f532b50..df04fb3b9c 100644 --- a/src/main/java/com/amazon/ion/impl/bin/IonEncoder_1_1.java +++ b/src/main/java/com/amazon/ion/impl/bin/IonEncoder_1_1.java @@ -171,7 +171,7 @@ public static int writeDecimalValue(WriteBuffer buffer, final BigDecimal value) int exponent = -value.scale(); int numExponentBytes = WriteBuffer.flexIntLength(exponent); - byte[] coefficientBytes = value.unscaledValue().toByteArray(); + byte[] coefficientBytes = null; int numCoefficientBytes; if (BigDecimal.ZERO.compareTo(value) == 0) { if (Decimal.isNegativeZero(value)) { @@ -183,6 +183,7 @@ public static int writeDecimalValue(WriteBuffer buffer, final BigDecimal value) numCoefficientBytes = 0; } } else { + coefficientBytes = value.unscaledValue().toByteArray(); numCoefficientBytes = coefficientBytes.length; } @@ -197,8 +198,14 @@ public static int writeDecimalValue(WriteBuffer buffer, final BigDecimal value) } buffer.writeFlexInt(exponent); - if (numCoefficientBytes != 0) { - buffer.writeFixedIntOrUInt(coefficientBytes); + if (numCoefficientBytes > 0) { + if (coefficientBytes != null) { + buffer.writeFixedIntOrUInt(coefficientBytes); + } else if (numCoefficientBytes == 1){ + buffer.writeByte((byte) 0); + } else { + throw new IllegalStateException("Unreachable! coefficientBytes should not be null when numCoefficientBytes > 1"); + } } return opCodeAndLengthBytes + numCoefficientBytes + numExponentBytes;