From 37ad98e74c68f6e472434c7e1f0abf8168ee4509 Mon Sep 17 00:00:00 2001 From: mrmx Date: Mon, 22 Aug 2016 21:48:30 +0200 Subject: [PATCH 1/5] Reformatted and make file final using ctor for init --- Parser.java | 74 +++++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/Parser.java b/Parser.java index d6d65d3..e3d8237 100644 --- a/Parser.java +++ b/Parser.java @@ -1,42 +1,50 @@ + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; + /** * This class is thread safe. */ public class Parser { - private File file; - 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; - } - public String getContentWithoutUnicode() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - if (data < 0x80) { - output += (char) data; - } - } - 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)); - } - } + + private final File file; + + public Parser(File file) { + this.file = file; + } + + public 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; + } + + public String getContentWithoutUnicode() throws IOException { + FileInputStream i = new FileInputStream(file); + String output = ""; + int data; + while ((data = i.read()) > 0) { + if (data < 0x80) { + output += (char) data; + } + } + 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)); + } + } } From 34a1d2265e57bf9938805b2e0a99c4d3343058d1 Mon Sep 17 00:00:00 2001 From: mrmx Date: Mon, 22 Aug 2016 21:52:31 +0200 Subject: [PATCH 2/5] Refactored saveContent to handle argument and saving using content bytes --- Parser.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Parser.java b/Parser.java index e3d8237..2caeb25 100644 --- a/Parser.java +++ b/Parser.java @@ -40,11 +40,17 @@ 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)); + if(content == null) { + throw new IllegalArgumentException("no content"); + } + FileOutputStream os = new FileOutputStream(file); + try { + os.write(content.getBytes()); + os.flush(); + } finally { + os.close(); } } } From 7c4b1620544adee16914289dd582f25870cef52a Mon Sep 17 00:00:00 2001 From: mrmx Date: Mon, 22 Aug 2016 22:15:55 +0200 Subject: [PATCH 3/5] Final refactor using inner interface for extension and default impls for content processing strategies --- Parser.java | 71 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 16 deletions(-) diff --git a/Parser.java b/Parser.java index 2caeb25..9d2f0a3 100644 --- a/Parser.java +++ b/Parser.java @@ -14,35 +14,37 @@ public class Parser { public Parser(File file) { this.file = file; } - + public File getFile() { return file; } + public String getContentWithoutUnicode() throws IOException { + return getContent(new UnicodeFilteredContentProcessor()); + } + public String getContent() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - output += (char) data; - } - return output; + return getContent(new DefaultContentProcessor()); } - public String getContentWithoutUnicode() throws IOException { + public String getContent(ContentProcessor contentProcessor) throws IOException { + if (contentProcessor == null) { + throw new IllegalArgumentException("no content processor"); + } FileInputStream i = new FileInputStream(file); - String output = ""; int data; - while ((data = i.read()) > 0) { - if (data < 0x80) { - output += (char) data; + try { + while ((data = i.read()) != -1) { + contentProcessor.process(data); } + } finally { + i.close(); } - return output; + return contentProcessor.getContent(); } - + public void saveContent(String content) throws IOException { - if(content == null) { + if (content == null) { throw new IllegalArgumentException("no content"); } FileOutputStream os = new FileOutputStream(file); @@ -53,4 +55,41 @@ public void saveContent(String content) throws IOException { os.close(); } } + + public static interface ContentProcessor { + + void process(int data); + + String getContent(); + } + + static class DefaultContentProcessor implements ContentProcessor { + + private StringBuilder contentBuilder; + + public DefaultContentProcessor() { + contentBuilder = new StringBuilder(); + } + + @Override + public void process(int data) { + contentBuilder.append(data); + } + + @Override + public String getContent() { + return contentBuilder.toString(); + } + + } + + static class UnicodeFilteredContentProcessor extends DefaultContentProcessor { + + @Override + public void process(int data) { + if (data < 0x80) { + super.process(data); + } + } + } } From 0daf0a1a243a67da2b50b78ec66e588285c9c9fa Mon Sep 17 00:00:00 2001 From: mrmx Date: Sat, 3 Sep 2016 13:15:20 +0200 Subject: [PATCH 4/5] Fixed format --- Parser.java | 166 ++++++++++++++++++++++++++-------------------------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/Parser.java b/Parser.java index 9d2f0a3..620ac4a 100644 --- a/Parser.java +++ b/Parser.java @@ -9,87 +9,87 @@ */ public class Parser { - private final File file; - - public Parser(File file) { - this.file = file; - } - - public File getFile() { - return file; - } - - public String getContentWithoutUnicode() throws IOException { - return getContent(new UnicodeFilteredContentProcessor()); - } - - public String getContent() throws IOException { - return getContent(new DefaultContentProcessor()); - } - - public String getContent(ContentProcessor contentProcessor) throws IOException { - if (contentProcessor == null) { - throw new IllegalArgumentException("no content processor"); - } - FileInputStream i = new FileInputStream(file); - int data; - try { - while ((data = i.read()) != -1) { - contentProcessor.process(data); - } - } finally { - i.close(); - } - return contentProcessor.getContent(); - } - - public void saveContent(String content) throws IOException { - if (content == null) { - throw new IllegalArgumentException("no content"); - } - FileOutputStream os = new FileOutputStream(file); - try { - os.write(content.getBytes()); - os.flush(); - } finally { - os.close(); - } - } - - public static interface ContentProcessor { - - void process(int data); - - String getContent(); - } - - static class DefaultContentProcessor implements ContentProcessor { - - private StringBuilder contentBuilder; - - public DefaultContentProcessor() { - contentBuilder = new StringBuilder(); - } - - @Override - public void process(int data) { - contentBuilder.append(data); - } - - @Override - public String getContent() { - return contentBuilder.toString(); - } - - } - - static class UnicodeFilteredContentProcessor extends DefaultContentProcessor { - - @Override - public void process(int data) { - if (data < 0x80) { - super.process(data); - } - } - } + private final File file; + + public Parser(File file) { + this.file = file; + } + + public File getFile() { + return file; + } + + public String getContentWithoutUnicode() throws IOException { + return getContent(new UnicodeFilteredContentProcessor()); + } + + public String getContent() throws IOException { + return getContent(new DefaultContentProcessor()); + } + + public String getContent(ContentProcessor contentProcessor) throws IOException { + if (contentProcessor == null) { + throw new IllegalArgumentException("no content processor"); + } + FileInputStream i = new FileInputStream(file); + int data; + try { + while ((data = i.read()) != -1) { + contentProcessor.process(data); + } + } finally { + i.close(); + } + return contentProcessor.getContent(); + } + + public void saveContent(String content) throws IOException { + if (content == null) { + throw new IllegalArgumentException("no content"); + } + FileOutputStream os = new FileOutputStream(file); + try { + os.write(content.getBytes()); + os.flush(); + } finally { + os.close(); + } + } + + public static interface ContentProcessor { + + void process(int data); + + String getContent(); + } + + static class DefaultContentProcessor implements ContentProcessor { + + private StringBuilder contentBuilder; + + public DefaultContentProcessor() { + contentBuilder = new StringBuilder(); + } + + @Override + public void process(int data) { + contentBuilder.append(data); + } + + @Override + public String getContent() { + return contentBuilder.toString(); + } + + } + + static class UnicodeFilteredContentProcessor extends DefaultContentProcessor { + + @Override + public void process(int data) { + if (data < 0x80) { + super.process(data); + } + } + } } From 4bab30cb24efccc6776f39fa721812b0bbae82be Mon Sep 17 00:00:00 2001 From: mrmx Date: Sat, 3 Sep 2016 13:36:54 +0200 Subject: [PATCH 5/5] Made api change removing getContentWithoutUnicode enforcing getContent --- Parser.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Parser.java b/Parser.java index 620ac4a..481aca6 100644 --- a/Parser.java +++ b/Parser.java @@ -19,12 +19,15 @@ public File getFile() { return file; } - public String getContentWithoutUnicode() throws IOException { - return getContent(new UnicodeFilteredContentProcessor()); + public String getContent() throws IOException { + return getContent(true); } - public String getContent() throws IOException { - return getContent(new DefaultContentProcessor()); + public String getContent(boolean filterUnicode) throws IOException { + return getContent(filterUnicode + ? new UnicodeFilteredContentProcessor() + : new DefaultContentProcessor() + ); } public String getContent(ContentProcessor contentProcessor) throws IOException { @@ -65,7 +68,7 @@ public static interface ContentProcessor { static class DefaultContentProcessor implements ContentProcessor { - private StringBuilder contentBuilder; + private final StringBuilder contentBuilder; public DefaultContentProcessor() { contentBuilder = new StringBuilder();