Class: DatawireQuarkCore::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/datawire-quark-core.rb

Defined Under Namespace

Modules: BE, LE

Constant Summary collapse

BIN =
Encoding::ASCII_8BIT
UTF8 =
Encoding::UTF_8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Buffer

Returns a new instance of Buffer.



1156
1157
1158
1159
# File 'lib/datawire-quark-core.rb', line 1156

def initialize(data)
  @data = data.dup.force_encoding BIN
  @ord = BE
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



1155
1156
1157
# File 'lib/datawire-quark-core.rb', line 1155

def data
  @data
end

Instance Method Details

#capacityObject



1160
1161
1162
# File 'lib/datawire-quark-core.rb', line 1160

def capacity
  @data.length
end

#getByte(index) ⇒ Object



1172
1173
1174
# File 'lib/datawire-quark-core.rb', line 1172

def getByte(index)
  @data[index].bytes[0]
end

#getFloat(index) ⇒ Object



1200
1201
1202
# File 'lib/datawire-quark-core.rb', line 1200

def getFloat(index)
  @data[index..-1].unpack(@ord::FLOAT)[0]
end

#getInt(index) ⇒ Object



1188
1189
1190
# File 'lib/datawire-quark-core.rb', line 1188

def getInt(index)
  @data[index..-1].unpack(@ord::INT)[0]
end

#getLong(index) ⇒ Object



1194
1195
1196
# File 'lib/datawire-quark-core.rb', line 1194

def getLong(index)
  @data[index..-1].unpack(@ord::LONG)[0]
end

#getShort(index) ⇒ Object



1182
1183
1184
# File 'lib/datawire-quark-core.rb', line 1182

def getShort(index)
  @data[index..-1].unpack(@ord::SHORT)[0]
end

#getSlice(index, length) ⇒ Object



1206
1207
1208
# File 'lib/datawire-quark-core.rb', line 1206

def getSlice(index, length)
  return Buffer.new(@data[index...index+length])
end

#getStringUTF8(index, length) ⇒ Object



1169
1170
1171
# File 'lib/datawire-quark-core.rb', line 1169

def getStringUTF8(index, length)
  @data[index...index+length].force_encoding UTF8
end

#inspectObject



1209
1210
1211
# File 'lib/datawire-quark-core.rb', line 1209

def inspect
  "Buffer(%s)" % Codec.new.toHexdump(self, 0, @data.length, 3)
end

#littleEndianObject



1178
1179
1180
1181
# File 'lib/datawire-quark-core.rb', line 1178

def littleEndian
  @ord = LE
  self
end

#putByte(index, value) ⇒ Object



1175
1176
1177
# File 'lib/datawire-quark-core.rb', line 1175

def putByte(index, value)
  @data[index] = [value].pack("C")
end

#putFloat(index, value) ⇒ Object



1203
1204
1205
# File 'lib/datawire-quark-core.rb', line 1203

def putFloat(index, value)
  @data[index...index+8] = [value].pack(@ord::FLOAT)
end

#putInt(index, value) ⇒ Object



1191
1192
1193
# File 'lib/datawire-quark-core.rb', line 1191

def putInt(index, value)
  @data[index...index+4] = [value].pack(@ord::INT)
end

#putLong(index, value) ⇒ Object



1197
1198
1199
# File 'lib/datawire-quark-core.rb', line 1197

def putLong(index, value)
  @data[index...index+8] = [value].pack(@ord::LONG)
end

#putShort(index, value) ⇒ Object



1185
1186
1187
# File 'lib/datawire-quark-core.rb', line 1185

def putShort(index, value)
  @data[index...index+2] = [value].pack(@ord::SHORT)
end

#putStringUTF8(index, value) ⇒ Object



1163
1164
1165
1166
1167
1168
# File 'lib/datawire-quark-core.rb', line 1163

def putStringUTF8(index, value)
  value = value.dup.force_encoding BIN
  len = value.length
  @data[index...index+len] = value
  len
end