Class: JvmBytecode::Constants::Utf8

Inherits:
Constant
  • Object
show all
Defined in:
lib/jvm_bytecode/constants/utf8.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Constant

#bytecode, define, fetch

Constructor Details

#initialize(str) ⇒ Utf8

Returns a new instance of Utf8.



11
12
13
14
# File 'lib/jvm_bytecode/constants/utf8.rb', line 11

def initialize(str)
  raise "#{self.class} requires utf-8 string" unless str.encoding.to_s == 'UTF-8'
  @str = str
end

Class Method Details

.decode(io) ⇒ Object



6
7
8
9
# File 'lib/jvm_bytecode/constants/utf8.rb', line 6

def self.decode(io)
  str = io.read(io.read(2).unpack('S>').first).force_encoding('UTF-8')
  new(str)
end

Instance Method Details

#additional_bytecodeObject



20
21
22
23
# File 'lib/jvm_bytecode/constants/utf8.rb', line 20

def additional_bytecode
  s = @str.dup.force_encoding('ASCII-8BIT')
  [s.length].pack('S>') + s
end

#to_hashObject



25
26
27
# File 'lib/jvm_bytecode/constants/utf8.rb', line 25

def to_hash
  { string: @str }
end

#to_sObject



16
17
18
# File 'lib/jvm_bytecode/constants/utf8.rb', line 16

def to_s
  @str
end