enable right-padding for BCD codecs #66

This commit is contained in:
Enrique Zamudio
2018-04-17 14:59:48 -05:00
parent ee8a4bca08
commit 360cb3e3e5
2 changed files with 32 additions and 4 deletions
@@ -32,16 +32,30 @@ import java.math.BigInteger;
*/
public class BigIntBcdCodec implements CustomBinaryField<BigInteger> {
private final boolean rightPadded;
public BigIntBcdCodec() {
this(false);
}
public BigIntBcdCodec(boolean rightPadded) {
this.rightPadded = rightPadded;
}
@Override
public BigInteger decodeBinaryField(byte[] value, int pos, int len) {
return Bcd.decodeToBigInteger(value, pos, len*2);
return rightPadded ? Bcd.decodeRightPaddedToBigInteger(value, pos, len*2)
: Bcd.decodeToBigInteger(value, pos, len*2);
}
@Override
public byte[] encodeBinaryField(BigInteger value) {
final String s = value.toString(10);
final byte[] buf = new byte[s.length() / 2 + s.length() % 2];
Bcd.encode(s, buf);
if (rightPadded) {
Bcd.encodeRightPadded(s, buf);
} else {
Bcd.encode(s, buf);
}
return buf;
}
@@ -30,16 +30,30 @@ import com.solab.iso8583.util.Bcd;
*/
public class LongBcdCodec implements CustomBinaryField<Long> {
private final boolean rightPadded;
public LongBcdCodec() {
this(false);
}
public LongBcdCodec(boolean rightPadding) {
rightPadded = rightPadding;
}
@Override
public Long decodeBinaryField(byte[] value, int pos, int length) {
return Bcd.decodeToLong(value, pos, length*2);
return rightPadded ? Bcd.decodeRightPaddedToLong(value, pos, length*2)
: Bcd.decodeToLong(value, pos, length*2);
}
@Override
public byte[] encodeBinaryField(Long value) {
final String s = Long.toString(value, 10);
final byte[] buf = new byte[s.length() / 2 + s.length() % 2];
Bcd.encode(s, buf);
if (rightPadded) {
Bcd.encodeRightPadded(s, buf);
} else {
Bcd.encode(s, buf);
}
return buf;
}