Skip to content

Commit b386bc9

Browse files
committed
add docstring to unsafe_read
1 parent 9c9d94a commit b386bc9

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/io.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
function unsafe_read(stream::IO, ptr::Ptr{UInt8}, nbytes::Int)::Int
1+
# IO Functions
2+
# ------------
3+
4+
"""
5+
unsafe_read(input::IO, output::Ptr{UInt8}, nbytes::Int)::Int
6+
7+
Copy at most `nbytes` from `input` into `output`.
8+
9+
This function is similar to `Base.unsafe_read` but is different in some points:
10+
- It does not throw `EOFError` when it fails to read `nbytes` from `input`.
11+
- It returns the number of bytes written to `output`.
12+
- It does not block if there are buffered data in `input`.
13+
"""
14+
function unsafe_read(input::IO, output::Ptr{UInt8}, nbytes::Int)::Int
215
nread = 0
3-
navail = nb_available(stream)
4-
if navail == 0 && nbytes > 0 && !eof(stream)
5-
b = read(stream, UInt8)
6-
unsafe_store!(ptr, b)
7-
ptr += 1
16+
navail = nb_available(input)
17+
if navail == 0 && nbytes > 0 && !eof(input)
18+
b = read(input, UInt8)
19+
unsafe_store!(output, b)
20+
output += 1
821
nbytes -= 1
922
nread += 1
10-
navail = nb_available(stream)
23+
navail = nb_available(input)
1124
end
1225
n = min(navail, nbytes)
13-
Base.unsafe_read(stream, ptr, n)
26+
Base.unsafe_read(input, output, n)
1427
nread += n
1528
return nread
1629
end

0 commit comments

Comments
 (0)