Class: JavaClass::ClassFile::ClassMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/javaclass/classfile/class_magic.rb

Overview

The CAFEBABE magic of a class file. This just checks if CAFEBABE is here.

Author

Peter Kofler

Constant Summary collapse

CAFE_BABE =

ZenTest SKIP

"\xCA\xFE\xBA\xBE"

Instance Method Summary collapse

Constructor Details

#initialize(data, start = 0) ⇒ ClassMagic

Check the class magic in the data beginning at position start (which is usually 0).



13
14
15
# File 'lib/javaclass/classfile/class_magic.rb', line 13

def initialize(data, start=0)
  @bytes = data[start..start+3]
end

Instance Method Details

#bytesObject

Return the value of the magic in this class.



23
24
25
# File 'lib/javaclass/classfile/class_magic.rb', line 23

def bytes
  @bytes.dup
end

#check(msg = 'invalid java class magic') ⇒ Object

Check if this magic is valid and raise an ClassFormatError if not with an optional msg .



28
29
30
31
32
# File 'lib/javaclass/classfile/class_magic.rb', line 28

def check(msg='invalid java class magic')
  unless valid?
    raise(ClassFormatError, msg)
  end
end

#valid?Boolean

Return true if the data was valid, i.e. if the class started with CAFEBABE.

Returns:

  • (Boolean)


18
19
20
# File 'lib/javaclass/classfile/class_magic.rb', line 18

def valid?
  @bytes.same_bytes_as?(CAFE_BABE)
end