class Raca::WindowedIO
Wrap an IO object and expose only a partial subset of the underlying data in an IO-ish interface. Calling code will have access to the window of data starting at ‘offset’ and the following ‘length’ bytes.
This doesn’t implement the entire IO contract, just enough to make it work when handed to Net::HTTP as a request body.
Public Class Methods
new(io, offset, length)
click to toggle source
# File lib/raca/windowed_io.rb 11 def initialize(io, offset, length) 12 @io = io 13 @offset = offset 14 if @offset + length > @io.size 15 @length = @io.size - offset 16 else 17 @length = length 18 end 19 @io.seek(@offset) 20 end
Public Instance Methods
each(bytes) { |line| ... }
click to toggle source
# File lib/raca/windowed_io.rb 44 def each(bytes, &block) 45 loop do 46 line = read(bytes) 47 break if line.nil? || line == "" 48 yield line 49 end 50 end
eof?()
click to toggle source
# File lib/raca/windowed_io.rb 22 def eof? 23 @io.pos >= @offset + @length 24 end
pos()
click to toggle source
# File lib/raca/windowed_io.rb 26 def pos 27 @io.pos - @offset 28 end
read(bytes = 1024)
click to toggle source
# File lib/raca/windowed_io.rb 52 def read(bytes = 1024) 53 @io.read(capped_bytes_to_read(bytes)) 54 end
readpartial(bytes = 1024, outbuf = nil)
click to toggle source
# File lib/raca/windowed_io.rb 56 def readpartial(bytes = 1024, outbuf = nil) 57 raise EOFError.new("end of file reached") if eof? 58 bytes = capped_bytes_to_read(bytes) 59 @io.readpartial(bytes, outbuf) 60 end
seek(to)
click to toggle source
# File lib/raca/windowed_io.rb 30 def seek(to) 31 if to <= 0 32 @io.seek(@offset) 33 elsif to >= @length 34 @io.seek(@offset + @length) 35 else 36 @io.seek(@offset + to) 37 end 38 end
size()
click to toggle source
# File lib/raca/windowed_io.rb 40 def size 41 @length 42 end
Private Instance Methods
capped_bytes_to_read(bytes)
click to toggle source
# File lib/raca/windowed_io.rb 64 def capped_bytes_to_read(bytes) 65 if pos + bytes > @length 66 size - pos 67 else 68 bytes 69 end 70 end