Skip to content

Commit

Permalink
Added write analogues to the read methods in Bytestream.
Browse files Browse the repository at this point in the history
  • Loading branch information
threedaymonk committed Mar 6, 2010
1 parent 674b262 commit 32202b0
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/net/rtmp/bytestream.rb
Expand Up @@ -15,29 +15,56 @@ def read_uint8
read_and_unpack(1, 'C')
end

def write_uint8(value)
write [value].pack('C')
end

def read_uint16_be
read_and_unpack(2, 'n')
end

def write_uint16_be(value)
[value].pack('n')
end

def read_uint24_be
("\x00" + read(3)).unpack('N')[0]
end

def write_uint24_be(value)
write [value].pack('N')[1,3]
end

def read_uint32_le
read_and_unpack(4, 'V')
end

def write_uint32_le(value)
write [value].pack('V')
end

def read_double_be
read_and_unpack(8, 'G')
end

def write_double_be
write [value].pack('G')
end

def read_bitfield(*widths)
byte = read_uint8
shifts_and_masks(widths).map{ |shift, mask|
(byte >> shift) & mask
}
end

def write_bitfield(*values_and_widths)
sm = shifts_and_masks(values_and_widths.map{ |_,w| w })
write_uint8 values_and_widths.zip(sm).inject(0){ |byte, ((value, width), (shift, mask))|
byte | ((value & mask) << shift)
}
end

private
def read_and_unpack(length, specifier)
read(length).unpack(specifier)[0]
Expand Down

0 comments on commit 32202b0

Please sign in to comment.