Class: HTTP2Next::Buffer

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

Overview

Binary buffer wraps String.

Instance Method Summary collapse

Constructor Details

#initialize(str = "".b) ⇒ Buffer

Forces binary encoding on the string



21
22
23
24
# File 'lib/http/2/next/buffer.rb', line 21

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

Instance Method Details

#+(other) ⇒ Object



58
59
60
# File 'lib/http/2/next/buffer.rb', line 58

def +(other)
  @buffer += other
end

#==(other) ⇒ Object



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

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

#force_encoding(*args) ⇒ Object



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

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

#getbyteObject

Emulate StringIO#getbyte: slice first byte from buffer.



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

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



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

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

#read_uint32Integer

Slice unsigned 32-bit integer from buffer.

Returns:

  • (Integer)


69
70
71
# File 'lib/http/2/next/buffer.rb', line 69

def read_uint32
  read(4).unpack1(UINT32)
end

#readbyte(n) ⇒ Object

Emulate String#getbyte: return nth byte from buffer.



63
64
65
# File 'lib/http/2/next/buffer.rb', line 63

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

#slice(*args) ⇒ Object



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

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

#slice!(*args) ⇒ Object



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

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

#unpack1(*arg) ⇒ Object



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

def unpack1(*arg)
  @buffer.unpack1(*arg)
end