Class: PostgresPR::Utils::CustomBuffer
- Inherits:
-
Object
- Object
- PostgresPR::Utils::CustomBuffer
- Defined in:
- lib/postgres-pr/utils/buffer.rb
Defined Under Namespace
Constant Summary collapse
- NUL =
"\000"
Class Method Summary collapse
Instance Method Summary collapse
- #at_end? ⇒ Boolean
- #close ⇒ Object
- #content ⇒ Object
- #copy_from_stream(stream, n) ⇒ Object
- #init_buffer(content) ⇒ Object
-
#initialize(content) ⇒ CustomBuffer
constructor
A new instance of CustomBuffer.
- #position ⇒ Object
- #position=(new_pos) ⇒ Object
- #read(n) ⇒ Object
-
#read_cstring ⇒ Object
returns a Ruby string without the trailing NUL character.
- #read_int16_network ⇒ Object
- #read_int32_network ⇒ Object
-
#read_rest ⇒ Object
read till the end of the buffer.
- #readbyte ⇒ Object (also: #read_byte)
- #size ⇒ Object
- #write(str) ⇒ Object
- #write_cstring(cstr) ⇒ Object
- #write_int16_network(int16) ⇒ Object
- #write_int32_network(int32) ⇒ Object
- #writebyte(byte) ⇒ Object (also: #write_byte)
Constructor Details
#initialize(content) ⇒ CustomBuffer
Returns a new instance of CustomBuffer.
116 117 118 |
# File 'lib/postgres-pr/utils/buffer.rb', line 116 def initialize(content) self.init_buffer content end |
Class Method Details
.from_string(str) ⇒ Object
107 108 109 |
# File 'lib/postgres-pr/utils/buffer.rb', line 107 def self.from_string(str) new(str) end |
.of_size(size) ⇒ Object
111 112 113 114 |
# File 'lib/postgres-pr/utils/buffer.rb', line 111 def self.of_size(size) raise ArgumentError if size < 0 new('#' * size) end |
Instance Method Details
#at_end? ⇒ Boolean
142 143 144 |
# File 'lib/postgres-pr/utils/buffer.rb', line 142 def at_end? @position == @size end |
#close ⇒ Object
126 127 |
# File 'lib/postgres-pr/utils/buffer.rb', line 126 def close end |
#content ⇒ Object
146 147 148 |
# File 'lib/postgres-pr/utils/buffer.rb', line 146 def content @content end |
#copy_from_stream(stream, n) ⇒ Object
187 188 189 190 191 192 193 194 195 |
# 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) ⇒ Object
120 121 122 123 124 |
# File 'lib/postgres-pr/utils/buffer.rb', line 120 def init_buffer(content) @size = content.size @content = content @position = 0 end |
#position ⇒ Object
133 134 135 |
# File 'lib/postgres-pr/utils/buffer.rb', line 133 def position @position end |
#position=(new_pos) ⇒ Object
137 138 139 140 |
# 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) ⇒ Object
150 151 152 153 154 155 |
# 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 ⇒ Object
returns a Ruby string without the trailing NUL character
228 229 230 231 232 233 234 235 236 |
# 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 ⇒ Object
173 174 175 176 177 178 |
# 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 ⇒ Object
180 181 182 183 184 185 |
# 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 ⇒ Object
read till the end of the buffer
239 240 241 |
# File 'lib/postgres-pr/utils/buffer.rb', line 239 def read_rest read(self.size-@position) end |
#readbyte ⇒ Object Also known as: read_byte
165 166 167 168 169 170 |
# 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 |
#size ⇒ Object
129 130 131 |
# File 'lib/postgres-pr/utils/buffer.rb', line 129 def size @size end |
#write(str) ⇒ Object
157 158 159 160 161 162 163 |
# 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) ⇒ Object
221 222 223 224 225 |
# 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) ⇒ Object
205 206 207 208 209 210 |
# 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) ⇒ Object
212 213 214 215 216 217 |
# 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) ⇒ Object Also known as: write_byte
197 198 199 200 201 202 |
# 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 |