Class CharSequenceReader
- java.lang.Object
-
- java.io.Reader
-
- org.apache.commons.io.input.CharSequenceReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Serializable
,java.lang.AutoCloseable
,java.lang.Readable
public class CharSequenceReader extends java.io.Reader implements java.io.Serializable
Reader
implementation that can read from String, StringBuffer, StringBuilder or CharBuffer.- Since:
- 1.4
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.CharSequence
charSequence
private java.lang.Integer
end
The end index in the character sequence, exclusive.private int
idx
private int
mark
private static long
serialVersionUID
private int
start
The start index in the character sequence, inclusive.
-
Constructor Summary
Constructors Constructor Description CharSequenceReader(java.lang.CharSequence charSequence)
Constructs a new instance with the specified character sequence.CharSequenceReader(java.lang.CharSequence charSequence, int start)
Constructs a new instance with a portion of the specified character sequence.CharSequenceReader(java.lang.CharSequence charSequence, int start, int end)
Constructs a new instance with a portion of the specified character sequence.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close resets the file back to the start and removes any marked position.private int
end()
Returns the index in the character sequence to end reading at, taking into account its length.void
mark(int readAheadLimit)
Mark the current position.boolean
markSupported()
Mark is supported (returns true).int
read()
Read a single character.int
read(char[] array, int offset, int length)
Read the specified number of characters into the array.void
reset()
Reset the reader to the last marked position (or the beginning if mark has not been called).long
skip(long n)
Skip the specified number of characters.private int
start()
Returns the index in the character sequence to start reading from, taking into account its length.java.lang.String
toString()
Return a String representation of the underlying character sequence.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
charSequence
private final java.lang.CharSequence charSequence
-
idx
private int idx
-
mark
private int mark
-
start
private final int start
The start index in the character sequence, inclusive.When de-serializing a CharSequenceReader that was serialized before this fields was added, this field will be initialized to 0, which gives the same behavior as before: start reading from the start.
- Since:
- 2.7
- See Also:
start()
-
end
private final java.lang.Integer end
The end index in the character sequence, exclusive.When de-serializing a CharSequenceReader that was serialized before this fields was added, this field will be initialized to
null
, which gives the same behavior as before: stop reading at the CharSequence's length. If this field was an int instead, it would be initialized to 0 when the CharSequenceReader is de-serialized, causing it to not return any characters at all.- Since:
- 2.7
- See Also:
end()
-
-
Constructor Detail
-
CharSequenceReader
public CharSequenceReader(java.lang.CharSequence charSequence)
Constructs a new instance with the specified character sequence.- Parameters:
charSequence
- The character sequence, may benull
-
CharSequenceReader
public CharSequenceReader(java.lang.CharSequence charSequence, int start)
Constructs a new instance with a portion of the specified character sequence.The start index is not strictly enforced to be within the bounds of the character sequence. This allows the character sequence to grow or shrink in size without risking any
IndexOutOfBoundsException
to be thrown. Instead, if the character sequence grows smaller than the start index, this instance will act as if all characters have been read.- Parameters:
charSequence
- The character sequence, may benull
start
- The start index in the character sequence, inclusive- Throws:
java.lang.IllegalArgumentException
- if the start index is negative- Since:
- 2.7
-
CharSequenceReader
public CharSequenceReader(java.lang.CharSequence charSequence, int start, int end)
Constructs a new instance with a portion of the specified character sequence.The start and end indexes are not strictly enforced to be within the bounds of the character sequence. This allows the character sequence to grow or shrink in size without risking any
IndexOutOfBoundsException
to be thrown. Instead, if the character sequence grows smaller than the start index, this instance will act as if all characters have been read; if the character sequence grows smaller than the end, this instance will use the actual character sequence length.- Parameters:
charSequence
- The character sequence, may benull
start
- The start index in the character sequence, inclusiveend
- The end index in the character sequence, exclusive- Throws:
java.lang.IllegalArgumentException
- if the start index is negative, or if the end index is smaller than the start index- Since:
- 2.7
-
-
Method Detail
-
start
private int start()
Returns the index in the character sequence to start reading from, taking into account its length.- Returns:
- The start index in the character sequence (inclusive).
-
end
private int end()
Returns the index in the character sequence to end reading at, taking into account its length.- Returns:
- The end index in the character sequence (exclusive).
-
close
public void close()
Close resets the file back to the start and removes any marked position.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Reader
-
mark
public void mark(int readAheadLimit)
Mark the current position.- Overrides:
mark
in classjava.io.Reader
- Parameters:
readAheadLimit
- ignored
-
markSupported
public boolean markSupported()
Mark is supported (returns true).- Overrides:
markSupported
in classjava.io.Reader
- Returns:
true
-
read
public int read()
Read a single character.- Overrides:
read
in classjava.io.Reader
- Returns:
- the next character from the character sequence or -1 if the end has been reached.
-
read
public int read(char[] array, int offset, int length)
Read the specified number of characters into the array.- Specified by:
read
in classjava.io.Reader
- Parameters:
array
- The array to store the characters inoffset
- The starting position in the array to storelength
- The maximum number of characters to read- Returns:
- The number of characters read or -1 if there are no more
-
reset
public void reset()
Reset the reader to the last marked position (or the beginning if mark has not been called).- Overrides:
reset
in classjava.io.Reader
-
skip
public long skip(long n)
Skip the specified number of characters.- Overrides:
skip
in classjava.io.Reader
- Parameters:
n
- The number of characters to skip- Returns:
- The actual number of characters skipped
-
toString
public java.lang.String toString()
Return a String representation of the underlying character sequence.- Overrides:
toString
in classjava.lang.Object
- Returns:
- The contents of the character sequence
-
-