You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/23 23:18:40 UTC
[commons-io] 02/02: Add IOIntSupplier
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 23460adf8c5e236d2c4e291fba8c09c556ec1868
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jun 23 19:18:33 2023 -0400
Add IOIntSupplier
- Add Uncheck.getAsInt(IOIntSupplier)
- Add IOLongSupplier
- Add Uncheck.getAsLong(IOLongSupplier)
---
src/changes/changes.xml | 12 ++++
.../apache/commons/io/function/IOIntSupplier.java | 49 +++++++++++++
.../apache/commons/io/function/IOLongSupplier.java | 49 +++++++++++++
.../org/apache/commons/io/function/Uncheck.java | 32 +++++++++
.../apache/commons/io/input/ReaderInputStream.java | 16 ++---
.../apache/commons/io/FileCleaningTrackerTest.java | 80 +++++++++++-----------
.../commons/io/function/IOIntSupplierTest.java | 69 +++++++++++++++++++
.../commons/io/function/IOLongSupplierTest.java | 69 +++++++++++++++++++
.../apache/commons/io/function/TestConstants.java | 4 ++
.../org/apache/commons/io/function/TestUtils.java | 24 +++++++
.../apache/commons/io/function/UncheckTest.java | 14 +++-
.../io/input/CharSequenceInputStreamTest.java | 26 +++----
.../commons/io/input/ReaderInputStreamTest.java | 15 ++--
13 files changed, 390 insertions(+), 69 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index eed27eea..447d6f12 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -61,6 +61,18 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add FilesUncheck.find(Path, int, BiPredicate%lt;Path, BasicFileAttributes>, FileVisitOption...)
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOIntSupplier.
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add Uncheck.getAsInt(IOIntSupplier).
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOLongSupplier.
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add Uncheck.getAsLong(IOLongSupplier).
+ </action>
<!-- FIX -->
<action dev="ggregory" type="fix" issue="IO-799" due-to="Jeroen van der Vegt, Gary Gregory">
ReaderInputStream.read() throws an exception instead of returning -1 when called again after returning -1.
diff --git a/src/main/java/org/apache/commons/io/function/IOIntSupplier.java b/src/main/java/org/apache/commons/io/function/IOIntSupplier.java
new file mode 100644
index 00000000..420498b7
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/function/IOIntSupplier.java
@@ -0,0 +1,49 @@
+/*
+ * 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.function;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.function.IntSupplier;
+import java.util.function.Supplier;
+
+/**
+ * Like {@link IntSupplier} but throws {@link IOException}.
+ *
+ * @since 2.14.0
+ */
+@FunctionalInterface
+public interface IOIntSupplier {
+
+ /**
+ * Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
+ *
+ * @return an UncheckedIOException Supplier.
+ */
+ default IntSupplier asIntSupplier() {
+ return () -> Uncheck.getAsInt(this);
+ }
+
+ /**
+ * Gets a result.
+ *
+ * @return a result
+ * @throws IOException if an I/O error occurs.
+ */
+ int getAsInt() throws IOException;
+}
diff --git a/src/main/java/org/apache/commons/io/function/IOLongSupplier.java b/src/main/java/org/apache/commons/io/function/IOLongSupplier.java
new file mode 100644
index 00000000..ff5181c8
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/function/IOLongSupplier.java
@@ -0,0 +1,49 @@
+/*
+ * 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.function;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.function.LongSupplier;
+import java.util.function.Supplier;
+
+/**
+ * Like {@link IOLongSupplier} but throws {@link IOException}.
+ *
+ * @since 2.14.0
+ */
+@FunctionalInterface
+public interface IOLongSupplier {
+
+ /**
+ * Creates a {@link Supplier} for this instance that throws {@link UncheckedIOException} instead of {@link IOException}.
+ *
+ * @return an UncheckedIOException Supplier.
+ */
+ default LongSupplier asSupplier() {
+ return () -> Uncheck.getAsLong(this);
+ }
+
+ /**
+ * Gets a result.
+ *
+ * @return a result
+ * @throws IOException if an I/O error occurs.
+ */
+ long getAsLong() throws IOException;
+}
diff --git a/src/main/java/org/apache/commons/io/function/Uncheck.java b/src/main/java/org/apache/commons/io/function/Uncheck.java
index e18937f8..3c272da6 100644
--- a/src/main/java/org/apache/commons/io/function/Uncheck.java
+++ b/src/main/java/org/apache/commons/io/function/Uncheck.java
@@ -199,6 +199,38 @@ public final class Uncheck {
}
}
+ /**
+ * Gets the result from an IO int supplier.
+ *
+ * @param supplier Supplies the return value.
+ * @return result from the supplier.
+ * @throws UncheckedIOException if an I/O error occurs.
+ * @since 2.14.0
+ */
+ public static int getAsInt(final IOIntSupplier supplier) {
+ try {
+ return supplier.getAsInt();
+ } catch (final IOException e) {
+ throw wrap(e);
+ }
+ }
+
+ /**
+ * Gets the result from an IO long supplier.
+ *
+ * @param supplier Supplies the return value.
+ * @return result from the supplier.
+ * @throws UncheckedIOException if an I/O error occurs.
+ * @since 2.14.0
+ */
+ public static long getAsLong(final IOLongSupplier supplier) {
+ try {
+ return supplier.getAsLong();
+ } catch (final IOException e) {
+ throw wrap(e);
+ }
+ }
+
/**
* Runs an IO runnable.
*
diff --git a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
index bba17caa..cfe79fc1 100644
--- a/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReaderInputStream.java
@@ -148,14 +148,6 @@ public class ReaderInputStream extends InputStream {
}
- private static CharsetEncoder newEncoder(final Charset charset) {
- // @formatter:off
- return Charsets.toCharset(charset).newEncoder()
- .onMalformedInput(CodingErrorAction.REPLACE)
- .onUnmappableCharacter(CodingErrorAction.REPLACE);
- // @formatter:on
- }
-
/**
* Constructs a new {@link Builder}.
*
@@ -179,6 +171,14 @@ public class ReaderInputStream extends InputStream {
return charsetEncoder.maxBytesPerChar() * 2;
}
+ private static CharsetEncoder newEncoder(final Charset charset) {
+ // @formatter:off
+ return Charsets.toCharset(charset).newEncoder()
+ .onMalformedInput(CodingErrorAction.REPLACE)
+ .onUnmappableCharacter(CodingErrorAction.REPLACE);
+ // @formatter:on
+ }
+
private final Reader reader;
private final CharsetEncoder charsetEncoder;
diff --git a/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java b/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
index 579fcfce..b5074515 100644
--- a/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
+++ b/src/test/java/org/apache/commons/io/FileCleaningTrackerTest.java
@@ -111,46 +111,6 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
theInstance = null;
}
- @Test
- public void testFileCleanerDirectoryFileSource() throws Exception {
- TestUtils.createFile(testFile, 100);
- assertTrue(testFile.exists());
- assertTrue(tempDirFile.exists());
-
- Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirFile, obj);
- assertEquals(1, theInstance.getTrackCount());
-
- obj = null;
-
- waitUntilTrackCount();
-
- assertEquals(0, theInstance.getTrackCount());
- assertTrue(testFile.exists()); // not deleted, as dir not empty
- assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
- }
-
- @Test
- public void testFileCleanerDirectoryPathSource() throws Exception {
- TestUtils.createFile(testPath, 100);
- assertTrue(Files.exists(testPath));
- assertTrue(Files.exists(tempDirPath));
-
- Object obj = new Object();
- assertEquals(0, theInstance.getTrackCount());
- theInstance.track(tempDirPath, obj);
- assertEquals(1, theInstance.getTrackCount());
-
- obj = null;
-
- waitUntilTrackCount();
-
- assertEquals(0, theInstance.getTrackCount());
- assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
- assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
- }
-
@Test
public void testFileCleanerDirectory_ForceStrategy_FileSource() throws Exception {
if (!testFile.getParentFile().exists()) {
@@ -227,6 +187,46 @@ public class FileCleaningTrackerTest extends AbstractTempDirTest {
assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
}
+ @Test
+ public void testFileCleanerDirectoryFileSource() throws Exception {
+ TestUtils.createFile(testFile, 100);
+ assertTrue(testFile.exists());
+ assertTrue(tempDirFile.exists());
+
+ Object obj = new Object();
+ assertEquals(0, theInstance.getTrackCount());
+ theInstance.track(tempDirFile, obj);
+ assertEquals(1, theInstance.getTrackCount());
+
+ obj = null;
+
+ waitUntilTrackCount();
+
+ assertEquals(0, theInstance.getTrackCount());
+ assertTrue(testFile.exists()); // not deleted, as dir not empty
+ assertTrue(testFile.getParentFile().exists()); // not deleted, as dir not empty
+ }
+
+ @Test
+ public void testFileCleanerDirectoryPathSource() throws Exception {
+ TestUtils.createFile(testPath, 100);
+ assertTrue(Files.exists(testPath));
+ assertTrue(Files.exists(tempDirPath));
+
+ Object obj = new Object();
+ assertEquals(0, theInstance.getTrackCount());
+ theInstance.track(tempDirPath, obj);
+ assertEquals(1, theInstance.getTrackCount());
+
+ obj = null;
+
+ waitUntilTrackCount();
+
+ assertEquals(0, theInstance.getTrackCount());
+ assertTrue(Files.exists(testPath)); // not deleted, as dir not empty
+ assertTrue(Files.exists(testPath.getParent())); // not deleted, as dir not empty
+ }
+
@Test
public void testFileCleanerExitWhenFinished_NoTrackAfter() {
assertFalse(theInstance.exitWhenFinished);
diff --git a/src/test/java/org/apache/commons/io/function/IOIntSupplierTest.java b/src/test/java/org/apache/commons/io/function/IOIntSupplierTest.java
new file mode 100644
index 00000000..b23f8f67
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/function/IOIntSupplierTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.function;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IOIntSupplier}.
+ */
+public class IOIntSupplierTest {
+
+ private AtomicInteger atomicInt;
+
+ private int getThrowsIO(final IOIntSupplier supplier) throws IOException {
+ return supplier.getAsInt();
+ }
+
+ private int getThrowsNone(final IOIntSupplier supplier) {
+ return supplier.asIntSupplier().getAsInt();
+ }
+
+ @BeforeEach
+ public void initEach() {
+ atomicInt = new AtomicInteger();
+ }
+
+ @Test
+ public void testAsSupplier() {
+ assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier().getAsInt());
+ assertEquals(1, getThrowsNone(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
+ assertEquals(1, atomicInt.get());
+ assertNotEquals(TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier(), TestConstants.THROWING_IO_INT_SUPPLIER.asIntSupplier());
+ }
+
+ @Test
+ public void testGet() throws IOException {
+ assertThrows(IOException.class, () -> TestConstants.THROWING_IO_INT_SUPPLIER.getAsInt());
+ assertThrows(IOException.class, () -> {
+ throw new IOException();
+ });
+ assertEquals(1, getThrowsIO(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
+ assertEquals(1, atomicInt.get());
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/io/function/IOLongSupplierTest.java b/src/test/java/org/apache/commons/io/function/IOLongSupplierTest.java
new file mode 100644
index 00000000..07748ded
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/function/IOLongSupplierTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.function;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IOLongSupplier}.
+ */
+public class IOLongSupplierTest {
+
+ private AtomicLong atomicLong;
+
+ private long getThrowsIO(final IOLongSupplier supplier) throws IOException {
+ return supplier.getAsLong();
+ }
+
+ private long getThrowsNone(final IOLongSupplier supplier) {
+ return supplier.asSupplier().getAsLong();
+ }
+
+ @BeforeEach
+ public void initEach() {
+ atomicLong = new AtomicLong();
+ }
+
+ @Test
+ public void testAsSupplier() throws IOException {
+ assertThrows(UncheckedIOException.class, () -> TestConstants.THROWING_IO_LONG_SUPPLIER.asSupplier().getAsLong());
+ assertEquals(1L, getThrowsNone(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L)));
+ assertEquals(1L, atomicLong.get());
+ assertNotEquals(TestConstants.THROWING_IO_LONG_SUPPLIER.asSupplier(), TestConstants.THROWING_IO_LONG_SUPPLIER.asSupplier());
+ }
+
+ @Test
+ public void testGet() throws IOException {
+ assertThrows(IOException.class, () -> TestConstants.THROWING_IO_LONG_SUPPLIER.getAsLong());
+ assertThrows(IOException.class, () -> {
+ throw new IOException();
+ });
+ assertEquals(1L, getThrowsIO(() -> TestUtils.compareAndSetThrowsIO(atomicLong, 1L)));
+ assertEquals(1L, atomicLong.get());
+ }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/commons/io/function/TestConstants.java b/src/test/java/org/apache/commons/io/function/TestConstants.java
index 5f0581c7..bafe7ef0 100644
--- a/src/test/java/org/apache/commons/io/function/TestConstants.java
+++ b/src/test/java/org/apache/commons/io/function/TestConstants.java
@@ -44,6 +44,10 @@ class TestConstants {
static IOFunction<Object, Object> THROWING_IO_FUNCTION = t -> throwIOException();
+ static IOIntSupplier THROWING_IO_INT_SUPPLIER = () -> throwIOException();
+
+ static IOLongSupplier THROWING_IO_LONG_SUPPLIER = () -> throwIOException();
+
static IOPredicate<Object> THROWING_IO_PREDICATE = t -> throwIOException();
static IOQuadFunction<Object, Object, Object, Object, Object> THROWING_IO_QUAD_FUNCTION = (t, u, v, w) -> throwIOException();
diff --git a/src/test/java/org/apache/commons/io/function/TestUtils.java b/src/test/java/org/apache/commons/io/function/TestUtils.java
index 9b6f3aa7..9d05f026 100644
--- a/src/test/java/org/apache/commons/io/function/TestUtils.java
+++ b/src/test/java/org/apache/commons/io/function/TestUtils.java
@@ -18,10 +18,34 @@
package org.apache.commons.io.function;
import java.io.IOException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
class TestUtils {
+ static int compareAndSetThrowsIO(final AtomicInteger ref, final int update) throws IOException {
+ return compareAndSetThrowsIO(ref, 0, update);
+ }
+
+ static int compareAndSetThrowsIO(final AtomicInteger ref, final int expected, final int update) throws IOException {
+ if (!ref.compareAndSet(expected, update)) {
+ throw new IOException("Unexpected");
+ }
+ return ref.get(); // same as update
+ }
+
+ static long compareAndSetThrowsIO(final AtomicLong ref, final long update) throws IOException {
+ return compareAndSetThrowsIO(ref, 0, update);
+ }
+
+ static long compareAndSetThrowsIO(final AtomicLong ref, final long expected, final long update) throws IOException {
+ if (!ref.compareAndSet(expected, update)) {
+ throw new IOException("Unexpected");
+ }
+ return ref.get(); // same as update
+ }
+
static <T> T compareAndSetThrowsIO(final AtomicReference<T> ref, final T update) throws IOException {
return compareAndSetThrowsIO(ref, null, update);
}
diff --git a/src/test/java/org/apache/commons/io/function/UncheckTest.java b/src/test/java/org/apache/commons/io/function/UncheckTest.java
index ee73abc6..fedd08a2 100644
--- a/src/test/java/org/apache/commons/io/function/UncheckTest.java
+++ b/src/test/java/org/apache/commons/io/function/UncheckTest.java
@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.BeforeEach;
@@ -36,8 +37,8 @@ public class UncheckTest {
private AtomicReference<String> ref1;
private AtomicReference<String> ref2;
private AtomicReference<String> ref3;
-
private AtomicReference<String> ref4;
+ private AtomicInteger atomicInt;
@BeforeEach
public void initEach() {
@@ -45,6 +46,7 @@ public class UncheckTest {
ref2 = new AtomicReference<>();
ref3 = new AtomicReference<>();
ref4 = new AtomicReference<>();
+ atomicInt = new AtomicInteger();
}
@Test
@@ -158,6 +160,16 @@ public class UncheckTest {
assertEquals("new1", ref1.get());
}
+ @Test
+ public void testGetAsInt() {
+ assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(() -> {
+ throw new IOException();
+ }));
+ assertThrows(UncheckedIOException.class, () -> Uncheck.getAsInt(TestConstants.THROWING_IO_INT_SUPPLIER));
+ assertEquals(1, Uncheck.getAsInt(() -> TestUtils.compareAndSetThrowsIO(atomicInt, 1)));
+ assertEquals(1, atomicInt.get());
+ }
+
@Test
public void testRun() {
assertThrows(UncheckedIOException.class, () -> Uncheck.run(() -> {
diff --git a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
index 97c3f1d3..7bc997e0 100644
--- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java
@@ -425,19 +425,6 @@ public class CharSequenceInputStreamTest {
}
}
- private void testSingleByteRead(final String testString, final String charsetName) throws IOException {
- final byte[] bytes = testString.getBytes(charsetName);
- try (InputStream in = new CharSequenceInputStream(testString, charsetName, 512)) {
- for (final byte b : bytes) {
- final int read = in.read();
- assertTrue(read >= 0, "read " + read + " >=0 ");
- assertTrue(read <= 255, "read " + read + " <= 255");
- assertEquals(b, (byte) read, "Should agree with input");
- }
- assertEquals(-1, in.read());
- }
- }
-
@Test
public void testResetCharset() {
assertNotNull(CharSequenceInputStream.builder().setReader(new StringReader("\uD800")).setCharset((Charset) null).getCharset());
@@ -453,6 +440,19 @@ public class CharSequenceInputStreamTest {
assertNotNull(CharSequenceInputStream.builder().setReader(new StringReader("\uD800")).setCharset((String) null).getCharset());
}
+ private void testSingleByteRead(final String testString, final String charsetName) throws IOException {
+ final byte[] bytes = testString.getBytes(charsetName);
+ try (InputStream in = new CharSequenceInputStream(testString, charsetName, 512)) {
+ for (final byte b : bytes) {
+ final int read = in.read();
+ assertTrue(read >= 0, "read " + read + " >=0 ");
+ assertTrue(read <= 255, "read " + read + " <= 255");
+ assertEquals(b, (byte) read, "Should agree with input");
+ }
+ assertEquals(-1, in.read());
+ }
+ }
+
@Test
public void testSingleByteRead_RequiredCharsets() throws IOException {
for (final String csName : getRequiredCharsetNames()) {
diff --git a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
index fb0636d4..7aaaa3d9 100644
--- a/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ReaderInputStreamTest.java
@@ -37,6 +37,7 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.xml.parsers.DocumentBuilderFactory;
+
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
@@ -202,13 +203,6 @@ public class ReaderInputStreamTest {
}
}
- @Test
- public void testIo803StringReaderSanityCheck() {
- final StringReader reader = new StringReader("");
- final InputSource inputSource = new InputSource(reader);
- assertThrows(SAXException.class, () -> DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource));
- }
-
@Test
public void testIo803SAXException() throws IOException {
final StringReader reader = new StringReader("");
@@ -218,6 +212,13 @@ public class ReaderInputStreamTest {
}
}
+ @Test
+ public void testIo803StringReaderSanityCheck() {
+ final StringReader reader = new StringReader("");
+ final InputSource inputSource = new InputSource(reader);
+ assertThrows(SAXException.class, () -> DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource));
+ }
+
@Test
public void testLargeUTF8WithBufferedRead() throws IOException {
testWithBufferedRead(LARGE_TEST_STRING, UTF_8);