@@ -83,13 +83,14 @@ public static long serialize(WriteChannel out, Schema schema) throws IOException
8383 int schemaOffset = schema .getSchema (builder );
8484 ByteBuffer serializedMessage = serializeMessage (builder , MessageHeader .Schema , schemaOffset , 0 );
8585 int size = serializedMessage .remaining ();
86- // ensure that message aligns to 8 byte padding - start position, 4 bytes for size, message body
86+ // ensure that message aligns to 8 byte padding
87+ // start position (e.g. magic bytes), 4 bytes for size, message body
8788 if ((start + 4 + size ) % 8 != 0 ) {
8889 size += 8 - (start + 4 + size ) % 8 ;
8990 }
9091 out .writeIntLittleEndian (size );
9192 out .write (serializedMessage );
92- out .align ();
93+ out .align (); // any bytes written are already captured by our size modification above
9394 return size + 4 ;
9495 }
9596
@@ -127,6 +128,9 @@ public static ArrowBlock serialize(WriteChannel out, ArrowRecordBatch batch)
127128
128129 long start = out .getCurrentPosition ();
129130 int bodyLength = batch .computeBodyLength ();
131+ // make sure that the body ends on an 8 byte boundary
132+ // each buffer in the body gets started on an 8 byte boundary in ArrowRecordBatch,
133+ // but we need to handle the final end alignment here
130134 if (bodyLength % 8 != 0 ) {
131135 bodyLength += 8 - (bodyLength % 8 );
132136 }
@@ -151,6 +155,7 @@ public static ArrowBlock serialize(WriteChannel out, ArrowRecordBatch batch)
151155 out .align ();
152156
153157 long bufferLength = writeBatchBuffers (out , batch );
158+ // align again - the size is captured in out bodyLength check above
154159 bufferLength += out .align ();
155160
156161 // Metadata size in the Block account for the size prefix
@@ -279,6 +284,9 @@ public static ArrowRecordBatch deserializeRecordBatch(RecordBatch recordBatchFB,
279284 public static ArrowBlock serialize (WriteChannel out , ArrowDictionaryBatch batch ) throws IOException {
280285 long start = out .getCurrentPosition ();
281286 int bodyLength = batch .computeBodyLength ();
287+ // make sure that the body ends on an 8 byte boundary
288+ // each buffer in the body gets started on an 8 byte boundary in ArrowRecordBatch,
289+ // but we need to handle the final end alignment here
282290 if (bodyLength % 8 != 0 ) {
283291 bodyLength += 8 - (bodyLength % 8 );
284292 }
@@ -304,6 +312,7 @@ public static ArrowBlock serialize(WriteChannel out, ArrowDictionaryBatch batch)
304312
305313 // write the embedded record batch
306314 long bufferLength = writeBatchBuffers (out , batch .getDictionary ());
315+ // align again - the size is captured in out bodyLength check above
307316 bufferLength += out .align ();
308317
309318 // Metadata size in the Block account for the size prefix
0 commit comments