Package org.apache.commons.text.io
Class StringSubstitutorReader
- java.lang.Object
-
- java.io.Reader
-
- java.io.FilterReader
-
- org.apache.commons.text.io.StringSubstitutorReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Readable
public class StringSubstitutorReader extends java.io.FilterReader
AReader
that performs string substitution on a sourceReader
using aStringSubstitutor
.Using this Reader avoids reading a whole file into memory as a
String
to perform string substitution, for example, when a Servlet filters a file to a client.This class is not thread-safe.
- Since:
- 1.9
-
-
Field Summary
Fields Modifier and Type Field Description private TextStringBuilder
buffer
Our internal buffer.private boolean
eos
End-of-Stream flag.private static int
EOS
The end-of-stream character marker.private StringMatcher
prefixEscapeMatcher
Matches escaped variable starts.private char[]
read1CharBuffer
Internal buffer forread()
method.private StringSubstitutor
stringSubstitutor
The underlying StringSubstitutor.private int
toDrain
We don't always want to drain the whole buffer.
-
Constructor Summary
Constructors Constructor Description StringSubstitutorReader(java.io.Reader reader, StringSubstitutor stringSubstitutor)
Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
buffer(int requestReadCount)
Buffers the requested number of characters if available.private int
bufferOrDrainOnEos(int requestReadCount, char[] target, int targetIndex, int targetLength)
Reads a requested number of chars from the underlying reader into the buffer.private int
drain(char[] target, int targetIndex, int targetLength)
Drains characters from our buffer to the giventarget
.private int
drainOnEos(int readCountOrEos, char[] target, int targetIndex, int targetLength)
Drains from the buffer to the target only if we are at EOS per the input count.private boolean
isBufferMatchAt(StringMatcher stringMatcher, int pos)
Tests if our buffer matches the given string matcher at the given position in the buffer.private boolean
isDraining()
Tests if we are draining.int
read()
Reads a single character.int
read(char[] target, int targetIndexIn, int targetLengthIn)
Reads characters into a portion of an array.private int
readCount(int count, int pos)
Returns how many chars to attempt reading to have room in the buffer forcount
chars starting at positionpos
.
-
-
-
Field Detail
-
EOS
private static final int EOS
The end-of-stream character marker.- See Also:
- Constant Field Values
-
buffer
private final TextStringBuilder buffer
Our internal buffer.
-
eos
private boolean eos
End-of-Stream flag.
-
prefixEscapeMatcher
private final StringMatcher prefixEscapeMatcher
Matches escaped variable starts.
-
read1CharBuffer
private final char[] read1CharBuffer
Internal buffer forread()
method.
-
stringSubstitutor
private final StringSubstitutor stringSubstitutor
The underlying StringSubstitutor.
-
toDrain
private int toDrain
We don't always want to drain the whole buffer.
-
-
Constructor Detail
-
StringSubstitutorReader
public StringSubstitutorReader(java.io.Reader reader, StringSubstitutor stringSubstitutor)
Constructs a new instance.- Parameters:
reader
- the underlying reader containing the template text known to the givenStringSubstitutor
.stringSubstitutor
- How to replace as we read.- Throws:
java.lang.NullPointerException
- ifreader
isnull
.java.lang.NullPointerException
- ifstringSubstitutor
isnull
.
-
-
Method Detail
-
buffer
private int buffer(int requestReadCount) throws java.io.IOException
Buffers the requested number of characters if available.- Throws:
java.io.IOException
-
bufferOrDrainOnEos
private int bufferOrDrainOnEos(int requestReadCount, char[] target, int targetIndex, int targetLength) throws java.io.IOException
Reads a requested number of chars from the underlying reader into the buffer. On EOS, set the state is DRAINING, drain, and return a drain count, otherwise, returns the actual read count.- Throws:
java.io.IOException
-
drain
private int drain(char[] target, int targetIndex, int targetLength)
Drains characters from our buffer to the giventarget
.
-
drainOnEos
private int drainOnEos(int readCountOrEos, char[] target, int targetIndex, int targetLength)
Drains from the buffer to the target only if we are at EOS per the input count. If input count is EOS, drain and returns the drain count, otherwise return the input count. If draining, the state is set to DRAINING.
-
isBufferMatchAt
private boolean isBufferMatchAt(StringMatcher stringMatcher, int pos)
Tests if our buffer matches the given string matcher at the given position in the buffer.
-
isDraining
private boolean isDraining()
Tests if we are draining.
-
read
public int read() throws java.io.IOException
Reads a single character.- Overrides:
read
in classjava.io.FilterReader
- Returns:
- a character as an
int
or-1
for end-of-stream. - Throws:
java.io.IOException
- If an I/O error occurs
-
read
public int read(char[] target, int targetIndexIn, int targetLengthIn) throws java.io.IOException
Reads characters into a portion of an array.- Overrides:
read
in classjava.io.FilterReader
- Parameters:
target
- Target buffer.targetIndexIn
- Index in the target at which to start storing characters.targetLengthIn
- Maximum number of characters to read.- Returns:
- The number of characters read, or -1 on end of stream.
- Throws:
java.io.IOException
- If an I/O error occurs
-
readCount
private int readCount(int count, int pos)
Returns how many chars to attempt reading to have room in the buffer forcount
chars starting at positionpos
.
-
-