Skip to content

Commit 6feec37

Browse files
committed
IO-406: Introduce new class AppendableOutputStream. Thanks to Niall Pemberton.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1681007 13f79535-47bb-0310-9956-ffa450edef68
1 parent de71c0e commit 6feec37

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2014-??-??" description="New features and bug fixes.">
50+
<action issue="IO-402" dev="britter" type="add" due-to="Niall Pemberton">
51+
Introduce new class AppendableOutputStream
52+
</action>
5053
<action issue="IO-465" dev="britter" type="update" due-to="based2">
5154
Update to JUnit 4.12
5255
</action>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
22+
/**
23+
* OutputStream implementation that writes the data to an {@link Appendable}
24+
* Object.
25+
* <p>
26+
* For example, can be used with any {@link java.io.Writer} or a {@link java.lang.StringBuilder}
27+
* or {@link java.lang.StringBuffer}.
28+
*
29+
* @see java.lang.Appendable
30+
* @version $Id$
31+
*/
32+
public class AppendableOutputStream <T extends Appendable> extends OutputStream {
33+
34+
private final T appendable;
35+
36+
/**
37+
* Construct a new instance with the specified appendable.
38+
*
39+
* @param appendable the appendable to write to
40+
*/
41+
public AppendableOutputStream(T appendable) {
42+
this.appendable = appendable;
43+
}
44+
45+
/**
46+
* Write a character to the underlying appendable.
47+
*
48+
* @param b the character to write
49+
*/
50+
@Override
51+
public void write(int b) throws IOException {
52+
appendable.append((char)b);
53+
}
54+
55+
/**
56+
* Return the target appendable.
57+
*
58+
* @return the target appendable
59+
*/
60+
public T getAppendable() {
61+
return appendable;
62+
}
63+
64+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
/**
25+
* Unit tests for {@link AppendableOutputStream}.
26+
*
27+
* @version $Id$
28+
*/
29+
public class AppendableOutputStreamTest {
30+
31+
private AppendableOutputStream<StringBuilder> out;
32+
33+
@Before
34+
public void setUp() throws Exception {
35+
out = new AppendableOutputStream<StringBuilder>(new StringBuilder());
36+
}
37+
38+
@Test
39+
public void testWriteStringBuilder() throws Exception {
40+
String testData = "ABCD";
41+
42+
out.write(testData.getBytes());
43+
44+
assertEquals(testData, out.getAppendable().toString());
45+
}
46+
47+
@Test
48+
public void testWriteInt() throws Exception {
49+
out.write((int) 'F');
50+
51+
assertEquals("F", out.getAppendable().toString());
52+
}
53+
}

0 commit comments

Comments
 (0)