Updated methods to use path insetad of file from java.nio

This commit is contained in:
Isaac Parenteau
2019-07-21 10:52:22 -05:00
parent 4000f46e71
commit 0c3739c8db
8 changed files with 141 additions and 72 deletions

View File

@ -12,6 +12,6 @@ Maven
<dependency>
<groupId>net.locusworks</groupId>
<artifactId>locusworks-commons</artifactId>
<version>1.1.0-RELEASE</version>
<version>${latest_version}</version>
</dependency>
```

View File

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

View File

@ -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,18 +42,32 @@ 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));
try(BufferedReader br = Files.newBufferedReader(path)) {
return loadConfiguration(br);
}
}
/**
* Load configuration from a buffered reader
@ -110,13 +125,26 @@ public class PropertiesManager {
}
/**
* Save the properties file to disk
* <p>Save the properties file to disk.</p>
* <p><b>This method has been depecreated and could be removed in future release.<br/>Please use java.nio.Path</b></p>
* @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)) {
saveConfiguration(props, fileToSave.toPath(), comment);
}
/**
* <p>Save the properties file to disk.</p>
* @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);

View File

@ -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;
@ -310,33 +311,44 @@ public class IOUtils {
}
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);
}
}

View File

@ -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<FileReader.LineInfo>, 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<FileReader.LineInfo>, 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<FileReader.LineInfo>, 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);
}

View File

@ -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;
/**
@ -56,16 +57,26 @@ public class HashUtils {
return hash(hashType, stringData, toLower);
}
@Deprecated
public static String hash(String hashType, File data) {
return hash(hashType, data.toPath());
}
/**
* Hash a file
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
* @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
* @param hashType Hash types supported by MessageDigest (i.e MD5, SHA-1, SHA-512)
@ -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);
}
/**

View File

@ -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<LineInfo> iter = new FileReader(new File(TEST_FILE))) {
try(AutoCloseableIterator<LineInfo> 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<LineInfo> iter = new FileReader(new File(TEST_FILE)); iter.hasNext();) {
for(Iterator<LineInfo> iter = new FileReader(Paths.get(TEST_FILE)); iter.hasNext();) {
lineCount++;
LineInfo s = iter.next();
Integer lineNumber = s.getLineNumber();

View File

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