Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: Java code for determining binary file equality (http://www.velocityreviews.com/forums/t123770-re-java-code-for-determining-binary-file-equality.html)

javaBeginner 06-27-2003 05:31 PM

Re: Java code for determining binary file equality
 
Thanks for your help. This is exactly what I was looking for.



"Søren Bak" <not@for.email> wrote in message news:<EYjKa.90$ZK1.53@news.get2net.dk>...
> Using a diff-command is overkill if you are only interested in equality.
> There is no reason to compute the difference. You can implement using two
> streams. Something like this:
>
> boolean areFilesEqual(File f1, File f2) throws IOException {
> // compare file sizes
> if (f1.length() != f2.length())
> return false;
>
> // read and compare bytes pair-wise
> InputStream i1 = new FileInputStream(f1);
> InputStream i2 = new FileInputStream(f2);
> int b1, b2;
> do {
> b1 = i1.read();
> b2 = i2.read();
> } while (b1 == b2 && b1 != -1);
> i1.close();
> i2.close();
>
> // true only if end of file is reached
> return b1 == -1;
> }
>
> Rgds,
> Søren Bak
>
> "javaBeginner" <eriddle@thomasassociates.cc> skrev i en meddelelse
> news:b49317ea.0306250801.54e2d7f3@posting.google.c om...
> > Does anyone know where I could find sample java code
> > for comparing two binary files?
> >
> > I only need to know whether the two files are equal or not (nothing more).
> >
> > Is there a Java class similar to the Unix "diff" command?


snoopyjc 04-30-2008 12:33 PM

Faster file compare
 
Here is a faster file compare:

Code:

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

        public static boolean fileContentsEquals(File file1, File file2) {
                InputStream is1 = null;
                InputStream is2 = null;
                if(file1.length() != file2.length()) return false;

                try {
                        is1 = new FileInputStream(file1);
                        is2 = new FileInputStream(file2);

                        return inputStreamEquals(is1, is2);

                } catch (Exception ei) {
                        return false;
                } finally {
                        try {
                                if(is1 != null) is1.close();
                                if(is2 != null) is2.close();
                        } catch (Exception ei2) {}
                }
        }

        public static boolean fileContentsEquals(String fn1, String fn2) {
                return fileContentsEquals(new File(fn1), new File(fn2));
        }



--joe orost


All times are GMT. The time now is 05:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.