Class: Google::APIClient::RangedIO
- Inherits:
-
Object
- Object
- Google::APIClient::RangedIO
- Defined in:
- lib/google/api_client/media.rb
Overview
Wraps an input stream and limits data to a given range
Instance Method Summary collapse
-
#initialize(io, offset, length) ⇒ RangedIO
constructor
Bind an input stream to a specific range.
- #pos ⇒ Object
- #pos=(pos) ⇒ Object
- #read(amount = nil, buf = nil) ⇒ Object
- #rewind ⇒ Object
Constructor Details
#initialize(io, offset, length) ⇒ RangedIO
Bind an input stream to a specific range.
55 56 57 58 59 60 |
# File 'lib/google/api_client/media.rb', line 55 def initialize(io, offset, length) @io = io @offset = offset @length = length self.rewind end |
Instance Method Details
#pos ⇒ Object
99 100 101 |
# File 'lib/google/api_client/media.rb', line 99 def pos @pos end |
#pos=(pos) ⇒ Object
105 106 107 108 |
# File 'lib/google/api_client/media.rb', line 105 def pos=(pos) @pos = pos @io.pos = @offset + pos end |
#read(amount = nil, buf = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/google/api_client/media.rb', line 64 def read(amount = nil, buf = nil) buffer = buf || '' if amount.nil? size = @length - @pos done = '' elsif amount == 0 size = 0 done = '' else size = [@length - @pos, amount].min done = nil end if size > 0 result = @io.read(size) result.force_encoding("BINARY") if result.respond_to?(:force_encoding) buffer << result if result @pos = @pos + size end if buffer.length > 0 buffer else done end end |
#rewind ⇒ Object
93 94 95 |
# File 'lib/google/api_client/media.rb', line 93 def rewind self.pos = 0 end |