Module: Rack::Process::NetString

Defined in:
lib/rack/process/net_string.rb

Overview

Class Method Summary collapse

Class Method Details

.decode(str) ⇒ Object



33
34
35
36
37
# File 'lib/rack/process/net_string.rb', line 33

def decode str
  io = StringIO.new str
  io.rewind
  read io
end

.encode(str) ⇒ Object



27
28
29
30
31
# File 'lib/rack/process/net_string.rb', line 27

def encode str
  io = StringIO.new
  write io, str
  io.string
end

.read(io) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rack/process/net_string.rb', line 9

def read io
  length = ns_length io.readline ":"
  buffer = io.read length

  if io.eof?
    return
  elsif io.getc != ?,
    raise Error, "Invalid netstring length, expected to be #{length}"
  end

  buffer
end

.write(io, str) ⇒ Object



22
23
24
25
# File 'lib/rack/process/net_string.rb', line 22

def write io, str
  io << "#{str.bytesize}:" << str << ","
  io.flush
end