Class: String

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

Overview

we violate the users’ namespaces here. but it’s not in too egregious of a way, and it’s a royal pita to remove, so i’m keeping it in for the time being.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bencoded?(c) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/rubytorrent/bencoding.rb', line 65

def self.bencoded?(c)
  (?0 .. ?9).include? c
end

.parse_bencoding(c, s) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubytorrent/bencoding.rb', line 69

def self.parse_bencoding(c, s)
  lens = c.chr
  while ((x = s.getc) != ?:)
    unless (?0 .. ?9).include? x
      s.ungetc x
      raise RubyTorrent::BEncodingError, "invalid bencoded string length #{lens} + #{x}" 
    end
    lens += x.chr
  end
  raise RubyTorrent::BEncodingError, %{invalid length #{lens} in bencoded string} unless lens.length <= 20
  len = lens.to_i
  raise RubyTorrent::BEncodingError, %{invalid length #{lens} in bencoded string} unless len >= 0
  (len > 0 ? s.read(len) : "")
end

Instance Method Details

#from_fbbeObject

four-byte big-endian integer



18
19
20
21
# File 'lib/rubytorrent/message.rb', line 18

def from_fbbe # four-byte big-endian integer
  raise "fbbe must be four-byte string (got #{self.inspect})" unless length == 4
  (self[0] << 24) + (self[1] << 16) + (self[2] << 8) + self[3]
end

#to_bencodingObject



61
62
63
# File 'lib/rubytorrent/bencoding.rb', line 61

def to_bencoding
  self.length.to_s + ":" + self.to_s
end