Skip to content

Commit

Permalink
Core: Update TableMetadataParser to ensure all streams closed (apache…
Browse files Browse the repository at this point in the history
…#11220)

* Update TableMetadataParser to close streams

* Fix OutputStreamWriter

* Update TableMetadataParser.java

* spotlessApply
  • Loading branch information
erik-grepr authored Oct 26, 2024
1 parent 7ad11b2 commit 2b55fef
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/apache/iceberg/TableMetadataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ 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;
OutputStream stream = overwrite ? outputFile.createOrOverwrite() : outputFile.create();
try (OutputStream ou = isGzip ? new GZIPOutputStream(stream) : stream;
OutputStreamWriter writer = new OutputStreamWriter(ou, StandardCharsets.UTF_8)) {
try (OutputStream os = overwrite ? outputFile.createOrOverwrite() : outputFile.create();
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);
Expand Down Expand Up @@ -275,9 +275,9 @@ public static TableMetadata read(FileIO io, String path) {

public static TableMetadata read(FileIO io, InputFile file) {
Codec codec = Codec.fromFileName(file.location());
try (InputStream is =
codec == Codec.GZIP ? new GZIPInputStream(file.newStream()) : file.newStream()) {
return fromJson(file, JsonUtil.mapper().readValue(is, JsonNode.class));
try (InputStream is = file.newStream();
InputStream gis = codec == Codec.GZIP ? new GZIPInputStream(is) : is) {
return fromJson(file, JsonUtil.mapper().readValue(gis, JsonNode.class));
} catch (IOException e) {
throw new RuntimeIOException(e, "Failed to read file: %s", file);
}
Expand Down

0 comments on commit 2b55fef

Please sign in to comment.