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

Update Parser.java #442

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
104 changes: 73 additions & 31 deletions Parser.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,84 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* This class is thread safe.
* This class is immutable.
*/
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;

private final File file;
private String content;
private String contentWithoutUnicode;

public Parser(File file) {
this.file = 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 getContent() {
if (content != null) return content;
content = getContentInternal(true);
return content;
}
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));

public String getContentWithoutUnicode() {
if (contentWithoutUnicode != null) return contentWithoutUnicode;
contentWithoutUnicode = getContentInternal(false);
return contentWithoutUnicode;
}

private String getContentInternal(boolean unicode) {
FileInputStream fis = loadStream();
StringBuilder sb = new StringBuilder();
int data;
try {
while ((data = fis.read()) > 0) {
if (unicode) sb.append(data);
else if (data < 0x80) {
sb.append(data);
}
}
} catch (IOException e) {
closeStream(fis);
throw new RuntimeException(/* appropriate message */);
}
closeStream(fis);
return sb.toString();
}

private FileInputStream loadStream() {
FileInputStream fis;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(/* appropriate message */);
}
return fis;
}

private void closeStream(FileInputStream fis) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(/* appropriate message */);
}
}
public void saveContent(String content) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(/* appropriate message */);
}

try {
bos.write(content.getBytes());
bos.close();
} catch (IOException e) {
throw new RuntimeException(/* appropriate message */);
}
}
}
}