Class: HTTP2::Buffer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/http/2/buffer.rb

Overview

Binary buffer wraps String.

Instance Method Summary collapse

Constructor Details

#initialize(str = '') ⇒ Buffer

Forces binary encoding on the string


17
18
19
20
# File 'lib/http/2/buffer.rb', line 17

def initialize(str = '')
  str = str.dup if str.frozen?
  @buffer = str.force_encoding(Encoding::BINARY)
end

Instance Method Details

#+(other) ⇒ Object


50
51
52
# File 'lib/http/2/buffer.rb', line 50

def +(other)
  @buffer += other
end

#==(other) ⇒ Object


46
47
48
# File 'lib/http/2/buffer.rb', line 46

def ==(other)
  @buffer == other
end

#force_encoding(*args) ⇒ Object


42
43
44
# File 'lib/http/2/buffer.rb', line 42

def force_encoding(*args)
  @buffer = @buffer.force_encoding(*args)
end

#getbyteObject

Emulate StringIO#getbyte: slice first byte from buffer.


30
31
32
# File 'lib/http/2/buffer.rb', line 30

def getbyte
  read(1).ord
end

#read(n) ⇒ Object

Emulate StringIO#read: slice first n bytes from the buffer.

Parameters:

  • n (Integer)

    number of bytes to slice from the buffer


25
26
27
# File 'lib/http/2/buffer.rb', line 25

def read(n)
  Buffer.new(@buffer.slice!(0, n))
end

#read_uint32Integer

Slice unsigned 32-bit integer from buffer.

Returns:

  • (Integer)

61
62
63
# File 'lib/http/2/buffer.rb', line 61

def read_uint32
  read(4).unpack(UINT32).first
end

#readbyte(n) ⇒ Object

Emulate String#getbyte: return nth byte from buffer.


55
56
57
# File 'lib/http/2/buffer.rb', line 55

def readbyte(n)
  @buffer[n].ord
end

#slice(*args) ⇒ Object


38
39
40
# File 'lib/http/2/buffer.rb', line 38

def slice(*args)
  Buffer.new(@buffer.slice(*args))
end

#slice!(*args) ⇒ Object


34
35
36
# File 'lib/http/2/buffer.rb', line 34

def slice!(*args)
  Buffer.new(@buffer.slice!(*args))
end