From 0c3739c8db2c2560632cc19ab9bc3d48e027d555 Mon Sep 17 00:00:00 2001 From: Isaac Parenteau Date: Sun, 21 Jul 2019 10:52:22 -0500 Subject: [PATCH] Updated methods to use path insetad of file from java.nio --- README.md | 2 +- .../configuration/ConfigurationManager.java | 9 +- .../configuration/PropertiesManager.java | 90 ++++++++++++------- .../net/locusworks/common/io/IOUtils.java | 42 +++++---- .../locusworks/common/utils/FileReader.java | 34 +++++-- .../locusworks/common/utils/HashUtils.java | 25 ++++-- .../net/locusworks/test/FileReaderTest.java | 7 +- .../test/PropertiesManagerTest.java | 4 +- 8 files changed, 141 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index fe1da5e..1fe46b5 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ Maven net.locusworks locusworks-commons - 1.1.0-RELEASE + ${latest_version} ``` \ No newline at end of file diff --git a/src/main/java/net/locusworks/common/configuration/ConfigurationManager.java b/src/main/java/net/locusworks/common/configuration/ConfigurationManager.java index 975d1f7..24c5d7e 100644 --- a/src/main/java/net/locusworks/common/configuration/ConfigurationManager.java +++ b/src/main/java/net/locusworks/common/configuration/ConfigurationManager.java @@ -1,8 +1,9 @@ package net.locusworks.common.configuration; -import java.io.File; import java.io.IOException; import java.lang.reflect.Field; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; @@ -20,7 +21,7 @@ public class ConfigurationManager { private Properties configuration; private Properties defaults = null; - private File conf = null; + private Path conf = null; protected AES aes; @@ -39,7 +40,7 @@ public class ConfigurationManager { throw ex; } // create patchrepoConf File object - conf = new File(String.format("%s/%s", baseDir, propertiesFile)); + conf = Paths.get(baseDir).resolve(propertiesFile); loadConfiguration(); } @@ -88,7 +89,7 @@ public class ConfigurationManager { * @throws Exception general exception */ public void saveToConf(Properties confs) throws Exception { - PropertiesManager.saveConfiguration(confs, conf, conf.getName()); + PropertiesManager.saveConfiguration(confs, conf, conf.getFileName().toString()); callbackMessage("Saved config file: " + conf + ", " + confs.size() + " entries"); loadConfiguration(); } diff --git a/src/main/java/net/locusworks/common/configuration/PropertiesManager.java b/src/main/java/net/locusworks/common/configuration/PropertiesManager.java index 54da672..c99a16d 100644 --- a/src/main/java/net/locusworks/common/configuration/PropertiesManager.java +++ b/src/main/java/net/locusworks/common/configuration/PropertiesManager.java @@ -4,11 +4,12 @@ import static net.locusworks.common.Charsets.UTF_8; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; @@ -41,17 +42,31 @@ public class PropertiesManager { } /** - * Load configuration from a file + * Load configuration from a file. This method has been deprecated and may be removed in + * future released. Please use java.nio.Path * @param file File to load * @return properties * @throws IOException Exception thrown the file can't be read */ + @Deprecated public static Properties loadConfiguration(File file) throws IOException { - if (!file.exists()) { + return loadConfiguration(file.toPath()); + } + + /** + * Load configuration from a file. + * @param path the path to the file + * @return properties + * @throws IOException Exception thrown the file can't be read + */ + public static Properties loadConfiguration(Path path) throws IOException { + if (Files.notExists(path)) { return new Properties(); } - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8)); - return loadConfiguration(br); + + try(BufferedReader br = Files.newBufferedReader(path)) { + return loadConfiguration(br); + } } /** @@ -74,19 +89,19 @@ public class PropertiesManager { */ public static Map addConfiguration(Properties to, Properties from) { Map results = from.entrySet() - .stream() - .filter(entry -> !to.containsKey(entry.getKey())) - .map(entry -> { - String key = entry.getKey().toString(); - String value = entry.getValue().toString(); - to.put(key, value); - return new Pair(key, value); - }) - .collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); - + .stream() + .filter(entry -> !to.containsKey(entry.getKey())) + .map(entry -> { + String key = entry.getKey().toString(); + String value = entry.getValue().toString(); + to.put(key, value); + return new Pair(key, value); + }) + .collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); + return results; } - + /** * Removes configuration values that are not present in the comparedTo * @param from Properties file to remove values from @@ -95,29 +110,42 @@ public class PropertiesManager { */ public static Map removeConfiguration(Properties from, Properties comparedTo) { Map results = from.keySet() - .stream() - .filter(key -> !comparedTo.containsKey(key)) //only get the items that are not in the comparedTo properties - .map(key -> new Pair(String.valueOf(key), String.valueOf(from.get(key)))) - .collect(Collectors.toList()) //Create a list of paired items (key value) of the items that were filtered - .stream() - .map(pair -> { //remove those pairs from the from properties - from.remove(pair.getValue1()); - return pair; - }) - .collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); //create a map of what was removed - + .stream() + .filter(key -> !comparedTo.containsKey(key)) //only get the items that are not in the comparedTo properties + .map(key -> new Pair(String.valueOf(key), String.valueOf(from.get(key)))) + .collect(Collectors.toList()) //Create a list of paired items (key value) of the items that were filtered + .stream() + .map(pair -> { //remove those pairs from the from properties + from.remove(pair.getValue1()); + return pair; + }) + .collect(Collectors.toMap(key -> key.getValue1(), value -> value.getValue2())); //create a map of what was removed + return results; } /** - * Save the properties file to disk + *

Save the properties file to disk.

+ *

This method has been depecreated and could be removed in future release.
Please use java.nio.Path

* @param props Properties file to save * @param fileToSave File to save to * @param comment Any comments to add */ + @Deprecated public static void saveConfiguration(Properties props, File fileToSave, String comment) { - try(FileOutputStream fos = new FileOutputStream(fileToSave)) { - props.store(fos, comment == null ? "" : comment); + saveConfiguration(props, fileToSave.toPath(), comment); + + } + + /** + *

Save the properties file to disk.

+ * @param props Properties file to save + * @param fileToSave File to save to + * @param comment Any comments to add + */ + public static void saveConfiguration(Properties props, Path fileToSave, String comment) { + try(OutputStream fos = Files.newOutputStream(fileToSave)) { + props.store(fos, comment == null ? "" : comment); } catch (IOException ex) { throw new RuntimeException(ex.getMessage(), ex); } diff --git a/src/main/java/net/locusworks/common/io/IOUtils.java b/src/main/java/net/locusworks/common/io/IOUtils.java index 11cb25a..7ce89f1 100644 --- a/src/main/java/net/locusworks/common/io/IOUtils.java +++ b/src/main/java/net/locusworks/common/io/IOUtils.java @@ -19,16 +19,17 @@ package net.locusworks.common.io; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -199,7 +200,7 @@ public class IOUtils { } return (int) count; } - + /** * Copies chars from a large (over 2GB) Reader to a Writer. *

@@ -308,38 +309,49 @@ public class IOUtils { return sw.toString(); } } - + public static void writeStringToFile(String fileName, String data) throws IOException { - writeStringToFile(new File(fileName), data, Charsets.UTF_8); + writeStringToFile(Paths.get(fileName), data, Charsets.UTF_8); } - + public static void writeStringToFile(String fileName, String data, Charset charset) throws IOException { - writeStringToFile(new File(fileName), data, charset); + writeStringToFile(Paths.get(fileName), data, charset); } - - public static void writeStringToFile(File file, String data) throws IOException { + + public static void writeStringToFile(Path file, String data) throws IOException { writeStringToFile(file, data, Charsets.UTF_8); } + @Deprecated public static void writeStringToFile(File file, String data, Charset charset) throws IOException { - try(Writer writer = new OutputStreamWriter(new FileOutputStream(file), charset)) { + writeStringToFile(file.toPath(), data, charset); + } + + public static void writeStringToFile(Path file, String data, Charset charset) throws IOException { + try(Writer writer = Files.newBufferedWriter(file, charset)) { writer.write(data); writer.flush(); } } public static void deleteFile(String fileName) { - deleteFile(new File(fileName)); + deleteFile(Paths.get(fileName)); } + @Deprecated public static void deleteFile(File file) { - if (file.exists()) { - file.delete(); - file.deleteOnExit(); + deleteFile(file.toPath()); + } + + public static void deleteFile(Path file) { + try { + Files.deleteIfExists(file); + } catch (IOException ex) { + throw new IllegalArgumentException(ex); } } - + public static void deleteFiles(String... fileNames) { Arrays.asList(fileNames).forEach(file -> deleteFile(file)); } diff --git a/src/main/java/net/locusworks/common/utils/FileReader.java b/src/main/java/net/locusworks/common/utils/FileReader.java index 34fa588..1a2741b 100644 --- a/src/main/java/net/locusworks/common/utils/FileReader.java +++ b/src/main/java/net/locusworks/common/utils/FileReader.java @@ -4,9 +4,12 @@ import static net.locusworks.common.Charsets.UTF_8; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Iterator; import java.util.NoSuchElementException; @@ -36,7 +39,16 @@ public class FileReader implements AutoCloseableIterator, I * Constructor * @param file File to read */ + @Deprecated public FileReader(File file) { + init(file.toPath()); + } + + /** + * Constructor + * @param file File to read + */ + public FileReader(Path file) { init(file); } @@ -56,8 +68,8 @@ public class FileReader implements AutoCloseableIterator, I */ private void init(String fileName) { //check to see if the file exists - File f = new File(fileName); - if (f.exists()) { + Path f = Paths.get(fileName); + if (Files.exists(f)) { init(f); //If it does. load through the file initializer return; } @@ -73,19 +85,23 @@ public class FileReader implements AutoCloseableIterator, I } //Call the buffered reader initializer once the file is found - init(new BufferedReader(new InputStreamReader(is, UTF_8))); + try(BufferedReader br = new BufferedReader(new InputStreamReader(is, UTF_8))) { + init(br); + } catch (IOException e) { + throw new IllegalArgumentException(e); + } } /** * Initializer helper to load file * @param file File to load */ - private void init(File file) { + private void init(Path file) { if (file == null) throw new IllegalArgumentException("File cannot be null"); - if (!file.exists()) throw new IllegalArgumentException("File " + file + " does not exist"); - if (!file.isFile()) throw new IllegalArgumentException("File " + file + " is not a file"); - try { - init(new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8))); + if (Files.notExists(file)) throw new IllegalArgumentException("File " + file + " does not exist"); + if (!Files.isRegularFile(file)) throw new IllegalArgumentException("File " + file + " is not a file"); + try (BufferedReader br = Files.newBufferedReader(file)) { + init(br); } catch (Exception ex) { throw new RuntimeException(ex); } diff --git a/src/main/java/net/locusworks/common/utils/HashUtils.java b/src/main/java/net/locusworks/common/utils/HashUtils.java index ead20e9..054d180 100644 --- a/src/main/java/net/locusworks/common/utils/HashUtils.java +++ b/src/main/java/net/locusworks/common/utils/HashUtils.java @@ -3,11 +3,12 @@ package net.locusworks.common.utils; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.security.MessageDigest; /** @@ -43,7 +44,7 @@ public class HashUtils { public static String hash(String hashType, String data) { return hash(hashType, data, true); } - + /** * Hash a string literal * @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512) @@ -55,6 +56,11 @@ public class HashUtils { byte[] stringData = data.getBytes(UTF_8); return hash(hashType, stringData, toLower); } + + @Deprecated + public static String hash(String hashType, File data) { + return hash(hashType, data.toPath()); + } /** * Hash a file @@ -62,9 +68,14 @@ public class HashUtils { * @param data File to hash * @return hash value of the file */ - public static String hash(String hashType, File data) { + public static String hash(String hashType, Path data) { return hash(hashType, data, true); } + + @Deprecated + public static String hash(String hashType, File data, boolean toLower) { + return hash(hashType, data.toPath(), toLower); + } /** * Hash a file @@ -73,14 +84,12 @@ public class HashUtils { * @param toLower True to output the hash in lower case. False to output in upper case * @return hash value of the file */ - public static String hash(String hashType, File data, boolean toLower) { - InputStream stream; - try { - stream = new FileInputStream(data); + public static String hash(String hashType, Path data, boolean toLower) { + try (InputStream stream = Files.newInputStream(data)) { + return hash(stream, hashType, toLower); } catch (IOException ex) { throw new IllegalArgumentException(ex.getMessage()); } - return hash(stream, hashType, toLower); } /** diff --git a/src/test/java/net/locusworks/test/FileReaderTest.java b/src/test/java/net/locusworks/test/FileReaderTest.java index 7453ba3..5e8b6f6 100644 --- a/src/test/java/net/locusworks/test/FileReaderTest.java +++ b/src/test/java/net/locusworks/test/FileReaderTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.*; import java.io.File; import java.io.FileOutputStream; +import java.nio.file.Paths; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -48,7 +49,7 @@ public class FileReaderTest { @Test public void testForLoop() { Integer lineCount = 0; - for(LineInfo s : new FileReader(new File(TEST_FILE))) { + for(LineInfo s : new FileReader(Paths.get(TEST_FILE))) { lineCount++; Integer lineNumber = s.getLineNumber(); Integer lineLength = s.getLine().length(); @@ -62,7 +63,7 @@ public class FileReaderTest { public void testIterator() { Integer lineCount = 0; - try(AutoCloseableIterator iter = new FileReader(new File(TEST_FILE))) { + try(AutoCloseableIterator iter = new FileReader(Paths.get(TEST_FILE))) { while(iter.hasNext()) { lineCount++; LineInfo s = iter.next(); @@ -78,7 +79,7 @@ public class FileReaderTest { @Test public void testForIterator() { Integer lineCount = 0; - for(Iterator iter = new FileReader(new File(TEST_FILE)); iter.hasNext();) { + for(Iterator iter = new FileReader(Paths.get(TEST_FILE)); iter.hasNext();) { lineCount++; LineInfo s = iter.next(); Integer lineNumber = s.getLineNumber(); diff --git a/src/test/java/net/locusworks/test/PropertiesManagerTest.java b/src/test/java/net/locusworks/test/PropertiesManagerTest.java index 7ab0684..0c9f166 100644 --- a/src/test/java/net/locusworks/test/PropertiesManagerTest.java +++ b/src/test/java/net/locusworks/test/PropertiesManagerTest.java @@ -4,6 +4,8 @@ import static org.junit.Assert.*; import java.io.File; import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Properties; import org.junit.AfterClass; @@ -107,7 +109,7 @@ public class PropertiesManagerTest { public void testSaveConfiguration() { try { Properties props = PropertiesManager.loadConfiguration(this.getClass(), PROPERTIES_FILE); - File tmpFile = new File(TMP_PROPS); + Path tmpFile = Paths.get(TMP_PROPS); PropertiesManager.saveConfiguration(props, tmpFile, "test propertis"); Properties tmp = PropertiesManager.loadConfiguration(tmpFile); assertTrue(tmp.keySet().size() == ENTRY_SIZE);