Class: ReDNS::Buffer
- Inherits:
-
String
- Object
- String
- ReDNS::Buffer
- Defined in:
- lib/redns/buffer.rb
Instance Attribute Summary collapse
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#size ⇒ Object
(also: #total_size, #length)
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #advance(chars = 1) ⇒ Object
- #append(contents, length = nil) ⇒ Object
-
#initialize(contents = nil, offset = nil, size = nil) ⇒ Buffer
constructor
Create a buffer with arbitrary String contents.
- #inspect ⇒ Object
- #pack(contents, format) ⇒ Object
- #read(chars = 1) ⇒ Object
- #restore_state {|_self| ... } ⇒ Object
- #rewind(chars = nil) ⇒ Object
- #to_s ⇒ Object
- #unpack(format) ⇒ Object
- #write(contents, length = nil) ⇒ Object
Constructor Details
#initialize(contents = nil, offset = nil, size = nil) ⇒ Buffer
Create a buffer with arbitrary String contents. The offset parameter indicates where to start reading, which defaults to character 0 at the start of the string. The size parameter is used to limit how much of the content is used
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/redns/buffer.rb', line 22 def initialize(contents = nil, offset = nil, size = nil) if (contents.respond_to?(:serialize)) super('') @offset = 0 @size = total_length contents.serialize(self) else super(contents || '') @offset = offset ? offset.to_i : 0 @size = size ? size.to_i : nil end advance(0) end |
Instance Attribute Details
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
9 10 11 |
# File 'lib/redns/buffer.rb', line 9 def offset @offset end |
#size ⇒ Object (readonly) Also known as: total_size, length
Returns the value of attribute size.
10 11 12 |
# File 'lib/redns/buffer.rb', line 10 def size @size end |
Instance Method Details
#advance(chars = 1) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/redns/buffer.rb', line 83 def advance(chars = 1) if (chars < 0) rewind(-chars) else @offset += chars if (@offset > total_length) @offset = total_length end max_length = (total_length - @offset) if (!@size or @size > max_length) @size = max_length end end @size ||= total_length - @offset self end |
#append(contents, length = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/redns/buffer.rb', line 73 def append(contents, length = nil) insertion = length ? contents[0, length] : contents self << insertion @size += insertion.length self end |
#inspect ⇒ Object
138 139 140 |
# File 'lib/redns/buffer.rb', line 138 def inspect "\#<#{self.class} [#{to_s.bytes.collect { |c| '%02x' % c }.join(' ')}]>" end |
#pack(contents, format) ⇒ Object
53 54 55 |
# File 'lib/redns/buffer.rb', line 53 def pack(contents, format) append(contents.pack(format)) end |
#read(chars = 1) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/redns/buffer.rb', line 57 def read(chars = 1) return if (@offset + chars > total_length) result = to_str[@offset, chars] advance(chars) result end |
#restore_state {|_self| ... } ⇒ Object
126 127 128 129 130 131 132 |
# File 'lib/redns/buffer.rb', line 126 def restore_state offset = @offset size = @size yield(self) if (block_given?) @offset = offset @size = size end |
#rewind(chars = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/redns/buffer.rb', line 105 def rewind(chars = nil) if (chars) if (chars < 0) advance(-chars) else @offset -= chars @size += chars if (@offset < 0) @size += @offset @offset = 0 end end else @size += @offset @offset = 0 end self end |
#to_s ⇒ Object
134 135 136 |
# File 'lib/redns/buffer.rb', line 134 def to_s to_str[@offset, @size] end |
#unpack(format) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/redns/buffer.rb', line 40 def unpack(format) return [ ] if (@size <= 0) return if (@offset > total_length) data = to_s.unpack(format) advance(data.pack(format).length) data rescue TypeError [ ] end |
#write(contents, length = nil) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/redns/buffer.rb', line 65 def write(contents, length = nil) insertion = length ? contents[0, length] : contents self[@offset, 0] = insertion advance(insertion.length) end |