Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.6k
[PARQUET-1500] Replace Closeables with try-with-resources#597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
8 changes: 5 additions & 3 deletions
8 parquet-common/src/main/java/org/apache/parquet/Closeables.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 14 additions & 32 deletions
46 parquet-hadoop/src/main/java/org/apache/parquet/hadoop/util/SerializationUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| /* | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| @@ -23,13 +23,13 @@ | ||
| import java.io.IOException; | ||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.zip.GZIPInputStream; | ||
| import java.util.zip.GZIPOutputStream; | ||
| import org.apache.commons.codec.binary.Base64; | ||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.parquet.Closeables; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| @@ -53,22 +53,13 @@ private SerializationUtil() { } | ||
| * @throws IOException if there is an error while writing | ||
| */ | ||
| public static void writeObjectToConfAsBase64(String key, Object obj, Configuration conf) throws IOException { | ||
| ByteArrayOutputStream baos = null; | ||
| GZIPOutputStream gos = null; | ||
| ObjectOutputStream oos = null; | ||
| try { | ||
| baos = new ByteArrayOutputStream(); | ||
| gos = new GZIPOutputStream(baos); | ||
| oos = new ObjectOutputStream(gos); | ||
| oos.writeObject(obj); | ||
| } finally { | ||
| Closeables.close(oos); | ||
| Closeables.close(gos); | ||
| Closeables.close(baos); | ||
| try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||
| try(GZIPOutputStream gos = new GZIPOutputStream(baos); | ||
| ObjectOutputStream oos = new ObjectOutputStream(gos)) { | ||
| oos.writeObject(obj); | ||
| } | ||
| conf.set(key, new String(Base64.encodeBase64(baos.toByteArray()), StandardCharsets.UTF_8)); | ||
gszadovszky marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| conf.set(key, new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8")); | ||
| } | ||
| /** | ||
| @@ -88,25 +79,16 @@ public static <T> T readObjectFromConfAsBase64(String key, Configuration conf) t | ||
| return null; | ||
| } | ||
| byte[] bytes = Base64.decodeBase64(b64.getBytes("UTF-8")); | ||
| ByteArrayInputStream bais = null; | ||
| GZIPInputStream gis = null; | ||
| ObjectInputStream ois = null; | ||
| byte[] bytes = Base64.decodeBase64(b64.getBytes(StandardCharsets.UTF_8)); | ||
| try { | ||
| bais = new ByteArrayInputStream(bytes); | ||
| gis = new GZIPInputStream(bais); | ||
| ois = new ObjectInputStream(gis); | ||
| try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); | ||
| GZIPInputStream gis = new GZIPInputStream(bais); | ||
| ObjectInputStream ois = new ObjectInputStream(gis)) { | ||
| return (T) ois.readObject(); | ||
| } catch (ClassNotFoundException e) { | ||
| throw new IOException("Could not read object from config with key " + key, e); | ||
| } catch (ClassCastException e) { | ||
| throw new IOException("Couldn't cast object read from config with key " + key, e); | ||
| } finally { | ||
| Closeables.close(ois); | ||
| Closeables.close(gis); | ||
| Closeables.close(bais); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can consolidate these into a single
trye.g.:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet suggestion, thanks!