0

The application of Base64 encoding that I have read was to convert binary data or some string in to Base64 format. But I came to know of files (example: PDF, Excel) themselves Base64 encoded which can't be even opened/supported by respective software.

My questions are:

  1. Can we encode whole files in to Base64.
  2. What is the application scenario of this.
  3. Can we know by looking at the content which decoder to use.

(FYI: I have read wiki Base64 )

sio2deep
  • 113
  • 2
  • 4
  • 12
  • 1
    1) is easy to answer. Of course we can encode whole file with base64. 2)-ish. This was needed when you have 8bit binaries which you wanted to trabsfer over a 7 bit medium (E.g. when transfering non-text files via FTP in ASCII mode, or when sending binaries per mail. (Mail only supported text, and FTP defaulted to 7 bits to save 1/8th of the bandwidth. This was very useful in the time of 300 bit modems). – Hennes Jul 28 '13 at 17:04
  • A PDF *is* not Base64 encoded (after encoding it will no longer be a valid PDF), it *supports* Base-64 encoding for storage of some of its objects. – Jongware Jul 28 '13 at 17:16

1 Answers1

0

Base64 decoding in Java and i think encoding also can done with some classes in this package

package is

import org.apache.commons.codec.binary.Base64;

and the logic is

 String filepath = "C:\\Users\\sandeep\\somefile";
    String encodedString = null;

    try {
        File file=new File(filepath);
        FileReader fr = new FileReader(file);
        BufferedReader reader = new BufferedReader(fr);

        while ((encodedString = reader.readLine()) != null) {

            byte[] binOfEncoded = Base64.decodeBase64(encodedString.getBytes());  
            System.out.println("Base64 Decoded  String     :       " + binOfEncoded);               
        }

    } catch (IOException x) {
        System.err.format("IOException: %s%n", x);
    }
sio2deep
  • 113
  • 2
  • 4
  • 12