-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes for position
with nested NoopStreams
#203
Changes from 4 commits
952130e
56707b3
c588f03
5fae7a4
69abbdf
f26655e
c9b062b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,16 +53,18 @@ Note that this method may return a wrong position when | |
- some data have been inserted by `TranscodingStreams.unread`, or | ||
- the position of the wrapped stream has been changed outside of this package. | ||
""" | ||
function Base.position(stream::NoopStream) | ||
function Base.position(stream::NoopStream)::Int64 | ||
mode = stream.state.mode | ||
if mode === :idle | ||
return Int64(0) | ||
if !isopen(stream) | ||
throw_invalid_mode(mode) | ||
elseif mode === :idle | ||
return 0 | ||
nhz2 marked this conversation as resolved.
Show resolved
Hide resolved
nhz2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elseif has_sharedbuf(stream) | ||
return position(stream.stream) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I treat the buffer as being "owned" by the underlying stream even though it is being shared. This is also why I need to add special cases for writing to a |
||
elseif mode === :write | ||
return position(stream.stream) + buffersize(stream.buffer1) | ||
elseif mode === :read | ||
else # read | ||
return position(stream.stream) - buffersize(stream.buffer1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
else | ||
throw_invalid_mode(mode) | ||
end | ||
@assert false "unreachable" | ||
end | ||
|
@@ -97,16 +99,34 @@ function Base.seekend(stream::NoopStream) | |
return stream | ||
end | ||
|
||
function Base.unsafe_write(stream::NoopStream, input::Ptr{UInt8}, nbytes::UInt) | ||
function Base.write(stream::NoopStream, b::UInt8)::Int | ||
changemode!(stream, :write) | ||
if has_sharedbuf(stream) | ||
# directly write data to the underlying stream | ||
n = Int(write(stream.stream, b)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need the explicit cast to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need this to fix the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now the cast isn't doing anything. |
||
return n | ||
end | ||
buffer1 = stream.buffer1 | ||
marginsize(buffer1) > 0 || flushbuffer(stream) | ||
return writebyte!(buffer1, b) | ||
end | ||
|
||
function Base.unsafe_write(stream::NoopStream, input::Ptr{UInt8}, nbytes::UInt)::Int | ||
changemode!(stream, :write) | ||
if has_sharedbuf(stream) | ||
# directly write data to the underlying stream | ||
n = Int(unsafe_write(stream.stream, input, nbytes)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need the explicit cast to Int here? |
||
return n | ||
end | ||
buffer = stream.buffer1 | ||
if marginsize(buffer) ≥ nbytes | ||
copydata!(buffer, input, nbytes) | ||
return Int(nbytes) | ||
else | ||
flushbuffer(stream) | ||
# directly write data to the underlying stream | ||
return unsafe_write(stream.stream, input, nbytes) | ||
n = Int(unsafe_write(stream.stream, input, nbytes)) | ||
return n | ||
end | ||
end | ||
|
||
|
@@ -152,7 +172,7 @@ function fillbuffer(stream::NoopStream; eager::Bool = false)::Int | |
changemode!(stream, :read) | ||
buffer = stream.buffer1 | ||
@assert buffer === stream.buffer2 | ||
if stream.stream isa TranscodingStream && buffer === stream.stream.buffer1 | ||
if has_sharedbuf(stream) | ||
# Delegate the operation when buffers are shared. | ||
underlying_mode::Symbol = stream.stream.state.mode | ||
if underlying_mode === :idle || underlying_mode === :read | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to decide if this should be
Int64
orInt
. The latter varies based on the system word size.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I didn't know juliaup had 32-bit julia.
This should be
Int64
to support large files.