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:
Closeable,AutoCloseable,Readable
A
Reader that performs string substitution on a source Reader using a StringSubstitutor.
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
FieldsModifier and TypeFieldDescriptionprivate final TextStringBuilderOur internal buffer.private booleanEnd-of-Stream flag.private static final intThe end-of-stream character marker.private final StringMatcherMatches escaped variable starts.private final char[]Internal buffer forread()method.private final StringSubstitutorThe underlying StringSubstitutor.private intWe don't always want to drain the whole buffer.Fields inherited from class java.io.FilterReader
in -
Constructor Summary
ConstructorsConstructorDescriptionStringSubstitutorReader(Reader reader, StringSubstitutor stringSubstitutor) Constructs a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate intbuffer(int requestReadCount) Buffers the requested number of characters if available.private intbufferOrDrainOnEos(int requestReadCount, char[] target, int targetIndex, int targetLength) Reads a requested number of chars from the underlying reader into the buffer.private intdrain(char[] target, int targetIndex, int targetLength) Drains characters from our buffer to the giventarget.private intdrainOnEos(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 booleanisBufferMatchAt(StringMatcher stringMatcher, int pos) Tests if our buffer matches the given string matcher at the given position in the buffer.private booleanTests if we are draining.intread()Reads a single character.intread(char[] target, int targetIndexIn, int targetLengthIn) Reads characters into a portion of an array.private intreadCount(int count, int pos) Returns how many chars to attempt reading to have room in the buffer forcountchars starting at positionpos.Methods inherited from class java.io.FilterReader
close, mark, markSupported, ready, reset, skip
-
Field Details
-
EOS
private static final int EOSThe end-of-stream character marker.- See Also:
-
buffer
Our internal buffer. -
eos
private boolean eosEnd-of-Stream flag. -
prefixEscapeMatcher
Matches escaped variable starts. -
read1CharBuffer
private final char[] read1CharBufferInternal buffer forread()method. -
stringSubstitutor
The underlying StringSubstitutor. -
toDrain
private int toDrainWe don't always want to drain the whole buffer.
-
-
Constructor Details
-
StringSubstitutorReader
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:
NullPointerException- ifreaderisnull.NullPointerException- ifstringSubstitutorisnull.
-
-
Method Details
-
buffer
Buffers the requested number of characters if available.- Throws:
IOException
-
bufferOrDrainOnEos
private int bufferOrDrainOnEos(int requestReadCount, char[] target, int targetIndex, int targetLength) throws 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:
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
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
Reads a single character.- Overrides:
readin classFilterReader- Returns:
- a character as an
intor-1for end-of-stream. - Throws:
IOException- If an I/O error occurs
-
read
Reads characters into a portion of an array.- Overrides:
readin classFilterReader- 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:
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 forcountchars starting at positionpos.
-