# Using IOBuffer

**URL:** <https://discourse.julialang.org/t/using-iobuffer/5216>\
**Category:** General Usage\
**Tags:** question, first-steps\
**Created:** [August 4, 2017, 2:24am UTC](https://discourse.julialang.org/t/using-iobuffer/5216 "2017-08-04T02:24:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)\
**Post date:** [August 4, 2017, 2:24am UTC](https://discourse.julialang.org/t/using-iobuffer/5216/1 "2017-08-04T02:24:10Z")

</div>

Regarding IOBuffer, the current documentation ([https://docs.julialang.org/en/stable/stdlib/io-network/](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
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
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?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 4, 2017, 3:18am UTC](https://discourse.julialang.org/t/using-iobuffer/5216/2 "2017-08-04T03:18:19Z")

</div>

> [@clinton](#):
>
> By default the buffer is readable but not writable

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

> [@clinton](#):
>
> Which seems to be appending,

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

---

<div class="post-metadata">

**Author:** ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)\
**Post date:** [August 4, 2017, 3:48am UTC](https://discourse.julialang.org/t/using-iobuffer/5216/3 "2017-08-04T03:48:19Z")

</div>

> [@yuyichao](#):
>
> This should be applied only to when you provide an array I believe.

That makes sense.

> [@yuyichao](#):
>
> No it’s not. Appending means keep previous content (i.e. the one you passed in).

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

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 4, 2017, 4:17am UTC](https://discourse.julialang.org/t/using-iobuffer/5216/4 "2017-08-04T04:17:54Z")

</div>

> [@clinton](#):
>
> 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
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"

```

---

<div class="post-metadata">

**Author:** ![clinton](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@clinton](https://discourse.julialang.org/u/clinton)\
**Post date:** [August 4, 2017, 4:22am UTC](https://discourse.julialang.org/t/using-iobuffer/5216/5 "2017-08-04T04:22:38Z")

</div>

That makes sense, thanks.
