Class: ImageSize::ImageReader
- Inherits:
-
Object
- Object
- ImageSize::ImageReader
- Defined in:
- lib/image_size.rb
Overview
:nodoc:
Constant Summary collapse
- CHUNK =
1024
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #[](offset, length) ⇒ Object
- #close ⇒ Object
-
#initialize(data_or_io) ⇒ ImageReader
constructor
A new instance of ImageReader.
Constructor Details
#initialize(data_or_io) ⇒ ImageReader
Returns a new instance of ImageReader.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/image_size.rb', line 18 def initialize(data_or_io) @io = case data_or_io when IO, StringIO, Tempfile data_or_io.dup.tap(&:rewind) when String StringIO.new(data_or_io) else raise ArgumentError.new("expected instance of IO, StringIO, Tempfile or String, got #{data_or_io.class}") end @read = 0 @data = '' end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
17 18 19 |
# File 'lib/image_size.rb', line 17 def data @data end |
Instance Method Details
#[](offset, length) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/image_size.rb', line 37 def [](offset, length) while offset + length > @read @read += CHUNK if data = @io.read(CHUNK) if data.respond_to?(:encoding) data.force_encoding(@data.encoding) end @data << data end end @data[offset, length] end |
#close ⇒ Object
31 32 33 34 |
# File 'lib/image_size.rb', line 31 def close @io.rewind @io.close if IO === @io end |