Skip to content

Commit 7edda72

Browse files
committed
refactor unsafe_read
1 parent 4b5ac77 commit 7edda72

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/io.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ This function is similar to `Base.unsafe_read` but is different in some points:
1212
- It does not block if there are buffered data in `input`.
1313
"""
1414
function unsafe_read(input::IO, output::Ptr{UInt8}, nbytes::Int)::Int
15-
nread = 0
15+
p = output
1616
navail = nb_available(input)
1717
if navail == 0 && nbytes > 0 && !eof(input)
1818
b = read(input, UInt8)
19-
unsafe_store!(output, b)
20-
output += 1
19+
unsafe_store!(p, b)
20+
p += 1
2121
nbytes -= 1
22-
nread += 1
2322
navail = nb_available(input)
2423
end
2524
n = min(navail, nbytes)
26-
Base.unsafe_read(input, output, n)
27-
nread += n
28-
return nread
25+
Base.unsafe_read(input, p, n)
26+
p += n
27+
return Int(p - output)
2928
end

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ using Base.Test
7979
@test eof(stream)
8080
close(stream)
8181

82+
stream = TranscodingStream(Identity(), IOBuffer("foo"))
83+
out = zeros(UInt8, 3)
84+
@test nb_available(stream) == 0
85+
@test TranscodingStreams.unsafe_read(stream, pointer(out), 10) == 3
86+
@test out == b"foo"
87+
close(stream)
88+
8289
s = TranscodingStream(Identity(), IOBuffer(b"baz"))
8390
@test endof(s.state.buffer1) == 0
8491
read(s, UInt8)

0 commit comments

Comments
 (0)