Skip to content

Commit

Permalink
Merge pull request #89 from rofinn/rf/filebuffer-io
Browse files Browse the repository at this point in the history
Add byte I/O support for FileBuffer
  • Loading branch information
rofinn authored May 1, 2020
2 parents b01aa13 + dda3fc2 commit 2da225e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FilePathsBase"
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
authors = ["Rory Finnegan"]
version = "0.9.1"
version = "0.9.2"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
13 changes: 13 additions & 0 deletions src/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ function Base.read(buffer::FileBuffer, ::Type{String})
read(buffer.io, String)
end

function Base.read(buffer::FileBuffer, ::Type{UInt8})
if buffer.io.size == 0
write(buffer.io, read(buffer.path))
seekstart(buffer)
end
read(buffer.io, UInt8)
end

#=
NOTE: We need to define multiple methods because of ambiguity error with base IO methods.
=#
Expand All @@ -74,6 +82,11 @@ function Base.write(buffer::FileBuffer, x::String)
write(buffer.io, x)
end

function Base.write(buffer::FileBuffer, x::UInt8)
iswritable(buffer) || throw(ArgumentError("write failed, FileBuffer is not writeable"))
write(buffer.io, x)
end

function Base.flush(buffer::FileBuffer)
if iswritable(buffer)
seekstart(buffer)
Expand Down
27 changes: 27 additions & 0 deletions test/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ using FilePathsBase: FileBuffer
finally
close(io)
end

io = FileBuffer(p)
try
for b in read(p)
@test read(io, UInt8) == b
end
@test eof(io)
finally
close(io)
end
end

@testset "write" begin
Expand Down Expand Up @@ -45,6 +55,23 @@ using FilePathsBase: FileBuffer
finally
close(io)
end

rm(p2)

io = FileBuffer(p2; read=true,write=true)
try
write(io, read(p1))
flush(io)

seekstart(io)
for b in read(p1)
write(io, b)
end
flush(io)
@test read(p1) == read(p2)
finally
close(io)
end
end
end
end
Expand Down
1 change: 0 additions & 1 deletion test/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ ps = PathSet(; symlink=true)
@test ispath("cpstr")

# Recursive directory
@show docsdir
@test cp(docsdir, p"cpdstpath"; force=true) == p"cpdstpath"
@test ispath(p"cpdstpath")
@test ispath(p"cpdstpath/src/api.md")
Expand Down

2 comments on commit 2da225e

@rofinn
Copy link
Owner Author

@rofinn rofinn commented on 2da225e May 1, 2020

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/14004

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.2 -m "<description of version>" 2da225ec3ea2afa1c85f4a7b29e58cdbe386110a
git push origin v0.9.2

Please sign in to comment.