You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@parquet.apache.org by ga...@apache.org on 2019/01/25 08:07:53 UTC
[parquet-mr] branch master updated: [PARQUET-1500] Replace
Closeables with try-with-resources (#597)
This is an automated email from the ASF dual-hosted git repository.
gabor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/parquet-mr.git
The following commit(s) were added to refs/heads/master by this push:
new f36dd08 [PARQUET-1500] Replace Closeables with try-with-resources (#597)
f36dd08 is described below
commit f36dd08505b5dc799d2e4e92328901796f7b3cb8
Author: Fokko Driesprong <fo...@driesprong.frl>
AuthorDate: Fri Jan 25 09:07:48 2019 +0100
[PARQUET-1500] Replace Closeables with try-with-resources (#597)
---
.../main/java/org/apache/parquet/Closeables.java | 8 ++--
.../parquet/hadoop/util/SerializationUtil.java | 46 +++++++---------------
2 files changed, 19 insertions(+), 35 deletions(-)
diff --git a/parquet-common/src/main/java/org/apache/parquet/Closeables.java b/parquet-common/src/main/java/org/apache/parquet/Closeables.java
index 086f6cc..2f312aa 100644
--- a/parquet-common/src/main/java/org/apache/parquet/Closeables.java
+++ b/parquet-common/src/main/java/org/apache/parquet/Closeables.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -6,9 +6,9 @@
* 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
@@ -26,7 +26,9 @@ import org.slf4j.LoggerFactory;
/**
* Utility for working with {@link java.io.Closeable}ss
+ * @deprecated will be removed in 2.0.0. Use Java try-with-resource instead.
*/
+@Deprecated
public final class Closeables {
private Closeables() { }
diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/util/SerializationUtil.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/util/SerializationUtil.java
index 529115b..06d5fea 100644
--- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/util/SerializationUtil.java
+++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/util/SerializationUtil.java
@@ -1,4 +1,4 @@
-/*
+/*
* 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
@@ -6,9 +6,9 @@
* 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.ByteArrayOutputStream;
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 @@ public final class 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));
}
-
- conf.set(key, new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8"));
}
/**
@@ -88,25 +79,16 @@ public final class SerializationUtil {
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);
}
}
}