Skip to content

Commit aef2fdf

Browse files
committed
Add IOUtils.readLines(CharSequence)
1 parent bcbe1c5 commit aef2fdf

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
5959
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IORandomAccessFile.</action>
6060
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileMode.io(String).</action>
6161
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileAlterationObserver.Builder() and deprecate most constructors.</action>
62+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOUtils.readLines(CharSequence).</action>
6263
<!-- UPDATE -->
6364
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 74 to 77 #670, #676, #679.</action>
6465
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons.bytebuddy.version from 1.15.1 to 1.15.5 #672, #673, #685, #686.</action>

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.apache.commons.io.function.IOConsumer;
6565
import org.apache.commons.io.function.IOSupplier;
6666
import org.apache.commons.io.function.IOTriFunction;
67+
import org.apache.commons.io.input.CharSequenceReader;
6768
import org.apache.commons.io.input.QueueInputStream;
6869
import org.apache.commons.io.output.AppendableWriter;
6970
import org.apache.commons.io.output.ByteArrayOutputStream;
@@ -2243,6 +2244,20 @@ public static List<String> readLines(final Reader reader) throws UncheckedIOExce
22432244
return toBufferedReader(reader).lines().collect(Collectors.toList());
22442245
}
22452246

2247+
/**
2248+
* Gets the contents of a {@link CharSequence} as a list of Strings, one entry per line.
2249+
*
2250+
* @param csq the {@link CharSequence} to read, not null
2251+
* @return the list of Strings, never null
2252+
* @throws UncheckedIOException if an I/O error occurs
2253+
* @since 2.18.0
2254+
*/
2255+
public static List<String> readLines(final CharSequence csq) throws UncheckedIOException {
2256+
try (CharSequenceReader reader = new CharSequenceReader(csq)) {
2257+
return readLines(reader);
2258+
}
2259+
}
2260+
22462261
/**
22472262
* Gets the contents of a resource as a byte array.
22482263
* <p>

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,36 @@ public void testReadFully_Reader_Offset() throws Exception {
10151015
IOUtils.closeQuietly(reader);
10161016
}
10171017

1018+
@Test
1019+
public void testReadLines_CharSequence() throws IOException {
1020+
final File file = TestUtils.newFile(temporaryFolder, "lines.txt");
1021+
CharSequence csq = null;
1022+
try {
1023+
final String[] data = {"hello", "/u1234", "", "this is", "some text"};
1024+
TestUtils.createLineBasedFile(file, data);
1025+
csq = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
1026+
final List<String> lines = IOUtils.readLines(csq);
1027+
assertEquals(Arrays.asList(data), lines);
1028+
} finally {
1029+
TestUtils.deleteFile(file);
1030+
}
1031+
}
1032+
1033+
@Test
1034+
public void testReadLines_CharSequenceAsStringBuilder() throws IOException {
1035+
final File file = TestUtils.newFile(temporaryFolder, "lines.txt");
1036+
StringBuilder csq = null;
1037+
try {
1038+
final String[] data = {"hello", "/u1234", "", "this is", "some text"};
1039+
TestUtils.createLineBasedFile(file, data);
1040+
csq = new StringBuilder(new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8));
1041+
final List<String> lines = IOUtils.readLines(csq);
1042+
assertEquals(Arrays.asList(data), lines);
1043+
} finally {
1044+
TestUtils.deleteFile(file);
1045+
}
1046+
}
1047+
10181048
@Test
10191049
public void testReadLines_InputStream() throws Exception {
10201050
final File file = TestUtils.newFile(temporaryFolder, "lines.txt");
@@ -1058,7 +1088,6 @@ public void testReadLines_Reader() throws Exception {
10581088
try {
10591089
final String[] data = {"hello", "/u1234", "", "this is", "some text"};
10601090
TestUtils.createLineBasedFile(file, data);
1061-
10621091
in = new InputStreamReader(Files.newInputStream(file.toPath()));
10631092
final List<String> lines = IOUtils.readLines(in);
10641093
assertEquals(Arrays.asList(data), lines);

0 commit comments

Comments
 (0)