Class: Vertx::RecordParser

Inherits:
Object
  • Object
show all
Defined in:
src/main/api_shim/core/parsetools.rb

Overview

A helper class which allows you to easily parse protocols which are delimited by a sequence of bytes, or fixed size records. Instances of this class take as input Buffer instances containing raw bytes, and output records. For example, if I had a simple ASCII text protocol delimited by '\n' and the input was the following: buffer1:HELLO\nHOW ARE Y buffer2:OU?\nI AM buffer3: DOING OK buffer4:\n Then the output would be: buffer1:HELLO buffer2:HOW ARE YOU? buffer3:I AM DOING OK Instances of this class can be changed between delimited mode and fixed size record mode on the fly as individual records are read, this allows you to parse protocols where, for example, the first 5 records might all be fixed size (of potentially different sizes), followed by some delimited records, followed by more fixed size records. Instances of this class can't currently be used for protocols where the text is encoded with something other than a 1-1 byte-char mapping.

Author:

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (RecordParser) new_delimited(delim, proc = nil, &output_block)

Create a new RecordParser instance, initially in delimited mode, and where the delimiter can be represented by a delimiter string endcoded in latin-1 . Don't use this if your String contains other than latin-1 characters.

Parameters:

  • delim. (String)
    The delimiter string.
  • proc (Proc) (defaults to: nil)
    A proc to be used as the output handler
  • output_block (Block)
    A block to be used as the output handler

Returns:



74
75
76
77
# File 'src/main/api_shim/core/parsetools.rb', line 74

def RecordParser.new_delimited(delim, proc = nil, &output_block)
  output_block = proc if proc
  RecordParser.new(org.vertx.java.core.parsetools.RecordParser.newDelimited(delim, output_block))
end

+ (RecordParser) new_fixed(size, proc = nil, &output_block)

Create a new RecordParser instance, initially in fixed size mode.

Parameters:

  • size. (FixNum)
    The initial record size.
  • proc (Proc) (defaults to: nil)
    A proc to be used as the output handler
  • output_block (Block)
    A block to be used as the output handler

Returns:



84
85
86
87
# File 'src/main/api_shim/core/parsetools.rb', line 84

def RecordParser.new_fixed(size, proc = nil, &output_block)
  output_block = proc if proc
  RecordParser.new(org.vertx.java.core.parsetools.RecordParser.newFixed(size, output_block))
end

Instance Method Details

- (Object) delimited_mode(delim)

Flip the parser into delimited mode. This method can be called multiple times with different values of delim while data is being parsed.

Parameters:

  • delim. (String)
    The delimiter string.


92
93
94
# File 'src/main/api_shim/core/parsetools.rb', line 92

def delimited_mode(delim)
  @java_parser.delimitedMode(delim)
end

- (Object) fixed_size_mode(size)

Flip the parser into fixed size mode. This method can be called multiple times with different values of size while data is being parsed.

Parameters:

  • size. (FixNum)
    The record size.


99
100
101
# File 'src/main/api_shim/core/parsetools.rb', line 99

def fixed_size_mode(size)
  @java_parser.fixedSizeMode(size)
end

- (Object) input(data)

This method is called to provide the parser with data.

Parameters:

  • data. (Buffer)
    Input data to the parser.


64
65
66
# File 'src/main/api_shim/core/parsetools.rb', line 64

def input(data)
  @java_parser.handle(data._to_java_buffer)
end

- (Object) to_proc

Convert it to a Proc


56
57
58
59
60
# File 'src/main/api_shim/core/parsetools.rb', line 56

def to_proc
  return Proc.new do |data|
    input(data)
  end
end