Skip to content

Commit

Permalink
fix shiftdata! (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
bicycle1885 authored Mar 10, 2017
1 parent fd271a2 commit f4e2f9f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/bufferedinputstream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ function shiftdata!(stream::BufferedInputStream)
n = stream.available - shift
copy!(stream.buffer, 1, stream.buffer, stream.position, n)
else
shift = 0
# no data to be kept
@assert stream.position > stream.available
shift = stream.available
end
stream.position -= shift
stream.available -= shift
Expand Down
38 changes: 38 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ else
const Test = BaseTestNext
end

immutable InfiniteStream <: IO
byte::UInt8
end

function Base.eof(stream::InfiniteStream)
return false
end

function Base.read(stream::InfiniteStream, ::Type{UInt8})
return stream.byte
end

if VERSION >= v"0.5-"
function Base.unsafe_read(stream::InfiniteStream, pointer::Ptr{UInt8}, n::UInt)
ccall(:memset, Void, (Ptr{Void}, Cint, Csize_t), pointer, stream.byte, n)
return nothing
end
end


# A few things that might not be obvious:
# * In a few places we wrap an array in an IOBuffer before wrapping it in a
# BufferedInputStream. This is to force the BufferedInputStream to read
Expand Down Expand Up @@ -311,6 +331,24 @@ end
@test ismatch(r"^BufferedStreams\.BufferedInputStream{.*}\(<closed>\)$", string(stream))
@test_throws ArgumentError BufferedInputStream(IOBuffer("foo"), 0)
end

@testset "massive read" begin
byte = 0x34
bufsize = 1024
stream = BufferedInputStream(InfiniteStream(byte), bufsize)

# read byte by byte
ok = true
for i in 1:2^30
ok = ok && (read(stream, UInt8) == byte)
end
@test ok
@test length(stream.buffer) == 1024

# read in one shot
@test all(read(stream, 5 * bufsize) .== byte)
@test length(stream.buffer) == 1024
end
end


Expand Down

0 comments on commit f4e2f9f

Please sign in to comment.