Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/io.cr
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,28 @@ abstract class IO
count
end

# Similar to `#read`, but with the additional guarantee that either
# the buffer will be entirely filled or the EOF will be reached while trying.
#
# Calling this method may result in multiple read calls if necessary.
#
# ```
# io = IO::Memory.new "123451234"
# slice = Bytes.new(5)
# io.read_greedy(slice) # => 5
# slice # => Bytes[49, 50, 51, 52, 53]
# io.read_greedy(slice) # => 4
# ```
def read_greedy(slice : Bytes) : Int32
count = slice.size
while slice.size > 0
read_bytes = read(slice)
return slice_total_size &- slice.size if read_bytes == 0
slice += read_bytes
end
count
end

# Reads the rest of this `IO` data as a `String`.
#
# ```
Expand Down
5 changes: 5 additions & 0 deletions src/io/memory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ class IO::Memory < IO
end
end

def read_greedy(slice : Bytes) : Int32
# IO::Memory is always greedy
read(slice)
end

def peek : Bytes
check_open

Expand Down