# MemoryBlock - maybe an unusual use

**URL:** <https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722>\
**Category:** General JUCE discussion\
**Created:** [February 18, 2016, 9:08pm UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722 "2016-02-18T21:08:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)\
**Post date:** [February 18, 2016, 9:08pm UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722/1 "2016-02-18T21:08:06Z")

</div>

Hi all

I need to create a Memory Block consisting of various type data.  
More specifically, I need to have a memory block with the following structure:  
At the begining, it holds the raw contents of a two or more uint32 vars, indicating the length of the consecutive data.  
The consecutive data are plain Strings with the appropriate size, as indicated by uint32 vars.  
When I try to copy these data using the Memory Block::copy From() function, what I take is a completely unexpected result.

Any idea about it ?

Thanks in advance

George

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;

&nbsp;

&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)\
**Post date:** [February 19, 2016, 7:20am UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722/2 "2016-02-19T07:20:12Z")

</div>

I thing it's the encoding..

I'll try with that and if I have no a desired result I'll be back !

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 19, 2016, 8:29am UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722/3 "2016-02-19T08:29:52Z")

</div>

Not a problem with MemoryBlock, it'll be something else you're doing wrong, though impossible to say exactly what..

---

<div class="post-metadata">

**Author:** ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)\
**Post date:** [February 21, 2016, 7:45pm UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722/4 "2016-02-21T19:45:48Z")

</div>

Finally, I spent a hole day, but I can't solve my problem.

Here is a piece of code to write a Memory block to a file :

> static bool&nbsp;writeKeyFile(String& JSON\_Keys)
> 
> &nbsp; &nbsp; {  
> &nbsp; &nbsp; &nbsp; &nbsp; MemoryBlock Mb(JSON\_Keys.toUTF8(), &nbsp;JSON\_Keys.getNumBytesAsUTF8());  
> &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream Out(File("Some\_File");
> 
> &nbsp; &nbsp; &nbsp; &nbsp; size\_t S1 = Keys.getSize(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// it returns the size of memory block  
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (Out.openedOk())  
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{  
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Out.setPosition(0);  
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;&nbsp;Out.write(&Keys, Keys.getSize());  
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}  
> &nbsp; }  
> &nbsp;

I don't understand what am I doing wrong. The following piece of code has the following unexpected results :  
- the keys.getSize() returns a value less than the actual size of its content (i.e. the actual size of data is 253 chars, the .getSize() returns a value of 203 ( chars).  
- As a result, the data written to Out file, has a length of 203 chars instead of its actual size.&nbsp;  
- Even though the MemoryBlock's data seem to be ok, after turned them to UTF8 format , unfortunatelly, when I read back the data, the following &nbsp;**_jassert (CharPointer\_UTF8::isValidString (buffer, bufferSizeBytes)),&nbsp;_**interrupts the proccess of reading..&nbsp;  
  
Any idea please ?

George

&nbsp;&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 21, 2016, 8:50pm UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722/5 "2016-02-21T20:50:32Z")

</div>

Almost none of your code makes any sense, I'm afraid!

Perhaps:

```
static bool writeKeyFile (const String& keys)
{
    return File ("my_file").replaceWithText (keys);
}
```

?

---

<div class="post-metadata">

**Author:** ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)\
**Post date:** [February 22, 2016, 9:29am UTC](https://forum.juce.com/t/memoryblock-maybe-an-unusual-use/16722/6 "2016-02-22T09:29:00Z")

</div>

&nbsp;

Ah, I need&nbsp;to have a holliday for a few days ..

&nbsp;

29/2/2016 : Finally it was an illegal use of the&nbsp;encoding&nbsp;
