private static boolean equals()

in ch-commons-util/src/main/java/com/cloudhopper/commons/util/FileUtil.java [49:98]


	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;
	}