Skip to content

Commit 469ef76

Browse files
elharogarydgregory
andauthored
[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 <garydgregory@users.noreply.github.com>
1 parent 3d85ccc commit 469ef76

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.nio.channels.ReadableByteChannel;
4646
import java.nio.channels.Selector;
4747
import java.nio.charset.Charset;
48+
import java.nio.charset.StandardCharsets;
4849
import java.nio.file.Files;
4950
import java.util.Arrays;
5051
import java.util.Collection;
@@ -3828,11 +3829,15 @@ public static void writeLines(final Collection<?> lines, final String lineEnding
38283829
* an {@link OutputStream} line by line, using the specified character
38293830
* encoding and the specified line ending.
38303831
*
3832+
* UTF-16 is written big-endian with no byte order mark.
3833+
* For little endian, use UTF-16LE. For a BOM, write it to the stream
3834+
* before calling this method.
3835+
*
38313836
* @param lines the lines to write, null entries produce blank lines
38323837
* @param lineEnding the line separator to use, null is system default
38333838
* @param output the {@link OutputStream} to write to, not null, not closed
38343839
* @param charset the charset to use, null means platform default
3835-
* @throws NullPointerException if the output is null
3840+
* @throws NullPointerException if output is null
38363841
* @throws IOException if an I/O error occurs
38373842
* @since 2.3
38383843
*/
@@ -3844,7 +3849,11 @@ public static void writeLines(final Collection<?> lines, String lineEnding, fina
38443849
if (lineEnding == null) {
38453850
lineEnding = System.lineSeparator();
38463851
}
3847-
final Charset cs = Charsets.toCharset(charset);
3852+
Charset cs = Charsets.toCharset(charset);
3853+
// don't write a BOM
3854+
if (cs == StandardCharsets.UTF_16) {
3855+
cs = StandardCharsets.UTF_16BE;
3856+
}
38483857
final byte[] eolBytes = lineEnding.getBytes(cs);
38493858
for (final Object line : lines) {
38503859
if (line != null) {

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,4 +1761,13 @@ public void testWriteLittleString() throws IOException {
17611761
}
17621762
}
17631763

1764+
@Test
1765+
public void testWriteLines() throws IOException {
1766+
final String[] data = {"The", "quick"};
1767+
final ByteArrayOutputStream out = new ByteArrayOutputStream();
1768+
IOUtils.writeLines(Arrays.asList(data), "\n", out, "UTF-16");
1769+
final String result = new String(out.toByteArray(), StandardCharsets.UTF_16);
1770+
assertEquals("The\nquick\n", result);
1771+
}
1772+
17641773
}

0 commit comments

Comments
 (0)