/* * [Example.java] * * Summary: Example code to test and use Base64 encode and decode. to run: java.exe com.mindprod.base64.Example. * * Copyright: (c) 2007-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.7 2007-03-15 Created with IntelliJ IDEA. */ package com.mindprod.base64; import static java.lang.System.*; /** * Example code to test and use Base64 encode and decode. to run: java.exe com.mindprod.base64.Example. * * @author Roedy Green, Canadian Mind Products * @version 1.7 2007-03-15 Created with IntelliJ IDEA. * @since 2007 */ public final class Example { public static void main( String arg[] ) throws java.io.UnsupportedEncodingException { String originalString = "Hello world"; byte[] sendBytes = originalString.getBytes( "8859_1"/* encoding */ ); Base64 base64 = new Base64(); base64.setLineLength( 72 ); String encoded = base64.encode( sendBytes ); byte[] reconstitutedBytes = base64.decode( encoded ); String reconstitutedString = new String( reconstitutedBytes, "8859_1"/* encoding */ ); /* display all intermediate results in encode decode process */ // in JDK 1.5+ could import static System.out; out.println( "originalString:" + " " + originalString ); out.print( "sendBytes:" ); // in JDK 1.5+ could use for each for ( byte b : sendBytes ) { out.print( " " + Integer.toHexString( b ) ); } out.println(); out.println( "encoded:" + encoded + "\n" ); out.print( "reconstitutedBytes:" ); for ( byte b : reconstitutedBytes ) { out.print( " " + Integer.toHexString( b ) ); } out.println(); out.println( "reconstitutedString:" + " " + reconstitutedString ); } }