Decode a Base64 encoded file in Java 

Joined:
04/09/2007
Posts:
776

August 24, 2010 21:59:22    Last update: August 24, 2010 21:59:22
This class decodes a Base64 encoded file with the Apache commons codec package:
import java.io.*;
import org.apache.commons.codec.binary.Base64InputStream;

public class DecodeBase64WithApache {
    public static void main(String[] args) throws Exception {
	if (args.length < 2) {
	    System.out.println("Usage: java DecodeBase64WithApache <inputFile> <outputFile>");
	    return;
	}

	int BUFFER_SIZE = 4096;
	byte[] buffer = new byte[BUFFER_SIZE];
	InputStream input = new Base64InputStream(new FileInputStream(args[0]));
	OutputStream output = new FileOutputStream(args[1]);
	int n = input.read(buffer, 0, BUFFER_SIZE);
	while (n >= 0) {
	    output.write(buffer, 0, n);
	    n = input.read(buffer, 0, BUFFER_SIZE);
	}
	input.close();
	output.close();
    }
}
Share |
| Comment  | Tags