Skip to content

Commit d428a08

Browse files
committed
[IO-829] Don't decode and reencode characters in a potentially different
charset in AbstractOrigin.CharSequenceOrigin.getReader(Charset) - Add unit test - Javadoc - Use API internally
1 parent fc8a044 commit d428a08

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ The <action> type attribute can be add,update,fix,remove.
8383
<action dev="ggregory" type="fix" issue="IO-828" due-to="Gary Gregory">Deprecate CountingInputStream.getMaxLength() in favor of getMaxCount()).</action>
8484
<action dev="ggregory" type="fix" issue="IO-818" due-to="Gary Gregory">NullInputStream breaks InputStream's read method contract.</action>
8585
<action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">Javadoc shouldn't reference 1.x behavior #539.</action>
86+
<action dev="ggregory" type="fix" issue="IO-829" due-to="Elliotte Rusty Harold, Gary Gregory">Don't decode and reencode characters in a potentially different charset in AbstractOrigin.CharSequenceOrigin.getReader(Charset).</action>
8687
<!-- Add -->
8788
<action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
8889
<action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action>

src/main/java/org/apache/commons/io/build/AbstractOrigin.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public byte[] getByteArray() {
115115
return origin.toString().getBytes(Charset.defaultCharset());
116116
}
117117

118+
/**
119+
* {@inheritDoc}
120+
* <p>
121+
* In this case, the {@code charset} parameter is ignored, since a {@link CharSequence} does not need a {@link Charset} to be read.
122+
* </p>
123+
*/
118124
@Override
119125
public CharSequence getCharSequence(final Charset charset) {
120126
// No conversion
@@ -127,9 +133,15 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
127133
return CharSequenceInputStream.builder().setCharSequence(getCharSequence(Charset.defaultCharset())).get();
128134
}
129135

136+
/**
137+
* {@inheritDoc}
138+
* <p>
139+
* In this case, the {@code charset} parameter is ignored, since a {@link CharSequence} does not need a {@link Charset} to be read.
140+
* </p>
141+
*/
130142
@Override
131-
public Reader getReader(final Charset charset) throws IOException {
132-
return new CharSequenceReader(origin);
143+
public Reader getReader(final Charset ignore) throws IOException {
144+
return new CharSequenceReader(get());
133145
}
134146

135147
@Override

src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
*/
1717
package org.apache.commons.io.build;
1818

19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1921
import static org.junit.jupiter.api.Assertions.assertThrows;
2022

2123
import java.io.FileNotFoundException;
2224
import java.io.IOException;
25+
import java.io.Reader;
2326
import java.nio.charset.StandardCharsets;
2427

2528
import org.apache.commons.io.IOUtils;
@@ -36,10 +39,14 @@ public class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, Cha
3639

3740
@BeforeEach
3841
public void beforeEach() throws FileNotFoundException, IOException {
39-
setOriginRo(new CharSequenceOrigin(IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8)));
42+
setOriginRo(new CharSequenceOrigin(getFixtureStringFromFile()));
4043
setOriginRw(new CharSequenceOrigin("World"));
4144
}
4245

46+
private String getFixtureStringFromFile() throws IOException {
47+
return IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8);
48+
}
49+
4350
@Override
4451
@Test
4552
public void testGetFile() {
@@ -61,6 +68,15 @@ public void testGetPath() {
6168
assertThrows(UnsupportedOperationException.class, super::testGetPath);
6269
}
6370

71+
@Test
72+
public void testGetReaderIgnoreCharset() throws IOException {
73+
// The CharSequenceOrigin ignores the given Charset.
74+
try (final Reader reader = getOriginRo().getReader(StandardCharsets.UTF_16LE)) {
75+
assertNotNull(reader);
76+
assertEquals(getFixtureStringFromFile(), IOUtils.toString(reader));
77+
}
78+
}
79+
6480
@Override
6581
@Test
6682
public void testGetWriter() {

0 commit comments

Comments
 (0)