Skip to content

Commit

Permalink
MFile#read should not raise IO::EOFError
Browse files Browse the repository at this point in the history
IO#read reads at most slice.size bytes from the IO into slice.
Returns the number of bytes read, which is 0 if and only if
there is no more data to read.
  • Loading branch information
carlhoerberg committed Jan 1, 2025
1 parent 3c0813c commit 205184a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 17 additions & 0 deletions spec/mfile_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,21 @@ describe MFile do
file.delete
end
end

it "can be read" do
file = File.tempfile "mfile_spec"
file.print "hello world"
file.flush
begin
MFile.open(file.path) do |mfile|
buf = Bytes.new(6)
cnt = mfile.read(buf)
String.new(buf[0, cnt]).should eq "hello "
cnt = mfile.read(buf)
String.new(buf[0, cnt]).should eq "world"
end
ensure
file.delete
end
end
end
9 changes: 4 additions & 5 deletions src/lavinmq/mfile.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,10 @@ class MFile < IO

def read(slice : Bytes)
pos = @pos
new_pos = pos + slice.size
raise IO::EOFError.new if new_pos > @size
slice.copy_from(buffer + pos, slice.size)
@pos = new_pos
slice.size
len = Math.min(slice.size, @size - pos)
slice.copy_from(buffer + pos, len)
@pos = pos + len
len
end

def rewind
Expand Down

0 comments on commit 205184a

Please sign in to comment.