This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 469ef769 [IO-414] don't write a BOM on every (or any) line (#492) 469ef769 is described below commit 469ef769dfda5da478bdf802d883bf28874ded79 Author: Elliotte Rusty Harold <elh...@users.noreply.github.com> AuthorDate: Sat Oct 7 10:44:10 2023 -0400 [IO-414] don't write a BOM on every (or any) line (#492) * don't write a BOM on every (or any) line * detab * don't write a BOM on every (or any) line * whitespace * checkstyle * Remove trailing whitespace --------- Co-authored-by: Gary Gregory <garydgreg...@users.noreply.github.com> --- src/main/java/org/apache/commons/io/IOUtils.java | 13 +++++++++++-- src/test/java/org/apache/commons/io/IOUtilsTest.java | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index 6d1ebe7e..3382e865 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -45,6 +45,7 @@ import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.channels.Selector; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.Arrays; import java.util.Collection; @@ -3828,11 +3829,15 @@ public class IOUtils { * an {@link OutputStream} line by line, using the specified character * encoding and the specified line ending. * + * UTF-16 is written big-endian with no byte order mark. + * For little endian, use UTF-16LE. For a BOM, write it to the stream + * before calling this method. + * * @param lines the lines to write, null entries produce blank lines * @param lineEnding the line separator to use, null is system default * @param output the {@link OutputStream} to write to, not null, not closed * @param charset the charset to use, null means platform default - * @throws NullPointerException if the output is null + * @throws NullPointerException if output is null * @throws IOException if an I/O error occurs * @since 2.3 */ @@ -3844,7 +3849,11 @@ public class IOUtils { if (lineEnding == null) { lineEnding = System.lineSeparator(); } - final Charset cs = Charsets.toCharset(charset); + Charset cs = Charsets.toCharset(charset); + // don't write a BOM + if (cs == StandardCharsets.UTF_16) { + cs = StandardCharsets.UTF_16BE; + } final byte[] eolBytes = lineEnding.getBytes(cs); for (final Object line : lines) { if (line != null) { diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java b/src/test/java/org/apache/commons/io/IOUtilsTest.java index e361e885..0a3e7d39 100644 --- a/src/test/java/org/apache/commons/io/IOUtilsTest.java +++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java @@ -1761,4 +1761,13 @@ public class IOUtilsTest { } } + @Test + public void testWriteLines() throws IOException { + final String[] data = {"The", "quick"}; + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + IOUtils.writeLines(Arrays.asList(data), "\n", out, "UTF-16"); + final String result = new String(out.toByteArray(), StandardCharsets.UTF_16); + assertEquals("The\nquick\n", result); + } + }