Skip to Content

Converting Input Text file in Base64Encoded String

Hi,

I have an inbound text file (can contain any data in any format in a .txt file). This file needs to be Base64Encoded and put in the field on target XML:

<Target>
     <Field>BASE64_Encoded_String</Field>
</Target>

I am using a JAVA Mapping with below related code:

import sun.misc.BASE64Encoder;
public void execute(InputStream inputstream,OutputStream outputstream) throws StreamTransformationException, SAXException, IOException {
     try
     {
     .......some logic for other processing.....
     ................
     BASE64Encoder encoder = new BASE64Encoder();
     String encoded = ?? // What should be the logic written here to convert the inputstream into Base64
}
catch(Exception e) {
            throw new StreamTransformationException(e.toString());  
        }
}

Can you please help me with what should be the statement that will encode the input file and put it in the variable?

Referred threads:

http://scn.sap.com/people/farooq.farooqui3/blog/2008/09/24/sap-xipi-encode-outgoing-payload-using-adapter-module

How to zip and encode a field as base64 with a UDF in a Message Mapping

Thank you!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Aug 28, 2015 at 08:31 AM

    First of all, which PI version do you use? If you use PI 7.1 or later, then you will notice that sun.misc.BASE64Encoder is deprecated. You can use javax.xml.bind.DatatypeConverter instead

    The logic could be like this:

         byte input[] = new byte[inputstream.available()];

         in.read(input);

         String encoded = DatatypeConverter.printBase64Binary(input);

       

    with sun encoder it would be:   

         byte input[] = new byte[inputstream.available()];

         in.read(input);

         BASE64Encoder encoder = new BASE64Encoder();

         String encoded = encoder.encode(input);

    The ouput can be done like this:    

        String output = "<Target><field>" + encoded + ">/Target></field>";    

        outputstream.write(output.getbytes());  

    Add comment
    10|10000 characters needed characters exceeded

  • Aug 28, 2015 at 06:20 AM

    HI Abhishek,

    you can achieve this using UDF also..below is the code

    Add comment
    10|10000 characters needed characters exceeded