9

Is there a way in Apex to combine multiple Blob values and get a single Blob value?

Blob b1 = Blob.valueOf('s1');
Blob b2 = Blob.valueOf('s2');
Blob b3 = Blob.valueOf('s3');
Blob combined = b1 + b2 + b3; //Error: expression must use numeric arguments.

Concatenation operator does not work on Blob, and there is no option like java's Byte[] array or ByteBuffer class in Apex.

15

It is possible to do it by converting Blob to Hex using EncodingUtil.convertToHex() method, concat the Hex Sequence and then convert concatenated Hex string back to Blob using EncodingUtil.ConvertFromHex() method.

Blob b1 = Blob.valueOf('s1');
Blob b2 = Blob.valueOf('s2');
Blob b3 = Blob.valueOf('s3');

String combinedDataAsHex = EncodingUtil.convertToHex(b1) + EncodingUtil.convertToHex(b2) + EncodingUtil.convertToHex(b3);
Blob combinedDataAsBlob = EncodingUtil.convertFromHex(combinedDataAsHex);

Blob is a sequence of Bytes and it need to be converted to string representation for concatenation. In Apex there are only two ways to convert Blob to String, either use base64Encode or convertToHex as suggested above. base64Encode will not work in this case as base64Encode appends additional information during encoding and this causes corrupted data if two base64Encoded Blob values are combined and converted back to Blob using EncodingUtil.base64Decode() method.

  • If community members have a better/alternate approach please suggest. – Amit Jain Jul 27 '16 at 19:00
  • What if the value of resulting string is large than 6000000 ? String combinedDataAsHex = EncodingUtil.convertToHex(b1) + EncodingUtil.convertToHex(b2) + EncodingUtil.convertToHex(b3); – redfox88 Sep 1 '16 at 7:35
  • I believe by 6000000 you mean 6 MB heap limit? If so, nothing can be processed if we hit governor limits. We can optimise our code so that we can delay hitting governor limit under controlled situations. – Amit Jain Sep 2 '16 at 16:15
  • @AmitJain I always get one blob result only after doing concatenate operation. I am concatenating 2 pdf blob. – Ankuli Oct 2 '16 at 16:28
  • @ankuli I am not sure what exactly you are asking here? – Amit Jain Oct 3 '16 at 20:10
1

It's worth considering Base 64 instead of Hex as the intermediate representation here. The Hex representation is about 1.5 times larger than the Base 64, so Base 64 is better for avoiding the Apex heap limit.

So, the above example becomes:

Blob b1 = Blob.valueOf('s1');
Blob b2 = Blob.valueOf('s2');
Blob b3 = Blob.valueOf('s3');

String combinedDataAsBase64 = EncodingUtil.base64Encode(b1) + EncodingUtil.base64Encode(b2) + EncodingUtil.base64Encode(b3);
Blob combinedDataAsBlob = EncodingUtil.base64Decode(combinedDataAsBase64);

Correction

This only works if the blobs produce no padding in their Base64 form, which may be true for some applications where we can choose the size of the chunks (e.g. handling file uploads in chunks from the Lightning UI) but it is not always the case.

  • I mentioned in my original reply to this question that base64Encode will not work in this case as base64Encode appends additional information during encoding and this causes corrupted data if two base64Encoded Blob values are combined and converted back to Blob using EncodingUtil.base64Decode() method. Please let me know if you have been able to do it successfully. – Amit Jain Jun 2 '19 at 10:45
  • Actually, you're right. I was doing this with Blobs that had been cut up so that there was never any padding except on the last one, so this wasn't a factor. In general, though, the padding can mess everything up. – Aidan Jun 3 '19 at 7:12
  • Will it work if I try to put this blob in form of a PDF Attachment in Salesforce? Already tried doing so, but its not working out, specifically for PDF. All other file types are working. – Shobhit Saxena Jul 30 '19 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.