Class: AppInfo::PngUncrush::PngReader

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

Overview

:nodoc:

Constant Summary collapse

PNG_HEADER =
"\x89PNG\r\n\x1a\n".bytes
CHUNK =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ PngReader

Returns a new instance of PngReader.



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

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

  @data = String.new
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



18
19
20
# File 'lib/app_info/png_uncrush.rb', line 18

def data
  @data
end

Instance Method Details

#[](offset, length) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/app_info/png_uncrush.rb', line 48

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

#headerObject



40
41
42
# File 'lib/app_info/png_uncrush.rb', line 40

def header
  @header ||= self[0, 8]
end

#png?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/app_info/png_uncrush.rb', line 44

def png?
  PNG_HEADER == header.bytes
end

#sizeObject



32
33
34
# File 'lib/app_info/png_uncrush.rb', line 32

def size
  @io.size
end

#unpack(a) ⇒ Object



36
37
38
# File 'lib/app_info/png_uncrush.rb', line 36

def unpack(a)
  @io.unpack(a)
end