slurride
slurride

Reputation: 127

Binary files differ but not to JVM?

I'm having an issue where org.apache.commons.io.FileUtils.copyFile(File, File) is producing slightly different files. When I compare these files with bsdiff or in an editor, I can tell they're different. Certain bytes are being copied as question marks. For example 0200 (octal) is being copied as ? (077 octal).

So, I create a test case to include in a bug report. I make a copy of the executable, and then compare using FileUtils.checksumCRC32(File). Unexpectedly, the files have the same checksum. I then compare them by iterating through a FileInputStream of each file. This also asserts that the files are the same.

The files certainly differ. One runs, the other doesn't. bsdiff produces a diff of the two files. I can tell that certain bytes are being copied wrong by inspecting the files with my eyes.

However, to the JVM these files are the same. Any ideas of why I'm observing this behavior?

System info: Windows 7, 64 bit; JVM 1.6.0_22, 32 bit

Upvotes: 2

Views: 386

Answers (2)

slurride
slurride

Reputation: 127

Eh, sorry everyone. Maven was 'filtering' the executable, which changed the encoding before copying it to maven's 'target' directory. Then FileUtils was correctly copying the messed up executable from 'target' to the destination. I was comparing the version in my source directory to the one in the destination.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533530

This program writes every possible byte and reads them back in again. If the files were being corrupted how would Java turn those bytes back into their original values. i.e. how could it tell that 077 is 0200 and not 077.

byte[] bytes = new byte[256];
for(int i=0;i<256;i++)
    bytes[i] = (byte) i;
FileUtils.writeByteArrayToFile(new File("tmp.dat"), bytes);
byte[] bytes2 = FileUtils.readFileToByteArray(new File("tmp.dat"));
System.out.println("equals "+Arrays.equals(bytes, bytes2));

a dump of the file shows.

od -x tmp.dat 
0000000 0100 0302 0504 0706 0908 0b0a 0d0c 0f0e
0000020 1110 1312 1514 1716 1918 1b1a 1d1c 1f1e
0000040 2120 2322 2524 2726 2928 2b2a 2d2c 2f2e
0000060 3130 3332 3534 3736 3938 3b3a 3d3c 3f3e
0000100 4140 4342 4544 4746 4948 4b4a 4d4c 4f4e
0000120 5150 5352 5554 5756 5958 5b5a 5d5c 5f5e
0000140 6160 6362 6564 6766 6968 6b6a 6d6c 6f6e
0000160 7170 7372 7574 7776 7978 7b7a 7d7c 7f7e
0000200 8180 8382 8584 8786 8988 8b8a 8d8c 8f8e
0000220 9190 9392 9594 9796 9998 9b9a 9d9c 9f9e
0000240 a1a0 a3a2 a5a4 a7a6 a9a8 abaa adac afae
0000260 b1b0 b3b2 b5b4 b7b6 b9b8 bbba bdbc bfbe
0000300 c1c0 c3c2 c5c4 c7c6 c9c8 cbca cdcc cfce
0000320 d1d0 d3d2 d5d4 d7d6 d9d8 dbda dddc dfde
0000340 e1e0 e3e2 e5e4 e7e6 e9e8 ebea edec efee
0000360 f1f0 f3f2 f5f4 f7f6 f9f8 fbfa fdfc fffe

Upvotes: 1

Related Questions