Class: PostgresPR::Buffer
- Inherits:
-
Object
- Object
- PostgresPR::Buffer
- Defined in:
- lib/postgres-pr/buffer.rb
Overview
Fixed size buffer.
Defined Under Namespace
Constant Summary collapse
- NUL =
"\000"
Class Method Summary collapse
Instance Method Summary collapse
- #at_end? ⇒ Boolean
- #content ⇒ Object
- #copy_from_stream(stream, n) ⇒ Object
-
#initialize(content) ⇒ Buffer
constructor
A new instance of Buffer.
- #position ⇒ Object
- #position=(new_pos) ⇒ Object
- #read(n) ⇒ Object
- #read_byte ⇒ Object
-
#read_cstring ⇒ Object
returns a Ruby string without the trailing NUL character.
- #read_int16 ⇒ Object
- #read_int32 ⇒ Object
- #size ⇒ Object
- #write(str) ⇒ Object
- #write_byte(val) ⇒ Object
- #write_cstring(cstr) ⇒ Object
- #write_int32(val) ⇒ Object
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
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
31 32 33 |
# File 'lib/postgres-pr/buffer.rb', line 31 def at_end? @position == @size end |
#content ⇒ Object
35 36 37 |
# File 'lib/postgres-pr/buffer.rb', line 35 def content @content end |
#copy_from_stream(stream, n) ⇒ Object
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 |
#position ⇒ Object
22 23 24 |
# File 'lib/postgres-pr/buffer.rb', line 22 def position @position end |
#position=(new_pos) ⇒ Object
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
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_byte ⇒ Object
86 87 88 |
# File 'lib/postgres-pr/buffer.rb', line 86 def read_byte ru(1, 'C') end |
#read_cstring ⇒ Object
returns a Ruby string without the trailing NUL character
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_int16 ⇒ Object
90 91 92 |
# File 'lib/postgres-pr/buffer.rb', line 90 def read_int16 ru_swap(2, 's') end |
#read_int32 ⇒ Object
94 95 96 |
# File 'lib/postgres-pr/buffer.rb', line 94 def read_int32 ru_swap(4, 'l') end |
#size ⇒ Object
18 19 20 |
# File 'lib/postgres-pr/buffer.rb', line 18 def size @size end |
#write(str) ⇒ Object
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
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 |