Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Java code for determining binary file equality

Reply
Thread Tools

Re: Java code for determining binary file equality

 
 
javaBeginner
Guest
Posts: n/a
 
      06-27-2003
Thanks for your help. This is exactly what I was looking for.



"Søren Bak" <> wrote in message news:<EYjKa.90$>...
> 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: 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?

 
Reply With Quote
 
 
 
 
snoopyjc snoopyjc is offline
Junior Member
Join Date: Apr 2008
Location: NJ
Posts: 1
 
      04-30-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining if a file is binary or text James Masters Ruby 13 09-21-2009 03:30 PM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
Newbie: working with binary files/extract png from a binary file Jim Ruby 5 02-01-2008 11:21 AM
Determining if file is valid image file =?iso-8859-1?B?QW5kcuk=?= Python 13 08-02-2007 09:20 PM
Determining ContentType for binary attachments Stephen Walch ASP .Net 2 01-06-2005 02:32 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57