Android InputStream Compare equals(InputStream is1, InputStream is2)

Here you can find the source of equals(InputStream is1, InputStream is2)

Description

equals

License

Apache License

Declaration

private static boolean equals(InputStream is1, InputStream is2)
            throws IOException 

Method Source Code

//package com.java2s;
/*/* w  w w .  j  a  v a2s  . c o  m*/
 * #%L
 * ch-commons-util
 * %%
 * Copyright (C) 2012 Cloudhopper by Twitter
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.util.Arrays;

public class Main {
    private static boolean equals(InputStream is1, InputStream is2)
            throws IOException {
        int BUFFSIZE = 1024;
        byte buf1[] = new byte[BUFFSIZE];
        byte buf2[] = new byte[BUFFSIZE];

        if (is1 == is2) {
            return true;
        }
        if (is1 == null && is2 == null) {
            return true;
        }
        if (is1 == null || is2 == null) {
            return false;
        }

        int read1 = -1;
        int read2 = -1;

        do {
            int offset1 = 0;
            while (offset1 < BUFFSIZE
                    && (read1 = is1.read(buf1, offset1, BUFFSIZE - offset1)) >= 0) {
                offset1 += read1;
            }

            int offset2 = 0;
            while (offset2 < BUFFSIZE
                    && (read2 = is2.read(buf2, offset2, BUFFSIZE - offset2)) >= 0) {
                offset2 += read2;
            }

            if (offset1 != offset2) {
                return false;
            }

            if (offset1 != BUFFSIZE) {
                Arrays.fill(buf1, offset1, BUFFSIZE, (byte) 0);
                Arrays.fill(buf2, offset2, BUFFSIZE, (byte) 0);
            }

            if (!Arrays.equals(buf1, buf2)) {
                return false;
            }

        } while (read1 >= 0 && read2 >= 0);

        if (read1 < 0 && read2 < 0) {
            return true; // both at EOF
        }

        return false;
    }

    /**
     * Tests whether the contents of two files equals each other by performing
     * a byte-by-byte comparison.  Each byte must match each other in both files.
     * @param file1 The file to compare
     * @param file2 The other file to compare
     * @return True if file contents are equal, otherwise false.
     * @throws IOException Thrown if there is an underlying IO error while
     *      attempt to compare the bytes.
     */
    public static boolean equals(File file1, File file2) throws IOException {
        // file lengths must match
        if (file1.length() != file2.length()) {
            return false;
        }

        InputStream is1 = null;
        InputStream is2 = null;

        try {
            is1 = new FileInputStream(file1);
            is2 = new FileInputStream(file2);
            return equals(is1, is2);
        } finally {
            // make sure input streams are closed
            if (is1 != null) {
                try {
                    is1.close();
                } catch (Exception e) {
                }
            }
            if (is2 != null) {
                try {
                    is2.close();
                } catch (Exception e) {
                }
            }
        }
    }
}