public class Files extends Object
| Constructor and Description |
|---|
Files() |
| Modifier and Type | Method and Description |
|---|---|
static void |
cleanDirectByteBuffer(ByteBuffer byteBuffer)
Clean or unmap a direct ByteBuffer
|
static Boolean |
contentEquals(java.nio.file.Path file1,
java.nio.file.Path file2)
Identical File Contents
|
public static Boolean contentEquals(java.nio.file.Path file1, java.nio.file.Path file2) throws IOException
Uses Memory Mapped Files for fast comparison of large files. This is about 4 times faster than calling
org.apache.commons.io.FileUtils.contentEquals(File file1,File file2)
with identical 4 GiB files. Performance may suffer slightly for smaller files. A future version may be optimized for smaller files too.
Useful for checking if media files, such as music and video, have identical or duplicate content. Video files can easily be over 4 GiB in size. Technically this method will compare files as large as the file system supports.
Performance is about 16 seconds for two 4 GiB files with identical content on an Intel Xeon 5580 at 3.2 GHz using a file system on a LSI MegaRAID SAS 9266-8i.
file1 - 1st file to be comparedfile2 - 2nd file to be comparedIllegalArgumentException - if the arguments are not files.IOException - if there is a problem with File I/ORuntimeException - if there was a problem unmapping the
buffers or closing the channels, which may leave one or more
files locked for read.public static void cleanDirectByteBuffer(ByteBuffer byteBuffer)
DirectByteBuffers are garbage collected by using a phantom reference and a
reference queue. Every once a while, the JVM checks the reference queue and
cleans the DirectByteBuffers. However, as this doesn't happen immediately
after discarding all references to a DirectByteBuffer, it's easy get
OutOfMemoryError problems using direct ByteBuffers.
Also, if a file is still mapped, via MappedByteBuffer, then it is
locked and cannot be destroyed or possibly written to if it was previously
mapped read. Trying to destroy or write to a locked file will result in an
IOException
This function explicitly calls the cleaner method of a ByteBuffer using
reflection because it is not publicly accessible.
byteBuffer - The DirectByteBuffer that will be "cleaned". Returns immediately if
the argument is null.IllegalArgumentException - if byteBuffer isn't directRuntimeException - if cleaning may have failedCopyright © 2014 Open Source by Eric Kolotyluk. All Rights Reserved.