Avoid Code Duplication: Reuse Sleep from ThreadMonitor.
Redo changes from PR from DaGeRe.
Use sleepQuietly.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -80,9 +80,12 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-749" dev="ggregory" type="fix" due-to="haihuiyang, Gary Gregory">
FileUtils.listFiles() does not list matching files if File parameter is a symbolic link.
</action>
- <action issue="IO-749" dev="ggregory" type="fix" due-to="niranjanghule, Gary Gregory">
+ <action dev="ggregory" type="fix" due-to="niranjanghule, Gary Gregory">
Fix typo in Javadocs for FileUtils#convertFileCollectionToFileArray() #276.
</action>
+ <action dev="ggregory" type="fix" due-to="DaGeRe, Gary Gregory">
+ Avoid Code Duplication: Reuse Sleep from ThreadMonitor #66.
+ </action>
<!-- ADD -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add BrokenReader.INSTANCE.

MODIFY
src/main/java/org/apache/commons/io/ThreadMonitor.java
src/main/java/org/apache/commons/io/ThreadMonitor.java
@@ -117,7 +117,7 @@ public void run() {
* @param duration the sleep duration.
* @throws InterruptedException if interrupted.
*/
- private static void sleep(final Duration duration) throws InterruptedException {
+ static void sleep(final Duration duration) throws InterruptedException {
final Instant finishInstant = Instant.now().plus(duration);
Duration remainingDuration = duration;
do {

MODIFY
src/test/java/org/apache/commons/io/DemuxTest.java
src/test/java/org/apache/commons/io/DemuxTest.java
@@ -64,8 +64,8 @@ public void run() {
//System.out.println( "Reading: " + (char)ch );
stringBuffer.append((char) ch);
- final int sleepTime = Math.abs(c_random.nextInt() % 10);
- TestUtils.sleep(sleepTime);
+ final int sleepMillis = Math.abs(c_random.nextInt() % 10);
+ TestUtils.sleep(sleepMillis);
ch = demuxInputStream.read();
}
} catch (final Exception e) {
@@ -96,8 +96,8 @@ public void run() {
try {
//System.out.println( "Writing: " + (char)byteArray[ i ] );
demuxOutputStream.write(element);
- final int sleepTime = Math.abs(c_random.nextInt() % 10);
- TestUtils.sleep(sleepTime);
+ final int sleepMillis = Math.abs(c_random.nextInt() % 10);
+ TestUtils.sleep(sleepMillis);
} catch (final Exception e) {
e.printStackTrace();
}

MODIFY
src/test/java/org/apache/commons/io/FileUtilsTest.java
src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -1668,11 +1668,7 @@ public void testIsFileNewerOlder() throws Exception {
}
do {
- try {
- TestUtils.sleep(1000);
- } catch (final InterruptedException ie) {
- // ignore
- }
+ TestUtils.sleepQuietly(1000);
if (!reference.getParentFile().exists()) {
throw new IOException("Cannot create file " + reference
+ " as the parent directory does not exist");

MODIFY
src/test/java/org/apache/commons/io/test/TestUtils.java
src/test/java/org/apache/commons/io/test/TestUtils.java
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.time.Duration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.commons.lang3.ThreadUtils;
/**
* Base class for testcases doing tests with files.
@@ -185,8 +187,7 @@ public static void generateTestData(final File file, final long size) throws IOE
}
}
- public static void generateTestData(final OutputStream out, final long size)
- throws IOException {
+ public static void generateTestData(final OutputStream out, final long size) throws IOException {
for (int i = 0; i < size; i++) {
// output.write((byte)'X');
// nice varied byte pattern compatible with Readers and Writers
@@ -207,23 +208,23 @@ public static File newFile(final File testDirectory, final String filename) thro
}
/**
- * Sleep for a guaranteed number of milliseconds unless interrupted.
+ * Sleeps for a guaranteed number of milliseconds unless interrupted.
*
* This method exists because Thread.sleep(100) can sleep for 0, 70, 100 or 200ms or anything else
* it deems appropriate. Read the docs on Thread.sleep for further details.
*
- * @param millis the number of milliseconds to sleep for
- * @throws InterruptedException if interrupted
+ * @param millis the number of milliseconds to sleep.
+ * @throws InterruptedException if interrupted.
*/
public static void sleep(final long millis) throws InterruptedException {
- final long finishAtMillis = System.currentTimeMillis() + millis;
- long remainingMillis = millis;
- do {
- Thread.sleep(remainingMillis);
- remainingMillis = finishAtMillis - System.currentTimeMillis();
- } while (remainingMillis > 0);
+ ThreadUtils.sleep(Duration.ofMillis(millis));
}
+ /**
+ * Sleeps and swallows InterruptedException.
+ *
+ * @param millis the number of milliseconds to sleep.
+ */
public static void sleepQuietly(final long millis) {
try {
sleep(millis);

Avoid Code Duplication: Reuse Sleep from ThreadMonitor.

- Redo changes from PR #66 from DaGeRe.
- Use sleepQuietly.