Skip to content

Commit bf12f98

Browse files
author
Fokko Driesprong
committed
[PARQUET-1500] Replace Closeables with try-with-resources
1 parent e9c2837 commit bf12f98

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

parquet-common/src/main/java/org/apache/parquet/Closeables.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/*
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
55
* regarding copyright ownership. The ASF licenses this file
66
* to you under the Apache License, Version 2.0 (the
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
9-
*
9+
*
1010
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
11+
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
1414
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -26,7 +26,9 @@
2626

2727
/**
2828
* Utility for working with {@link java.io.Closeable}ss
29+
* @deprecated will be removed in 2.0.0. Use Java try-with-resource instead.
2930
*/
31+
@Deprecated
3032
public final class Closeables {
3133
private Closeables() { }
3234

parquet-hadoop/src/main/java/org/apache/parquet/hadoop/util/SerializationUtil.java

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/*
1+
/*
22
* Licensed to the Apache Software Foundation (ASF) under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
55
* regarding copyright ownership. The ASF licenses this file
66
* to you under the Apache License, Version 2.0 (the
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
9-
*
9+
*
1010
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
11+
*
1212
* Unless required by applicable law or agreed to in writing,
1313
* software distributed under the License is distributed on an
1414
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -23,13 +23,13 @@
2323
import java.io.IOException;
2424
import java.io.ObjectInputStream;
2525
import java.io.ObjectOutputStream;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.zip.GZIPInputStream;
2728
import java.util.zip.GZIPOutputStream;
2829

2930
import org.apache.commons.codec.binary.Base64;
3031
import org.apache.hadoop.conf.Configuration;
3132

32-
import org.apache.parquet.Closeables;
3333
import org.slf4j.Logger;
3434
import org.slf4j.LoggerFactory;
3535

@@ -53,22 +53,12 @@ private SerializationUtil() { }
5353
* @throws IOException if there is an error while writing
5454
*/
5555
public static void writeObjectToConfAsBase64(String key, Object obj, Configuration conf) throws IOException {
56-
ByteArrayOutputStream baos = null;
57-
GZIPOutputStream gos = null;
58-
ObjectOutputStream oos = null;
59-
60-
try {
61-
baos = new ByteArrayOutputStream();
62-
gos = new GZIPOutputStream(baos);
63-
oos = new ObjectOutputStream(gos);
56+
try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
57+
GZIPOutputStream gos = new GZIPOutputStream(baos);
58+
ObjectOutputStream oos = new ObjectOutputStream(gos)) {
6459
oos.writeObject(obj);
65-
} finally {
66-
Closeables.close(oos);
67-
Closeables.close(gos);
68-
Closeables.close(baos);
60+
conf.set(key, new String(Base64.encodeBase64(baos.toByteArray()), StandardCharsets.UTF_8));
6961
}
70-
71-
conf.set(key, new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8"));
7262
}
7363

7464
/**
@@ -88,25 +78,16 @@ public static <T> T readObjectFromConfAsBase64(String key, Configuration conf) t
8878
return null;
8979
}
9080

91-
byte[] bytes = Base64.decodeBase64(b64.getBytes("UTF-8"));
92-
93-
ByteArrayInputStream bais = null;
94-
GZIPInputStream gis = null;
95-
ObjectInputStream ois = null;
81+
byte[] bytes = Base64.decodeBase64(b64.getBytes(StandardCharsets.UTF_8));
9682

97-
try {
98-
bais = new ByteArrayInputStream(bytes);
99-
gis = new GZIPInputStream(bais);
100-
ois = new ObjectInputStream(gis);
83+
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
84+
GZIPInputStream gis = new GZIPInputStream(bais);
85+
ObjectInputStream ois = new ObjectInputStream(gis)) {
10186
return (T) ois.readObject();
10287
} catch (ClassNotFoundException e) {
10388
throw new IOException("Could not read object from config with key " + key, e);
10489
} catch (ClassCastException e) {
10590
throw new IOException("Couldn't cast object read from config with key " + key, e);
106-
} finally {
107-
Closeables.close(ois);
108-
Closeables.close(gis);
109-
Closeables.close(bais);
11091
}
11192
}
11293
}

0 commit comments

Comments
 (0)