Skip to content

Commit

Permalink
offloading file copy work to operating system allows potential speedup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mkarg committed Jun 13, 2016
1 parent 658fb00 commit 8b16d8b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/codehaus/plexus/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <code>destination</code> will be created if they don't already exist.
Expand Down

0 comments on commit 8b16d8b

Please sign in to comment.