Deprecate IOUtils.LINE_SEPARATOR in favor of Java 7's
System.lineSeparator().
Remove unused imports.
Fix compiler warnings by using try-with-resources.
Document empty block.
Remove checked exceptions from test methods that do not throw them.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -125,6 +125,9 @@ The <action> type attribute can be add,update,fix,remove.
Add IOUtils.consume(InputStream).
</action>
<!-- UPDATES -->
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Deprecate IOUtils.LINE_SEPARATOR in favor of Java 7's System.lineSeparator().
+ </action>
<action dev="ggregory" type="update" due-to="Gary Gregory">
Replace FindBugs with SpotBugs.
</action>

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
-import org.apache.commons.io.output.NullOutputStream;
/**
* General file manipulation utilities.

MODIFY
src/main/java/org/apache/commons/io/IOUtils.java
src/main/java/org/apache/commons/io/IOUtils.java
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
/**
* The system line separator string.
+ *
+ * @deprecated Use {@link System#lineSeparator()}.
*/
- public static final String LINE_SEPARATOR;
+ @Deprecated
+ public static final String LINE_SEPARATOR = System.lineSeparator();
/**
* The Unix line separator string.
*/
private static char[] SKIP_CHAR_BUFFER;
- static {
- // avoid security issues
- try (final StringBuilderWriter buf = new StringBuilderWriter(4);
- final PrintWriter out = new PrintWriter(buf)) {
- out.println();
- LINE_SEPARATOR = buf.toString();
- }
- }
-
/**
* Returns the given InputStream if it is already a {@link BufferedInputStream}, otherwise creates a
* BufferedInputStream from the given InputStream.
@@ -3289,7 +3282,7 @@ public static void writeLines(final Collection<?> lines, String lineEnding, fina
return;
}
if (lineEnding == null) {
- lineEnding = LINE_SEPARATOR;
+ lineEnding = System.lineSeparator();
}
final Charset cs = Charsets.toCharset(charset);
for (final Object line : lines) {
@@ -3341,7 +3334,7 @@ public static void writeLines(final Collection<?> lines, String lineEnding,
return;
}
if (lineEnding == null) {
- lineEnding = LINE_SEPARATOR;
+ lineEnding = System.lineSeparator();
}
for (final Object line : lines) {
if (line != null) {

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
src/test/java/org/apache/commons/io/FileUtilsTestCase.java
@@ -2102,9 +2102,9 @@ public void testWriteLines_4arg_nullSeparator() throws Exception {
final File file = TestUtils.newFile(temporaryFolder, "lines.txt");
FileUtils.writeLines(file, "US-ASCII", list, null);
- final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
- IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
- IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
+ final String expected = "hello" + System.lineSeparator() + "world" + System.lineSeparator() +
+ System.lineSeparator() + "this is" + System.lineSeparator() +
+ System.lineSeparator() + "some text" + System.lineSeparator();
final String actual = FileUtils.readFileToString(file, "US-ASCII");
assertEquals(expected, actual);
}
@@ -2118,9 +2118,9 @@ public void testWriteLines_3arg_nullSeparator() throws Exception {
final File file = TestUtils.newFile(temporaryFolder, "lines.txt");
FileUtils.writeLines(file, "US-ASCII", list);
- final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR +
- IOUtils.LINE_SEPARATOR + "this is" + IOUtils.LINE_SEPARATOR +
- IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
+ final String expected = "hello" + System.lineSeparator() + "world" + System.lineSeparator() +
+ System.lineSeparator() + "this is" + System.lineSeparator() +
+ System.lineSeparator() + "some text" + System.lineSeparator();
final String actual = FileUtils.readFileToString(file, "US-ASCII");
assertEquals(expected, actual);
}
@@ -2135,8 +2135,8 @@ public void testWriteLines_5argsWithAppendOptionTrue_ShouldNotDeletePreviousFile
final String expected = "This line was there before you..."
+ "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2150,8 +2150,8 @@ public void testWriteLines_5argsWithAppendOptionFalse_ShouldDeletePreviousFileLi
FileUtils.writeLines(file, null, linesToAppend, null, false);
final String expected = "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2166,8 +2166,8 @@ public void testWriteLines_4argsWithAppendOptionTrue_ShouldNotDeletePreviousFile
final String expected = "This line was there before you..."
+ "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2181,8 +2181,8 @@ public void testWriteLines_4argsWithAppendOptionFalse_ShouldDeletePreviousFileLi
FileUtils.writeLines(file, linesToAppend, null, false);
final String expected = "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2198,8 +2198,8 @@ public void testWriteLinesEncoding_WithAppendOptionTrue_ShouldNotDeletePreviousF
final String expected = "This line was there before you..."
+ "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2213,8 +2213,8 @@ public void testWriteLinesEncoding_WithAppendOptionFalse_ShouldDeletePreviousFil
FileUtils.writeLines(file, null, linesToAppend, false);
final String expected = "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2229,8 +2229,8 @@ public void testWriteLines_3argsWithAppendOptionTrue_ShouldNotDeletePreviousFile
final String expected = "This line was there before you..."
+ "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}
@@ -2244,8 +2244,8 @@ public void testWriteLines_3argsWithAppendOptionFalse_ShouldDeletePreviousFileLi
FileUtils.writeLines(file, linesToAppend, false);
final String expected = "my first line"
- + IOUtils.LINE_SEPARATOR + "The second Line"
- + IOUtils.LINE_SEPARATOR;
+ + System.lineSeparator() + "The second Line"
+ + System.lineSeparator();
final String actual = FileUtils.readFileToString(file);
assertEquals(expected, actual);
}

MODIFY
src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java
src/test/java/org/apache/commons/io/IOUtilsWriteTestCase.java
@@ -622,7 +622,7 @@ public void testWriteLines_OutputStream_nullSeparator() throws Exception {
out.off();
out.flush();
- final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
+ final String expected = "hello" + System.lineSeparator() + "world" + System.lineSeparator();
final String actual = baout.toString();
assertEquals(expected, actual);
}
@@ -682,7 +682,7 @@ public void testWriteLines_OutputStream_Encoding_nullSeparator() throws Exceptio
out.off();
out.flush();
- final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
+ final String expected = "hello" + System.lineSeparator() + "world" + System.lineSeparator();
final String actual = baout.toString();
assertEquals(expected, actual);
}
@@ -767,7 +767,7 @@ public void testWriteLines_Writer_nullSeparator() throws Exception {
out.off();
writer.flush();
- final String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
+ final String expected = "hello" + System.lineSeparator() + "world" + System.lineSeparator();
final String actual = baout.toString();
assertEquals(expected, actual);
}

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java
src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
import java.io.IOException;
-import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

MODIFY
src/test/java/org/apache/commons/io/file/PathUtilsDeleteTest.java
src/test/java/org/apache/commons/io/file/PathUtilsDeleteTest.java
import static org.apache.commons.io.file.CounterAssertions.assertCounts;
-import java.awt.geom.PathIterator;
import java.io.IOException;
import java.nio.file.Files;
-import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

MODIFY
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
src/test/java/org/apache/commons/io/input/CountingInputStreamTest.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
-import org.apache.commons.io.output.NullOutputStream;
import org.junit.jupiter.api.Test;
/**

MODIFY
src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
src/test/java/org/apache/commons/io/output/CountingOutputStreamTest.java
*/
package org.apache.commons.io.output;
-
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.NullInputStream;
import org.junit.jupiter.api.Test;
-
/**
*
*/
-
public class CountingOutputStreamTest {
-
@Test
public void testCounting() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();

MODIFY
src/test/java/org/apache/commons/io/test/TestUtils.java
src/test/java/org/apache/commons/io/test/TestUtils.java
@@ -60,9 +60,10 @@ public static void createFile(final File file, final long size)
public static byte[] generateTestData(final long size) {
try {
- final ByteArrayOutputStream baout = new ByteArrayOutputStream();
+ try (final ByteArrayOutputStream baout = new ByteArrayOutputStream()) {
generateTestData(baout, size);
return baout.toByteArray();
+ }
} catch (final IOException ioe) {
throw new RuntimeException("This should never happen: " + ioe.getMessage());
}
@@ -183,7 +184,7 @@ public static void assertEqualContent(final char[] c0, final File file) throws I
}
}
- public static void checkWrite(final OutputStream output) throws Exception {
+ public static void checkWrite(final OutputStream output) {
try {
new java.io.PrintStream(output).write(0);
} catch (final Throwable t) {
@@ -191,7 +192,7 @@ public static void checkWrite(final OutputStream output) throws Exception {
}
}
- public static void checkWrite(final Writer output) throws Exception {
+ public static void checkWrite(final Writer output) {
try {
new java.io.PrintWriter(output).write('a');
} catch (final Throwable t) {
@@ -199,8 +200,7 @@ public static void checkWrite(final Writer output) throws Exception {
}
}
- public static void deleteFile(final File file)
- throws Exception {
+ public static void deleteFile(final File file) {
if (file.exists()) {
assertTrue(file.delete(), "Couldn't delete file: " + file);
}
@@ -228,6 +228,7 @@ public static void sleepQuietly(final long ms) {
try {
sleep(ms);
} catch (final InterruptedException ignored){
+ // ignore InterruptedException.
}
}

Deprecate IOUtils.LINE_SEPARATOR in favor of Java 7's
System.lineSeparator().

- Remove unused imports.
- Fix compiler warnings by using try-with-resources.
- Document empty block.
- Remove checked exceptions from test methods that do not throw them.