Class: XDR::Opaque

Inherits:
Object
  • Object
show all
Includes:
Concerns::ConvertsToXDR, Concerns::StringConverter
Defined in:
lib/xdr/opaque.rb

Instance Method Summary collapse

Methods included from Concerns::StringConverter

#valid?

Methods included from Concerns::ConvertsToXDR

#from_xdr, #to_xdr, #valid?

Constructor Details

#initialize(length) ⇒ Opaque

Returns a new instance of Opaque.



7
8
9
10
# File 'lib/xdr/opaque.rb', line 7

def initialize(length)
  @length = length
  @padding = padding_for length
end

Instance Method Details

#read(io) ⇒ Object



12
13
14
15
16
# File 'lib/xdr/opaque.rb', line 12

def read(io)
  # read and return @length bytes
  # throw away @padding bytes
  read_bytes(io, @length).tap { read_zeros(io, @padding) }
end

#write(val, io) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/xdr/opaque.rb', line 18

def write(val, io)
  length = val.bytesize

  if val.length != @length
    raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
  end

  io.write val
  io.write "\x00" * @padding
end