Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
lib/rubytorrent/message.rb,
lib/rubytorrent/bencoding.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bencoded?(c) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rubytorrent/bencoding.rb', line 92

def self.bencoded?(c)
  c == ?i
end

.parse_bencoding(c, s) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubytorrent/bencoding.rb', line 96

def self.parse_bencoding(c, s)
  ints = ""
  while ((x = s.getc.chr) != 'e')
    raise RubyTorrent::BEncodingError, "invalid bencoded integer #{x.inspect}" unless x =~ /\d|-/
    ints += x
  end
  raise RubyTorrent::BEncodingError, "invalid integer #{ints} (too long)" unless ints.length <= 20
  int = ints.to_i
  raise RubyTorrent::BEncodingError, %{can't parse bencoded integer "#{ints}"} if (int == 0) && (ints !~ /^0$/) #'
  int
end

Instance Method Details

#to_bencodingObject



88
89
90
# File 'lib/rubytorrent/bencoding.rb', line 88

def to_bencoding
  "i" + self.to_s + "e"
end

#to_fbbeObject

four-byte big-endian integer



25
26
27
28
29
30
31
32
33
34
# File 'lib/rubytorrent/message.rb', line 25

def to_fbbe # four-byte big-endian integer
  raise "fbbe must be < 2^32" unless self <= 2**32
  raise "fbbe must be >= 0" unless self >= 0
  s = "    "
  s[0] = (self >> 24) % 256
  s[1] = (self >> 16) % 256
  s[2] = (self >>  8) % 256
  s[3] = (self      ) % 256
  s
end