class XDR::VarOpaque

Public Class Methods

new(length=XDR::MAX_SIZE) click to toggle source
# File lib/xdr/var_opaque.rb, line 7
def initialize(length=XDR::MAX_SIZE)
  @length = length
end

Public Instance Methods

read(io) click to toggle source
# File lib/xdr/var_opaque.rb, line 23
def read(io)
  length = XDR::Int.read(io)

  if length > @length
    raise XDR::ReadError, "VarOpaque length #{length}"
  end

  padding = padding_for length

  # read and return length bytes
  # throw away padding bytes
  read_bytes(io, length).tap{ read_bytes(io, padding) }
end
write(val, io) click to toggle source
# File lib/xdr/var_opaque.rb, line 11
def write(val, io)
  length = val.bytesize

  if length > @length
    raise XDR::WriteError, "Value length #{length} exceeds max #{@length}"
  end

  XDR::Int.write(length, io)
  io.write val
  io.write "\x00" * padding_for(length)
end