OSDN Git Service

Merge pull request #38 from dictzip/topic/miurahr/fix-dictzip-outputstream
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / tokyo / northside / io / FileUtils2.java
1 /*
2  * FileUtils library.
3  *
4  * Copyright (C) 2016,2022 Hiroshi Miura
5  *
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package tokyo.northside.io;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.IOException;
24 import java.nio.file.Path;
25
26 import org.apache.commons.io.IOUtils;
27 import org.jetbrains.annotations.NotNull;
28
29
30 /**
31  * General File manipulation utility.
32  * <p>
33  * This class provides static utility methods for input/output operations.
34  * <ul>
35  * <li>contentEquals - these methods compare the content of two files
36  * </ul>
37  * <p>
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.
40  * <p>
41  * Created by Hiroshi Miura on 16/04/09.
42  *
43  * @author Hiroshi Miura
44  */
45 public final class FileUtils2 {
46
47     /**
48      * Compare file contents in range. Both files must be files (not directories) and exist.
49      *
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
54      */
55     public static boolean contentEquals(@NotNull final Path first, @NotNull final Path second) throws IOException {
56         if (first.toFile().length() != second.toFile().length()) {
57             return false;
58         }
59         return contentEquals(first.toFile(), second.toFile(), 0, first.toFile().length());
60     }
61
62     /**
63      * Compare file contents in range. Both files must be files (not directories) and exist.
64      *
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
69      */
70     public static boolean contentEquals(@NotNull final File first, @NotNull final File second) throws IOException {
71         if (!first.exists() || !second.exists()) {
72             return false;
73         }
74         if (!first.isFile() || !second.isFile()) {
75             return false;
76         }
77         if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
78             return true;
79         }
80         if (first.length() != second.length()) {
81             return false;
82         }
83         if (first.length() == 0) {
84             return true;
85         }
86         return contentEquals(first, second, 0, first.length());
87     }
88
89     /**
90      * Compare file contents in range. Both files must be files (not directories) and exist.
91      *
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
98      */
99     public static boolean contentEquals(@NotNull final File first, @NotNull final File second, final long off,
100         final long len) throws IOException {
101         if (len < 1) {
102             throw new IllegalArgumentException();
103         }
104         if (off < 0) {
105             throw new IllegalArgumentException();
106         }
107         if (!first.exists() || !second.exists()) {
108             return false;
109         }
110         if (!first.isFile() || !second.isFile()) {
111             return false;
112         }
113         if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
114             return true;
115         }
116
117         FileInputStream firstInput = null;
118         FileInputStream secondInput = null;
119         boolean result;
120         try {
121             firstInput = new FileInputStream(first);
122             secondInput = new FileInputStream(second);
123             result = IOUtils2.contentEquals(firstInput, secondInput, off, len);
124         } finally {
125              IOUtils.closeQuietly(firstInput);
126              IOUtils.closeQuietly(secondInput);
127         }
128         return result;
129     }
130
131 }