Class: PostgresPR::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/postgres-pr/buffer.rb

Overview

Fixed size buffer.

Defined Under Namespace

Classes: EOF, Error

Constant Summary collapse

NUL =
"\000"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Buffer

Returns a new instance of Buffer.



12
13
14
15
16
# File 'lib/postgres-pr/buffer.rb', line 12

def initialize(content)
  @size = content.size
  @content = content
  @position = 0
end

Class Method Details

.of_size(size) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
# File 'lib/postgres-pr/buffer.rb', line 7

def self.of_size(size)
  raise ArgumentError if size < 0
  new('#' * size)
end

Instance Method Details

#at_end?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/postgres-pr/buffer.rb', line 31

def at_end?
  @position == @size
end

#contentObject



35
36
37
# File 'lib/postgres-pr/buffer.rb', line 35

def content
  @content
end

#copy_from_stream(stream, n) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
# File 'lib/postgres-pr/buffer.rb', line 54

def copy_from_stream(stream, n)
  raise ArgumentError if n < 0
  while n > 0
    str = stream.read(n) 
    write(str)
    n -= str.size
  end
  raise if n < 0 
end

#positionObject



22
23
24
# File 'lib/postgres-pr/buffer.rb', line 22

def position
  @position
end

#position=(new_pos) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
# File 'lib/postgres-pr/buffer.rb', line 26

def position=(new_pos)
  raise ArgumentError if new_pos < 0 or new_pos > @size
  @position = new_pos
end

#read(n) ⇒ Object

Raises:



39
40
41
42
43
44
# File 'lib/postgres-pr/buffer.rb', line 39

def read(n)
  raise EOF, 'cannot read beyond the end of buffer' if @position + n > @size
  str = @content[@position, n]
  @position += n
  str
end

#read_byteObject



86
87
88
# File 'lib/postgres-pr/buffer.rb', line 86

def read_byte
  ru(1, 'C')
end

#read_cstringObject

returns a Ruby string without the trailing NUL character

Raises:



73
74
75
76
77
78
79
80
81
# File 'lib/postgres-pr/buffer.rb', line 73

def read_cstring
  nul_pos = @content.index(NUL, @position)
  raise Error, "no cstring found!" unless nul_pos

  sz = nul_pos - @position
  str = @content[@position, sz]
  @position += sz + 1
  return str
end

#read_int16Object



90
91
92
# File 'lib/postgres-pr/buffer.rb', line 90

def read_int16
  ru_swap(2, 's') 
end

#read_int32Object



94
95
96
# File 'lib/postgres-pr/buffer.rb', line 94

def read_int32
  ru_swap(4, 'l') 
end

#sizeObject



18
19
20
# File 'lib/postgres-pr/buffer.rb', line 18

def size
  @size
end

#write(str) ⇒ Object

Raises:



46
47
48
49
50
51
52
# File 'lib/postgres-pr/buffer.rb', line 46

def write(str)
  sz = str.size
  raise EOF, 'cannot write beyond the end of buffer' if @position + sz > @size
  @content[@position, sz] = str
  @position += sz
  self
end

#write_byte(val) ⇒ Object



98
99
100
# File 'lib/postgres-pr/buffer.rb', line 98

def write_byte(val)
  pw(val, 'C')
end

#write_cstring(cstr) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/postgres-pr/buffer.rb', line 66

def write_cstring(cstr)
  raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?(NUL)
  write(cstr)
  write(NUL)
end

#write_int32(val) ⇒ Object



102
103
104
# File 'lib/postgres-pr/buffer.rb', line 102

def write_int32(val)
  pw(val, 'N')
end