FileChannels.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.channels;

  18. import java.io.IOException;
  19. import java.nio.ByteBuffer;
  20. import java.nio.channels.FileChannel;
  21. import java.util.Objects;

  22. import org.apache.commons.io.IOUtils;

  23. /**
  24.  * Works with {@link FileChannel}.
  25.  *
  26.  * @since 2.15.0
  27.  */
  28. public final class FileChannels {

  29.     /**
  30.      * Tests if two RandomAccessFiles contents are equal.
  31.      *
  32.      * @param channel1       A FileChannel.
  33.      * @param channel2       Another FileChannel.
  34.      * @param byteBufferSize The two internal buffer capacities, in bytes.
  35.      * @return true if the contents of both RandomAccessFiles are equal, false otherwise.
  36.      * @throws IOException if an I/O error occurs.
  37.      */
  38.     public static boolean contentEquals(final FileChannel channel1, final FileChannel channel2, final int byteBufferSize) throws IOException {
  39.         // Short-circuit test
  40.         if (Objects.equals(channel1, channel2)) {
  41.             return true;
  42.         }
  43.         // Short-circuit test
  44.         final long size1 = size(channel1);
  45.         final long size2 = size(channel2);
  46.         if (size1 != size2) {
  47.             return false;
  48.         }
  49.         if (size1 == 0 && size2 == 0) {
  50.             return true;
  51.         }
  52.         // Dig in and do the work
  53.         final ByteBuffer byteBuffer1 = ByteBuffer.allocateDirect(byteBufferSize);
  54.         final ByteBuffer byteBuffer2 = ByteBuffer.allocateDirect(byteBufferSize);
  55.         while (true) {
  56.             final int read1 = channel1.read(byteBuffer1);
  57.             final int read2 = channel2.read(byteBuffer2);
  58.             byteBuffer1.clear();
  59.             byteBuffer2.clear();
  60.             if (read1 == IOUtils.EOF && read2 == IOUtils.EOF) {
  61.                 return byteBuffer1.equals(byteBuffer2);
  62.             }
  63.             if (read1 != read2) {
  64.                 return false;
  65.             }
  66.             if (!byteBuffer1.equals(byteBuffer2)) {
  67.                 return false;
  68.             }
  69.         }
  70.     }

  71.     private static long size(final FileChannel channel) throws IOException {
  72.         return channel != null ? channel.size() : 0;
  73.     }

  74.     /**
  75.      * Don't instantiate.
  76.      */
  77.     private FileChannels() {
  78.         // no-op
  79.     }
  80. }