class Care::IOWrapper
Wraps any given IO with Care
caching superpowers. Supports the subset of IO declared in IOConstraint.
Public Class Methods
new(io, page_size: DEFAULT_PAGE_SIZE)
click to toggle source
Public Instance Methods
clear()
click to toggle source
Clears all the cached pages explicitly to help GC
@return void
# File lib/care.rb, line 62 def clear @cache.clear end
close()
click to toggle source
Clears all the cached pages explicitly to help GC, and calls `#close` on the source IO if the IO responds to `#close`
@return void
# File lib/care.rb, line 70 def close clear @io.close if @io.respond_to?(:close) end
pos()
click to toggle source
Returns the current position/offset within the IO
@return Integer
# File lib/care.rb, line 41 def pos @pos end
read(n_bytes)
click to toggle source
Returns at most `n_bytes` of data from the IO or less if less data was available before the EOF was hit
@param n_bytes @return [String, nil] the content read from the IO or `nil` if no data was available
# File lib/care.rb, line 50 def read(n_bytes) return '' if n_bytes == 0 # As hardcoded for all Ruby IO objects raise ArgumentError, "negative length #{n_bytes} given" if n_bytes < 0 # also as per Ruby IO objects read = @cache.byteslice(@io, @pos, n_bytes) return unless read && !read.empty? @pos += read.bytesize read end
seek(to)
click to toggle source
Seeks the IO to the given absolute offset from the start of the file/resource
@param to offset in the IO @return Integer
# File lib/care.rb, line 34 def seek(to) @pos = to end
size()
click to toggle source
Returns the size of the resource contained in the IO
@return Integer
# File lib/care.rb, line 26 def size @io.size end