Class: PostgresPR::Utils::NativeBuffer

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

Constant Summary collapse

NUL =
"\000"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.of_size(size) ⇒ Object



43
44
45
# File 'lib/postgres-pr/utils/buffer.rb', line 43

def self.of_size(size)
  new('#'*size, 'r+')
end

Instance Method Details

#copy_from_stream(stream, n) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
# File 'lib/postgres-pr/utils/buffer.rb', line 76

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

#read_cstringObject

returns a Ruby string without the trailing NUL character



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

def read_cstring
  s = readline(NUL)
  s.chop!
  s
end

#read_int16_networkObject



56
57
58
59
# File 'lib/postgres-pr/utils/buffer.rb', line 56

def read_int16_network
  byte1, byte2 = readbyte, readbyte
  (byte1 < 128 ? byte1 : byte1 - 256) * 256 + byte2
end

#read_int32_networkObject



60
61
62
63
64
# File 'lib/postgres-pr/utils/buffer.rb', line 60

def read_int32_network
  byte1, byte2 = readbyte, readbyte
  byte3, byte4 = readbyte, readbyte
  ((((byte1 < 128 ? byte1 : byte1 - 256) * 256 + byte2) * 256) + byte3) * 256 + byte4
end

#write_byte(byte) ⇒ Object



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

def write_byte(byte)
  write(byte.chr)
end

#write_cstring(cstr) ⇒ Object

Raises:

  • (ArgumentError)


87
88
89
90
91
# File 'lib/postgres-pr/utils/buffer.rb', line 87

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

#write_int16_network(int16) ⇒ Object



72
73
74
# File 'lib/postgres-pr/utils/buffer.rb', line 72

def write_int16_network(int16)
  write([int16].pack('n'))
end

#write_int32_network(int32) ⇒ Object



69
70
71
# File 'lib/postgres-pr/utils/buffer.rb', line 69

def write_int32_network(int32)
  write([int32].pack('N'))
end