Class: Vertx::RecordParser
- Inherits:
-
Object
- Object
- Vertx::RecordParser
- Defined in:
- src/main/ruby_scripts/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. TODO extend this class to cope with arbitrary character encodings.
Class Method Summary (collapse)
-
+ (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 .
-
+ (RecordParser) new_fixed(size, proc = nil, &output_block)
Create a new RecordParser instance, initially in fixed size mode.
Instance Method Summary (collapse)
-
- (Object) delimited_mode(delim)
Flip the parser into delimited mode.
-
- (Object) fixed_size_mode(size)
Flip the parser into fixed size mode.
-
- (Object) input(data)
This method is called to provide the parser with data.
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.
67 68 69 70 |
# File 'src/main/ruby_scripts/core/parsetools.rb', line 67 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.
77 78 79 80 |
# File 'src/main/ruby_scripts/core/parsetools.rb', line 77 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.
85 86 87 |
# File 'src/main/ruby_scripts/core/parsetools.rb', line 85 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.
92 93 94 |
# File 'src/main/ruby_scripts/core/parsetools.rb', line 92 def fixed_size_mode(size) @java_parser.fixedSizeMode(size) end |
- (Object) input(data)
This method is called to provide the parser with data.
57 58 59 |
# File 'src/main/ruby_scripts/core/parsetools.rb', line 57 def input(data) @java_parser.handle(data._to_java_buffer) end |