Skip to content

Commit 8bdf569

Browse files
authored
Base64: add isreadable and iswritable for Base64EncodePipe/Base64DecodePipe (#37520)
1 parent a0a68a5 commit 8bdf569

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

stdlib/Base64/src/decode.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ struct Base64DecodePipe <: IO
4343
end
4444
end
4545

46+
Base.isreadable(pipe::Base64DecodePipe) = !isempty(pipe.rest) || isreadable(pipe.io)
47+
Base.iswritable(::Base64DecodePipe) = false
48+
4649
function Base.unsafe_read(pipe::Base64DecodePipe, ptr::Ptr{UInt8}, n::UInt)
4750
p = read_until_end(pipe, ptr, n)
4851
if p < ptr + n

stdlib/Base64/src/encode.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ struct Base64EncodePipe <: IO
4444
end
4545
end
4646

47+
Base.isreadable(::Base64EncodePipe) = false
48+
Base.iswritable(pipe::Base64EncodePipe) = iswritable(pipe.io)
49+
4750
function Base.unsafe_write(pipe::Base64EncodePipe, ptr::Ptr{UInt8}, n::UInt)::Int
4851
buffer = pipe.buffer
4952
m = buffer.size

stdlib/Base64/test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,31 @@ const longDecodedText = "name = \"Genie\"\nuuid = \"c43c736e-a2d1-11e8-161f-af95
3838
# Byte-by-byte encode and decode.
3939
buf = IOBuffer()
4040
pipe = Base64EncodePipe(buf)
41+
@test !isreadable(pipe) && iswritable(pipe)
4142
for char in inputText
4243
write(pipe, UInt8(char))
4344
end
4445
close(pipe)
4546
pipe = Base64DecodePipe(IOBuffer(take!(buf)))
47+
@test isreadable(ipipe) && !iswritable(ipipe)
4648
decoded = UInt8[]
4749
while !eof(pipe)
4850
push!(decoded, read(pipe, UInt8))
4951
end
5052
@test String(decoded) == inputText
5153

54+
buf = IOBuffer(write=false)
55+
pipe = Base64EncodePipe(buf)
56+
@test !isreadable(pipe) && !iswritable(pipe)
57+
@test_throws ArgumentError write(pipe, "Hello!")
58+
close(pipe)
59+
buf = IOBuffer(read=false)
60+
write(buf, "SGVsbG8h")
61+
pipe = Base64DecodePipe(buf)
62+
@test !isreadable(pipe) && !iswritable(pipe)
63+
@test isempty(read(pipe))
64+
65+
5266
# Encode to string and decode
5367
@test String(base64decode(base64encode(inputText))) == inputText
5468

0 commit comments

Comments
 (0)