Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand All @@ -26,7 +26,9 @@

/**
* 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() { }

Expand Down
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
Expand All @@ -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;

Expand All @@ -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()) {
Copy link
Contributor

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 try e.g.:

try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
       GZIPOutputStream gos = new GZIPOutputStream(baos);
       ObjectOutputStream oos = new ObjectOutputStream(gos)) {
    oos.writeObject(obj);
    conf.set(key, new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8"));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet suggestion, thanks!

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"));
}

/**
Expand All @@ -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);
}
}
}