class PostgresPR::Utils::CustomBuffer
Constants
- NUL
Public Class Methods
from_string(str)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 107 def self.from_string(str) new(str) end
new(content)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 116 def initialize(content) self.init_buffer content end
of_size(size)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 111 def self.of_size(size) raise ArgumentError if size < 0 new('#' * size) end
Public Instance Methods
at_end?()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 142 def at_end? @position == @size end
close()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 126 def close end
content()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 146 def content @content end
copy_from_stream(stream, n)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 187 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
init_buffer(content)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 120 def init_buffer(content) @size = content.size @content = content @position = 0 end
position()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 133 def position @position end
position=(new_pos)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 137 def position=(new_pos) raise ArgumentError if new_pos < 0 or new_pos > @size @position = new_pos end
read(n)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 150 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_cstring()
click to toggle source
returns a Ruby string without the trailing NUL
character
# File lib/postgres-pr/utils/buffer.rb, line 228 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_int16_network()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 173 def read_int16_network pos = @position raise EOF, 'cannot read beyond the end of buffer' if pos + 2 > @size @position += 2 @content.get_int16_network(pos) end
read_int32_network()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 180 def read_int32_network pos = @position raise EOF, 'cannot read beyond the end of buffer' if pos + 4 > @size @position += 4 @content.get_int32_network(pos) end
read_rest()
click to toggle source
read till the end of the buffer
# File lib/postgres-pr/utils/buffer.rb, line 239 def read_rest read(self.size-@position) end
readbyte()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 165 def readbyte raise EOF, 'cannot read beyond the end of buffer' if @position >= @size byte = @content.getbyte(@position) @position += 1 byte end
Also aliased as: read_byte
size()
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 129 def size @size end
write(str)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 157 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_cstring(cstr)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 221 def write_cstring(cstr) raise ArgumentError, "Invalid Ruby/cstring" if cstr.include?(NUL) write(cstr) write(NUL) end
write_int16_network(int16)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 205 def write_int16_network(int16) raise EOF, 'cannot write beyond the end of buffer' if @position + 2 > @size @content[@position, 2] = [int16].pack('n') @position += 2 self end
write_int32_network(int32)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 212 def write_int32_network(int32) raise EOF, 'cannot write beyond the end of buffer' if @position + 4 > @size @content[@position, 4] = [int32].pack('N') @position += 4 self end
writebyte(byte)
click to toggle source
# File lib/postgres-pr/utils/buffer.rb, line 197 def writebyte(byte) raise EOF, 'cannot write beyond the end of buffer' if @position >= @size @content.setbyte(@position, byte) @position += 1 self end
Also aliased as: write_byte