How to create an IOBuffer backed by a String

Probably unnecessary backstory: I need to read a bunch of files from a zip archive and then pass them to a Threads.@threads for loop. Unfortunately this doesn’t work. My guess is that the datatype created by ZipFile is not thread safe. So I would like to read each file into memory sequentially, create an IOBuffer for each of them, and then iterate over these using the @threads macro.

I can read each file to a String, but that’s where I get stuck. IOBuffer doesn’t seem to have any functions that allow you to create a buffer backed by a string.

Is there any way to create an IOBuffer backed by a String? Along the lines of

data = "my data"
io = IOBuffer(data)  # no such constructor

This code works and is the way you should do it:

julia> data = "my data"
"my data"

julia> io = IOBuffer(data)
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=7, maxsize=Inf, ptr=1, mark=-1)
1 Like

Well I’ll be, that works and I cannot reproduce the issue I was having. No idea what was going on there.


Okay I had a typo in my for loop guard

for (name, data) in datasets

where datasets was a Vector and not a Dict. IOBuffer then couldn’t handle the variable data because it was a Char and not a String.