diff options
Diffstat (limited to 'src/main/java/org/apache/commons/io/IOUtils.java')
-rw-r--r-- | src/main/java/org/apache/commons/io/IOUtils.java | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index b6b6f137..dfb4c19e 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; @@ -118,7 +119,7 @@ import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; * closing streams after use. * </p> * <p> - * Origin of code: Excalibur. + * Provenance: Excalibur. * </p> */ public class IOUtils { @@ -893,7 +894,6 @@ public class IOUtils { * @param input2 the second stream * @return true if the content of the streams are equal or they both don't * exist, false otherwise - * @throws NullPointerException if either input is null * @throws IOException if an I/O error occurs */ public static boolean contentEquals(final InputStream input1, final InputStream input2) throws IOException { @@ -3827,28 +3827,36 @@ public class IOUtils { * Writes the {@link #toString()} value of each item in a collection to * an {@link OutputStream} line by line, using the specified character * encoding and the specified line ending. + * <p> + * 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. + * </p> * * @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 */ public static void writeLines(final Collection<?> lines, String lineEnding, final OutputStream output, - final Charset charset) throws IOException { + Charset charset) throws IOException { if (lines == null) { return; } if (lineEnding == null) { lineEnding = System.lineSeparator(); } - final Charset cs = Charsets.toCharset(charset); - final byte[] eolBytes = lineEnding.getBytes(cs); + if (StandardCharsets.UTF_16.equals(charset)) { + // don't write a BOM + charset = StandardCharsets.UTF_16BE; + } + final byte[] eolBytes = lineEnding.getBytes(charset); for (final Object line : lines) { if (line != null) { - write(line.toString(), output, cs); + write(line.toString(), output, charset); } output.write(eolBytes); } |