-
Notifications
You must be signed in to change notification settings - Fork 671
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of PathUtils.fileContentEquals(Path, Path)
- Add org.apache.commons.io.channels.FileChannels. - Add RandomAccessFiles#contentEquals(RandomAccessFile, RandomAccessFile). - Add RandomAccessFiles#reset(RandomAccessFile). - Add PathUtilsContentEqualsBenchmark.
- Loading branch information
1 parent
67bc02c
commit dd93554
Showing
9 changed files
with
400 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/org/apache/commons/io/channels/FileChannels.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.commons.io.channels; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.FileChannel; | ||
import java.util.Objects; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
/** | ||
* Works with {@link FileChannel}. | ||
* | ||
* @since 2.15.0 | ||
*/ | ||
public final class FileChannels { | ||
|
||
/** | ||
* Don't instantiate. | ||
*/ | ||
private FileChannels() { | ||
// no-op | ||
} | ||
|
||
/** | ||
* Tests if two RandomAccessFiles contents are equal. | ||
* | ||
* @param channel1 A FileChannel. | ||
* @param channel2 Another FileChannel. | ||
* @param byteBufferSize The two internal buffer capacities, in bytes. | ||
* @return true if the contents of both RandomAccessFiles are equal, false otherwise. | ||
* @throws IOException if an I/O error occurs. | ||
*/ | ||
public static boolean contentEquals(final FileChannel channel1, final FileChannel channel2, final int byteBufferSize) throws IOException { | ||
// Short-circuit test | ||
if (Objects.equals(channel1, channel2)) { | ||
return true; | ||
} | ||
// Short-circuit test | ||
final long size1 = size(channel1); | ||
final long size2 = size(channel2); | ||
if (size1 != size2) { | ||
return false; | ||
} | ||
if (size1 == 0 && size2 == 0) { | ||
return true; | ||
} | ||
// Dig in and do the work | ||
final ByteBuffer byteBuffer1 = ByteBuffer.allocateDirect(byteBufferSize); | ||
final ByteBuffer byteBuffer2 = ByteBuffer.allocateDirect(byteBufferSize); | ||
while (true) { | ||
final int read1 = channel1.read(byteBuffer1); | ||
final int read2 = channel2.read(byteBuffer2); | ||
if (read1 == IOUtils.EOF && read2 == IOUtils.EOF) { | ||
return byteBuffer1.equals(byteBuffer2); | ||
} | ||
if (read1 != read2) { | ||
return false; | ||
} | ||
if (!byteBuffer1.equals(byteBuffer2)) { | ||
return false; | ||
} | ||
byteBuffer1.clear(); | ||
byteBuffer2.clear(); | ||
} | ||
} | ||
|
||
private static long size(final FileChannel channel) throws IOException { | ||
return channel != null ? channel.size() : 0; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/apache/commons/io/channels/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Provides classes to work with {@link java.nio.channels}. | ||
* | ||
* @since 2.15.0 | ||
*/ | ||
package org.apache.commons.io.channels; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.