From 7cda114d8d4c5489861d549dcfeb37931fa8c37c Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Fri, 28 Apr 2017 00:00:59 +0200 Subject: [PATCH] Completed StringListReader class. --- .../strings/BufferedStringListReader.java | 46 -------- .../util/strings/StringListReader.java | 111 +++++++++++++++++- 2 files changed, 105 insertions(+), 52 deletions(-) delete mode 100644 src/fr/devinsy/util/strings/BufferedStringListReader.java diff --git a/src/fr/devinsy/util/strings/BufferedStringListReader.java b/src/fr/devinsy/util/strings/BufferedStringListReader.java deleted file mode 100644 index 3ed2d58..0000000 --- a/src/fr/devinsy/util/strings/BufferedStringListReader.java +++ /dev/null @@ -1,46 +0,0 @@ -package fr.devinsy.util.strings; - -import java.io.BufferedReader; -import java.io.IOException; - -/** - * - * @author Christian Pierre MOMON (christian.momon@devinsy.fr) - * - */ -public class BufferedStringListReader implements StringListReader -{ - private BufferedReader in; - - /** - * - * @param in - */ - public BufferedStringListReader(final BufferedReader in) - { - this.in = in; - } - - /** - * - */ - @Override - public void close() - { - - } - - /** - * - */ - @Override - public String readLine() throws IOException - { - String result; - - result = this.in.readLine(); - - // - return result; - } -} diff --git a/src/fr/devinsy/util/strings/StringListReader.java b/src/fr/devinsy/util/strings/StringListReader.java index b5f852e..465739a 100644 --- a/src/fr/devinsy/util/strings/StringListReader.java +++ b/src/fr/devinsy/util/strings/StringListReader.java @@ -1,23 +1,122 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * This file is part of Devinsy-utils. + * + * Devinsy-utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Devinsy-utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Devinsy-utils. If not, see + */ package fr.devinsy.util.strings; -import java.io.Closeable; +import java.io.BufferedReader; import java.io.IOException; +import java.io.Reader; /** * * @author Christian Pierre MOMON (christian.momon@devinsy.fr) - * */ -public interface StringListReader extends Closeable +public class StringListReader extends Reader { + private StringList in; + private StringListCharIterator iterator; + /** * + * @param in */ - @Override - void close(); + public StringListReader(final StringList in) + { + this.in = in; + this.iterator = new StringListCharIterator(in); + } /** * */ - String readLine() throws IOException; + @Override + public void close() + { + } + + /** + * + */ + @Override + public synchronized int read(final char[] cbuf, final int off, final int len) throws IOException + { + int result; + + BufferedReader a; + + if ((off < 0) || (off > cbuf.length) || (len < 0) || ((off + len) > cbuf.length) || ((off + len) < 0)) + { + throw new IndexOutOfBoundsException(); + } + else if (len == 0) + { + result = 0; + } + else if (this.iterator.hasNext()) + { + // + result = 0; + + // Read off characters. + { + boolean ended = false; + int offCount = 0; + while (!ended) + { + if ((offCount < off) && (this.iterator.hasNext())) + { + this.iterator.next(); + offCount += 1; + result += 1; + } + else + { + ended = true; + } + } + } + + // Read len characters. + { + boolean ended = false; + int lenCount = 0; + while (!ended) + { + if ((lenCount < len) && (this.iterator.hasNext())) + { + char letter = this.iterator.next(); + cbuf[lenCount] = letter; + lenCount += 1; + result += 1; + } + else + { + ended = true; + } + } + } + } + else + { + result = -1; + } + + // + return result; + } }