opwvhk commented on code in PR #2652:
URL: https://github.com/apache/avro/pull/2652#discussion_r1450511234
##########
lang/java/avro/src/main/java/org/apache/avro/Conversions.java:
##########
@@ -68,6 +79,40 @@ public UUID fromCharSequence(CharSequence value, Schema
schema, LogicalType type
public CharSequence toCharSequence(UUID value, Schema schema, LogicalType
type) {
return value.toString();
}
+
+ @Override
+ public UUID fromFixed(final GenericFixed value, final Schema schema, final
LogicalType type) {
+ long mostSigBits = 0;
+ long leastSigBits = 0;
+ byte[] bytes = value.bytes();
+ for (int i = 0; i < Long.BYTES; i++) {
+ mostSigBits |= ((long) (bytes[i] & 255)) << (Byte.SIZE * i);
+ leastSigBits |= ((long) (bytes[i + Long.BYTES] & 255)) << (Byte.SIZE *
i);
+ }
+
+ return new UUID(this.convert(mostSigBits), this.convert(leastSigBits));
+ }
+
+ private long convert(long value) {
+ if (this.isBigEndian) {
+ return value;
+ } else {
+ return Long.reverseBytes(value);
+ }
+ }
+
+ @Override
+ public GenericFixed toFixed(final UUID value, final Schema schema, final
LogicalType type) {
+ final long mostSigBits = this.convert(value.getMostSignificantBits());
+ final long leastSigBits = this.convert(value.getLeastSignificantBits());
+ byte[] result = new byte[2 * Long.BYTES];
+ for (int i = 0; i < Long.BYTES; i++) {
+ result[i] = (byte) ((mostSigBits >> (i * Byte.SIZE)) & 255);
+ result[i + Long.BYTES] = (byte) ((leastSigBits >> (i * Byte.SIZE)) &
255);
+ }
+
+ return new GenericData.Fixed(schema, result);
Review Comment:
Similar to `fromFixed`, I'd choose an implementation like this:
```
ByteBuffer buffer = ByteBuffer.allocate(2 * Long.BYTES);
buffer.putLong(value.getMostSignificantBits());
buffer.putLong(value.getLeastSignificantBits());
return new GenericData.Fixed(schema, buffer.array());
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]