Blog Archive

RPGLE base64 decoding using Java

What is Base64 encoding ?

Base64 is a group of similar encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The Base64 term originates from a specific MIME content transfer encoding.
Click here for Base64.java source code

RPGLE Source code for base64 decoding

h DftActgrp(*NO) ActGrp(*Caller)                                           
                                                                           
d Exc_Cmd         PR                  extpgm('QCMDEXC')                    
d  command                     200A   const                                
d  length                       15P 5 const                                
                                                                           
 * Prototype for Java String Object                                        
d crtString       PR              o   EXTPROC(*JAVA:                       
d                                             'java.lang.String':          
d                                             *CONSTRUCTOR)                
d RPGBytes                      25A   Const Varying                        
                                                                           
 * Prototype for Java String's getBytes method                             
d cvtToBytes      PR            25A   EXTPROC(*JAVA:                       
d                                             'java.lang.String':          
d                                             'getBytes')                  
d                                     Varying                              
                                                                           
 * DecodeBase64String Object                                               
d DecodeBase64_   s               o   Class(*JAVA:                         
d                                           'com.myPackage.DecodeBas-      
d                                     e64String')                          
                                                                            
 * Prototype for DecodeBase64String Object                                  
d DecodeBase64    PR              o   EXTPROC(*JAVA:                        
d                                           'com.myPackage.DecodeBas-       
d                                     e64String':                           
d                                            *CONSTRUCTOR)                  
                                                                            
 * Prototype for DecodeBase64String decodeString Method                     
d decodeString    PR              o   EXTPROC(*JAVA:                        
d                                           'com.myPackage.DecodeBas-       
d                                     e64String':                           
d                                            'decodeString')                
d                                     Class   (*JAVA:'java.lang.String')    
d String                          o   Class   (*JAVA:'java.lang.String')    
d                                             Const                         
                                                                            
d encodedData     s               o   Class(*JAVA:'java.lang.String')       
d decodedData     s               o   Class(*JAVA:'java.lang.String')       
                                                                            
d $encodedData    s             25a                                         
d $decodedData    s             25a                                         
                                                                            
 *                                                                                                                                                     
*----- Main Routine                                          
 *                                                            
c     *entry        plist                                     
c                   parm                    $encodedData      
c                   parm                    $decodedData      
                                                              
 /free                                                        
                                                              
    Exc_Cmd('CD DIR(''/AS400_Java/Encoding'')':200);            
                                                              
    encodedData    = crtString(%trim($encodedData));          
    DecodeBase64_  = DecodeBase64();                          
    decodedData    = decodeString(DecodeBase64_:encodedData); 
    $decodedData   = CvtToBytes(decodedData);                 
    dsply $decodedData;                                       
                                                              
    *inlr = *on;                                              
    return;                                                   
                                                              
 /end-free             

Java Source code for base64 decoding

package com.myPackage;

public class DecodeBase64String {

    public static void main(String[] args) {

        String encodedString = args[0];
        DecodeBase64String decodeBase64String = new DecodeBase64String();
        String decodedString = decodeBase64String.decodeString(encodedString);
        System.out.println(decodedString);

    }   

    private String decodeString(String encodedString){

        String decodedString = null;

        try {

            byte[] ba = Base64.decode(encodedString);
            decodedString = new String(ba);

        }
        catch(Exception e ) {
            System.out.println(e);
        } 

        return decodedString;
    }
}