# Getting IOBuffer data after close method is called

**URL:** <https://discourse.julialang.org/t/getting-iobuffer-data-after-close-method-is-called/11873>\
**Category:** General Usage\
**Created:** [June 21, 2018, 9:46pm UTC](https://discourse.julialang.org/t/getting-iobuffer-data-after-close-method-is-called/11873 "2018-06-21T21:46:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ward9250](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ward9250/32/42768_2.png) [@Ward9250](https://discourse.julialang.org/u/Ward9250)\
**Post date:** [June 21, 2018, 9:46pm UTC](https://discourse.julialang.org/t/getting-iobuffer-data-after-close-method-is-called/11873/1 "2018-06-21T21:46:44Z")

</div>

Hi All.

I’ve been working with some code that writes data to an IOBuffer, calls the `close`, method on said buffer.

```julia
i = IOBuffer()
write(i, "Wooo look at meeeee!")
close(i)

```

Obv the above is a really dumb toy example, but this is basically the essence of my question:

I want the data of a closed IOBuffer. It must be a closed buffer. And I want to do it in the most efficient way possible.

I know the `take!` method is supposed to give you the data array, without copying, and so was a good candidate:

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

julia> write(i, "Wooo look at meeeee!")
20

julia> close(i)

julia> take!(i)
ERROR: ArgumentError: read failed, IOBuffer is not readable

```

This is because the close method makes the IOBuffer not readable, writable, or appendable.

I can see two options:

A). Open a new buffer with the data:

```julia
i2 = IOBuffer(i.data)
take!(i2)

```

B). Just take the reference to the array already!

```julia
i.data

```

However I thought I would ask here what the preferred approach to doing this would be, since `i.data` is directly accessing an internal field and not calling some method which would be more robust to internal struct changes in the future, and actually so is doing IOBuffer(i.data) followed by a `take!`.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [June 25, 2018, 3:21pm UTC](https://discourse.julialang.org/t/getting-iobuffer-data-after-close-method-is-called/11873/2 "2018-06-25T15:21:55Z")

</div>

`close` might be permitted to destroy the buffer, so it can’t give it back to you afterwards.

Perhaps you’re looking for `BufferStream` instead?
