Completed StringListReader class.
This commit is contained in:
parent
0a7d17ebc0
commit
7cda114d8d
2 changed files with 105 additions and 52 deletions
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue