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

My refactoring #451

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
49 changes: 0 additions & 49 deletions Document.php

This file was deleted.

88 changes: 50 additions & 38 deletions Parser.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import reader.PlaneReaderImpl;
import reader.Reader;
import reader.UnicodeReaderImpl;

import java.io.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
* 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;
private final File file;
private final int UNICODE_END = 0X80;
ReadWriteLock lock = new ReentrantReadWriteLock();

public File getFile() {
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 Parser(File file) {
this.file = file;
}
return output;
}
public void saveContent(String content) {
FileOutputStream o = new FileOutputStream(file);
try {
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
} catch (IOException e) {
e.printStackTrace();

public String getContent() {
return commonContentReader(new PlaneReaderImpl());
}

public String getContentWithoutUnicode() {
return commonContentReader(new UnicodeReaderImpl());
}

private String commonContentReader(Reader reader) {
StringBuilder output = new StringBuilder();
lock.readLock().lock();
try (BufferedInputStream i = new BufferedInputStream(new FileInputStream(file))) {
return reader.getContent(output, i);
} catch (IOException e) {
e.printStackTrace();
} finally {
lock.readLock().unlock();
}
return "";
}

public void saveContent(String content) {
lock.writeLock().lock();
try (FileOutputStream o = new FileOutputStream(file)) {
for (int i = 0; i < content.length(); i++) {
o.write(content.charAt(i));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
lock.writeLock().unlock();
}
}
}
}
20 changes: 20 additions & 0 deletions reader/PlaneReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package reader;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
* @author madrabit on 15.10.2020
* @version 1$
* @since 0.1
*/
public class PlaneReaderImpl implements Reader {
@Override
public String getContent(StringBuilder output, BufferedInputStream i) throws IOException {
int data;
while ((data = i.read()) > 0) {
output.append((char) data);
}
return output.toString();
}
}
13 changes: 13 additions & 0 deletions reader/Reader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package reader;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
* @author madrabit on 15.10.2020
* @version 1$
* @since 0.1
*/
public interface Reader {
String getContent(StringBuilder output, BufferedInputStream i) throws IOException;
}
22 changes: 22 additions & 0 deletions reader/UnicodeReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package reader;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
* @author madrabit on 15.10.2020
* @version 1$
* @since 0.1
*/
public class UnicodeReaderImpl implements Reader {
@Override
public String getContent(StringBuilder output, BufferedInputStream i) throws IOException {
int data;
while ((data = i.read()) > 0) {
if (data < 0x80) {
output.append((char) data);
}
}
return output.toString();
}
}