Base64 string to memoryblock - size?


Hi everybody,

I am reading base64 encoded data like this:

MemoryBlock decoded;
MemoryOutputStream output (decoded, false);
if (Base64::convertFromBase64 (output, text)) {
    for (int i=0; i < decoded.getSize(); ++i) {
        // here i am overreading...

Unfortunately the MemoryBlock wouldn’t tell me it’s size. MemoryBlock::getSize() returns the allocated size.
And also Base64::convertFromBase64() doesn’t set the size either.

I understand the reason from MemoryBlock and its size? not to change the API, but the proposed workaround doesn’t apply here either.

Is there a solution besides calculating from the input size:

int length = floor (text.length() * 3 / 4);



Trying to calculate the size yourself is just silly! If you read the docs, it tells you what to do to force the size to be truncated.

/** Creates a memory stream for writing into into a pre-existing MemoryBlock object.

    Note that the destination block will always be larger than the amount of data
    that has been written to the stream, because the MemoryOutputStream keeps some
    spare capactity at its end. To trim the block's size down to fit the actual
    data, call flush(), or delete the MemoryOutputStream.

Alternatively, the stream obviously knows exactly how many bytes have been written to it, just call MemoryOutputStream::getDataSize()!


It is, and I found it silly that I should have to do that, that’s why I asked :wink:

Thanks for the missing tile, that helped a lot.

Do you mind adding a hint to MemoryBlock::setSize(), because that’s the place one would actually look up, if it doesn’t give the results one would expect (and yes, it says “the allocated size”, so it is correct, but not helpful…)


[quote=“daniel, post:3, topic:20254”]
Do you mind adding a hint to MemoryBlock::setSize(), because that’s the place one would actually look up[/quote]

I think you’re getting muddled between MemoryBlock and MemoryOutputStream, they’re very different classes, and it’d make no sense to put comments about MemoryOutputStream in the the MemoryBlock class docs, because it’s not MemoryBlock’s job to be aware of how other classes use it.

It might have been less confusing if you’d not created your own MemoryBlock, but instead just created a MemoryOutputStream and written to it, allowing it to manage its own storage. Then when you ask it for the data, it’d give you the answer you expect.


Ok, it wasn’t obvious to me, that normally you would use the MemoryOutputStream without a MemoryBlock.
Seems like I am not the first one to misunderstand that.
Thanks for the hint.