Add asWriter()

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230918 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-08-08 22:44:59 +00:00
parent 5108fdc8fc
commit d7210650ff
2 changed files with 101 additions and 0 deletions

View File

@ -17,6 +17,7 @@ package org.apache.commons.lang.text;
import java.io.CharArrayReader;
import java.io.Reader;
import java.io.Writer;
import java.util.Collection;
import java.util.Iterator;
@ -1578,6 +1579,26 @@ public class StrBuilder implements Cloneable {
return new CharArrayReader(buffer, 0, size);
}
//-----------------------------------------------------------------------
/**
* Gets this builder as a Writer that can be written to.
* <p>
* This method allows you to populate the contents of the builder
* using any standard method that takes a Writer.
* <p>
* To use, simply create a <code>StrBuilder</code>,
* call <code>asWriter</code>, and populate away. The data is available
* at any time using the methods of the <code>StrBuilder</code>.
* Note however, that no synchronization occurs, so you must not read
* the builder from one thread while writing in another thread.
* Note also that close and flush have no effect on the writer.
*
* @return a writer that populates this builder
*/
public Writer asWriter() {
return new StrBuilderWriter();
}
//-----------------------------------------------------------------------
// /**
// * Gets a String version of the string builder by calling the internal
@ -1666,4 +1687,49 @@ public class StrBuilder implements Cloneable {
}
}
//-----------------------------------------------------------------------
/**
* Inner class to allow StrBuilder to operate as a writer.
*/
class StrBuilderWriter extends Writer {
StrBuilderWriter() {
super();
}
/** @inheritdoc */
public void close() {
// do nothing
}
/** @inheritdoc */
public void flush() {
// do nothing
}
/** @inheritdoc */
public void write(int c) {
append((char) c);
}
/** @inheritdoc */
public void write(char[] cbuf) {
append(cbuf);
}
/** @inheritdoc */
public void write(char[] cbuf, int off, int len) {
append(cbuf, off, len);
}
/** @inheritdoc */
public void write(String str) {
append(str);
}
/** @inheritdoc */
public void write(String str, int off, int len) {
append(str, off, len);
}
}
}

View File

@ -17,6 +17,7 @@
package org.apache.commons.lang.text;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -1839,4 +1840,38 @@ public class StrBuilderTest extends TestCase {
assertEquals(-1, reader.read(buf));
}
//-----------------------------------------------------------------------
public void testAsWriter() throws Exception {
StrBuilder sb = new StrBuilder ("base");
Writer writer = sb.asWriter();
writer.write('l');
assertEquals("basel", sb.toString());
writer.write(new char[] {'i', 'n'});
assertEquals("baselin", sb.toString());
writer.write(new char[] {'n', 'e', 'r'}, 1, 2);
assertEquals("baseliner", sb.toString());
writer.write(" rout");
assertEquals("baseliner rout", sb.toString());
writer.write("ping that server", 1, 3);
assertEquals("baseliner routing", sb.toString());
writer.flush(); // no effect
assertEquals("baseliner routing", sb.toString());
writer.close(); // no effect
assertEquals("baseliner routing", sb.toString());
writer.write(" hi"); // works after close
assertEquals("baseliner routing hi", sb.toString());
sb.setLength(4); // mix and match
writer.write('d');
assertEquals("based", sb.toString());
}
}