Java InputStream Compare inputStreamEquals(InputStream is1, InputStream is2)

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

Description

input Stream Equals

License

Apache License

Declaration

public static boolean inputStreamEquals(InputStream is1, InputStream is2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;
import java.util.Arrays;

public class Main {
    private final static int BUFFSIZE = 1024;
    private static byte buff1[] = new byte[BUFFSIZE];
    private static byte buff2[] = new byte[BUFFSIZE];

    public static boolean inputStreamEquals(InputStream is1, InputStream is2) {
        if (is1 == is2)
            return true;
        if (is1 == null && is2 == null)
            return true;
        if (is1 == null || is2 == null)
            return false;
        try {//from  w  w w .j a  va2s  .  co  m
            int read1 = -1;
            int read2 = -1;

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

                int offset2 = 0;
                while (offset2 < BUFFSIZE
                        && (read2 = is2.read(buff2, offset2, BUFFSIZE
                                - offset2)) >= 0) {
                    offset2 += read2;
                }
                if (offset1 != offset2)
                    return false;
                if (offset1 != BUFFSIZE) {
                    Arrays.fill(buff1, offset1, BUFFSIZE, (byte) 0);
                    Arrays.fill(buff2, offset2, BUFFSIZE, (byte) 0);
                }
                if (!Arrays.equals(buff1, buff2))
                    return false;
            } while (read1 >= 0 && read2 >= 0);
            if (read1 < 0 && read2 < 0)
                return true; // both at EOF
            return false;

        } catch (Exception ei) {
            return false;
        }
    }
}

Related

  1. binaryStreamEquals(InputStream left, InputStream right)
  2. binaryStreamEquals(InputStream left, InputStream right)