From 8b16d8b61abfaac13aa5df3ef9894ca857b6623a Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Tue, 24 May 2016 23:59:55 +0200 Subject: [PATCH] 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. --- .../java/org/codehaus/plexus/util/FileUtils.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/codehaus/plexus/util/FileUtils.java b/src/main/java/org/codehaus/plexus/util/FileUtils.java index 40c063a9..a038d0b0 100644 --- a/src/main/java/org/codehaus/plexus/util/FileUtils.java +++ b/src/main/java/org/codehaus/plexus/util/FileUtils.java @@ -1104,6 +1104,16 @@ public static void copyFile( final File source, final File destination ) private static void doCopyFile( File source, File destination ) throws IOException + { + // offload to operating system if supported + if ( Java7Detector.isJava7() ) + doCopyFileUsingNewIO( source, destination ); + else + doCopyFileUsingLegacyIO( source, destination ); + } + + private static void doCopyFileUsingLegacyIO( File source, File destination ) + throws IOException { FileInputStream fis = null; FileOutputStream fos = null; @@ -1141,6 +1151,12 @@ private static void doCopyFile( File source, File destination ) } } + private static void doCopyFileUsingNewIO( File source, File destination ) + throws IOException + { + NioFiles.copy( source, destination ); + } + /** * Copy file from source to destination only if source timestamp is later than the destination timestamp. * The directories up to destination will be created if they don't already exist.