Skip to content

Commit

Permalink
Base64: add isreadable and iswritable for Base64EncodePipe/Base64Deco…
Browse files Browse the repository at this point in the history
…dePipe (#37520)
  • Loading branch information
johnnychen94 committed Sep 15, 2020
1 parent a0a68a5 commit 8bdf569
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stdlib/Base64/src/decode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct Base64DecodePipe <: IO
end
end

Base.isreadable(pipe::Base64DecodePipe) = !isempty(pipe.rest) || isreadable(pipe.io)
Base.iswritable(::Base64DecodePipe) = false

function Base.unsafe_read(pipe::Base64DecodePipe, ptr::Ptr{UInt8}, n::UInt)
p = read_until_end(pipe, ptr, n)
if p < ptr + n
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Base64/src/encode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ struct Base64EncodePipe <: IO
end
end

Base.isreadable(::Base64EncodePipe) = false
Base.iswritable(pipe::Base64EncodePipe) = iswritable(pipe.io)

function Base.unsafe_write(pipe::Base64EncodePipe, ptr::Ptr{UInt8}, n::UInt)::Int
buffer = pipe.buffer
m = buffer.size
Expand Down
14 changes: 14 additions & 0 deletions stdlib/Base64/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,31 @@ const longDecodedText = "name = \"Genie\"\nuuid = \"c43c736e-a2d1-11e8-161f-af95
# Byte-by-byte encode and decode.
buf = IOBuffer()
pipe = Base64EncodePipe(buf)
@test !isreadable(pipe) && iswritable(pipe)
for char in inputText
write(pipe, UInt8(char))
end
close(pipe)
pipe = Base64DecodePipe(IOBuffer(take!(buf)))
@test isreadable(ipipe) && !iswritable(ipipe)
decoded = UInt8[]
while !eof(pipe)
push!(decoded, read(pipe, UInt8))
end
@test String(decoded) == inputText

buf = IOBuffer(write=false)
pipe = Base64EncodePipe(buf)
@test !isreadable(pipe) && !iswritable(pipe)
@test_throws ArgumentError write(pipe, "Hello!")
close(pipe)
buf = IOBuffer(read=false)
write(buf, "SGVsbG8h")
pipe = Base64DecodePipe(buf)
@test !isreadable(pipe) && !iswritable(pipe)
@test isempty(read(pipe))


# Encode to string and decode
@test String(base64decode(base64encode(inputText))) == inputText

Expand Down

4 comments on commit 8bdf569

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.