I have a binhex file. This file should be converted to normal readable file using java code.
I found a similar question here,
Binhex decoding using java code
But the answer is not working.
I tried Base64, the file is converted to some other format which is not human readable.
Please help me to resolve this issue.
The code i tried is as below
File f = new File("Sample.pdf");
Base64 base64 = new Base64();
byte[] b = base64.decode(getBytesFromFile(f));
FileOutputStream fos = new FileOutputStream("Dcode.pdf");
fos.write(b);
fos.close();
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0)
{
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
is.close();
return bytes;
}
The file Sample.pdf is BinHex ecoded. I want the file to be decoded.
Difference to Base64
From what I find online, there are different versions of the BinHex format. None of them is exactly the same as Base64. There are however major similarities. Taking for example the BinHex 4.0 specs, we see that the major binary content is encoded using an encoding scheme with base 64, thus encoing 3 octets to 4 characters. It uses a different alphabet, though:
So you’d either have to translate from one set of characters to the other, or do the decoding yourself.
Apart from the bulk of the binary content, there is some additional meta data included in the format. According to the spec, that includes delimiters between data and resource fork content, checksums, and some other bits of information.
Implementation
The following code will decode a BinHex input file and write its data fork to an output file. It should be easy enough to adjust this code to your needs.