HomeForumSourceResearchGuide
Sign in to reply to forum posts.
Using in-memory files?

Hi,

Is it possible to create in-memory files in Dana? I want to write some temporary image data to a file, then load that image with a PNG image encoder, without using the disk to do it.

Thanks.

Hi,

Yes, you can do this! I think quite a few of the standard library APIs are designed with this in mind :)

You need to use the :mem variant of io.File to do it, here's an example:

component provides App requires io.Output out, io.File:mem {
	int App:main(AppParam params[]) {
		File file = new File:mem("tmp", File.CREATE)
		file.write("some stuff is here")
		file.setPos(0)
		//do something with the file :)
		file.close()
		return 0
		}
}

If you are going to read the data into something else, make sure you do the file.setPos(0) part before you try to do the read.

Hope that helps! :)

Oh that worked great. Thanks so much for the quick reply.