View Javadoc
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  
18  package org.apache.commons.io.channels;
19  
20  import java.io.IOException;
21  import java.nio.ByteBuffer;
22  import java.nio.channels.FileChannel;
23  import java.util.Objects;
24  
25  import org.apache.commons.io.IOUtils;
26  
27  /**
28   * Works with {@link FileChannel}.
29   *
30   * @since 2.15.0
31   */
32  public final class FileChannels {
33  
34      /**
35       * Tests if two RandomAccessFiles contents are equal.
36       *
37       * @param channel1       A FileChannel.
38       * @param channel2       Another FileChannel.
39       * @param byteBufferSize The two internal buffer capacities, in bytes.
40       * @return true if the contents of both RandomAccessFiles are equal, false otherwise.
41       * @throws IOException if an I/O error occurs.
42       */
43      public static boolean contentEquals(final FileChannel channel1, final FileChannel channel2, final int byteBufferSize) throws IOException {
44          // Short-circuit test
45          if (Objects.equals(channel1, channel2)) {
46              return true;
47          }
48          // Short-circuit test
49          final long size1 = size(channel1);
50          final long size2 = size(channel2);
51          if (size1 != size2) {
52              return false;
53          }
54          if (size1 == 0 && size2 == 0) {
55              return true;
56          }
57          // Dig in and do the work
58          final ByteBuffer byteBuffer1 = ByteBuffer.allocateDirect(byteBufferSize);
59          final ByteBuffer byteBuffer2 = ByteBuffer.allocateDirect(byteBufferSize);
60          while (true) {
61              final int read1 = channel1.read(byteBuffer1);
62              final int read2 = channel2.read(byteBuffer2);
63              byteBuffer1.clear();
64              byteBuffer2.clear();
65              if (read1 == IOUtils.EOF && read2 == IOUtils.EOF) {
66                  return byteBuffer1.equals(byteBuffer2);
67              }
68              if (read1 != read2) {
69                  return false;
70              }
71              if (!byteBuffer1.equals(byteBuffer2)) {
72                  return false;
73              }
74          }
75      }
76  
77      private static long size(final FileChannel channel) throws IOException {
78          return channel != null ? channel.size() : 0;
79      }
80  
81      /**
82       * Don't instantiate.
83       */
84      private FileChannels() {
85          // no-op
86      }
87  }