Add StringInputStream.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -63,6 +63,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add UncheckedFilterWriter.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add StringInputStream.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

MODIFY
src/test/java/org/apache/commons/io/CopyUtilsTest.java
src/test/java/org/apache/commons/io/CopyUtilsTest.java
import java.io.Writer;
import java.nio.charset.StandardCharsets;
+import org.apache.commons.io.input.StringInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.io.test.TestUtils;
import org.apache.commons.io.test.ThrowOnCloseInputStream;
@@ -136,7 +137,7 @@ public void copy_inputStreamToWriterWithEncoding() throws Exception {
final String inDataStr = "data";
final String charsetName = "UTF-8";
final StringWriter writer = new StringWriter();
- CopyUtils.copy(new ByteArrayInputStream(inDataStr.getBytes(charsetName)), writer, charsetName);
+ CopyUtils.copy(new StringInputStream(inDataStr, charsetName), writer, charsetName);
assertEquals(inDataStr, writer.toString());
}

MODIFY
src/test/java/org/apache/commons/io/DemuxTestCase.java
src/test/java/org/apache/commons/io/DemuxTestCase.java
import java.util.Random;
import org.apache.commons.io.input.DemuxInputStream;
+import org.apache.commons.io.input.StringInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.io.output.DemuxOutputStream;
import org.apache.commons.io.test.TestUtils;
@@ -93,7 +94,7 @@ private void startWriter(final String name,
private void startReader(final String name,
final String data,
final DemuxInputStream demux) {
- final ByteArrayInputStream input = new ByteArrayInputStream(data.getBytes());
+ final InputStream input = new StringInputStream(data);
final ReaderThread thread = new ReaderThread(name, input, demux);
threadMap.put(name, thread);
}

MODIFY
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
src/test/java/org/apache/commons/io/IOUtilsTestCase.java
import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.input.CircularInputStream;
import org.apache.commons.io.input.NullInputStream;
+import org.apache.commons.io.input.StringInputStream;
import org.apache.commons.io.output.AppendableWriter;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.io.output.StringBuilderWriter;
@@ -976,8 +977,7 @@ public void testReadFully_InputStream_ByteArray() throws Exception {
@Test
public void testReadFully_InputStream_Offset() throws Exception {
- final byte[] bytes = "abcd1234".getBytes(StandardCharsets.UTF_8);
- final ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
+ final StringInputStream stream = new StringInputStream("abcd1234", StandardCharsets.UTF_8);
final byte[] buffer = "wx00000000".getBytes(StandardCharsets.UTF_8);
IOUtils.readFully(stream, buffer, 2, 8);
assertEquals("wxabcd1234", new String(buffer, 0, buffer.length, StandardCharsets.UTF_8));

MODIFY
src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
src/test/java/org/apache/commons/io/input/BOMInputStreamTest.java
@@ -777,10 +777,9 @@ private boolean jvmAndSaxBothSupportCharset(final String charSetName) throws Par
}
private boolean doesSaxSupportCharacterSet(final String charSetName) throws ParserConfigurationException, SAXException, IOException {
- final byte[] data = ("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>").getBytes(charSetName);
final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- try {
- final InputSource is = new InputSource(new ByteArrayInputStream(data));
+ try (final StringInputStream byteStream = new StringInputStream("<?xml version=\"1.0\" encoding=\"" + charSetName + "\"?><Z/>", charSetName)) {
+ final InputSource is = new InputSource(byteStream);
is.setEncoding(charSetName);
documentBuilder.parse(is);
} catch (final SAXParseException e) {

MODIFY
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
@Test
public void testCounting() throws Exception {
final String text = "A piece of text";
- final byte[] bytes = text.getBytes();
- final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- try (final CountingInputStream cis = new CountingInputStream(bais)) {
+ try (final CountingInputStream cis = new CountingInputStream(new StringInputStream(text))) {
// have to declare this larger as we're going to read
// off the end of the stream and input stream seems
@@ -199,9 +198,7 @@ public void testEOF3() throws Exception {
@Test
public void testSkipping() throws IOException {
final String text = "Hello World!";
- final byte[] bytes = text.getBytes();
- final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
- try (final CountingInputStream cis = new CountingInputStream(bais)) {
+ try (final CountingInputStream cis = new CountingInputStream(new StringInputStream(text))) {
assertEquals(6, cis.skip(6));
assertEquals(6, cis.getCount());

MODIFY
src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
src/test/java/org/apache/commons/io/input/MessageDigestCalculatingInputStreamTest.java
@@ -38,8 +38,7 @@ public void test() throws Exception {
final byte[] buffer = generateRandomByteStream(i);
final MessageDigest md5Sum = MessageDigest.getInstance("MD5");
final byte[] expect = md5Sum.digest(buffer);
- try (final MessageDigestCalculatingInputStream md5InputStream =
- new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer))) {
+ try (final MessageDigestCalculatingInputStream md5InputStream = new MessageDigestCalculatingInputStream(new ByteArrayInputStream(buffer))) {
md5InputStream.consume();
final byte[] got = md5InputStream.getMessageDigest().digest();
assertArrayEquals(expect, got);

ADD
src/test/java/org/apache/commons/io/input/StringInputStream.java
+/*
+ * 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.input;
+
+import java.io.InputStream;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+
+/**
+ * An {@link InputStream} on a String.
+ *
+ * @since 2.12.0
+ */
+public class StringInputStream extends ReaderInputStream {
+
+ /**
+ * Creates a new instance on a String.
+ *
+ * @param source The source string, MUST not be null.
+ * @return A new instance.
+ */
+ public static StringInputStream on(final String source) {
+ return new StringInputStream(source);
+ }
+
+ /**
+ * Creates a new instance on the empty String.
+ */
+ public StringInputStream() {
+ this("", Charset.defaultCharset());
+ }
+
+ /**
+ * Creates a new instance on a String.
+ *
+ * @param source The source string, MUST not be null.
+ */
+ public StringInputStream(final String source) {
+ this(source, Charset.defaultCharset());
+ }
+
+ /**
+ * Creates a new instance on a String for a Charset.
+ *
+ * @param source The source string, MUST not be null.
+ * @param charset The source charset, MUST not be null.
+ */
+ public StringInputStream(final String source, final Charset charset) {
+ super(new StringReader(source), charset);
+ }
+
+ /**
+ * Creates a new instance on a String and for a Charset.
+ *
+ * @param source The source string, MUST not be null.
+ * @param charset The source charset, MUST not be null.
+ */
+ public StringInputStream(final String source, final String charset) {
+ super(new StringReader(source), charset);
+ }
+
+}

ADD
src/test/java/org/apache/commons/io/input/StringInputStreamTest.java
+/*
+ * 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.input;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link StringInputStream}.
+ */
+public class StringInputStreamTest {
+
+ @Test
+ public void testStrinConstructorString() throws IOException {
+ try (final StringInputStream input = StringInputStream.on("01")) {
+ assertEquals("01", IOUtils.toString(input, Charset.defaultCharset()));
+ }
+ }
+
+ @Test
+ public void testStrinConstructorStringCharset() throws IOException {
+ try (final StringInputStream input = new StringInputStream("01", Charset.defaultCharset())) {
+ assertEquals("01", IOUtils.toString(input, Charset.defaultCharset()));
+ }
+ }
+}

MODIFY
src/test/java/org/apache/commons/io/input/WindowsLineEndingInputStreamTest.java
src/test/java/org/apache/commons/io/input/WindowsLineEndingInputStreamTest.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
@@ -51,7 +52,6 @@ public void linuxLinefeeds() throws Exception {
assertEquals("ab\r\nc", roundtrip);
}
-
@Test
public void malformed() throws Exception {
assertEquals("a\rbc", roundtrip("a\rbc", false));
@@ -68,11 +68,10 @@ private String roundtrip( final String msg ) throws IOException {
}
private String roundtrip(final String msg, final boolean ensure) throws IOException {
- final ByteArrayInputStream baos = new ByteArrayInputStream( msg.getBytes(StandardCharsets.UTF_8) );
- final WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream( baos, ensure );
+ try (final WindowsLineEndingInputStream lf = new WindowsLineEndingInputStream(new StringInputStream(msg, StandardCharsets.UTF_8), ensure)) {
final byte[] buf = new byte[100];
final int read = lf.read(buf);
- lf.close();
return new String(buf, 0, read, StandardCharsets.UTF_8);
}
}
+}
\ No newline at end of file

MODIFY
src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
src/test/java/org/apache/commons/io/input/XmlStreamReaderTest.java
@@ -325,21 +325,21 @@ public void testLowerCaseEncodingWithTurkishLocale_IO_557() throws Exception {
public void testRawContent() throws Exception {
final String encoding = "UTF-8";
final String xml = getXML("no-bom", XML3, encoding, encoding);
- final ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes(encoding));
- final XmlStreamReader xmlReader = new XmlStreamReader(is);
+ try (final XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream(xml, encoding))) {
assertEquals(xmlReader.getEncoding(), encoding, "Check encoding");
assertEquals(xml, IOUtils.toString(xmlReader), "Check content");
}
+ }
@Test
public void testHttpContent() throws Exception {
final String encoding = "UTF-8";
final String xml = getXML("no-bom", XML3, encoding, encoding);
- final ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes(encoding));
- final XmlStreamReader xmlReader = new XmlStreamReader(is, encoding);
+ try (final XmlStreamReader xmlReader = new XmlStreamReader(new StringInputStream(xml, encoding))) {
assertEquals(xmlReader.getEncoding(), encoding, "Check encoding");
assertEquals(xml, IOUtils.toString(xmlReader), "Check content");
}
+ }
public void testAlternateDefaultEncoding(final String cT, final String bomEnc,
final String streamEnc, final String prologEnc, final String alternateEnc)

MODIFY
src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java
src/test/java/org/apache/commons/io/input/XmlStreamReaderUtilitiesTest.java
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.junit.jupiter.api.Test;
@@ -324,7 +323,7 @@ private void checkHttpError(final String msgSuffix, final boolean lenient, final
/** Mock {@link XmlStreamReader} implementation */
private static class MockXmlStreamReader extends XmlStreamReader {
MockXmlStreamReader(final String defaultEncoding) throws IOException {
- super(new ByteArrayInputStream("".getBytes()), null, true, defaultEncoding);
+ super(new StringInputStream(), null, true, defaultEncoding);
}
}
}

MODIFY
src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
@@ -80,10 +80,7 @@ public void testRandomRead() throws Exception {
@Test
public void testIO683() throws IOException {
final byte[] buffer = {0, 1, -2, -2, -1, 4};
- try (
- final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
- final CircularBufferInputStream cbis = new CircularBufferInputStream(bais)
- ){
+ try (final ByteArrayInputStream bais = new ByteArrayInputStream(buffer); final CircularBufferInputStream cbis = new CircularBufferInputStream(bais)) {
int b;
int i = 0;
while ((b = cbis.read()) != -1) {

MODIFY
src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java
src/test/java/org/apache/commons/io/input/compatibility/XmlStreamReaderUtilitiesCompatibilityTest.java
*/
package org.apache.commons.io.input.compatibility;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
+import org.apache.commons.io.input.StringInputStream;
import org.apache.commons.io.input.XmlStreamReaderUtilitiesTest;
/**
@@ -47,7 +47,7 @@ protected String calculateHttpEncoding(final String httpContentType, final Strin
/** Mock {@link XmlStreamReader} implementation */
private static class MockXmlStreamReader extends XmlStreamReader {
MockXmlStreamReader(final String defaultEncoding) throws IOException {
- super(new ByteArrayInputStream("".getBytes()), null, true, defaultEncoding);
+ super(new StringInputStream(), null, true, defaultEncoding);
}
}
}

Add StringInputStream.