diff --git a/base/iobuffer.jl b/base/iobuffer.jl index a41bb4fe745900..51910a42f7ebdd 100644 --- a/base/iobuffer.jl +++ b/base/iobuffer.jl @@ -60,13 +60,17 @@ julia> String(take!(io)) "JuliaLang is a GitHub organization" ``` """ -IOBuffer(data::AbstractVector{UInt8}, readable::Bool=true, writable::Bool=false, maxsize::Integer=typemax(Int)) = - GenericIOBuffer(data, readable, writable, true, false, maxsize) -function IOBuffer(readable::Bool, writable::Bool) - b = IOBuffer(StringVector(32), readable, writable) - b.data[:] = 0 - b.size = 0 - return b +function IOBuffer( + data::AbstractVector{UInt8}; + read::Union{Bool,Nothing}=nothing, + write::Union{Bool,Nothing}=nothing, + append::Union{Bool,Nothing}=nothing, + maxsize::Integer=typemax(Int)) + if maxsize < 0 + throw(ArgumentError("negative maxsize: $(maxsize)")) + end + flags = open_flags(read=read, write=write, append=append) + return GenericIOBuffer(data, flags.read, flags.write, true, flags.append, Int(maxsize)) end """ @@ -85,7 +89,16 @@ julia> String(take!(io)) "JuliaLang is a GitHub organization. It has many members." ``` """ -IOBuffer() = IOBuffer(true, true) +function IOBuffer(;read::Bool=true, write::Bool=true) + b = IOBuffer(StringVector(32), read=read, write=write) + b.data[:] = 0 + b.size = 0 + return b +end + +# TODO: deprecate these methods? +IOBuffer(data::AbstractVector{UInt8}, read::Bool, write::Bool, maxsize::Integer=typemax(Int)) = IOBuffer(data, read=read, write=write, maxsize=maxsize) +IOBuffer(read::Bool, write::Bool) = IOBuffer(read=read, write=write) """ IOBuffer(size::Integer) @@ -110,7 +123,11 @@ julia> String(take!(io)) "Hello world " ``` """ -IOBuffer(maxsize::Integer) = (x=IOBuffer(StringVector(maxsize), true, true, maxsize); x.size=0; x) +function IOBuffer(maxsize::Integer) + buf = IOBuffer(StringVector(maxsize), read=true, write=true, maxsize=maxsize) + buf.size = 0 + return buf +end # PipeBuffers behave like Unix Pipes. They are typically readable and writable, they act appendable, and are not seekable.