From ceb23f2cbfcc18ddb4526b988a7b9acbba9752a7 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Tue, 23 Jan 2024 07:13:29 -0500 Subject: [PATCH] deprecate writeLines methods without a charset --- .../java/org/apache/commons/io/FileUtils.java | 16 ++++++++++++---- src/main/java/org/apache/commons/io/IOUtils.java | 4 +--- .../org/apache/commons/io/FileUtilsTest.java | 8 ++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index d2e17d6b629..e156ac10772 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -3297,7 +3297,9 @@ public static void writeByteArrayToFile(final File file, final byte[] data, fina * @param lines the lines to write, {@code null} entries produce blank lines * @throws IOException in case of an I/O error * @since 1.3 + * @deprecated use {@link #writeLines(File, String, Collection)} instead (and specify the appropriate encoding) */ + @Deprecated public static void writeLines(final File file, final Collection lines) throws IOException { writeLines(file, null, lines, null, false); } @@ -3313,7 +3315,9 @@ public static void writeLines(final File file, final Collection lines) throws * end of the file rather than overwriting * @throws IOException in case of an I/O error * @since 2.1 + * @deprecated use {@link #writeLines(File, String, Collection, boolean)} instead (and specify the appropriate encoding) */ + @Deprecated public static void writeLines(final File file, final Collection lines, final boolean append) throws IOException { writeLines(file, null, lines, null, append); } @@ -3328,7 +3332,9 @@ public static void writeLines(final File file, final Collection lines, final * @param lineEnding the line separator to use, {@code null} is system default * @throws IOException in case of an I/O error * @since 1.3 + * @deprecated use {@link #writeLines(File, String, Collection, String)} instead (and specify the appropriate encoding) */ + @Deprecated public static void writeLines(final File file, final Collection lines, final String lineEnding) throws IOException { writeLines(file, null, lines, lineEnding, false); } @@ -3345,7 +3351,9 @@ public static void writeLines(final File file, final Collection lines, final * end of the file rather than overwriting * @throws IOException in case of an I/O error * @since 2.1 + * @deprecated use {@link #writeLines(File, String, Collection, boolean)} instead and specify the appropriate encoding */ + @Deprecated public static void writeLines(final File file, final Collection lines, final String lineEnding, final boolean append) throws IOException { writeLines(file, null, lines, lineEnding, append); } @@ -3363,7 +3371,7 @@ public static void writeLines(final File file, final Collection lines, final * @param charsetName the name of the requested charset, {@code null} means platform default * @param lines the lines to write, {@code null} entries produce blank lines * @throws IOException in case of an I/O error - * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM + * @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM * @since 1.1 */ public static void writeLines(final File file, final String charsetName, final Collection lines) throws IOException { @@ -3381,7 +3389,7 @@ public static void writeLines(final File file, final String charsetName, final C * @param append if {@code true}, then the lines will be added to the * end of the file rather than overwriting * @throws IOException in case of an I/O error - * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM + * @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM * @since 2.1 */ public static void writeLines(final File file, final String charsetName, final Collection lines, final boolean append) throws IOException { @@ -3402,7 +3410,7 @@ public static void writeLines(final File file, final String charsetName, final C * @param lines the lines to write, {@code null} entries produce blank lines * @param lineEnding the line separator to use, {@code null} is system default * @throws IOException in case of an I/O error - * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM + * @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM * @since 1.1 */ public static void writeLines(final File file, final String charsetName, final Collection lines, final String lineEnding) throws IOException { @@ -3421,7 +3429,7 @@ public static void writeLines(final File file, final String charsetName, final C * @param append if {@code true}, then the lines will be added to the * end of the file rather than overwriting * @throws IOException in case of an I/O error - * @throws java.io.UnsupportedEncodingException if the encoding is not supported by the VM + * @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported by the VM * @since 2.1 */ public static void writeLines(final File file, final String charsetName, final Collection lines, final String lineEnding, final boolean append) diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index 6b48d09fd39..79660fbc5ca 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -3876,9 +3876,7 @@ public static void writeLines(final Collection lines, String lineEnding, fina * @param charsetName the name of the requested charset, null means platform default * @throws NullPointerException if the output is null * @throws IOException if an I/O error occurs - * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io - * .UnsupportedEncodingException} in version 2.2 if the - * encoding is not supported. + * @throws java.nio.charset.UnsupportedCharsetException if the encoding is not supported * @since 1.1 */ public static void writeLines(final Collection lines, final String lineEnding, diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java index 71811ac8ec4..0a7b70d5444 100644 --- a/src/test/java/org/apache/commons/io/FileUtilsTest.java +++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java @@ -3160,6 +3160,14 @@ public void testWriteLinesEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousF assertEquals(expected, actual); } + @Test + public void testWriteLinesUnsupportedCharset() throws Exception { + final File file = TestUtils.newFile(tempDirFile, "lines.txt"); + + final List lines = Arrays.asList("my first line", "The second Line"); + assertThrows(UnsupportedCharsetException.class, () -> FileUtils.writeLines(file, "there_is_no_such_charset", lines)); + } + @Test public void testWriteStringToFile_WithAppendOptionFalse_ShouldDeletePreviousFileLines() throws Exception { final File file = TestUtils.newFile(tempDirFile, "lines.txt");