Skip to content

Commit

Permalink
Add Base.position for BufferedOutputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Drvi committed Jul 16, 2022
1 parent c02696f commit 9bbd426
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bufferedoutputstream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ function Base.eof(stream::BufferedOutputStream)
return true
end

function Base.position(stream::BufferedOutputStream)
return max(0, position(stream.sink) + stream.position - 1)
end

function Base.pointer(stream::BufferedOutputStream, index::Integer = 1)
return pointer(stream.buffer, stream.position + index - 1)
end
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,30 @@ end
end
end

@testset "position" begin
iob = IOBuffer()
sink = IOBuffer()
stream = BufferedOutputStream(sink, 16)
for len in 0:10:100
write(stream, repeat("x", len))
write(iob, repeat("x", len))
@test position(stream) == position(iob)
end
close(stream)
close(iob)
@test position(stream) == position(sink) == position(iob)

mktemp() do path, sink
stream = BufferedOutputStream(sink, 16)
pos = 0
for len in 0:10:100
write(stream, repeat("x", len))
pos += len
@test position(stream) == pos
end
end
end

@testset "misc." begin
stream = BufferedOutputStream(IOBuffer(), 5)
@test eof(stream)
Expand Down

0 comments on commit 9bbd426

Please sign in to comment.