diff --git a/spec/std/file_spec.cr b/spec/std/file_spec.cr index 50192d20f402..fdf071bf0cb0 100644 --- a/spec/std/file_spec.cr +++ b/spec/std/file_spec.cr @@ -946,9 +946,14 @@ describe "File" do file.read_at(6, 100) do |io| io.gets_to_end.should eq("World\nHello World\nHello World\nHello World\nHello World\nHello World\nHello World\nHello World\nHello Worl") end + file.read_at(0, 240) do |io| io.gets_to_end.should eq(File.read(filename)) end + + file.read_at(6_i64, 5_i64) do |io| + io.gets_to_end.should eq("World") + end end end diff --git a/src/file.cr b/src/file.cr index 27313753ef56..06bea69f11f5 100644 --- a/src/file.cr +++ b/src/file.cr @@ -804,7 +804,7 @@ class File < IO::FileDescriptor # Yields an `IO` to read a section inside this file. # Multiple sections can be read concurrently. - def read_at(offset, bytesize, &block) + def read_at(offset, bytesize, & : IO ->) self_bytesize = self.size unless 0 <= offset <= self_bytesize diff --git a/src/file/preader.cr b/src/file/preader.cr index fa39b48ffa8f..e8b91a8d6b55 100644 --- a/src/file/preader.cr +++ b/src/file/preader.cr @@ -4,7 +4,12 @@ class File::PReader < IO getter? closed = false - def initialize(@file : File, @offset : Int32, @bytesize : Int32) + @offset : Int64 + @bytesize : Int64 + + def initialize(@file : File, offset : Int, bytesize : Int) + @offset = offset.to_i64 + @bytesize = bytesize.to_i64 @pos = 0 end diff --git a/src/io.cr b/src/io.cr index d17f9c670651..37abcdea3722 100644 --- a/src/io.cr +++ b/src/io.cr @@ -1099,7 +1099,7 @@ abstract class IO # `File` and `IO::Memory` implement it. # # Multiple sections can be read concurrently. - def read_at(offset, bytesize, &block) + def read_at(offset, bytesize, & : IO ->) raise Error.new "Unable to read_at" end diff --git a/src/io/memory.cr b/src/io/memory.cr index e447c9f5da93..082e16477ece 100644 --- a/src/io/memory.cr +++ b/src/io/memory.cr @@ -338,7 +338,7 @@ class IO::Memory < IO # # During the block duration `self` becomes read-only, # so multiple concurrent open are allowed. - def read_at(offset, bytesize) + def read_at(offset, bytesize, & : IO ->) unless 0 <= offset <= @bytesize raise ArgumentError.new("Offset out of bounds") end