Modified serializeSizedString() and deserializeSizedString() to cope with null strings.

This affects various other parts of the system, not just arbitrary transactions.
This commit is contained in:
CalDescent 2021-07-17 14:43:02 +01:00
parent 6f05de2fcc
commit f599aa4852

View File

@ -101,9 +101,17 @@ public class Serialization {
} }
public static void serializeSizedString(ByteArrayOutputStream bytes, String string) throws UnsupportedEncodingException, IOException { public static void serializeSizedString(ByteArrayOutputStream bytes, String string) throws UnsupportedEncodingException, IOException {
byte[] stringBytes = string.getBytes(StandardCharsets.UTF_8); byte[] stringBytes = null;
bytes.write(Ints.toByteArray(stringBytes.length)); int stringBytesLength = 0;
bytes.write(stringBytes);
if (string != null) {
stringBytes = string.getBytes(StandardCharsets.UTF_8);
stringBytesLength = stringBytes.length;
}
bytes.write(Ints.toByteArray(stringBytesLength));
if (stringBytesLength > 0) {
bytes.write(stringBytes);
}
} }
public static String deserializeSizedString(ByteBuffer byteBuffer, int maxSize) throws TransformationException { public static String deserializeSizedString(ByteBuffer byteBuffer, int maxSize) throws TransformationException {
@ -114,6 +122,9 @@ public class Serialization {
if (size > byteBuffer.remaining()) if (size > byteBuffer.remaining())
throw new TransformationException("Byte data too short for serialized string"); throw new TransformationException("Byte data too short for serialized string");
if (size == 0)
return null;
byte[] bytes = new byte[size]; byte[] bytes = new byte[size];
byteBuffer.get(bytes); byteBuffer.get(bytes);