You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary D. Gregory (Jira)" <ji...@apache.org> on 2021/02/17 16:21:00 UTC

[jira] [Commented] (IO-714) ReaderInputStream does not call CharsetEncoder.flush(...)

    [ https://issues.apache.org/jira/browse/IO-714?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17285950#comment-17285950 ] 

Gary D. Gregory commented on IO-714:
------------------------------------

[~Marcono1234]

Thank you for your report.
Feel free to provide a PR on GitHub with tests ;-)

> ReaderInputStream does not call CharsetEncoder.flush(...)
> ---------------------------------------------------------
>
>                 Key: IO-714
>                 URL: https://issues.apache.org/jira/browse/IO-714
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Streams/Writers
>    Affects Versions: 2.8.0
>            Reporter: Marcono1234
>            Priority: Major
>
> As defined by the {{CharsetEncoder}} [documentation|https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/charset/CharsetEncoder.html] the _encoding operation_ consists of:
> # (reset)
> # encode
> # flush
> However, {{org.apache.commons.io.input.ReaderInputStream}} does not call {{flush}}. This leads to incorrect results for charsets whose {{flush}} method appends additional bytes.
> Example:
> {code}
> // Charset whose CharsetEncoder.flush(...) puts bytes
> Charset charset = Charset.forName("Cp930");
> // \u0391: Causes CharsetEncoder.flush(...) to put additional bytes
> String s = "\u0391";
> byte[] expected = s.getBytes(charset);
> byte[] actual;
> try (InputStream in = new ReaderInputStream(new StringReader(s), charset)) {
>     actual = IOUtils.toByteArray(in);
> }
> if (!Arrays.equals(expected, actual)) {
>     throw new AssertionError("\n  Expected: " + Arrays.toString(expected) + "\n  Actual:   " + Arrays.toString(actual));
> }
> {code}
> Also make sure to check the result of {{flush()}} because {{OVERFLOW}} as result is possible. In theory {{isError() == true}} might be possible as well, but I don't think any of charset implementations currently return that.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)