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.



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

#dataObject (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

#closeObject



31
32
33
34
# File 'lib/image_size.rb', line 31

def close
  @io.rewind
  @io.close if IO === @io
end