Skip to Content
0

HCI - Adding Byte Order Mark (BOM) to UTF-8 file content

Nov 07, 2016 at 12:14 PM

450

avatar image

Dear Experts,

I'm trying to add BOM (Byte Order Mark) characters to UTF-8 file content thru Groovy script while sending file to 3rd party SFTP server.

  • Added CamelCharsetName with UTF-8 as Header parameter

  • In Groovy script, tried appending BOM with payload content
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;

def Message processData(Message message) {
            
        Map headerMap = message.getHeaders();
        def payload = message.getBody(java.lang.String);
        String bom = new String("\u00EF\u00BB\u00BF"); //"0xEF,0xBB,0xBF";
                
        //Add ByteOrderMark to payload
        payload = bom+payload;
        message.setBody(payload);
    
return message;
}

The file created at the target does not have correct BOM characters and failing in target. When examined the target file with Hex editor we can clearly see that the BOM is not correct (the same code in Groovy console is creating correct BOM so editors like Notepad & Notepad++ recognize correctly as UTF-8 with BOM).

Correct BOM & Text Content (expected) as viewed in -- HEX -- Mode

Correct BOM & Text Content (expected) as viewed in -- Normal -- Mode

Incorrect BOM (produced by HCI thru Groovy) in -- HEX -- Mode

Anybody experienced similar issue and found any work around to fix the issue?

Thanks in advance.

Ananth Chinnaraj

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Ananth Chinnaraj Nov 09, 2016 at 09:43 AM
0

Hi All,

After a lot of trial and error, the following code worked for me.

def Message processData(Message message) {
            
        Map headerMap = message.getHeaders();
        def payload = message.getBody(java.lang.String) as String;
       
        String charset = "utf-8";
        byte[] BOM = [0xEF,0xBB,0xBF];
        
        String byteordermark = new String (BOM, charset); 
        String s1 = new String(payload.decodeBase64(),charset); // payload is encoded in Base64 format
        String s2 = new String(BOM,charset) + s1;
        
        message.setBody(s2);

}

Thanks,

Ananth

Share
10 |10000 characters needed characters left characters exceeded