Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To: Teamed Subject:Java refactoring #438

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions FileParser.java
Original file line number Diff line number Diff line change
@@ -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;
}
47 changes: 18 additions & 29 deletions Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
33 changes: 33 additions & 0 deletions ParserWithoutUnicode.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}