Top Description Fields Constructors Methods
org.apache.commons.text.io

public Class StringSubstitutorReader

extends FilterReader
Class Inheritance
Imports
java.io.FilterReader, .IOException, .Reader, org.apache.commons.text.StringSubstitutor, .TextStringBuilder, org.apache.commons.text.matcher.StringMatcher, .StringMatcherFactory

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

Modifier and TypeField and Description
private final TextStringBuilder
buffer

References Deprecated TextStringBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Our internal buffer.
private static final int
EOS

The end-of-stream character marker.

private boolean
eos

End-of-Stream flag.

private final StringMatcher
prefixEscapeMatcher

Matches escaped variable starts.

private final char[]
read1CharBuffer

Internal buffer for read() method.

private final StringSubstitutor
stringSubstitutor

The underlying StringSubstitutor.

private int
toDrain

We don't always want to drain the whole buffer.

Inherited from java.io.FilterReader:
in

Constructor Summary

AccessConstructor and Description
public
StringSubstitutorReader(final Reader
the underlying reader containing the template text known to the given StringSubstitutor.
reader
,
final StringSubstitutor
How to replace as we read.
stringSubstitutor
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
private int
buffer(final int requestReadCount)

Buffers the requested number of characters if available.

private int
bufferOrDrainOnEos(final int requestReadCount, final char[] target, final int targetIndex, final int targetLength)

Reads a requested number of chars from the underlying reader into the buffer.

private int
drain(final char[] target, final int targetIndex, final int targetLength)

Drains characters from our buffer to the given target.

private int
drainOnEos(final int readCountOrEos, final char[] target, final int targetIndex, final int targetLength)

Drains from the buffer to the target only if we are at EOS per the input count.

private boolean
isBufferMatchAt(final StringMatcher stringMatcher, final 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.

public int

Returns:

a character as an int or -1 for end-of-stream.
read
()

Overrides java.io.FilterReader.read.

Reads a single character.

public int

Returns:

The number of characters read, or -1 on end of stream.
read
(final char[]
Target buffer.
target
,
final int
Index in the target at which to start storing characters.
targetIndexIn
,
final int
Maximum number of characters to read.
targetLengthIn
)

Overrides java.io.FilterReader.read.

Reads characters into a portion of an array.

private int
readCount(final int count, final int pos)

Returns how many chars to attempt reading to have room in the buffer for count chars starting at position pos.

Inherited from java.io.FilterReader:
closemarkmarkSupportedreadyresetskip

Field Detail

bufferback to summary
private final TextStringBuilder buffer

References Deprecated

TextStringBuilder is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Our internal buffer.

EOSback to summary
private static final int EOS

The end-of-stream character marker.

eosback to summary
private boolean eos

End-of-Stream flag.

prefixEscapeMatcherback to summary
private final StringMatcher prefixEscapeMatcher

Matches escaped variable starts.

read1CharBufferback to summary
private final char[] read1CharBuffer

Internal buffer for read() method.

stringSubstitutorback to summary
private final StringSubstitutor stringSubstitutor

The underlying StringSubstitutor.

toDrainback to summary
private int toDrain

We don't always want to drain the whole buffer.

Constructor Detail

StringSubstitutorReaderback to summary
public StringSubstitutorReader(final Reader reader, final StringSubstitutor stringSubstitutor)

Constructs a new instance.

Parameters
reader:Reader

the underlying reader containing the template text known to the given StringSubstitutor.

stringSubstitutor:StringSubstitutor

How to replace as we read.

Exceptions
NullPointerException:
  • if reader is null.
  • if stringSubstitutor is null.

Method Detail

bufferback to summary
private int buffer(final int requestReadCount) throws IOException

Buffers the requested number of characters if available.

bufferOrDrainOnEosback to summary
private int bufferOrDrainOnEos(final int requestReadCount, final char[] target, final int targetIndex, final 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.

drainback to summary
private int drain(final char[] target, final int targetIndex, final int targetLength)

Drains characters from our buffer to the given target.

drainOnEosback to summary
private int drainOnEos(final int readCountOrEos, final char[] target, final int targetIndex, final 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.

isBufferMatchAtback to summary
private boolean isBufferMatchAt(final StringMatcher stringMatcher, final int pos)

Tests if our buffer matches the given string matcher at the given position in the buffer.

isDrainingback to summary
private boolean isDraining()

Tests if we are draining.

readback to summary
public int read() throws IOException

Overrides java.io.FilterReader.read.

Reads a single character.

Returns:int

a character as an int or -1 for end-of-stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs.
readback to summary
public int read(final char[] target, final int targetIndexIn, final int targetLengthIn) throws IOException

Overrides java.io.FilterReader.read.

Reads characters into a portion of an array.

Parameters
target:char[]

Target buffer.

targetIndexIn:int

Index in the target at which to start storing characters.

targetLengthIn:int

Maximum number of characters to read.

Returns:int

The number of characters read, or -1 on end of stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs.
readCountback to summary
private int readCount(final int count, final int pos)

Returns how many chars to attempt reading to have room in the buffer for count chars starting at position pos.