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
2323import java .io .IOException ;
2424import java .io .ObjectInputStream ;
2525import java .io .ObjectOutputStream ;
26+ import java .nio .charset .StandardCharsets ;
2627import java .util .zip .GZIPInputStream ;
2728import java .util .zip .GZIPOutputStream ;
2829
2930import org .apache .commons .codec .binary .Base64 ;
3031import org .apache .hadoop .conf .Configuration ;
3132
32- import org .apache .parquet .Closeables ;
3333import org .slf4j .Logger ;
3434import 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