Add asReader method

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@227320 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-08-04 07:53:46 +00:00
parent e77e2124c4
commit 93a49f6cb0
2 changed files with 32 additions and 0 deletions

View File

@ -15,6 +15,8 @@
*/
package org.apache.commons.lang.text;
import java.io.CharArrayReader;
import java.io.Reader;
import java.util.Collection;
import java.util.Iterator;
@ -1477,6 +1479,22 @@ public class StrBuilder implements Cloneable {
return this;
}
//-----------------------------------------------------------------------
/**
* Gets the contents of this builder as a Reader.
* <p>
* This method allows the contents of the builder to be read
* using any standard method that expects a Reader.
* The current implementation returns a CharArrayReader, but
* you should not rely on this.
*
* @return a reader
*/
public Reader asReader() {
return new CharArrayReader(buffer, 0, size);
}
//-----------------------------------------------------------------------
// /**
// * Gets a String version of the string builder by calling the internal
// * constructor of String by reflection.

View File

@ -16,6 +16,7 @@
package org.apache.commons.lang.text;
import java.io.Reader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -1601,4 +1602,17 @@ public class StrBuilderTest extends TestCase {
fail();
} catch (StringIndexOutOfBoundsException e) {}
}
//-----------------------------------------------------------------------
public void testAsReader() throws Exception {
StrBuilder sb = new StrBuilder ("some text");
Reader reader = sb.asReader();
char[] buf = new char[40];
assertEquals(9, reader.read(buf));
assertEquals("some text", new String(buf, 0, 9));
buf = new char[40];
assertEquals(-1, reader.read(buf));
}
}