4 * Copyright (C) 2016,2022 Hiroshi Miura
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package tokyo.northside.io;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.nio.file.Path;
26 import org.apache.commons.io.IOUtils;
27 import org.jetbrains.annotations.NotNull;
31 * General File manipulation utility.
33 * This class provides static utility methods for input/output operations.
35 * <li>contentEquals - these methods compare the content of two files
38 * The methods in this class that read a file are buffered internally.
39 * The default buffer size of 4K has been shown to be efficient in tests.
41 * Created by Hiroshi Miura on 16/04/09.
43 * @author Hiroshi Miura
45 public final class FileUtils2 {
48 * Compare file contents in range. Both files must be files (not directories) and exist.
50 * @param first first file
51 * @param second second file
52 * @return boolean true if files are equal, otherwise false
53 * @throws IOException error in function
55 public static boolean contentEquals(@NotNull final Path first, @NotNull final Path second) throws IOException {
56 if (first.toFile().length() != second.toFile().length()) {
59 return contentEquals(first.toFile(), second.toFile(), 0, first.toFile().length());
63 * Compare file contents in range. Both files must be files (not directories) and exist.
65 * @param first first file
66 * @param second second file
67 * @return boolean true if files are equal, otherwise false
68 * @throws IOException error in function
70 public static boolean contentEquals(@NotNull final File first, @NotNull final File second) throws IOException {
71 if (!first.exists() || !second.exists()) {
74 if (!first.isFile() || !second.isFile()) {
77 if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
80 if (first.length() != second.length()) {
83 if (first.length() == 0) {
86 return contentEquals(first, second, 0, first.length());
90 * Compare file contents in range. Both files must be files (not directories) and exist.
92 * @param first first file
93 * @param second second file
94 * @param off compare from offset
95 * @param len comparison length
96 * @return boolean true if files are equal, otherwise false
97 * @throws IOException error in function
99 public static boolean contentEquals(@NotNull final File first, @NotNull final File second, final long off,
100 final long len) throws IOException {
102 throw new IllegalArgumentException();
105 throw new IllegalArgumentException();
107 if (!first.exists() || !second.exists()) {
110 if (!first.isFile() || !second.isFile()) {
113 if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
117 FileInputStream firstInput = null;
118 FileInputStream secondInput = null;
121 firstInput = new FileInputStream(first);
122 secondInput = new FileInputStream(second);
123 result = IOUtils2.contentEquals(firstInput, secondInput, off, len);
125 IOUtils.closeQuietly(firstInput);
126 IOUtils.closeQuietly(secondInput);