Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MFile#read should not raise IO::EOFError #898

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading