Class: ImageSize::ImageReader

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

Overview

:nodoc:

Constant Summary collapse

CHUNK =
1024

Instance Method Summary collapse

Constructor Details

#initialize(data_or_io) ⇒ ImageReader

Returns a new instance of ImageReader.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/image_size.rb', line 13

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.class}")
  end
  @read = 0
  @data = ''
end

Instance Method Details

#[](offset, length) ⇒ Object



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

def [](offset, length)
  while offset + length > @read
    @read += CHUNK
    if data = @io.read(CHUNK)
      @data << data
    end
  end
  @data[offset, length]
end

#rewindObject



26
27
28
# File 'lib/image_size.rb', line 26

def rewind
  @io.rewind
end