Mike Dee
Mike Dee

Reputation: 580

Base64 encode and decode not give same result

I have been banging my head against a wall on this all day. I have a PDF file that we generate. The PDF file looks fine in Acrobat.

I need to encode the file in base64. Using Apache codec library I do this:

String base64buf = Base64.encodeBase64String( m_reportText.getBytes( "UTF-8" ) );

As a test I write base64buf out to a file:

Files.write( new File( "report.b64" ).toPath(), base64buf.getBytes( "UTF-8") );

Then I convert it back, just to see if it is working:

String encodedName = "report.b64";
String decodedName = "report.pdf";

// Read original file.
byte[] encodedBuffer = Files.readAllBytes( new File( encodedName ).toPath() );

// Decode
byte[] decodedBuffer = Base64.decodeBase64( encodedBuffer );

// Write out decodedBuffer.
FileOutputStream outputStream = new FileOutputStream( decodedName );
outputStream.write( decodedBuffer );
outputStream.close();

I open report.pdf in Acrobat and it is a blank document. It has the correct number of pages (all are blank).

What am I missing here?

Upvotes: 0

Views: 3347

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

m_reportText is a String, hence contains Unicode text. However a PDF is in general binary data. That should really be avoided, as the superfluous conversion in both directions is lossy and error prone. For a hack you could try storing and retrieving the PDF bytes as ISO-8859-1.

Use a byte[] m_reportText.

Upvotes: 1

Related Questions