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

Refactor Parser #437

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
26 changes: 26 additions & 0 deletions File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.io.IOException;

public interface File {
/**
* Get content of the file as string.
*
* @param ignoreUnicodeCharacters
* ignore Unicode characters
* @return File content as a string.
* @throws IOException
* if the file not exists or an I/O error occurs.
*/
String getContent(boolean ignoreUnicodeCharacters) throws IOException;

/**
* Write content to file.
*
* @param content
* content to be written.
* @throws IOException
* if the file exists but is a directory rather than a regular
* file, does not exist but cannot be created, or cannot be
* opened for any other reason or an I/O error occurs.
*/
void saveContent(String content) throws IOException;
}
36 changes: 36 additions & 0 deletions FileImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public final class FileImpl implements File {
private static final int UNICODE_CHAR_LIMIT = 0x80;
private final java.io.File file;

public FileImpl(java.io.File file) {
this.file = file;
}

@Override
public String getContent(boolean ignoreUnicodeCharacters) throws IOException {
try (FileInputStream fileInputStream = new FileInputStream(file)) {
StringBuilder output = new StringBuilder();
int data;
while ((data = fileInputStream.read()) > 0) {
if (ignoreUnicodeCharacters && data < UNICODE_CHAR_LIMIT) {
output.append((char) data);
}
}
return output.toString();
}
}

@Override
public void saveContent(String content) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
for (int i = 0; i < content.length(); i += 1) {
fileOutputStream.write(content.charAt(i));
}
}
}

}
42 changes: 0 additions & 42 deletions Parser.java

This file was deleted.