You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "adoroszlai (via GitHub)" <gi...@apache.org> on 2024/09/11 09:21:26 UTC

Re: [PR] HDDS-11438. Use try-with-resources or close this "DataInputBuffer" in a "finally" clause [ozone]

adoroszlai commented on code in PR #7182:
URL: https://github.com/apache/ozone/pull/7182#discussion_r1753746181


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/protocolPB/OMPBHelper.java:
##########
@@ -242,11 +242,20 @@ public static MD5MD5Crc32FileChecksumProto convert(
     DataOutputBuffer buf = new DataOutputBuffer();
     checksum.write(buf);
     byte[] bytes = buf.getData();
-    DataInputBuffer buffer = new DataInputBuffer();
-    buffer.reset(bytes, 0, bytes.length);
-    int bytesPerCRC = buffer.readInt();
-    long crcPerBlock = buffer.readLong();
-    buffer.close();
+    DataInputBuffer buffer = null;
+    int bytesPerCRC;
+    long crcPerBlock;
+    try {
+      buffer = new DataInputBuffer();
+      buffer.reset(bytes, 0, bytes.length);
+      bytesPerCRC = buffer.readInt();
+      crcPerBlock = buffer.readLong();
+    } catch (IOException e) {
+      throw new RuntimeException(e);

Review Comment:
   Since the method `throws IOException`, I don't think it's necessary to convert it to `RuntimeException`.  (In cases where conversion is needed, prefer the more specific `UncheckedIOException`.)



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/protocolPB/OMPBHelper.java:
##########
@@ -242,11 +242,20 @@ public static MD5MD5Crc32FileChecksumProto convert(
     DataOutputBuffer buf = new DataOutputBuffer();
     checksum.write(buf);
     byte[] bytes = buf.getData();
-    DataInputBuffer buffer = new DataInputBuffer();
-    buffer.reset(bytes, 0, bytes.length);
-    int bytesPerCRC = buffer.readInt();
-    long crcPerBlock = buffer.readLong();
-    buffer.close();
+    DataInputBuffer buffer = null;
+    int bytesPerCRC;
+    long crcPerBlock;
+    try {
+      buffer = new DataInputBuffer();
+      buffer.reset(bytes, 0, bytes.length);
+      bytesPerCRC = buffer.readInt();
+      crcPerBlock = buffer.readLong();
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    } finally {
+      assert buffer != null;
+      buffer.close();
+    }

Review Comment:
   Let's rely on auto-close:
   
   ```suggestion
       int bytesPerCRC;
       long crcPerBlock;
       try (DataInputBuffer buffer = new DataInputBuffer()) {
         buffer.reset(bytes, 0, bytes.length);
         bytesPerCRC = buffer.readInt();
         crcPerBlock = buffer.readLong();
       }
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org