SENTRY-1730: Remove FileInputStream/FileOutputStream - Reviewed by Na Li Change-Id: I2ef70ce7fc1cc114007b971d4fc62d052b2d6666 Reviewed-on: http://gerrit.sjc.cloudera.com:8080/22477 Tested-by: Jenkins User Reviewed-by: Alexander Kolbasov <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/sentry/repo Commit: http://git-wip-us.apache.org/repos/asf/sentry/commit/a32f9776 Tree: http://git-wip-us.apache.org/repos/asf/sentry/tree/a32f9776 Diff: http://git-wip-us.apache.org/repos/asf/sentry/diff/a32f9776 Branch: refs/for/cdh5-1.5.1_ha Commit: a32f9776ddf10ad11bab81163276e30098934b0e Parents: 06444b9 Author: Alexander Kolbasov <[email protected]> Authored: Wed May 10 12:37:07 2017 -0700 Committer: Alexander Kolbasov <[email protected]> Committed: Wed May 10 13:37:16 2017 -0700 ---------------------------------------------------------------------- .../main/java/org/apache/sentry/SentryMain.java | 10 ++++---- .../sentry/provider/file/PolicyFiles.java | 25 ++++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sentry/blob/a32f9776/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/SentryMain.java ---------------------------------------------------------------------- diff --git a/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/SentryMain.java b/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/SentryMain.java index 3536570..003f487 100644 --- a/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/SentryMain.java +++ b/sentry-core/sentry-core-common/src/main/java/org/apache/sentry/SentryMain.java @@ -27,7 +27,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableMap; -import java.io.FileInputStream; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Properties; public class SentryMain { @@ -63,9 +65,9 @@ public class SentryMain { Properties log4jProperties = new Properties(); // Firstly load log properties from properties file - FileInputStream istream = new FileInputStream(log4jconf); - log4jProperties.load(istream); - istream.close(); + try (InputStream istream = Files.newInputStream(Paths.get(log4jconf))) { + log4jProperties.load(istream); + } // Set the log level of DataNucleus.Query to INFO only if it is not set in the // properties file http://git-wip-us.apache.org/repos/asf/sentry/blob/a32f9776/sentry-provider/sentry-provider-file/src/main/java/org/apache/sentry/provider/file/PolicyFiles.java ---------------------------------------------------------------------- diff --git a/sentry-provider/sentry-provider-file/src/main/java/org/apache/sentry/provider/file/PolicyFiles.java b/sentry-provider/sentry-provider-file/src/main/java/org/apache/sentry/provider/file/PolicyFiles.java index 4e5d4b9..f143557 100644 --- a/sentry-provider/sentry-provider-file/src/main/java/org/apache/sentry/provider/file/PolicyFiles.java +++ b/sentry-provider/sentry-provider-file/src/main/java/org/apache/sentry/provider/file/PolicyFiles.java @@ -17,11 +17,11 @@ package org.apache.sentry.provider.file; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; @@ -33,7 +33,7 @@ import org.slf4j.LoggerFactory; import com.google.common.io.ByteStreams; import com.google.common.io.Resources; -public class PolicyFiles { +public final class PolicyFiles { private static final Logger LOGGER = LoggerFactory .getLogger(PolicyFiles.class); @@ -42,7 +42,9 @@ public class PolicyFiles { throws FileNotFoundException, IOException { for(String resource : resources) { LOGGER.debug("Copying " + resource + " to " + dest); - Resources.copy(Resources.getResource(resource), new FileOutputStream(new File(dest, resource))); + try (OutputStream output = Files.newOutputStream(new File(dest, resource).toPath())) { + Resources.copy(Resources.getResource(resource), output); + } } } @@ -61,12 +63,12 @@ public class PolicyFiles { public static void copyFilesToDir(FileSystem fs, Path dest, File inputFile) throws IOException { - InputStream input = new FileInputStream(inputFile.getPath()); - FSDataOutputStream out = fs.create(new Path(dest, inputFile.getName())); - ByteStreams.copy(input, out); - input.close(); - out.hflush(); - out.close(); + try (InputStream input = Files.newInputStream(inputFile.toPath()); + FSDataOutputStream out = fs.create(new Path(dest, inputFile.getName()))) { + ByteStreams.copy(input, out); + out.hflush(); + out.close(); + } } @@ -90,4 +92,7 @@ public class PolicyFiles { } } + private PolicyFiles() { + // Make constructor private to avoid instantiation + } }
