4 * Copyright (C) 2016 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;
25 import org.apache.commons.io.FileUtils;
29 * General File manipulation utility.
31 * This class provides static utility methods for input/output operations.
33 * <li>contentEquals - these methods compare the content of two files
36 * The methods in this class that read a file are buffered internally.
37 * The default buffer size of 4K has been shown to be efficient in tests.
39 * Created by Hiroshi Miura on 16/04/09.
41 * @author Hiroshi Miura
43 public final class FileUtils2 extends FileUtils {
46 * Compare file contents in range. Both files must be files (not directories) and exist.
48 * @param first first file
49 * @param second second file
50 * @param off compare from offset
51 * @param len comparison length
52 * @return boolean true if files are equal, otherwise false
53 * @throws IOException error in function
55 public static boolean contentEquals(final File first, final File second, final long off,
56 final long len) throws IOException {
57 boolean result = false;
60 throw new IllegalArgumentException();
63 throw new IllegalArgumentException();
66 if ((first.exists()) && (second.exists())
67 && (first.isFile()) && (second.isFile())) {
68 if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
71 FileInputStream firstInput = null;
72 FileInputStream secondInput = null;
75 firstInput = new FileInputStream(first);
76 secondInput = new FileInputStream(second);
77 result = IOUtils2.contentEquals(firstInput, secondInput, off, len);
78 } catch (RuntimeException e) {
80 } catch (IOException ioe) {
83 org.apache.commons.io.IOUtils.closeQuietly(firstInput);
84 org.apache.commons.io.IOUtils.closeQuietly(secondInput);