Using IOBuffer

Regarding IOBuffer, the current documentation (https://docs.julialang.org/en/stable/stdlib/io-network/) says:

Create an IOBuffer, which may optionally operate on a pre-existing array. If the readable/writable arguments are given, they restrict whether or not the buffer may be read from or written to respectively. By default the buffer is readable but not writable. The last argument optionally specifies a size beyond which the buffer may not be grown.

OK, clear enough. But then:

julia> b=IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)
julia> write(b,"abc")
3
julia> print(String(b))
abc

Which I guess implies that it is writable after-all? Furthermore:

julia> write(b,"def")
3
julia> print(String(b))
abcdef

Which seems to be appending, which seems to contradict the default options…is there something I am missing conceptually here?

This should be applied only to when you provide an array I believe.

No it’s not. Appending means keep previous content (i.e. the one you passed in).

That makes sense.

Still missing something…the default option says append=false , hence I would have thought the output would be “def”?

write will never overwrite a properly implemented io’s previously writen content. That’s what seek is for. The append only controls whether the initial content is overwritten and it does not mean that the write position is unwinded everytime.

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

julia> write(io, "bb")
2

julia> String(take!(io))
"bbaa"

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

julia> io.append = true
true

julia> write(io, "bb")
2

julia> String(take!(io))
"aaaabb"

That makes sense, thanks.