commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Florent Brosse (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (IO-414) IOUtils.writeLines is not correct with a charset with BOM
Date Sun, 08 Dec 2013 16:44:35 GMT

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

Florent Brosse edited comment on IO-414 at 12/8/13 4:43 PM:
------------------------------------------------------------

I suggest that code to correct that bug:

    public static void writeLines(final Collection<?> lines, String lineEnding, final
OutputStream output, final Charset encoding)
            throws IOException {
        if (lines == null) {
            return;
        }
        if (lineEnding == null) {
            lineEnding = LINE_SEPARATOR;
        }
        final Charset cs = Charsets.toCharset(encoding);
        StringBuilder stringBuilder = new StringBuilder();
        for (final Object line : lines) {
            if (line != null) {
            	stringBuilder.append(line.toString());
            }
            stringBuilder.append(lineEnding);
        }
        output.write(stringBuilder.toString().getBytes(cs));
    }

 public static void writeLines(final Collection<?> lines, String lineEnding,
            final Writer writer) throws IOException {
        if (lines == null) {
            return;
        }
        if (lineEnding == null) {
            lineEnding = LINE_SEPARATOR;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (final Object line : lines) {
            if (line != null) {
            	stringBuilder.append(line.toString());
            }
            stringBuilder.append(lineEnding);
        }
        writer.write(stringBuilder.toString());
    }



was (Author: florent.brosse):
I propose that code to correct that bug:

    public static void writeLines(final Collection<?> lines, String lineEnding, final
OutputStream output, final Charset encoding)
            throws IOException {
        if (lines == null) {
            return;
        }
        if (lineEnding == null) {
            lineEnding = LINE_SEPARATOR;
        }
        final Charset cs = Charsets.toCharset(encoding);
        StringBuilder stringBuilder = new StringBuilder();
        for (final Object line : lines) {
            if (line != null) {
            	stringBuilder.append(line.toString());
            }
            stringBuilder.append(lineEnding);
        }
        output.write(stringBuilder.toString().getBytes(cs));
    }

 public static void writeLines(final Collection<?> lines, String lineEnding,
            final Writer writer) throws IOException {
        if (lines == null) {
            return;
        }
        if (lineEnding == null) {
            lineEnding = LINE_SEPARATOR;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (final Object line : lines) {
            if (line != null) {
            	stringBuilder.append(line.toString());
            }
            stringBuilder.append(lineEnding);
        }
        writer.write(stringBuilder.toString());
    }


> IOUtils.writeLines is not correct with a charset with BOM
> ---------------------------------------------------------
>
>                 Key: IO-414
>                 URL: https://issues.apache.org/jira/browse/IO-414
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Streams/Writers, Utilities
>    Affects Versions: 2.4, 2.5
>            Reporter: Florent Brosse
>
> When a charset has a BOM (like UTF-16), the method IOUtils.writeLines put 2 BOM on each
line.



--
This message was sent by Atlassian JIRA
(v6.1#6144)

Mime
View raw message