Skip to content

Commit

Permalink
Consistency of read() and write() functions. JuliaLang#14608
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Jan 12, 2016
1 parent 305890a commit 923a1df
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 22 deletions.
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,9 @@ end
#14555
@deprecate_binding Coff_t Int64
@deprecate_binding FileOffset Int64


#https://github.com/JuliaLang/julia/issues/14608
@deprecate readall readstring
@deprecate readbytes read
@deprecate readbytes! read!
4 changes: 1 addition & 3 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1165,10 +1165,8 @@ export
RawFD,
read,
read!,
readall,
readstring,
readavailable,
readbytes!,
readbytes,
readchomp,
readcsv,
readdir,
Expand Down
10 changes: 5 additions & 5 deletions base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export File,
S_IROTH, S_IWOTH, S_IXOTH, S_IRWXO

import Base: uvtype, uvhandle, eventloop, fd, position, stat, close,
write, read, read!, readbytes, isopen, show,
write, read, read!, isopen, show,
check_open, _sizeof_uv_fs, uv_error, UVError

include("path.jl")
Expand Down Expand Up @@ -164,18 +164,18 @@ end

nb_available(f::File) = filesize(f) - position(f)

function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
function read!(f::File, b::Array{UInt8}, nb=length(b))
nr = min(nb, nb_available(f))
if length(b) < nr
resize!(b, nr)
end
read!(f, b, nr)
return nr
end
readbytes(io::File) = read!(io, Array(UInt8, nb_available(io)))
readbytes(io::File, nb) = read!(io, Array(UInt8, min(nb, nb_available(io))))
read(io::File) = read!(io, Array(UInt8, nb_available(io)))
read(io::File, nb::Integer) = read!(io, Array(UInt8, min(nb, nb_available(io))))

function readbytes(f::File)
function read(f::File)
a = Array(UInt8, nb_available(f))
read!(f,a)
return a
Expand Down
28 changes: 23 additions & 5 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ read!(io::AbstractPipe, bytes::Vector{UInt8}) = read!(pipe_reader(io), bytes)
read{T<:AbstractPipe}(io::T, args...) = read(pipe_reader(io), args...)
read!{T<:AbstractPipe}(io::T, args...) = read!(pipe_reader(io), args...)
readuntil{T<:AbstractPipe}(io::T, args...) = readuntil(pipe_reader(io), args...)
readbytes(io::AbstractPipe) = readbytes(pipe_reader(io))
read(io::AbstractPipe) = read(pipe_reader(io))
readavailable(io::AbstractPipe) = readavailable(pipe_reader(io))

isreadable(io::AbstractPipe) = isreadable(pipe_reader(io))
Expand All @@ -60,6 +60,18 @@ eof(io::AbstractPipe) = eof(pipe_reader(io))
reseteof(io::AbstractPipe) = reseteof(pipe_reader(io))


# Exception-safe wrappes. (io = open(); try f(io) finally close(io))

write(filename::AbstractString, args...) = open(io->write(io, args...), filename, "w")

read(filename::AbstractString, args...) = open(io->read(io, args...), filename)
read!(filename::AbstractString, a) = open(io->read!(io, a), filename)
readstring(filename::AbstractString) = open(readstring, filename)
readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename)
readline(filename::AbstractString) = open(readline, filename)
readlines(filename::AbstractString) = open(readlines, filename)


## byte-order mark, ntoh & hton ##

const ENDIAN_BOM = reinterpret(UInt32,UInt8[1:4;])[1]
Expand Down Expand Up @@ -160,6 +172,13 @@ function write(io::IO, s::Symbol)
return write(io, pname, Int(ccall(:strlen, Csize_t, (Cstring,), pname)))
end

function write(to::IO, from::IO)
while !eof(from)
write(to, readavailable(from))
end
end


read(s::IO, ::Type{Int8}) = reinterpret(Int8, read(s,UInt8))

function read{T <: Union{Int16,UInt16,Int32,UInt32,Int64,UInt64,Int128,UInt128}}(s::IO, ::Type{T})
Expand Down Expand Up @@ -288,7 +307,7 @@ readline(s::IO) = readuntil(s, '\n')
readchomp(x) = chomp!(readall(x))

# read up to nb bytes into nb, returning # bytes read
function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b))
function read!(s::IO, b::AbstractArray{UInt8}, nb=length(b))
olb = lb = length(b)
nr = 0
while nr < nb && !eof(s)
Expand All @@ -307,19 +326,18 @@ function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b))
end

# read up to nb bytes from s, returning a Vector{UInt8} of bytes read.
function readbytes(s::IO, nb=typemax(Int))
function read(s::IO, nb=typemax(Int))
# Let readbytes! grow the array progressively by default
# instead of taking of risk of over-allocating
b = Array(UInt8, nb == typemax(Int) ? 1024 : nb)
nr = readbytes!(s, b, nb)
resize!(b, nr)
end

function readall(s::IO)
function readstring(s::IO)
b = readbytes(s)
return isvalid(ASCIIString, b) ? ASCIIString(b) : UTF8String(b)
end
readall(filename::AbstractString) = open(readall, filename)

## high-level iterator interfaces ##

Expand Down
6 changes: 3 additions & 3 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,16 @@ function write(to::AbstractIOBuffer, a::UInt8)
sizeof(UInt8)
end

function readbytes!(io::AbstractIOBuffer, b::Array{UInt8}, nb=length(b))
function read!(io::AbstractIOBuffer, b::Array{UInt8}, nb=length(b))
nr = min(nb, nb_available(io))
if length(b) < nr
resize!(b, nr)
end
read_sub(io, b, 1, nr)
return nr
end
readbytes(io::AbstractIOBuffer) = read!(io, Array(UInt8, nb_available(io)))
readbytes(io::AbstractIOBuffer, nb) = read!(io, Array(UInt8, min(nb, nb_available(io))))
read(io::AbstractIOBuffer) = read!(io, Array(UInt8, nb_available(io)))
read(io::AbstractIOBuffer, nb::Integer) = read!(io, Array(UInt8, min(nb, nb_available(io))))

function search(buf::IOBuffer, delim::UInt8)
p = pointer(buf.data, buf.ptr)
Expand Down
6 changes: 3 additions & 3 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ function readbytes_some!(s::IOStream, b::Array{UInt8}, nb)
return nr
end

function readbytes!(s::IOStream, b::Array{UInt8}, nb=length(b); all::Bool=true)
function read!(s::IOStream, b::Array{UInt8}, nb=length(b); all::Bool=true)
return all ? readbytes_all!(s, b, nb) : readbytes_some!(s, b, nb)
end

function readbytes(s::IOStream)
function read(s::IOStream)
sz = 0
try # filesize is just a hint, so ignore if it fails
sz = filesize(s)
Expand All @@ -255,7 +255,7 @@ function readbytes(s::IOStream)
resize!(b, nr)
end

function readbytes(s::IOStream, nb::Integer; all::Bool=true)
function read(s::IOStream, nb::Integer; all::Bool=true)
b = Array(UInt8, nb)
nr = readbytes!(s, b, nb, all=all)
resize!(b, nr)
Expand Down
4 changes: 2 additions & 2 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,14 @@ function readandwrite(cmds::AbstractCmd)
(out, in, processes)
end

function readbytes(cmd::AbstractCmd, stdin::Redirectable=DevNull)
function read(cmd::AbstractCmd, stdin::Redirectable=DevNull)
out, procs = open(cmd, "r", stdin)
bytes = readbytes(out)
!success(procs) && pipeline_error(procs)
return bytes
end

function readall(cmd::AbstractCmd, stdin::Redirectable=DevNull)
function readstring(cmd::AbstractCmd, stdin::Redirectable=DevNull)
return bytestring(readbytes(cmd, stdin))
end

Expand Down
2 changes: 1 addition & 1 deletion base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ function stop_reading(stream::LibuvStream)
end
end

function readbytes(stream::LibuvStream)
function read(stream::LibuvStream)
wait_readnb(stream, typemax(Int))
return takebuf_array(stream.buffer)
end
Expand Down

0 comments on commit 923a1df

Please sign in to comment.