Module: ImageSize::ChunkyReader

Includes:
Reader
Included in:
SeekableIOReader, StreamIOReader, URIReader::BodyReader, URIReader::HTTPChunkyReader
Defined in:
lib/image_size/chunky_reader.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Reader

#fetch, open_with_uri, #stream, #unpack, #unpack1

Instance Method Details

#[](offset, length) ⇒ Object

Including class should define method chunk that accepts the chunk number and returns a string of chunk_size length or shorter for last chunk, or nil for further chunks. Determines required chunks, takes parts of them to construct desired substring, behaves same as str[start, length] except start can’t be negative.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/image_size/chunky_reader.rb', line 20

def [](offset, length)
  raise ArgumentError, "expected offset not to be negative, got #{offset}" if offset < 0
  return if length < 0

  first = offset / chunk_size
  return unless (first_chunk = chunk(first))

  last = (offset + length - 1) / chunk_size

  if first >= last
    first_chunk[offset - (first * chunk_size), length]
  else
    return unless (first_piece = first_chunk[offset - (first * chunk_size), chunk_size])

    chunks = (first.succ...last).map{ |i| chunk(i) }.unshift(first_piece)

    if (last_chunk = chunk(last))
      chunks.push(last_chunk[0, offset + length - (last * chunk_size)])
    end

    chunks.join
  end
end

#chunk_sizeObject

Size of a chunk in which to read



10
11
12
# File 'lib/image_size/chunky_reader.rb', line 10

def chunk_size
  4096
end