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.



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

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.



20
21
22
# File 'lib/app_info/png_uncrush.rb', line 20

def data
  @data
end

Instance Method Details

#[](offset, length) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/app_info/png_uncrush.rb', line 57

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

#headerString

Returns:

  • (String)


48
49
50
# File 'lib/app_info/png_uncrush.rb', line 48

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

#png?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/app_info/png_uncrush.rb', line 53

def png?
  header.bytes == PNG_HEADER
end

#sizeInteger

Returns:

  • (Integer)


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

def size
  @io.size
end

#unpack(format) ⇒ String

Parameters:

  • format (String)

Returns:

  • (String)

See Also:

  • package data


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

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