From d7210650ff041a58076280e4ea680305c81671bf Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Mon, 8 Aug 2005 22:44:59 +0000 Subject: [PATCH] Add asWriter() git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230918 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/commons/lang/text/StrBuilder.java | 66 +++++++++++++++++++ .../commons/lang/text/StrBuilderTest.java | 35 ++++++++++ 2 files changed, 101 insertions(+) diff --git a/src/java/org/apache/commons/lang/text/StrBuilder.java b/src/java/org/apache/commons/lang/text/StrBuilder.java index ccf0a7861..fba251cf4 100644 --- a/src/java/org/apache/commons/lang/text/StrBuilder.java +++ b/src/java/org/apache/commons/lang/text/StrBuilder.java @@ -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. + *

+ * This method allows you to populate the contents of the builder + * using any standard method that takes a Writer. + *

+ * To use, simply create a StrBuilder, + * call asWriter, and populate away. The data is available + * at any time using the methods of the StrBuilder. + * 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); + } + } + } diff --git a/src/test/org/apache/commons/lang/text/StrBuilderTest.java b/src/test/org/apache/commons/lang/text/StrBuilderTest.java index b4545e790..37995a322 100644 --- a/src/test/org/apache/commons/lang/text/StrBuilderTest.java +++ b/src/test/org/apache/commons/lang/text/StrBuilderTest.java @@ -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()); + } + }