(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);