cancel
Showing results for 
Search instead for 
Did you mean: 

Converting Input Text file in Base64Encoded String

former_member192105
Participant
0 Kudos

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-ad...

Thank you!

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

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());  

former_member192105
Participant
0 Kudos

Thank you for the response!

I will try this out and update you accordingly on Monday

Have a great weekend!

RaghuVamseedhar
Active Contributor
0 Kudos

Abhishek,

Adding to above discussion.

Please check 3rd example of this blog

former_member192105
Participant
0 Kudos

Thank you Stefan!

Your solution works for me.

Answers (1)

Answers (1)

maheswarareddykonda
Active Contributor
0 Kudos

HI Abhishek,

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

former_member192105
Participant
0 Kudos

Thank you for the reply!

Unfortunately i cannot use an UDF (infact cant use Message Mapping) as I do not know what will be there in the incoming file. So i prefer JAVA mapping as it can consume any stream and generate output structure as required.

I used the above statement earlier in the JAVA mapping and it failed with below error. So i assume that the statement i am using is wrong

iaki_vila
Active Contributor
0 Kudos

Hi Abhisek,

If you check your own help your need to set an OutputStream variable. Later, you can convert it to String if you want: http://stackoverflow.com/questions/216894/get-an-outputstream-into-a-string

Regards.

maheswarareddykonda
Active Contributor
0 Kudos

Abhishek,

do you want encode only perticular field then i would suggest to go for UDF , watever data comes from source, it will encode that data.

if you want encode everything data which you need to send to receiver then,, why dont you go for custome module , there is code vailable in below link..which u also posted in first post

also while do use that code , you suppose to be used jars related to that code.