Class: MysqlReplicator::StringIOUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_replicator/string_io_util.rb

Class Method Summary collapse

Class Method Details

.read_array_from_int8(io, length) ⇒ Object



194
195
196
197
# File 'lib/mysql_replicator/string_io_util.rb', line 194

def self.read_array_from_int8(io, length)
  value = io.read(length) || ''
  value.unpack('C*').map(&:to_i)
end

.read_double64(io) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/mysql_replicator/string_io_util.rb', line 185

def self.read_double64(io)
  value = io.read(8)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('E').to_f
end

.read_float32(io) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/mysql_replicator/string_io_util.rb', line 174

def self.read_float32(io)
  value = io.read(4)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('e').to_f
end

.read_int16(io) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/mysql_replicator/string_io_util.rb', line 55

def self.read_int16(io)
  value = io.read(2)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('s<').to_i
end

.read_int24(io) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/mysql_replicator/string_io_util.rb', line 77

def self.read_int24(io)
  payload = io.read(3)
  if payload.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  bytes = payload.unpack('C3').map(&:to_i)
  value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16)
  value >= 0x800000 ? value - 0x1000000 : value
end

.read_int32(io) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/mysql_replicator/string_io_util.rb', line 102

def self.read_int32(io)
  value = io.read(4)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('l<').to_i
end

.read_int48(io) ⇒ Object



135
136
137
138
139
140
# File 'lib/mysql_replicator/string_io_util.rb', line 135

def self.read_int48(io)
  low = read_int32(io)
  high = read_int16(io)
  combined = low | (high << 32)
  combined >= 0x800000000000 ? combined - 0x1000000000000 : combined
end

.read_int64(io) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/mysql_replicator/string_io_util.rb', line 152

def self.read_int64(io)
  value = io.read(8)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('q<').to_i
end

.read_int8(io) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/mysql_replicator/string_io_util.rb', line 33

def self.read_int8(io)
  value = io.read(1)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('c').to_i
end

.read_packed_integer(io) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mysql_replicator/string_io_util.rb', line 14

def self.read_packed_integer(io)
  first = read_uint8(io)

  case first
  when 0..250
    first
  when 252
    read_uint16(io)
  when 253
    read_uint24(io)
  when 254
    read_uint64(io)
  else
    raise "Invalid packed integer: #{first}"
  end
end

.read_str(io, length) ⇒ Object



8
9
10
# File 'lib/mysql_replicator/string_io_util.rb', line 8

def self.read_str(io, length)
  io.read(length) || ''
end

.read_uint16(io) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/mysql_replicator/string_io_util.rb', line 66

def self.read_uint16(io)
  value = io.read(2)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('v').to_i
end

.read_uint24(io) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/mysql_replicator/string_io_util.rb', line 90

def self.read_uint24(io)
  payload = io.read(3)
  if payload.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  bytes = payload.unpack('C3').map(&:to_i)
  bytes[0] | (bytes[1] << 8) | (bytes[2] << 16)
end

.read_uint32(io) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/mysql_replicator/string_io_util.rb', line 113

def self.read_uint32(io)
  value = io.read(4)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('V').to_i
end

.read_uint32_big_endian(io) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/mysql_replicator/string_io_util.rb', line 124

def self.read_uint32_big_endian(io)
  value = io.read(4)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('N').to_i
end

.read_uint48(io) ⇒ Object



144
145
146
147
148
# File 'lib/mysql_replicator/string_io_util.rb', line 144

def self.read_uint48(io)
  low = read_uint32(io)
  high = read_uint16(io)
  low | (high << 32)
end

.read_uint64(io) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/mysql_replicator/string_io_util.rb', line 163

def self.read_uint64(io)
  value = io.read(8)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('Q<').to_i
end

.read_uint8(io) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/mysql_replicator/string_io_util.rb', line 44

def self.read_uint8(io)
  value = io.read(1)
  if value.nil?
    raise MysqlReplicator::Error, 'payload is nil'
  end

  value.unpack1('C').to_i
end