MemoryBlock from BinaryStream (or other MB)

This LR without a single example is somewhat disconcerting.

Case in point :

[quote]Constructor(other As MemoryBlock)
Creates a new MemoryBlock with the data from an existing MemoryBlock.[/quote]

So why is this saying syntax error ?

dim m as memoryblock dim mb as memoryblock(m)

Ultimately, I want to put a binarystream read into a memoryblock.

This seems to work, so far :

dim f as folderitem = SpecialFolder.GetResource("myfile.dat") Dim BS as BinaryStream = BinaryStream.Create(f)

Now I know I can go BS.Read(60) to read 60 bytes and it comes back as a memoryblock. So I am probably very dumb, but I was believing that I could construct a new memoryblock with that read.

We have been spoiled with the old LR. This new thing with no pictures is no fun :frowning:

[quote=159038:@Michel Bujardet]So why is this saying syntax error ?

dim m as memoryblock dim mb as memoryblock(m)[/quote]
It should be:

dim m as memoryblock dim mb as NEW memoryblock(m)

I am assuming that you are using the new framework since it’s memoryblock has a constructor from memoryblock. The syntax error is because you left out a "new’. See below:

dim m as xojo.core.memoryblock dim mb as new xojo.core.memoryblock(m)

[quote=159046:@Jason King]It should be:

dim m as memoryblock
dim mb as NEW memoryblock(m)[/quote]

[quote=159047:@William James]error is because you left out a "new’. See below:

dim m as xojo.core.memoryblock
dim mb as new xojo.core.memoryblock(m)[/quote]

Thank you both. But it crashes :frowning: And the break in app.unhandledexception never takes place :frowning:

This works :

dim m as new memoryblock(1) dim mb as new xojo.core.memoryblock(m)

Now what I wanted to do was to assign the read from a binarystream to the MB memoryblock.

[code] dim f as folderitem = SpecialFolder.GetResource(“myfile.dat”)
dim longueur as integer = f.Length
system.debuglog longueur.ToText

Dim BS as BinaryStream = BinaryStream.Create(f)

dim mb as new xojo.core.memoryblock(BS.Read(f.length))
[/code]

The file length is good, there is no runtime exception in the getresource, but yet, the read triggers a Xojo.io.Exception “Error reading”. I checked the permissions, set chmod to 655, no luck.

Would a mutable memory block work?

dim mb as new mutablememoryblock(bs.read(f.length))

[quote=159099:@David Fleming]Would a mutable memory block work?

dim mb as new mutablememoryblock(bs.read(f.length)) [/quote]

No, it does not work either. Since the error is a Xojo.io.exception, it looks like a read permission. But I fail to understand why getresource does find the file fine, and then reading it does not work. Since the file is in the bundle, it is supposed to be read only, so it should work.

I am going to try and copy it to documents and try to read it from there.

Does not work. The folderitem does find and see the file fine. I tried with other types of files to make sure, and it finds the file length fine as well.

What does not work is reading the file. It is apparently not an issue with the binarystream itself, since folderitem.copyfileto() triggers the exception as well.

Now what is strange is that if I try with an image file it does not work either, but if I use iOSImage.FromFile(f) it works fine. So it is not a permission problem. Obviously, there is something wrong in copyFileTo as well as BinaryStream.Read()

I will appreciate some insight from Xojo, at this point.

Looks like you do a binarystream.create which wipes out your file
You sure you dont want OPEN ?

This code (now in the Ref Guide) seems to work for me:

[code]// Load an image that is copied to an iOS app using a Copy Files Build Step
Dim f As FolderItem = SpecialFolder.GetResource(“MyImage.JPG”)

Dim b As BinaryStream
b = BinaryStream.Open(f, BinaryStream.LockModes.Read)

Dim mb As New MemoryBlock(b.Read(b.Length))

b.Close

Dim image As iOSImage
image = Image.FromData(mb)

ImageView1.Image = image[/code]

[quote=159165:@Norman Palardy]Looks like you do a binarystream.create which wipes out your file
You sure you dont want OPEN ?[/quote]

[quote=159170:@Paul Lefebvre]This code (now in the Ref Guide) seems to work for me:

[code]// Load an image that is copied to an iOS app using a Copy Files Build Step
Dim f As FolderItem = SpecialFolder.GetResource(“MyImage.JPG”)

Dim b As BinaryStream
b = BinaryStream.Open(f, BinaryStream.LockModes.Read)

Dim mb As New MemoryBlock(b.Read(b.Length))

b.Close

Dim image As iOSImage
image = Image.FromData(mb)

ImageView1.Image = image[/code][/quote]

Thank you. That was it. I did not realize creating a binary stream would wipe the file. Strange how sometimes the brain tends to run circles.

Actually, maybe I am going to it the wrong way. When dragging a file into the project, does it not become a MemoryBlock ? It would be a lot simpler, then.

[quote=161573:@Roy de Martines]Sorry to hijack this thread but how would you do this on desktop?

[code]Dim b As BinaryStream
b = BinaryStream.Open(f, BinaryStream.LockModes.Read)

Dim mb As New MemoryBlock(b.Read(b.Length))

b.Close[/code]

Since a memory block can’t be generated with a String?[/quote]

Please post your question in a new thread in the OSX or Windows channel. You will get an answer right away.

Try

[code]dim m as new memoryblock(bs.length)

m.stringvalue(0,m.size) = bs.read(m.size)
bs.close[/code]

why not simply:

dim m as memoryblock = bs.read(bs.length)

[quote=162170:@Christian Schmitz]why not simply:

dim m as memoryblock = bs.read(bs.length)

Or that. :slight_smile: