You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2015/05/22 08:58:18 UTC

svn commit: r1681007 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/output/AppendableOutputStream.java test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java

Author: britter
Date: Fri May 22 06:58:18 2015
New Revision: 1681007

URL: http://svn.apache.org/r1681007
Log:
IO-406: Introduce new class AppendableOutputStream. Thanks to Niall Pemberton.

Added:
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java   (with props)
    commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java   (with props)
Modified:
    commons/proper/io/trunk/src/changes/changes.xml

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1681007&r1=1681006&r2=1681007&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Fri May 22 06:58:18 2015
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="2014-??-??" description="New features and bug fixes.">
+      <action issue="IO-402" dev="britter" type="add" due-to="Niall Pemberton">
+        Introduce new class AppendableOutputStream
+      </action>
       <action issue="IO-465" dev="britter" type="update" due-to="based2">
          Update to JUnit 4.12
       </action>

Added: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java?rev=1681007&view=auto
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java (added)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java Fri May 22 06:58:18 2015
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.output;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * OutputStream implementation that writes the data to an {@link Appendable}
+ * Object.
+ * <p>
+ * For example, can be used with any {@link java.io.Writer} or a {@link java.lang.StringBuilder}
+ * or {@link java.lang.StringBuffer}.
+ *
+ * @see java.lang.Appendable
+ * @version $Id$
+ */
+public class AppendableOutputStream <T extends Appendable> extends OutputStream {
+
+    private final T appendable;
+
+    /**
+     * Construct a new instance with the specified appendable.
+     *
+     * @param appendable the appendable to write to
+     */
+    public AppendableOutputStream(T appendable) {
+        this.appendable = appendable;
+    }
+
+    /**
+     * Write a character to the underlying appendable.
+     *
+     * @param b the character to write
+     */
+    @Override
+    public void write(int b) throws IOException {
+        appendable.append((char)b);
+    }
+
+    /**
+     * Return the target appendable.
+     *
+     * @return the target appendable
+     */
+    public T getAppendable() {
+        return appendable;
+    }
+
+}

Propchange: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/main/java/org/apache/commons/io/output/AppendableOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java?rev=1681007&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java (added)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java Fri May 22 06:58:18 2015
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.output;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Unit tests for {@link AppendableOutputStream}.
+ *
+ * @version $Id$
+ */
+public class AppendableOutputStreamTest {
+
+    private AppendableOutputStream<StringBuilder> out;
+
+    @Before
+    public void setUp() throws Exception {
+        out = new AppendableOutputStream<StringBuilder>(new StringBuilder());
+    }
+
+    @Test
+    public void testWriteStringBuilder() throws Exception {
+        String testData = "ABCD";
+
+        out.write(testData.getBytes());
+
+        assertEquals(testData, out.getAppendable().toString());
+    }
+
+    @Test
+    public void testWriteInt() throws Exception {
+        out.write((int) 'F');
+
+        assertEquals("F", out.getAppendable().toString());
+    }
+}

Propchange: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/test/java/org/apache/commons/io/output/AppendableOutputStreamTest.java
------------------------------------------------------------------------------
    svn:keywords = Id