Question

I'm trying to save java classes into a database. To do so I read files as follows:

bytes = com.google.common.io.ByteStreamsByteStreams.toByteArray(url.openStream());

Then, those bytes are encoded in base64:

org.apache.hadoop.hbase.util.Base64.encodeBytes(bytes);

When loading them again, the same thing happens vice versa:

org.apache.hadoop.hbase.util.Base64.decode(base64);

and its written to a file:

com.google.common.io.Files.write(binary, file);

However, source and targe files are not identical. Using a binary file viewer shows that they are indeed very similar; just one thing I spotted: The new file begins with

EF BF BD EF BF BD EF BF BD EF BF BD

instead of the expected

CA FE BA BE

After that, they're identical for at least a few hundert bytes.

As a logical consequence, I get the following error message when trying to load the class form the new file:

java.lang.ClassFormatError: Incompatible magic value 4022320623 in class file ch/unibe/scg/doodle/producerTest/GiantTurtle

I have no idea where it breaks, and it's quite hard to debug. My unit tests for file read/write are green. Where could the problem be?

Was it helpful?

Solution

I just found out I had already solved the problem in the code, but not updated the binaries yet: It's an eclipse plugin project, depending on another project for development, but the other project always needs to be built to a jar and copied to the plugin's directory in order to work at runtime.

The problem was that I first had used a String-based FileWriter:

FileWriter fstream = new FileWriter(file, append);
BufferedWriter out = new BufferedWriter(fstream);
out.write(content);
out.close();

Re-packing my project and actually using above code solved the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top