diff --git a/FileParser.java b/FileParser.java new file mode 100644 index 0000000..ac53737 --- /dev/null +++ b/FileParser.java @@ -0,0 +1,27 @@ +import java.io.File; +import java.io.IOException; + +/** + * File parser. + */ +public interface FileParser { + /** + * Return current file. + * @return Current file + */ + File file(); + + /** + * Return content. + * @return Content + * @throws IOException if something is wrong + */ + String content() throws IOException; + + /** + * Save content. + * @param Content to save + * @throws IOException if something went wrong + */ + void saveContent(final String content) throws IOException; +} diff --git a/Parser.java b/Parser.java index d6d65d3..14dc187 100644 --- a/Parser.java +++ b/Parser.java @@ -5,38 +5,27 @@ /** * This class is thread safe. */ -public class Parser { - private File file; - public synchronized void setFile(File f) { - file = f; +public class Parser implements FileParser { + private final File file; + public Parser(final File file) { + this.file = file; } - 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; + public synchronized File file() { + return file; } - 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; + public String content() throws IOException { + final FileInputStream i = new FileInputStream(this.file); + final StringBuilder output = new StringBuilder(); + int data; + while ((data = i.read()) > 0) { + output.append((char) data); } - } - return output; + return output.toString(); } - 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)); - } + public void saveContent(final String content) throws IOException { + final FileOutputStream o = new FileOutputStream(this.file); + for (int i = 0; i < content.length(); i += 1) { + o.write(content.charAt(i)); + } } } diff --git a/ParserWithoutUnicode.java b/ParserWithoutUnicode.java new file mode 100644 index 0000000..cf3cd79 --- /dev/null +++ b/ParserWithoutUnicode.java @@ -0,0 +1,33 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +/** + * This class is thread safe. + */ +public class ParserWithoutUnicode implements FileParser { + private final File file; + public ParserWithoutUnicode(final File file) { + this.file = file; + } + public synchronized File file() { + return file; + } + public String content() throws IOException { + final FileInputStream i = new FileInputStream(this.file); + final StringBuilder output = new StringBuilder(); + int data; + while ((data = i.read()) > 0) { + if (data < 0x80) { + output.append((char) data); + } + } + return output.toString(); + } + public void saveContent(final String content) throws IOException { + final FileOutputStream o = new FileOutputStream(this.file); + for (int i = 0; i < content.length(); i += 1) { + o.write(content.charAt(i)); + } + } +}