Skip to content

Commit 61c1b0d

Browse files
author
Gary Gregory
committed
Add StringInputStream.
1 parent 018eaa5 commit 61c1b0d

16 files changed

Lines changed: 184 additions & 64 deletions

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ The <action> type attribute can be add,update,fix,remove.
6363
<action dev="ggregory" type="add" due-to="Gary Gregory">
6464
Add UncheckedFilterWriter.
6565
</action>
66+
<action dev="ggregory" type="add" due-to="Gary Gregory">
67+
Add StringInputStream.
68+
</action>
6669
<!-- UPDATE -->
6770
<action dev="ggregory" type="update" due-to="Dependabot">
6871
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

src/test/java/org/apache/commons/io/CopyUtilsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.Writer;
2828
import java.nio.charset.StandardCharsets;
2929

30+
import org.apache.commons.io.input.StringInputStream;
3031
import org.apache.commons.io.output.ByteArrayOutputStream;
3132
import org.apache.commons.io.test.TestUtils;
3233
import org.apache.commons.io.test.ThrowOnCloseInputStream;
@@ -136,7 +137,7 @@ public void copy_inputStreamToWriterWithEncoding() throws Exception {
136137
final String inDataStr = "data";
137138
final String charsetName = "UTF-8";
138139
final StringWriter writer = new StringWriter();
139-
CopyUtils.copy(new ByteArrayInputStream(inDataStr.getBytes(charsetName)), writer, charsetName);
140+
CopyUtils.copy(new StringInputStream(inDataStr, charsetName), writer, charsetName);
140141
assertEquals(inDataStr, writer.toString());
141142
}
142143

src/test/java/org/apache/commons/io/DemuxTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Random;
2828

2929
import org.apache.commons.io.input.DemuxInputStream;
30+
import org.apache.commons.io.input.StringInputStream;
3031
import org.apache.commons.io.output.ByteArrayOutputStream;
3132
import org.apache.commons.io.output.DemuxOutputStream;
3233
import org.apache.commons.io.test.TestUtils;
@@ -93,7 +94,7 @@ private void startWriter(final String name,
9394
private void startReader(final String name,
9495
final String data,
9596
final DemuxInputStream demux) {
96-
final ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
97+
final InputStream input = new StringInputStream(data);
9798
final ReaderThread thread = new ReaderThread(name, input, demux);
9899
threadMap.put(name, thread);
99100
}

src/test/java/org/apache/commons/io/IOUtilsTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.apache.commons.io.function.IOConsumer;
6464
import org.apache.commons.io.input.CircularInputStream;
6565
import org.apache.commons.io.input.NullInputStream;
66+
import org.apache.commons.io.input.StringInputStream;
6667
import org.apache.commons.io.output.AppendableWriter;
6768
import org.apache.commons.io.output.NullOutputStream;
6869
import org.apache.commons.io.output.StringBuilderWriter;
@@ -976,8 +977,7 @@ public void testReadFully_InputStream_ByteArray() throws Exception {
976977

977978
@Test
978979
public void testReadFully_InputStream_Offset() throws Exception {
979-
final byte[] bytes = "abcd1234".getBytes(StandardCharsets.UTF_8);
980-
final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
980+
final StringInputStream stream = new StringInputStream("abcd1234", StandardCharsets.UTF_8);
981981
final byte[] buffer = "wx00000000".getBytes(StandardCharsets.UTF_8);
982982
IOUtils.readFully(stream, buffer, 2, 8);
983983
assertEquals("wxabcd1234", new String(buffer, 0, buffer.length, StandardCharsets.UTF_8));

src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,9 @@ private boolean jvmAndSaxBothSupportCharset(final String charSetName) throws Par
777777
}
778778

779779
private boolean doesSaxSupportCharacterSet(final String charSetName) throws ParserConfigurationException, SAXException, IOException {
780-
final byte[] data = ("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>").getBytes(charSetName);
781780
final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
782-
try {
783-
final InputSource is = new InputSource(new ByteArrayInputStream(data));
781+
try (final StringInputStream byteStream = new StringInputStream("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>", charSetName)) {
782+
final InputSource is = new InputSource(byteStream);
784783
is.setEncoding(charSetName);
785784
documentBuilder.parse(is);
786785
} catch (final SAXParseException e) {

src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class BoundedInputStreamTest {
3333
public void testReadSingle() throws Exception {
3434
BoundedInputStream bounded;
3535
final byte[] helloWorld = "Hello World".getBytes();
36-
final byte[] hello = "Hello".getBytes();
36+
final byte[] hello = "Hello".getBytes();
3737

3838
// limit = length
3939
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length);
@@ -62,7 +62,7 @@ public void testReadArray() throws Exception {
6262

6363
BoundedInputStream bounded;
6464
final byte[] helloWorld = "Hello World".getBytes();
65-
final byte[] hello = "Hello".getBytes();
65+
final byte[] hello = "Hello".getBytes();
6666

6767
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld));
6868
compare("limit = -1", helloWorld, IOUtils.toByteArray(bounded));

src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.ByteArrayInputStream;
2323
import java.io.IOException;
24+
import java.io.InputStream;
2425

2526
import org.apache.commons.io.IOUtils;
2627
import org.junit.jupiter.api.Test;
@@ -34,9 +35,7 @@ public class CountingInputStreamTest {
3435
@Test
3536
public void testCounting() throws Exception {
3637
final String text = "A piece of text";
37-
final byte[] bytes = text.getBytes();
38-
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
39-
try (final CountingInputStream cis = new CountingInputStream(bais)) {
38+
try (final CountingInputStream cis = new CountingInputStream(new StringInputStream(text))) {
4039

4140
// have to declare this larger as we're going to read
4241
// off the end of the stream and input stream seems
@@ -199,9 +198,7 @@ public void testEOF3() throws Exception {
199198
@Test
200199
public void testSkipping() throws IOException {
201200
final String text = "Hello World!";
202-
final byte[] bytes = text.getBytes();
203-
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
204-
try (final CountingInputStream cis = new CountingInputStream(bais)) {
201+
try (final CountingInputStream cis = new CountingInputStream(new StringInputStream(text))) {
205202

206203
assertEquals(6, cis.skip(6));
207204
assertEquals(6, cis.getCount());

src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ public static byte[] generateRandomByteStream(final int pSize) {
3434

3535
@Test
3636
public void test() throws Exception {
37-
for (int i = 256; i < 8192; i = i*2) {
37+
for (int i = 256; i < 8192; i = i * 2) {
3838
final byte[] buffer = generateRandomByteStream(i);
3939
final MessageDigest md5Sum = MessageDigest.getInstance("MD5");
4040
final byte[] expect = md5Sum.digest(buffer);
41-
try (final MessageDigestCalculatingInputStream md5InputStream =
42-
new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer))) {
41+
try (final MessageDigestCalculatingInputStream md5InputStream = new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer))) {
4342
md5InputStream.consume();
4443
final byte[] got = md5InputStream.getMessageDigest().digest();
4544
assertArrayEquals(expect, got);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
*/
18+
19+
package org.apache.commons.io.input;
20+
21+
import java.io.InputStream;
22+
import java.io.StringReader;
23+
import java.nio.charset.Charset;
24+
25+
/**
26+
* An {@link InputStream} on a String.
27+
*
28+
* @since 2.12.0
29+
*/
30+
public class StringInputStream extends ReaderInputStream {
31+
32+
/**
33+
* Creates a new instance on a String.
34+
*
35+
* @param source The source string, MUST not be null.
36+
* @return A new instance.
37+
*/
38+
public static StringInputStream on(final String source) {
39+
return new StringInputStream(source);
40+
}
41+
42+
/**
43+
* Creates a new instance on the empty String.
44+
*/
45+
public StringInputStream() {
46+
this("", Charset.defaultCharset());
47+
}
48+
49+
/**
50+
* Creates a new instance on a String.
51+
*
52+
* @param source The source string, MUST not be null.
53+
*/
54+
public StringInputStream(final String source) {
55+
this(source, Charset.defaultCharset());
56+
}
57+
58+
/**
59+
* Creates a new instance on a String for a Charset.
60+
*
61+
* @param source The source string, MUST not be null.
62+
* @param charset The source charset, MUST not be null.
63+
*/
64+
public StringInputStream(final String source, final Charset charset) {
65+
super(new StringReader(source), charset);
66+
}
67+
68+
/**
69+
* Creates a new instance on a String and for a Charset.
70+
*
71+
* @param source The source string, MUST not be null.
72+
* @param charset The source charset, MUST not be null.
73+
*/
74+
public StringInputStream(final String source, final String charset) {
75+
super(new StringReader(source), charset);
76+
}
77+
78+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
*/
18+
19+
package org.apache.commons.io.input;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
import java.io.IOException;
24+
import java.nio.charset.Charset;
25+
26+
import org.apache.commons.io.IOUtils;
27+
import org.junit.jupiter.api.Test;
28+
29+
/**
30+
* Tests {@link StringInputStream}.
31+
*/
32+
public class StringInputStreamTest {
33+
34+
@Test
35+
public void testStrinConstructorString() throws IOException {
36+
try (final StringInputStream input = StringInputStream.on("01")) {
37+
assertEquals("01", IOUtils.toString(input, Charset.defaultCharset()));
38+
}
39+
}
40+
41+
@Test
42+
public void testStrinConstructorStringCharset() throws IOException {
43+
try (final StringInputStream input = new StringInputStream("01", Charset.defaultCharset())) {
44+
assertEquals("01", IOUtils.toString(input, Charset.defaultCharset()));
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)