Class: ImageSize::ImageReader

Inherits:
Object
  • Object
show all
Defined in:
lib/image_size.rb

Overview

:nodoc:

Constant Summary collapse

CHUNK =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_or_io) ⇒ ImageReader

Returns a new instance of ImageReader.



20
21
22
23
24
25
26
27
28
29
# File 'lib/image_size.rb', line 20

def initialize(data_or_io)
  @io = if data_or_io.is_a?(String)
    StringIO.new(data_or_io)
  elsif data_or_io.respond_to?(:read) && data_or_io.respond_to?(:eof?)
    data_or_io
  else
    raise ArgumentError, "expected data as String or an object responding to read and eof?, got #{data_or_io.class}"
  end
  @data = String.new # not frozen
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/image_size.rb', line 19

def data
  @data
end

Instance Method Details

#[](offset, length) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/image_size.rb', line 32

def [](offset, length)
  while !@io.eof? && @data.length < offset + length
    data = @io.read(CHUNK)
    break unless data

    data.force_encoding(@data.encoding) if data.respond_to?(:encoding)
    @data << data
  end
  @data[offset, length]
end