commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Hane <chrish...@gmail.com>
Subject Re: [CODEC] Base64 vs. sun.misc.BASE64Decode
Date Sat, 19 Aug 2006 15:17:19 GMT
Ok - I agree that this is strange.  However, when I tried just bytes, 
the output file was even worse.  So that tells me I don't know much 
about how to correctly do IO (which I don't).  Can you suggest an 
alternative so that the bytes are outputted correctly.

I have tried a method like the following; but the output file isn't 
correct either.  The straight bytes out produces even more incorrectly 
translated characters when looking at the raw file using wordpad.  I 
have lot more ? characters than using the one that converted to a String 
first.  Could I be reading the input file incorrectly in the 
getBytesFromFile method.

   private static void test3() throws IOException{
      byte[] inBytes = getBytesFromFile(new File("c:\\var\\pdfif.txt"));
     
      Base64 decoder = new Base64();
      byte[] decodedBytes= decoder.decode(inBytes);
     
      BufferedWriter out = new BufferedWriter(new FileWriter(new 
File("c:\\var\\test.pdf")));
     
      for(int x=0; x<decodedBytes.length; x++){
         out.write(decodedBytes[x]);
      }
     
      out.flush();
      out.close();
   }

   // 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
             && (numRead=is.read(bytes, 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 
"+file.getName());
      }
 
      // Close the input stream and return bytes
      is.close();
      return bytes;
  }

Thanks for looking at this.

Chris....


luc.maisonobe@free.fr wrote:
> Selon Chris Hane <chrishane@gmail.com>:
>
>
>   
>>       char[] msgChars = new String(decoder.decode(b)).toCharArray();
>>     
>
> This looks rather strange to me. When you decode something from Base64, you
> expect the output to be (or contain in your case) pure binary data, you cannot
> put it into a character array. When you do this, the JVM uses its default
> character encoding setting and may well mangle your data. You should really
> stick to bytes, not characters.
>
> I'm not sure the problem is really here, though. It just looks strange to me.
>
> Luc
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>
>   

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message