Interface RecordParser
-
- All Superinterfaces:
Handler<Buffer>
,ReadStream<Buffer>
,StreamBase
public interface RecordParser extends Handler<Buffer>, ReadStream<Buffer>
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.
Please see the documentation for more information.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description void
delimitedMode(Buffer delim)
Flip the parser into delimited mode, and where the delimiter can be represented by the delimiterdelim
.void
delimitedMode(String delim)
Flip the parser into delimited mode, and where the delimiter can be represented by the Stringdelim
encoded in latin-1 .RecordParser
endHandler(Handler<Void> endHandler)
Set an end handler.RecordParser
exceptionHandler(Handler<Throwable> handler)
Set an exception handler on the read stream.RecordParser
fetch(long amount)
Fetch the specifiedamount
of elements.void
fixedSizeMode(int size)
Flip the parser into fixed size mode, where the record size is specified bysize
in bytes.void
handle(Buffer buffer)
This method is called to provide the parser with data.RecordParser
handler(Handler<Buffer> handler)
Set a data handler.RecordParser
maxRecordSize(int size)
Set the maximum allowed size for a record when using the delimited mode.static RecordParser
newDelimited(Buffer delim)
Create a newRecordParser
instance, initially in delimited mode, and where the delimiter can be represented by theBuffer
delim.static RecordParser
newDelimited(Buffer delim, Handler<Buffer> output)
LikenewDelimited(Buffer)
but set theoutput
that will receive whole records which have been parsed.static RecordParser
newDelimited(Buffer delim, ReadStream<Buffer> stream)
LikenewDelimited(Buffer)
but wraps thestream
.static RecordParser
newDelimited(String delim)
Create a newRecordParser
instance, initially in delimited mode, and where the delimiter can be represented by the String delim endcoded in latin-1 .static RecordParser
newDelimited(String delim, Handler<Buffer> output)
LikenewDelimited(String)
but set theoutput
that will receive whole records which have been parsed.static RecordParser
newDelimited(String delim, ReadStream<Buffer> stream)
LikenewDelimited(String)
but wraps thestream
.static RecordParser
newFixed(int size)
Create a newRecordParser
instance, initially in fixed size mode, and where the record size is specified by thesize
parameter.static RecordParser
newFixed(int size, Handler<Buffer> output)
LikenewFixed(int)
but set theoutput
that will receive whole records which have been parsed.static RecordParser
newFixed(int size, ReadStream<Buffer> stream)
LikenewFixed(int)
but wraps thestream
.RecordParser
pause()
Pause theReadStream
, it sets the buffer infetch
mode and clears the actual demand.RecordParser
resume()
Resume reading, and sets the buffer inflowing
mode.void
setOutput(Handler<Buffer> output)
-
Methods inherited from interface io.vertx.core.streams.ReadStream
collect, pipe, pipeTo
-
-
-
-
Method Detail
-
newDelimited
static RecordParser newDelimited(String delim, Handler<Buffer> output)
LikenewDelimited(String)
but set theoutput
that will receive whole records which have been parsed.- Parameters:
delim
- the initial delimiter stringoutput
- handler that will receive the output
-
newDelimited
static RecordParser newDelimited(String delim, ReadStream<Buffer> stream)
LikenewDelimited(String)
but wraps thestream
. Thestream
handlers will be set/unset when thehandler(Handler)
is set. Thepause()
/resume()
operations are propagated to thestream
.- Parameters:
delim
- the initial delimiter stringstream
- the wrapped stream
-
newDelimited
static RecordParser newDelimited(String delim)
Create a newRecordParser
instance, initially in delimited mode, and where the delimiter can be represented by the String delim endcoded in latin-1 . Don't use this if your String contains other than latin-1 characters.output
Will receive whole records which have been parsed.- Parameters:
delim
- the initial delimiter string
-
newDelimited
static RecordParser newDelimited(Buffer delim)
Create a newRecordParser
instance, initially in delimited mode, and where the delimiter can be represented by theBuffer
delim.- Parameters:
delim
- the initial delimiter buffer
-
newDelimited
static RecordParser newDelimited(Buffer delim, Handler<Buffer> output)
LikenewDelimited(Buffer)
but set theoutput
that will receive whole records which have been parsed.- Parameters:
delim
- the initial delimiter bufferoutput
- handler that will receive the output
-
newDelimited
static RecordParser newDelimited(Buffer delim, ReadStream<Buffer> stream)
LikenewDelimited(Buffer)
but wraps thestream
. Thestream
handlers will be set/unset when thehandler(Handler)
is set. Thepause()
/resume()
operations are propagated to thestream
.- Parameters:
delim
- the initial delimiter bufferstream
- the wrapped stream
-
newFixed
static RecordParser newFixed(int size)
Create a newRecordParser
instance, initially in fixed size mode, and where the record size is specified by thesize
parameter.output
Will receive whole records which have been parsed.- Parameters:
size
- the initial record size
-
newFixed
static RecordParser newFixed(int size, Handler<Buffer> output)
LikenewFixed(int)
but set theoutput
that will receive whole records which have been parsed.- Parameters:
size
- the initial record sizeoutput
- handler that will receive the output
-
newFixed
static RecordParser newFixed(int size, ReadStream<Buffer> stream)
LikenewFixed(int)
but wraps thestream
. Thestream
handlers will be set/unset when thehandler(Handler)
is set. Thepause()
/resume()
operations are propagated to thestream
.- Parameters:
size
- the initial record sizestream
- the wrapped stream
-
delimitedMode
void delimitedMode(String delim)
Flip the parser into delimited mode, and where the delimiter can be represented by the Stringdelim
encoded in latin-1 . Don't use this if your String contains other than latin-1 characters.This method can be called multiple times with different values of delim while data is being parsed.
- Parameters:
delim
- the new delimeter
-
delimitedMode
void delimitedMode(Buffer delim)
Flip the parser into delimited mode, and where the delimiter can be represented by the delimiterdelim
.This method can be called multiple times with different values of delim while data is being parsed.
- Parameters:
delim
- the new delimiter
-
fixedSizeMode
void fixedSizeMode(int size)
Flip the parser into fixed size mode, where the record size is specified bysize
in bytes.This method can be called multiple times with different values of size while data is being parsed.
- Parameters:
size
- the new record size
-
maxRecordSize
RecordParser maxRecordSize(int size)
Set the maximum allowed size for a record when using the delimited mode. The delimiter itself does not count for the record size.If a record is longer than specified, an
IllegalStateException
will be thrown.- Parameters:
size
- the maximum record size- Returns:
- a reference to this, so the API can be used fluently
-
handle
void handle(Buffer buffer)
This method is called to provide the parser with data.
-
exceptionHandler
RecordParser exceptionHandler(Handler<Throwable> handler)
Description copied from interface:ReadStream
Set an exception handler on the read stream.- Specified by:
exceptionHandler
in interfaceReadStream<Buffer>
- Specified by:
exceptionHandler
in interfaceStreamBase
- Parameters:
handler
- the exception handler- Returns:
- a reference to this, so the API can be used fluently
-
handler
RecordParser handler(Handler<Buffer> handler)
Description copied from interface:ReadStream
Set a data handler. As data is read, the handler will be called with the data.- Specified by:
handler
in interfaceReadStream<Buffer>
- Returns:
- a reference to this, so the API can be used fluently
-
pause
RecordParser pause()
Description copied from interface:ReadStream
Pause theReadStream
, it sets the buffer infetch
mode and clears the actual demand.While it's paused, no data will be sent to the data
handler
.- Specified by:
pause
in interfaceReadStream<Buffer>
- Returns:
- a reference to this, so the API can be used fluently
-
fetch
RecordParser fetch(long amount)
Description copied from interface:ReadStream
Fetch the specifiedamount
of elements. If theReadStream
has been paused, reading will recommence with the specifiedamount
of items, otherwise the specifiedamount
will be added to the current stream demand.- Specified by:
fetch
in interfaceReadStream<Buffer>
- Returns:
- a reference to this, so the API can be used fluently
-
resume
RecordParser resume()
Description copied from interface:ReadStream
Resume reading, and sets the buffer inflowing
mode. If theReadStream
has been paused, reading will recommence on it.- Specified by:
resume
in interfaceReadStream<Buffer>
- Returns:
- a reference to this, so the API can be used fluently
-
endHandler
RecordParser endHandler(Handler<Void> endHandler)
Description copied from interface:ReadStream
Set an end handler. Once the stream has ended, and there is no more data to be read, this handler will be called.- Specified by:
endHandler
in interfaceReadStream<Buffer>
- Returns:
- a reference to this, so the API can be used fluently
-
-