Skip to content

Commit 2da225e

Browse files
authored
Merge pull request #89 from rofinn/rf/filebuffer-io
Add byte I/O support for FileBuffer
2 parents b01aa13 + dda3fc2 commit 2da225e

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "FilePathsBase"
22
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
33
authors = ["Rory Finnegan"]
4-
version = "0.9.1"
4+
version = "0.9.2"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/buffer.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ function Base.read(buffer::FileBuffer, ::Type{String})
6161
read(buffer.io, String)
6262
end
6363

64+
function Base.read(buffer::FileBuffer, ::Type{UInt8})
65+
if buffer.io.size == 0
66+
write(buffer.io, read(buffer.path))
67+
seekstart(buffer)
68+
end
69+
read(buffer.io, UInt8)
70+
end
71+
6472
#=
6573
NOTE: We need to define multiple methods because of ambiguity error with base IO methods.
6674
=#
@@ -74,6 +82,11 @@ function Base.write(buffer::FileBuffer, x::String)
7482
write(buffer.io, x)
7583
end
7684

85+
function Base.write(buffer::FileBuffer, x::UInt8)
86+
iswritable(buffer) || throw(ArgumentError("write failed, FileBuffer is not writeable"))
87+
write(buffer.io, x)
88+
end
89+
7790
function Base.flush(buffer::FileBuffer)
7891
if iswritable(buffer)
7992
seekstart(buffer)

test/buffer.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ using FilePathsBase: FileBuffer
1717
finally
1818
close(io)
1919
end
20+
21+
io = FileBuffer(p)
22+
try
23+
for b in read(p)
24+
@test read(io, UInt8) == b
25+
end
26+
@test eof(io)
27+
finally
28+
close(io)
29+
end
2030
end
2131

2232
@testset "write" begin
@@ -45,6 +55,23 @@ using FilePathsBase: FileBuffer
4555
finally
4656
close(io)
4757
end
58+
59+
rm(p2)
60+
61+
io = FileBuffer(p2; read=true,write=true)
62+
try
63+
write(io, read(p1))
64+
flush(io)
65+
66+
seekstart(io)
67+
for b in read(p1)
68+
write(io, b)
69+
end
70+
flush(io)
71+
@test read(p1) == read(p2)
72+
finally
73+
close(io)
74+
end
4875
end
4976
end
5077
end

test/system.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ ps = PathSet(; symlink=true)
384384
@test ispath("cpstr")
385385

386386
# Recursive directory
387-
@show docsdir
388387
@test cp(docsdir, p"cpdstpath"; force=true) == p"cpdstpath"
389388
@test ispath(p"cpdstpath")
390389
@test ispath(p"cpdstpath/src/api.md")

0 commit comments

Comments
 (0)