Skip to content

Commit 8b16d8b

Browse files
committed
offloading file copy work to operating system allows potential speedup
Since Plexus Utils enforces Java 7 it is safe to invoke NIO operations introduced by JRE 7. Among those operations are methods for potentially offloading work to the operating system. Theoretically this should be (much) faster on modern JREs than pumping bytes up and down the JVM just to get them from one file into another. Even without any actual performance gain it makes sense to get rid of custom code in favor of JRE code, as it improves class loading speed and reduces memory and on-disk footprint, while cutting down number of potential bugs at the same time.
1 parent 658fb00 commit 8b16d8b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/main/java/org/codehaus/plexus/util/FileUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,16 @@ public static void copyFile( final File source, final File destination )
11041104

11051105
private static void doCopyFile( File source, File destination )
11061106
throws IOException
1107+
{
1108+
// offload to operating system if supported
1109+
if ( Java7Detector.isJava7() )
1110+
doCopyFileUsingNewIO( source, destination );
1111+
else
1112+
doCopyFileUsingLegacyIO( source, destination );
1113+
}
1114+
1115+
private static void doCopyFileUsingLegacyIO( File source, File destination )
1116+
throws IOException
11071117
{
11081118
FileInputStream fis = null;
11091119
FileOutputStream fos = null;
@@ -1141,6 +1151,12 @@ private static void doCopyFile( File source, File destination )
11411151
}
11421152
}
11431153

1154+
private static void doCopyFileUsingNewIO( File source, File destination )
1155+
throws IOException
1156+
{
1157+
NioFiles.copy( source, destination );
1158+
}
1159+
11441160
/**
11451161
* Copy file from source to destination only if source timestamp is later than the destination timestamp.
11461162
* The directories up to <code>destination</code> will be created if they don't already exist.

0 commit comments

Comments
 (0)