You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/04/15 17:16:05 UTC

[GitHub] [beam] lukecwik commented on a change in pull request #11397: [BEAM-9743] Fix TFRecordCodec to try harder to read/write

lukecwik commented on a change in pull request #11397: [BEAM-9743] Fix TFRecordCodec to try harder to read/write
URL: https://github.com/apache/beam/pull/11397#discussion_r409002145
 
 

 ##########
 File path: sdks/java/core/src/main/java/org/apache/beam/sdk/io/TFRecordIO.java
 ##########
 @@ -717,14 +716,38 @@ public void write(WritableByteChannel outChannel, byte[] data) throws IOExceptio
       header.clear();
       header.putLong(data.length).putInt(maskedCrc32OfLength);
       header.rewind();
-      outChannel.write(header);
+      writeFully(outChannel, header);
 
-      outChannel.write(ByteBuffer.wrap(data));
+      writeFully(outChannel, ByteBuffer.wrap(data));
 
       footer.clear();
       footer.putInt(maskedCrc32OfData);
       footer.rewind();
-      outChannel.write(footer);
+      writeFully(outChannel, footer);
+    }
+
+    @VisibleForTesting
+    static void readFully(ReadableByteChannel in, ByteBuffer bb) throws IOException {
+      int expected = bb.remaining();
+      int actual = read(in, bb);
+      if (expected != actual) {
+        throw new IOException(String.format("expected %d, but got %d", expected, actual));
+      }
+    }
+
+    private static int read(ReadableByteChannel in, ByteBuffer bb) throws IOException {
+      int n, read = 0;
+      while (bb.hasRemaining() && (n = in.read(bb)) >= 0) {
+        read += n;
+      }
+      return read;
+    }
 
 Review comment:
   ```suggestion
       private static int read(ReadableByteChannel in, ByteBuffer bb) throws IOException {
         int expected = bb.remaining();
         while (bb.hasRemaining() && in.read(bb) >= 0) {
         }
         return expected - bb.remaining();
       }
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services