OSDN Git Service

4ceda97b903df4e73850494603358b2df7b58a4f
[dictzip-java/dictzip-java.git] / dictzip-cli / src / test / java / tokyo / northside / io / FileUtils2.java
1 /*
2  * FileUtils library.
3  *
4  * Copyright (C) 2016 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
25 import org.apache.commons.io.FileUtils;
26
27
28 /**
29  * General File manipulation utility.
30  * <p>
31  * This class provides static utility methods for input/output operations.
32  * <ul>
33  * <li>contentEquals - these methods compare the content of two files
34  * </ul>
35  * <p>
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.
38  * <p>
39  * Created by Hiroshi Miura on 16/04/09.
40  *
41  * @author Hiroshi Miura
42  */
43 public final class FileUtils2 extends FileUtils {
44
45     /**
46      * Compare file contents in range. Both files must be files (not directories) and exist.
47      *
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
54      */
55     public static boolean contentEquals(final File first, final File second, final long off,
56         final long len) throws IOException {
57         boolean result = false;
58
59         if (len < 1) {
60             throw new IllegalArgumentException();
61         }
62         if (off < 0) {
63             throw new IllegalArgumentException();
64         }
65
66         if ((first.exists()) && (second.exists())
67                 && (first.isFile()) && (second.isFile())) {
68             if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
69                 result = true;
70             } else {
71                 FileInputStream firstInput = null;
72                 FileInputStream secondInput = null;
73
74                 try {
75                     firstInput = new FileInputStream(first);
76                     secondInput = new FileInputStream(second);
77                     result = IOUtils2.contentEquals(firstInput, secondInput, off, len);
78                 } catch (RuntimeException e) {
79                     throw e;
80                 } catch (IOException ioe) {
81                      throw ioe;
82                 } finally {
83                      org.apache.commons.io.IOUtils.closeQuietly(firstInput);
84                      org.apache.commons.io.IOUtils.closeQuietly(secondInput);
85                 }
86             }
87         }
88         return result;
89     }
90
91 }