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



19
20
21
22
# File 'lib/http/2/next/buffer.rb', line 19

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

Instance Method Details

#+(other) ⇒ Object



52
53
54
# File 'lib/http/2/next/buffer.rb', line 52

def +(other)
  @buffer += other
end

#==(other) ⇒ Object



48
49
50
# File 'lib/http/2/next/buffer.rb', line 48

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

#force_encoding(*args) ⇒ Object



44
45
46
# File 'lib/http/2/next/buffer.rb', line 44

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

#getbyteObject

Emulate StringIO#getbyte: slice first byte from buffer.



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

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



27
28
29
# File 'lib/http/2/next/buffer.rb', line 27

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

#read_uint32Integer

Slice unsigned 32-bit integer from buffer.

Returns:

  • (Integer)


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

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

#readbyte(n) ⇒ Object

Emulate String#getbyte: return nth byte from buffer.



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

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

#slice(*args) ⇒ Object



40
41
42
# File 'lib/http/2/next/buffer.rb', line 40

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

#slice!(*args) ⇒ Object



36
37
38
# File 'lib/http/2/next/buffer.rb', line 36

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