[
https://issues.apache.org/jira/browse/IO-887?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18065714#comment-18065714
]
Daniel Vega commented on IO-887:
--------------------------------
h3. Old code
{code:java}
var sw1 = new StringWriter();
var wos1 = new WriterOutputStream(sw1, StandardCharsets.UTF_8);
var bytes = "¿Cómo estás".getBytes("Cp850");
wos1.write(bytes); wos1.flush();
System.out.println("Old -> " + sw1);
{code}
h3. Old output
{code:none}
Old -> ?C?mo est?s
{code}
h3. New code
{code:java}
var sw2 = new StringWriter();
var wos2 = WriterOutputStream.builder()
.setWriter(sw2)
.setCharset(StandardCharsets.UTF_8).get();
var bytes = "¿Cómo estás".getBytes("Cp850");
wos2.write(bytes); wos2.flush();
System.out.println("New -> " + sw2);
{code}
h3. New output
{code:none}
java.io.IOException: Unexpected coder result
at
org.apache.commons.io.output.WriterOutputStream.processInput(WriterOutputStream.java:400)
at
org.apache.commons.io.output.WriterOutputStream.write(WriterOutputStream.java:431)
at
org.apache.commons.io.output.WriterOutputStream.write(WriterOutputStream.java:415)
{code}
> WriterOutputStream truncates the output by default
> --------------------------------------------------
>
> Key: IO-887
> URL: https://issues.apache.org/jira/browse/IO-887
> Project: Commons IO
> Issue Type: Bug
> Components: Streams/Writers
> Affects Versions: 2.12.0
> Reporter: Daniel Vega
> Priority: Minor
>
> {{WriterOutputStream}} switched from using constructors to builders in
> commons.io 2.12.0
> Prior to that change, you usually did something like this:
> {code:java}
> var wos = new WriterOutputStream(writer, StandardCharsets.UTF_8)
> {code}
> After version 2.12.0 you need to migrate that code. If you follow [the
> documentation|https://github.com/apache/commons-io/blob/350a4bff8e17bf549300307394922ed8a9c7e158/src/main/java/org/apache/commons/io/output/WriterOutputStream.java#L60-L63]
> you write:
> {code:java}
> var wos = WriterOutputStream.builder()
> .setWriter(writer)
> .setCharset(cs)
> .get();
> {code}
> The first minor annoyance is that this throws {{IOException}} and the
> previous no. Nothing serious. But the mayor problem is that this is not
> equivalent to the code before. The compatible change is:
> {code:java}
> var decoder = StandardCharsets.UTF_8
> .newDecoder()
> .onMalformedInput(CodingErrorAction.REPLACE)
> .onUnmappableCharacter(CodingErrorAction.REPLACE)
> .replaceWith("?");
> var wos = WriterOutputStream.builder()
> .setWriter(writer)
> .setCharsetDecoder(decoder)
> .get();
> {code}
> This change in behavior is not documented and caused some issues. It seems
> that the current default for Decoder creation is not sane as it truncates the
> output
--
This message was sent by Atlassian Jira
(v8.20.10#820010)