Daniel Vega created IO-887:
------------------------------

             Summary: 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


{{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 as 
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)

Reply via email to