Class StringBuilderReader
- java.lang.Object
-
- java.io.Reader
-
- org.apache.lucene.benchmark.byTask.utils.StringBuilderReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
public class StringBuilderReader extends Reader
Implements aReader
over aStringBuilder
instance. Although one can useStringReader
by passing itStringBuilder.toString()
, it is better to use this class, as it doesn't mark the passed-inStringBuilder
as shared (which will cause inner char[] allocations at the next append() attempt).
Notes:- This implementation assumes the underlying
StringBuilder
is not changed during the use of thisReader
implementation. - This implementation is thread-safe.
- The implementation looks very much like
StringReader
(for the right reasons). - If one wants to reuse that instance, then the following needs to be done:
StringBuilder sb = new StringBuilder("some text"); Reader reader = new StringBuilderReader(sb); ... read from reader - don't close it ! ... sb.setLength(0); sb.append("some new text"); reader.reset(); ... read the new string from the reader ...
-
-
Constructor Summary
Constructors Constructor Description StringBuilderReader(StringBuilder sb)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
mark(int readAheadLimit)
Mark the present position in the stream.boolean
markSupported()
int
read()
int
read(char[] cbuf, int off, int len)
boolean
ready()
void
reset()
void
set(StringBuilder sb)
long
skip(long ns)
-
Methods inherited from class java.io.Reader
nullReader, read, read, transferTo
-
-
-
-
Constructor Detail
-
StringBuilderReader
public StringBuilderReader(StringBuilder sb)
-
-
Method Detail
-
close
public void close()
-
mark
public void mark(int readAheadLimit) throws IOException
Mark the present position in the stream. Subsequent calls to reset() will reposition the stream to this point.- Overrides:
mark
in classReader
- Parameters:
readAheadLimit
- Limit on the number of characters that may be read while still preserving the mark. Because the stream's input comes from a StringBuilder, there is no actual limit, so this argument must not be negative, but is otherwise ignored.- Throws:
IllegalArgumentException
- If readAheadLimit is < 0IOException
- If an I/O error occurs
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classReader
-
read
public int read() throws IOException
- Overrides:
read
in classReader
- Throws:
IOException
-
read
public int read(char[] cbuf, int off, int len) throws IOException
- Specified by:
read
in classReader
- Throws:
IOException
-
ready
public boolean ready() throws IOException
- Overrides:
ready
in classReader
- Throws:
IOException
-
reset
public void reset() throws IOException
- Overrides:
reset
in classReader
- Throws:
IOException
-
set
public void set(StringBuilder sb)
-
skip
public long skip(long ns) throws IOException
- Overrides:
skip
in classReader
- Throws:
IOException
-
-