Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How can I efficiently compare two binary files for equality?

0
Posted

How can I efficiently compare two binary files for equality?

0

Location: http://www.jguru.com/faq/view.jsp?EID=66830 Created: Jun 7, 2000 Modified: 2000-06-07 18:11:07.065 Author: Brian O’Byrne (http://www.jguru.com/guru/viewbio.jsp?EID=38567) Question originally posed by Prabir Das (http://www.jguru.com/guru/viewbio.jsp?EID=60476 An option which I have used, and found to be reasonably performant, is to read the files in blocks into byte arrays and use equals() to compare the blocks. Something like this: private final static int BLOCK_SIZE = 65536; // vary BLOCK_SIZE to suit yourself. // it should probably a factor or multiple of the size of a disk sector/cluster. // Note that your max heap size may need to be adjused // if you have a very big block size or lots of these comparators. // assume inputStreamA and inputStreamB are streams from your two files. byte[] streamABlock = new byte[BLOCK_SIZE]; byte[] streamBBlock = new byte[BLOCK_SIZE]; boolean match; do { int bytesReadA = inputStreamA.read(streamABlock); int bytesReadB = inputStreamB.read(st

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.