Showing posts with label base64. Show all posts
Showing posts with label base64. Show all posts

Monday, October 12, 2015

Minimalistic Java application to encode/decode in Base64

If your Outlook blocks all kind of attachments, you have here the FINAL resolution to the problem (Outlook doesn't check for Base64 text attachments)


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class Decoder {

    public static void main(String[] args) throws Exception {
        if (args.length < 3) {
            System.out.println("Usage: java EncodeFileWithBASE64 d|e <inputFile> <outputFile>");
            return;
        }
        String op = args[0];
        String inputFile = args[1];
        String outputFile = args[2];


        if (op.equalsIgnoreCase("e")) {
            BASE64Encoder encoder = new BASE64Encoder();
            encoder.encode(
                    new FileInputStream(inputFile),
                    new FileOutputStream(outputFile)
            );
        } else {
            BASE64Decoder decoder = new BASE64Decoder();
            decoder.decodeBuffer(new FileInputStream(inputFile),
                    new FileOutputStream(outputFile));


        }
    }
}






If you put in in Eclipse you get an error, and this is how to solve it: http://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-results-in-error-compiled-in-eclipse (it's easier to ignore errors than using an Apache Commons library)

Go to Window-->Preferences-->Java-->Compiler-->Error/Warnings.
 Select Deprecated and Restricted API. Change it to warning.
 Change forbidden and Discouraged Reference and change it to warning. 
You can also use this online application



Thursday, October 10, 2013

base64 in WLST

if you try using base64 in WLST:
import base64
encoded = base64.b64encode('data to be encoded')


you get a:

AttributeError: 'module' object has no attribute 'b64encode'


reason being that there is:
/usr/lib64/python2.4/base64.py
and
/opt/oracle/fmw11_1_1_5/oracle_common/util/jython/Lib/base64.py

b64decode does:
import binascii
binascii.a2b_base64(s)
b64encode does:
import binascii
binascii.b2a_base64(s)[:-1]
So basically:
import binascii
s='string to encode'
encoded = binascii.b2a_base64(s)[:-1]
print binascii.a2b_base64(encoded)
string to encode
print encoded
c3RyaW5nIHRvIGVuY29kZQ==



Monday, November 14, 2011

opaq:opaqueElement and base64Binary

If you want to write binary data to a FTP in OSB (or simply you want to avoid any nXSD transformation in your XML), you have the option of using

opaq:opaqueElement

You should in this case base64-encode your payload.
The bad news is that in Xquery 1.0 there is not a function to base64encode

You can do a Java Callout or a Custom XPath to a Java function.

There are plenty of BASE64Encoder classes around... from the BEA WLI framework, from Sun, from Apache....

In fact, Sun implementation
http://stackoverflow.com/questions/5549464/import-sun-misc-base64encoder-got-error-in-eclipse is not really an option.

We have identified the Apache Commons base64 encoder - in alternative to Sun implementation, since Sun (Oracle) itself recommends NOT to use sun packages http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html - as the most commonly used encoder.


So, use this http://commons.apache.org/codec/ commons-codec-1.4.jar