From a7bf45989ee51e8e676a21a299fe2d15b3bfe8be Mon Sep 17 00:00:00 2001 From: madrabit Date: Fri, 16 Oct 2020 13:22:54 +0300 Subject: [PATCH] - Strategy pattern - ReadWriteLock - try with resources - set File with constructor - final (constant) unicode char - i + 1 -> i ++ (normal increment) - Buffer for IO --- .gitignore | 1 + Document.php | 49 ------------------- Parser.java | 88 ++++++++++++++++++++--------------- reader/PlaneReaderImpl.java | 20 ++++++++ reader/Reader.java | 13 ++++++ reader/UnicodeReaderImpl.java | 22 +++++++++ 6 files changed, 106 insertions(+), 87 deletions(-) create mode 100644 .gitignore delete mode 100644 Document.php create mode 100644 reader/PlaneReaderImpl.java create mode 100644 reader/Reader.java create mode 100644 reader/UnicodeReaderImpl.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85e7c1d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ diff --git a/Document.php b/Document.php deleted file mode 100644 index 7803df6..0000000 --- a/Document.php +++ /dev/null @@ -1,49 +0,0 @@ - 5); - $this->user = $user; - $this->name = $name; - } - - public function getTitle() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); - return $row[3]; // third column in a row - } - - public function getContent() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); - return $row[6]; // sixth column in a row - } - - public static function getAllDocuments() { - // to be implemented later - } - -} - -class User { - - public function makeNewDocument($name) { - $doc = new Document(); - $doc->init($name, $this); - return $doc; - } - - public function getMyDocuments() { - $list = array(); - foreach (Document::getAllDocuments() as $doc) { - if ($doc->user == $this) - $list[] = $doc; - } - return $list; - } - -} diff --git a/Parser.java b/Parser.java index c13a9fe..1589731 100644 --- a/Parser.java +++ b/Parser.java @@ -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(); + } } - } } diff --git a/reader/PlaneReaderImpl.java b/reader/PlaneReaderImpl.java new file mode 100644 index 0000000..d76bdc2 --- /dev/null +++ b/reader/PlaneReaderImpl.java @@ -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(); + } +} diff --git a/reader/Reader.java b/reader/Reader.java new file mode 100644 index 0000000..8ee80a6 --- /dev/null +++ b/reader/Reader.java @@ -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; +} diff --git a/reader/UnicodeReaderImpl.java b/reader/UnicodeReaderImpl.java new file mode 100644 index 0000000..ee13765 --- /dev/null +++ b/reader/UnicodeReaderImpl.java @@ -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(); + } +}