From eb63a7b34b73e406dcff9f267183408efc3e5041 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 17 Oct 2016 01:21:35 -0500 Subject: [PATCH 1/2] Initial refactoring pass. --- .gitignore | 13 +++++++++++++ Parser.java | 24 +++++++++++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..67b3b98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +*~ diff --git a/Parser.java b/Parser.java index d6d65d3..d60b7e8 100644 --- a/Parser.java +++ b/Parser.java @@ -2,26 +2,30 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; + /** * This class is thread safe. */ public class Parser { private File file; + //I would remove the setter/getter for file and make it a parameter to the constructor. + //If a user needs to change the file, it should require a new parser. public synchronized void setFile(File f) { file = f; } public synchronized File getFile() { return file; } + public String getContent() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - output += (char) data; - } - return output; + byte[] bytes = Files.readAllBytes(file.getPath()); + return new String(bytes); } + //TODO: implement a FilteredInputStream that takes a lambda for which characters to skip + //this would allow alternate cases to be easily implemented and use that stream to read + //the character. Also would use a stringbuffer instead of string concat (more performant) public String getContentWithoutUnicode() throws IOException { FileInputStream i = new FileInputStream(file); String output = ""; @@ -34,9 +38,7 @@ public String getContentWithoutUnicode() throws IOException { return output; } public void saveContent(String content) throws IOException { - FileOutputStream o = new FileOutputStream(file); - for (int i = 0; i < content.length(); i += 1) { - o.write(content.charAt(i)); - } + PrintWriter writer = new PrintWriter(file); + writer.print(content); } } From f5c4683d991e3582175ad57f71bd7c2068ccc4d7 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 17 Oct 2016 01:26:25 -0500 Subject: [PATCH 2/2] Forgot to use a path for the readAllBytes method --- Parser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser.java b/Parser.java index d60b7e8..c42e397 100644 --- a/Parser.java +++ b/Parser.java @@ -20,7 +20,7 @@ public synchronized File getFile() { } public String getContent() throws IOException { - byte[] bytes = Files.readAllBytes(file.getPath()); + byte[] bytes = Files.readAllBytes(file.toPath()); return new String(bytes); } //TODO: implement a FilteredInputStream that takes a lambda for which characters to skip