Re: Java code for determining binary file equality

Discussion in 'Java' started by javaBeginner, Jun 27, 2003.

  1. javaBeginner

    javaBeginner Guest

    Thanks for your help. This is exactly what I was looking for.



    "Søren Bak" <> wrote in message news:<EYjKa.90$2net.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" <> skrev i en meddelelse
    > news:...
    > > 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?
     
    javaBeginner, Jun 27, 2003
    #1
    1. Advertising

  2. javaBeginner

    snoopyjc

    Joined:
    Apr 30, 2008
    Messages:
    1
    Location:
    NJ
    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
     
    snoopyjc, Apr 30, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Walch

    Determining ContentType for binary attachments

    Stephen Walch, Jan 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,396
    Steven Cheng[MSFT]
    Jan 6, 2005
  2. =?iso-8859-1?B?QW5kcuk=?=

    Determining if file is valid image file

    =?iso-8859-1?B?QW5kcuk=?=, Aug 2, 2007, in forum: Python
    Replies:
    13
    Views:
    528
    Jarek Zgoda
    Aug 2, 2007
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    962
    James Kanze
    Apr 28, 2008
  4. Jim
    Replies:
    6
    Views:
    756
  5. James Masters

    Determining if a file is binary or text

    James Masters, Sep 19, 2009, in forum: Ruby
    Replies:
    13
    Views:
    246
    James Masters
    Sep 21, 2009
Loading...

Share This Page