Base 64 decoding a large mime message attachment

Greetings all.

I’m receiving data sent by customer through mime message. The message contains XMLs and attachments(base64 encoded, currently testing with attachment size until 25 MB). I’m able to extract the content using mime services but how to base64 decode the extracted content of size this big? I tried converting into a string and decoded using the built in service but it is not working. Decoded contents need to be sent through FTP, irrespective of the size of the attachment, I see the size of the file as 1 KB in the ftp location post put operation. Whereas if I put the extracted content as is, then I can see the file same as the attachment received.

Please provide your suggestions.

Hi Venkata,

can you try the service provided by the PSUtilities package?

This package is available in the download section of the Tech Community.

Regards,
Holger

I did try that, faced the same issue. Is there any other of decoding which doesn’t involve conversion to string. I’m skeptical about this approach. I think there will be data loss.

Hi Venkata,

in this case you might need to write your own java service to do so.

Which Version of wM are you using?
Is it running in Java 8?

There is a class java.util.Base64, which looks suitable for your scenario.
This class has been introduced with Java 8.

Check which type of Base64-Decoder you need.
See [url]JDK 19 Documentation - Home for JavaDoc details.

Regards,
Holger

You can also check MTOM features available in IS.
check watt.server.SOAP.MTOM* extended parameters available in IS.

For your reference you can check the MTOM sample package available on techcommunity.
http://techcommunity.softwareag.com/ecosystem/communities/public/webmethods/products/esb_and_integration/codesamples/6ef943eb-b1d4-11e4-8d00-cd8d7ef22065/

wM version is 9.9 and Java is 1.8.

I’m not clear about this. Will this work for my requirement here? There is no SOAP involved here and not sure about the decoding part.

To decode this base64 mime content, I use:

  1. pub.io:streamToString
  2. pub.string:base64Decode

I didn’t mention any encoding in these 2 services, left to Default. Is this might be the problem? What is the value to be given for Encoding filed while converting a base64 encoded mime stream?

The steps you mentioned will work for small files but if you are dealing with huge amounts of data then do not convert the stream to string in a Flow service. Map the stream directly to below service and that should give you the resulting decoded stream. Map the result from this service to the SFTP Put service in the WmPublic service for transmission.

I have not tested this code with large data but if you have issues, just tweak the byte allocation size.


IDataCursor pipelineCursor = pipeline.getCursor();
			Object	inStream = IDataUtil.get( pipelineCursor, "inStream" );
		pipelineCursor.destroy();
		
		try {
			InputStream is = (InputStream) inStream;
			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			int bytes;
			byte[] data = new byte[1024];
			while ((bytes= is.read(data, 0, data.length)) != -1) {
				buffer.write(data, 0, bytes);
			}
			is.close();
		
			InputStream resultStream = new ByteArrayInputStream(Base64.getDecoder().decode(buffer.toByteArray()));
			
			IDataUtil.put( pipelineCursor, "resultStream", resultStream );
			} catch (IOException e) {
				IDataUtil.put( pipelineCursor, "errorMessage", e.toString() );
			}
		pipelineCursor.destroy();

I converted base64 encoded .7z attachment to byte array and transferred through FTP and checked. I’m able to read the file, unzip and see the contents. Thank you for your suggestions.