Class: BinData::Buffer

Inherits:
Base
  • Object
show all
Includes:
DSLMixin
Defined in:
lib/bindata/buffer.rb

Overview

A Buffer is conceptually a substream within a data stream. It has a defined size and it will always read or write the exact number of bytes to fill the buffer. Short reads will skip over unused bytes and short writes will pad the substream with “0” bytes.

require 'bindata'

obj = BinData::Buffer.new(:length => 5, :type => [:string, {:value => "abc"}])
obj.to_binary_s #=> "abc\000\000"

class MyBuffer < BinData::Buffer
  default_parameter :length => 8

  endian :little

  uint16 :num1
  uint16 :num2
  # padding occurs here
end

obj = MyBuffer.read("\001\000\002\000\000\000\000\000")
obj.num1 #=> 1
obj.num1 #=> 2
obj.num_bytes #=> 8

class StringTable < BinData::Record
  endian :little

  uint16 :table_size_in_bytes
  buffer :strings, :length => :table_size_in_bytes do
    array :read_until => :eof do
      uint8 :len
      string :str, :length => :len
    end
  end
end

Parameters

Parameters may be provided at initialisation to control the behaviour of an object. These params are:

:length

The number of bytes in the buffer.

:type

The single type inside the buffer. Use a struct if multiple fields are required.

Instance Attribute Summary

Attributes inherited from Base

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSLMixin

included

Methods inherited from Base

#==, #=~, bindata_name, #clear, #debug_name, #eval_parameter, #get_parameter, #has_parameter?, #initialize_with_warning, #inspect, #lazy_evaluator, #new, #num_bytes, #offset, #pretty_print, #read, read, register_subclasses, #rel_offset, #to_binary_s, #to_s, unregister_self, #write

Methods included from AcceptedParametersPlugin

#accepted_parameters, #default_parameters, #mandatory_parameters, #mutually_exclusive_parameters, #optional_parameters

Methods included from RegisterNamePlugin

included, #initialize_shared_instance

Methods included from CheckOrAdjustOffsetPlugin

included, #initialize_shared_instance

Methods included from Framework

#debug_name_of, included, #offset_of

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

:nodoc:



99
100
101
# File 'lib/bindata/buffer.rb', line 99

def method_missing(symbol, *args, &block) #:nodoc:
  @type.__send__(symbol, *args, &block)
end

Class Method Details

.arg_extractorObject



61
62
63
# File 'lib/bindata/buffer.rb', line 61

def arg_extractor
  MultiFieldArgExtractor
end

.sanitize_parameters!(params) ⇒ Object

:nodoc:



65
66
67
68
69
70
71
72
# File 'lib/bindata/buffer.rb', line 65

def sanitize_parameters!(params) #:nodoc:
  params.merge!(dsl_params)

  if params.needs_sanitizing?(:type)
    el_type, el_params = params[:type]
    params[:type] = params.create_sanitized_object_prototype(el_type, el_params)
  end
end

Instance Method Details

#assign(val) ⇒ Object



83
84
85
# File 'lib/bindata/buffer.rb', line 83

def assign(val)
  @type.assign(val)
end

#clear?Boolean

:nodoc:

Returns:

  • (Boolean)


79
80
81
# File 'lib/bindata/buffer.rb', line 79

def clear? #:nodoc:
  @type.clear?
end

#do_num_bytesObject

:nodoc:



115
116
117
# File 'lib/bindata/buffer.rb', line 115

def do_num_bytes #:nodoc:
  eval_parameter(:length)
end

#do_read(io) ⇒ Object

:nodoc:



103
104
105
106
107
# File 'lib/bindata/buffer.rb', line 103

def do_read(io) #:nodoc:
  io.with_buffer(eval_parameter(:length)) do
    @type.do_read(io)
  end
end

#do_write(io) ⇒ Object

:nodoc:



109
110
111
112
113
# File 'lib/bindata/buffer.rb', line 109

def do_write(io) #:nodoc:
  io.with_buffer(eval_parameter(:length)) do
    @type.do_write(io)
  end
end

#initialize_instanceObject



75
76
77
# File 'lib/bindata/buffer.rb', line 75

def initialize_instance
  @type = get_parameter(:type).instantiate(nil, self)
end

#respond_to?(symbol, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


91
92
93
# File 'lib/bindata/buffer.rb', line 91

def respond_to?(symbol, include_private = false) #:nodoc:
  @type.respond_to?(symbol, include_private) || super
end

#safe_respond_to?(symbol, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


95
96
97
# File 'lib/bindata/buffer.rb', line 95

def safe_respond_to?(symbol, include_private = false) #:nodoc:
  base_respond_to?(symbol, include_private)
end

#snapshotObject



87
88
89
# File 'lib/bindata/buffer.rb', line 87

def snapshot
  @type.snapshot
end