Skip to content

Commit

Permalink
Adds suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
popematt committed Nov 20, 2023
1 parent 86cf5d7 commit 1a41e8a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/main/java/com/amazon/ion/impl/bin/IonEncoder_1_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -183,6 +183,7 @@ public static int writeDecimalValue(WriteBuffer buffer, final BigDecimal value)
numCoefficientBytes = 0;
}
} else {
coefficientBytes = value.unscaledValue().toByteArray();
numCoefficientBytes = coefficientBytes.length;
}

Expand All @@ -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;
Expand Down

0 comments on commit 1a41e8a

Please sign in to comment.