commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Hane <>
Subject [CODEC] Base64 vs. sun.misc.BASE64Decode
Date Sat, 19 Aug 2006 06:07:30 GMT

I am using Base64 in my application in a couple of places successfully. 

I am now trying to write a routine to decode a Base64 pdf file that I 
will be receiving from a vendor (I have no control over the pdf 
generation).  When I try and decode the file, it does not decode 
correctly so that I can open it in Acrobat.  I get an error from acrobat.

I'm not sure how to describe my problem.  It seems that a pdf file is 
made up of both text and binary data.  It appears that the text portion 
is decoding correctly.  The binary data is not.  When I compare my 
version with a known good version, I can see that some of the binary 
characters are different (for example, my file has a ? where the good 
file has a block character - editing with wordpad).

I have run the sample file through two other program and they both can 
decode the file correctly.  The first uses sun.misc.BASE64Decode and is 
this simple program:

import sun.misc.*;

public class Base64Decode {
  public static void main(String[] args) {
    if (args.length >= 2) {
        try {
           InputStream is = new FileInputStream(args[0]);
           OutputStream os = new FileOutputStream(args[1]);
           BASE64Decoder b64dc = new BASE64Decoder();
           b64dc.decodeBuffer(is, os);
        } catch (IOException e) {
    } else {
        System.out.println("Need 2 args");
  } // end if
}  // end main

The second is a utility from this website:

I'm sure I'm not doing something correctly; and any help would be 
appreciate for how I can use the commons.codec.Base64. 

Here is the code I am trying to write.  I have tried a couple of 
different version for writing out the file; but nothing seems to work.

public static void main(String[] args) throws Exception {
      byte[] b = getBytesFromFile(new File("pdf_input.txt"));
      Base64 decoder = new Base64();
      char[] msgChars = new String(decoder.decode(b)).toCharArray();

      BufferedWriter out = new BufferedWriter(new FileWriter(new 
      for(int x=0; x<msgChars.length; x++){
   // Returns the contents of the file in a byte array.
   public static byte[] getBytesFromFile(File file) throws IOException {
      InputStream is = new FileInputStream(file);
      // Get the size of the file
      long length = file.length();
      // You cannot create an array using a long type.
      // It needs to be an int type.
      // Before converting to an int type, check
      // to ensure that file is not larger than Integer.MAX_VALUE.
      if (length > Integer.MAX_VALUE) {
          // File is too large
      // Create the byte array to hold the data
      byte[] bytes = new byte[(int)length];
      // Read in the bytes
      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length
             && (, offset, bytes.length-offset)) >= 
0) {
          offset += numRead;
      // Ensure all the bytes have been read in
      if (offset < bytes.length) {
          throw new IOException("Could not completely read file 
      // Close the input stream and return bytes
      return bytes;

Thanks for any suggestions

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message