Class String
In: lib/javaclass/java_name.rb
lib/javaclass/string_20.rb
lib/javaclass/string_hexdump.rb
lib/javaclass/string_ux.rb
Parent: Object

Add some unpack helper methods for HI-LO byte order (network byte order) contained in this String.

Author:Peter Kofler

Methods

byte_at   double   hexdump   number_bytes   same_bytes_as?   single   strip_non_printable   to_javaname   u1   u2   u2rep   u4   u8  

Constants

TYPES = [JavaClass::JavaClassFileName, JavaClass::JavaVMName, JavaClass::JavaPackageName, JavaClass::JavaQualifiedName]
RUBY19 = ''.respond_to? :codepoints

Public Instance methods

Return the index‘th element as byte.

[Source]

# File lib/javaclass/string_20.rb, line 8
  def byte_at(index=0)
    if RUBY19
      self[index..index].unpack('C')[0]
    else
      self[index]
    end
  end

Return the index‘th and the next 7 elements as double precision float.

[Source]

# File lib/javaclass/string_ux.rb, line 42
  def double(index=0)
    self[index..index+7].unpack('G')[0]
  end

Return the hex dump of this String with columns columns of hexadecimal numbers per line.

[Source]

# File lib/javaclass/string_hexdump.rb, line 8
  def hexdump(columns=16)
    return StringLineHexdumper.empty(columns).format if size == 0

    input = [0, []]
    lines = 1..number_hexdump_lines(columns)
    output = lines.inject(input) { |result, line_index|
      offset, previous_lines = *result

      part = hexdump_line(line_index, columns)
      line = StringLineHexdumper.new(offset, columns, part).format
      
      [ offset + columns, previous_lines + [line] ]
    }
    offset, lines = *output
    lines.join
  end

[Source]

# File lib/javaclass/string_20.rb, line 24
  def number_bytes
    if RUBY19
      self.bytesize
    else
      self.length
    end
  end

[Source]

# File lib/javaclass/string_20.rb, line 16
  def same_bytes_as?(other)
    if RUBY19
      self.unpack('C*') == other.unpack('C*')
    else
      self == other
    end
  end

Return the index‘th and the next 3 elements as single precision float.

See:steve.hollasch.net/cgindex/coding/ieeefloat.html
See:blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/196633

[Source]

# File lib/javaclass/string_ux.rb, line 37
  def single(index=0)
    self[index..index+3].unpack('g')[0]
  end

[Source]

# File lib/javaclass/string_20.rb, line 32
  def strip_non_printable
    if RUBY19
      self.unpack('C*').map { |c| if c < 32 or c > 127 then 46 else c end }.pack('C*')
    else
      self.gsub(Regexp.new("[^ -\x7f]", nil, 'n'), '.')
    end
  end

Convert a Java classname or Java class filename to special String with methods to work with Java class or package names. If it‘s a pathname then it must be relative to the classpath. Source extension and additional JVM method declarations are dropped.

[Source]

# File lib/javaclass/java_name.rb, line 348
  def to_javaname

    match = TYPES.find { |type| type.valid?(self) }
    if match
      return match.new(self)
    end

    plain_name = self.sub(/#{JavaClass::JavaLanguage::SOURCE_REGEX}|".*$|\.<.*$/, '').gsub(/\\/, '/')
    match = TYPES.find { |type| type.valid?(plain_name) }
    if match
      match.new(plain_name)
    else
      raise ArgumentError, "unknown Java name #{self}"
    end
  end

Return the index‘th element as byte.

[Source]

# File lib/javaclass/string_ux.rb, line 8
  def u1(index=0)
    self.byte_at(index)
  end

Return the index‘th and the next element as unsigned word.

[Source]

# File lib/javaclass/string_ux.rb, line 13
  def u2(index=0)
    self[index..index+1].unpack('n')[0]
    # self[index]*256 + self[index+1]
  end

Return the index‘th and the next element as unsigned word, repeat it for count words in total and return an array of these words.

[Source]

# File lib/javaclass/string_ux.rb, line 20
  def u2rep(count=1, index=0)
    self[index...index+count*2].unpack('n'*count)
  end

Return the index‘th and the next 3 elements as unsigned dword.

[Source]

# File lib/javaclass/string_ux.rb, line 25
  def u4(index=0)
    self[index..index+3].unpack('N')[0]
  end

Return the index‘th and the next 7 elements as unsigned qword.

[Source]

# File lib/javaclass/string_ux.rb, line 30
  def u8(index=0)
    u4(index) * 256**4 + u4(index+4)
  end

[Validate]