Use getChars method for String, StringBuilder and StringBuffer in CharSequenceReader.read(char[], int, int)
MODIFY
src/main/java/org/apache/commons/io/input/CharSequenceReader.java
src/main/java/org/apache/commons/io/input/CharSequenceReader.java
@@ -109,6 +109,26 @@ public int read(final char[] array, final int offset, final int length) {
throw new IndexOutOfBoundsException("Array Size=" + array.length +
", offset=" + offset + ", length=" + length);
}
+
+ if (charSequence instanceof String) {
+ int count = Math.min(length, charSequence.length() - idx);
+ ((String) charSequence).getChars(idx, idx + count, array, offset);
+ idx += count;
+ return count;
+ }
+ if (charSequence instanceof StringBuilder) {
+ int count = Math.min(length, charSequence.length() - idx);
+ ((StringBuilder) charSequence).getChars(idx, idx + count, array, offset);
+ idx += count;
+ return count;
+ }
+ if (charSequence instanceof StringBuffer) {
+ int count = Math.min(length, charSequence.length() - idx);
+ ((StringBuffer) charSequence).getChars(idx, idx + count, array, offset);
+ idx += count;
+ return count;
+ }
+
int count = 0;
for (int i = 0; i < length; i++) {
final int c = read();

MODIFY
src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java
src/test/java/org/apache/commons/io/input/CharSequenceReaderTest.java
import java.io.IOException;
import java.io.Reader;
+import java.nio.CharBuffer;
import org.junit.Test;
@@ -78,7 +79,15 @@ public void testSkip() throws IOException {
@Test
public void testRead() throws IOException {
- try (final Reader reader = new CharSequenceReader("Foo")) {
+ final String value = "Foo";
+ testRead(value);
+ testRead(new StringBuilder(value));
+ testRead(new StringBuffer(value));
+ testRead(CharBuffer.wrap(value));
+ }
+
+ private void testRead(final CharSequence charSequence) throws IOException {
+ try (final Reader reader = new CharSequenceReader(charSequence)) {
assertEquals('F', reader.read());
assertEquals('o', reader.read());
assertEquals('o', reader.read());
@@ -89,7 +98,15 @@ public void testRead() throws IOException {
@Test
public void testReadCharArray() throws IOException {
- try (final Reader reader = new CharSequenceReader("FooBar")) {
+ final String value = "FooBar";
+ testReadCharArray(value);
+ testReadCharArray(new StringBuilder(value));
+ testReadCharArray(new StringBuffer(value));
+ testReadCharArray(CharBuffer.wrap(value));
+ }
+
+ private void testReadCharArray(final CharSequence charSequence) throws IOException {
+ try (final Reader reader = new CharSequenceReader(charSequence)) {
char[] chars = new char[2];
assertEquals(2, reader.read(chars));
checkArray(new char[] { 'F', 'o' }, chars);
@@ -105,8 +122,16 @@ public void testReadCharArray() throws IOException {
@Test
public void testReadCharArrayPortion() throws IOException {
+ final String value = "FooBar";
+ testReadCharArrayPortion(value);
+ testReadCharArrayPortion(new StringBuilder(value));
+ testReadCharArrayPortion(new StringBuffer(value));
+ testReadCharArrayPortion(CharBuffer.wrap(value));
+ }
+
+ private void testReadCharArrayPortion(final CharSequence charSequence) throws IOException {
final char[] chars = new char[10];
- try (final Reader reader = new CharSequenceReader("FooBar")) {
+ try (final Reader reader = new CharSequenceReader(charSequence)) {
assertEquals(3, reader.read(chars, 3, 3));
checkArray(new char[] { NONE, NONE, NONE, 'F', 'o', 'o' }, chars);
assertEquals(3, reader.read(chars, 0, 3));

Use getChars method for String, StringBuilder and StringBuffer in CharSequenceReader.read(char[], int, int) (#90)