Files
commons/src/main/java/net/locusworks/common/utils/FileReader.java
Isaac Parenteau 79529ecc40 Initial Commit
2019-07-20 12:39:03 -05:00

203 lines
4.8 KiB
Java

package net.locusworks.common.utils;
import static net.locusworks.common.Charsets.UTF_8;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.NoSuchElementException;
import net.locusworks.common.interfaces.AutoCloseableIterator;
/**
* Class to read in a file that can be used in the try-with-resource block
* @author Isaac Parenteau
* @version 1.0.0
* @date 02/15/2018
*/
public class FileReader implements AutoCloseableIterator<FileReader.LineInfo>, Iterable<FileReader.LineInfo> {
private BufferedReader reader;
private LineInfo info;
private Integer lineNumber;
/**
* Constructor
* @param fileName Name of the file to read
*/
public FileReader(String fileName) {
init(fileName);
}
/**
* Constructor
* @param file File to read
*/
public FileReader(File file) {
init(file);
}
/**
* Constructor
* @param reader Buffered reader to read data from
*/
public FileReader(BufferedReader reader) {
init(reader);
}
/**
* Initialization helper
* @param fileName Name of the file to load
* This will look into the resources directory if it cannot
* find the file directly.
*/
private void init(String fileName) {
//check to see if the file exists
File f = new File(fileName);
if (f.exists()) {
init(f); //If it does. load through the file initializer
return;
}
//Check to see if the file is in the resources directory
InputStream is = this.getClass().getResourceAsStream(fileName);
if (is == null) {
is = this.getClass().getClassLoader().getResourceAsStream(fileName);
}
//If it cant be found, throw a runtime exception
if (is == null) {
throw new IllegalArgumentException("Unable to find resource with name of" + fileName);
}
//Call the buffered reader initializer once the file is found
init(new BufferedReader(new InputStreamReader(is, UTF_8)));
}
/**
* Initializer helper to load file
* @param file File to load
*/
private void init(File 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)));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/**
* Initializer helper for buffered reader
* This is ultimately where all initializers end as a buffered reader
* @param reader buffered reader to load
*/
private void init(BufferedReader reader) {
this.reader = reader;
this.lineNumber = 0;
}
@Override
public boolean hasNext() {
try {
String line = this.reader.readLine();
if (line == null) {
this.close();
this.info = null;
return false;
}
this.lineNumber++;
this.info = new LineInfo(this.lineNumber, line);
return true;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public LineInfo next() {
if (this.info == null) {
throw new NoSuchElementException("Call to next was initiated but there are no more elements to read");
}
return this.info;
}
@Override
public Iterator<LineInfo> iterator() {
return this;
}
@Override
public void close() {
if (this.reader != null) {
try {
this.reader.close();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
public static class LineInfo {
private Integer lineNumber;
private Integer lineLength;
private String line;
/**
* @param lineNumber the current line number in the file
* @param line the line information from the file
*/
public LineInfo(Integer lineNumber, String line) {
this.lineNumber = lineNumber;
this.line = line;
this.lineLength = line.length();
}
/**
* @return the lineNumber
*/
public Integer getLineNumber() {
return lineNumber;
}
/**
* @param lineNumber the lineNumber to set
*/
public void setLineNumber(Integer lineNumber) {
this.lineNumber = lineNumber;
}
/**
* @return the lineLength
*/
public Integer getLineLength() {
return lineLength;
}
/**
* @param lineLength the lineLength to set
*/
public void setLineLength(Integer lineLength) {
this.lineLength = lineLength;
}
/**
* @return the line
*/
public String getLine() {
return line;
}
/**
* @param line the line to set
*/
public void setLine(String line) {
this.line = line;
}
}
}