How to check Blob objects for equality?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












Rather disappointingly for Blobs this assertion fails:



System.assert(Blob.valueOf('abc') == Blob.valueOf('abc'));


though the Apex Expression Operators documentation over ambitiously says that:




Unlike Java, == in Apex compares object value equality, not reference
equality, except for user-defined types.




So it seems that to test equality where the blobs have the same size requires the blobs to be put through EncodingUtil.base64Encode which may lead to heap limit problems e.g.:



private static Boolean eq(Blob b1, Blob b2) 
if (b1.size() == b2.size())
String s1 = EncodingUtil.base64Encode(b1);
String s2 = EncodingUtil.base64Encode(b2);
return s1 == s2;
else
return false;




I there a better way to go?










share|improve this question























  • I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
    – sfdcfox
    2 hours ago











  • @sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
    – Keith C
    2 hours ago
















up vote
4
down vote

favorite












Rather disappointingly for Blobs this assertion fails:



System.assert(Blob.valueOf('abc') == Blob.valueOf('abc'));


though the Apex Expression Operators documentation over ambitiously says that:




Unlike Java, == in Apex compares object value equality, not reference
equality, except for user-defined types.




So it seems that to test equality where the blobs have the same size requires the blobs to be put through EncodingUtil.base64Encode which may lead to heap limit problems e.g.:



private static Boolean eq(Blob b1, Blob b2) 
if (b1.size() == b2.size())
String s1 = EncodingUtil.base64Encode(b1);
String s2 = EncodingUtil.base64Encode(b2);
return s1 == s2;
else
return false;




I there a better way to go?










share|improve this question























  • I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
    – sfdcfox
    2 hours ago











  • @sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
    – Keith C
    2 hours ago












up vote
4
down vote

favorite









up vote
4
down vote

favorite











Rather disappointingly for Blobs this assertion fails:



System.assert(Blob.valueOf('abc') == Blob.valueOf('abc'));


though the Apex Expression Operators documentation over ambitiously says that:




Unlike Java, == in Apex compares object value equality, not reference
equality, except for user-defined types.




So it seems that to test equality where the blobs have the same size requires the blobs to be put through EncodingUtil.base64Encode which may lead to heap limit problems e.g.:



private static Boolean eq(Blob b1, Blob b2) 
if (b1.size() == b2.size())
String s1 = EncodingUtil.base64Encode(b1);
String s2 = EncodingUtil.base64Encode(b2);
return s1 == s2;
else
return false;




I there a better way to go?










share|improve this question















Rather disappointingly for Blobs this assertion fails:



System.assert(Blob.valueOf('abc') == Blob.valueOf('abc'));


though the Apex Expression Operators documentation over ambitiously says that:




Unlike Java, == in Apex compares object value equality, not reference
equality, except for user-defined types.




So it seems that to test equality where the blobs have the same size requires the blobs to be put through EncodingUtil.base64Encode which may lead to heap limit problems e.g.:



private static Boolean eq(Blob b1, Blob b2) 
if (b1.size() == b2.size())
String s1 = EncodingUtil.base64Encode(b1);
String s2 = EncodingUtil.base64Encode(b2);
return s1 == s2;
else
return false;




I there a better way to go?







apex blob






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago

























asked 2 hours ago









Keith C

91.7k1086191




91.7k1086191











  • I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
    – sfdcfox
    2 hours ago











  • @sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
    – Keith C
    2 hours ago
















  • I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
    – sfdcfox
    2 hours ago











  • @sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
    – Keith C
    2 hours ago















I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
– sfdcfox
2 hours ago





I just tried your code... it didn't fail. Perhaps you could provide a legitimately broken example instead of the apparent oversimplification here?
– sfdcfox
2 hours ago













@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
– Keith C
2 hours ago




@sfdcfox Duh... Not sure what I did wrong the first time. Please add that as an answer and I will accept.
– Keith C
2 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality:



Blob a = blob.valueof('hello world'),
b = blob.valueof('Hello world');
system.assertequals(a, b); // Assertion fails


This is in contrast to normal String comparison, which is case-insensitive.






share|improve this answer




















  • The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
    – Keith C
    2 hours ago











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236490%2fhow-to-check-blob-objects-for-equality%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality:



Blob a = blob.valueof('hello world'),
b = blob.valueof('Hello world');
system.assertequals(a, b); // Assertion fails


This is in contrast to normal String comparison, which is case-insensitive.






share|improve this answer




















  • The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
    – Keith C
    2 hours ago















up vote
3
down vote



accepted










The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality:



Blob a = blob.valueof('hello world'),
b = blob.valueof('Hello world');
system.assertequals(a, b); // Assertion fails


This is in contrast to normal String comparison, which is case-insensitive.






share|improve this answer




















  • The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
    – Keith C
    2 hours ago













up vote
3
down vote



accepted







up vote
3
down vote



accepted






The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality:



Blob a = blob.valueof('hello world'),
b = blob.valueof('Hello world');
system.assertequals(a, b); // Assertion fails


This is in contrast to normal String comparison, which is case-insensitive.






share|improve this answer












The equals operator == does work as expected with Blob, so there's no reason to jump through extra hoops, such as your eq method. However, one possibility is that you may have had a different case in your code; since Blobs are binary data, they are case-sensitive in respect to equality:



Blob a = blob.valueof('hello world'),
b = blob.valueof('Hello world');
system.assertequals(a, b); // Assertion fails


This is in contrast to normal String comparison, which is case-insensitive.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









sfdcfox

233k10179395




233k10179395











  • The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
    – Keith C
    2 hours ago

















  • The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
    – Keith C
    2 hours ago
















The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
– Keith C
2 hours ago





The problem was API version; the class with the comparison was set to version 29.0 and changing it to 43.0 fixed it. Thought I already had made that change but when double checking I had not.
– Keith C
2 hours ago


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236490%2fhow-to-check-blob-objects-for-equality%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery