lib/care.rb in format_parser-0.1.2 vs lib/care.rb in format_parser-0.1.3
- old
+ new
@@ -37,10 +37,14 @@
def close
clear
@io.close if @io.respond_to?(:close)
end
+
+ def size
+ @io.size
+ end
end
# Stores cached pages of data from the given IO as strings.
# Pages are sized to be `page_size` or less (for the last page).
class Cache
@@ -56,12 +60,15 @@
# If the IO has been exhausted, `nil` will be returned
# instead. Will use the cached pages where available,
# or fetch pages where necessary
def byteslice(io, at, n_bytes)
if n_bytes < 1
- raise ArgumentError, "The number of bytes to fetch must be a positive Integer"
+ raise ArgumentError, "The number of bytes to fetch must be a positive Integer"
end
+ if at < 0
+ raise ArgumentError, "Negative offsets are not supported (got #{at})"
+ end
first_page = at / @page_size
last_page = (at + n_bytes) / @page_size
relevant_pages = (first_page..last_page).map{|i| hydrate_page(io, i) }
@@ -122,10 +129,10 @@
elsif read_result.bytesize < @page_size
# If we read less than we initially wanted we know there are no pages
# to read following this one, so we can also optimize
@lowest_known_empty_page = page_i + 1
end
-
+
read_result
end
end
end