amogh-jahagirdar commented on code in PR #11609:
URL: https://github.com/apache/iceberg/pull/11609#discussion_r1852298123


##########
core/src/main/java/org/apache/iceberg/TableMetadataParser.java:
##########
@@ -122,15 +122,25 @@ public static void write(TableMetadata metadata, 
OutputFile outputFile) {
   public static void internalWrite(
       TableMetadata metadata, OutputFile outputFile, boolean overwrite) {
     boolean isGzip = Codec.fromFileName(outputFile.location()) == Codec.GZIP;
-    try (OutputStream os = overwrite ? outputFile.createOrOverwrite() : 
outputFile.create();
-        OutputStream gos = isGzip ? new GZIPOutputStream(os) : os;
+    OutputStream os = overwrite ? outputFile.createOrOverwrite() : 
outputFile.create();
+    // if isGzip is true, os will be closed by GZIPOutputStream,
+    // otherwise, os will be closed by try-with-resources
+    try (OutputStream gos = isGzip ? new GZIPOutputStream(os) : os;
         OutputStreamWriter writer = new OutputStreamWriter(gos, 
StandardCharsets.UTF_8)) {
       JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
       generator.useDefaultPrettyPrinter();
       toJson(metadata, generator);
       generator.flush();
     } catch (IOException e) {
       throw new RuntimeIOException(e, "Failed to write json to file: %s", 
outputFile);
+    } finally {
+      try {
+        // in case of an exception in GZIPOutputStream constructor,
+        // the stream is not closed and needs to be closed here

Review Comment:
   "Explicitly close the stream in case of exceptions thrown by 
GZIPOutputStream constructor. See https://github.com/apache/iceberg/pull/11220 
for more details"



##########
core/src/main/java/org/apache/iceberg/TableMetadataParser.java:
##########
@@ -122,15 +122,25 @@ public static void write(TableMetadata metadata, 
OutputFile outputFile) {
   public static void internalWrite(
       TableMetadata metadata, OutputFile outputFile, boolean overwrite) {
     boolean isGzip = Codec.fromFileName(outputFile.location()) == Codec.GZIP;
-    try (OutputStream os = overwrite ? outputFile.createOrOverwrite() : 
outputFile.create();
-        OutputStream gos = isGzip ? new GZIPOutputStream(os) : os;
+    OutputStream os = overwrite ? outputFile.createOrOverwrite() : 
outputFile.create();
+    // if isGzip is true, os will be closed by GZIPOutputStream,
+    // otherwise, os will be closed by try-with-resources
+    try (OutputStream gos = isGzip ? new GZIPOutputStream(os) : os;

Review Comment:
   We may want to have some sort of internal output stream implementation which 
delegates to the gzip or underlying oputput stream, and handles the complexity 
of closing in case of failures in GzipOutputStream constructor, and propogating 
that exception. Then the code below really just would have the delegate in the 
try with closeable.



-- 
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-unsubscr...@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to