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

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