Class RecordParser

java.lang.Object
io.vertx.mutiny.core.parsetools.RecordParser
All Implemented Interfaces:
MutinyDelegate, io.vertx.core.Handler<io.vertx.core.buffer.Buffer>, ReadStream<io.vertx.core.buffer.Buffer>, StreamBase, Consumer<io.vertx.core.buffer.Buffer>

public class RecordParser extends Object implements Consumer<io.vertx.core.buffer.Buffer>, io.vertx.core.Handler<io.vertx.core.buffer.Buffer>, ReadStream<io.vertx.core.buffer.Buffer>, MutinyDelegate
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.

NOTE: This class has been automatically generated from the original non Mutiny-ified interface.

Author:
Tim Fox, Lars Timm
See Also:
  • RecordParser
  • Field Details

  • Constructor Details

    • RecordParser

      public RecordParser(io.vertx.core.parsetools.RecordParser delegate)
      Create a new instance of RecordParser delegating to the given (non-null) instance of RecordParser.
    • RecordParser

      public RecordParser(Object delegate)
  • Method Details

    • accept

      public void accept(io.vertx.core.buffer.Buffer item)
      Handle an item. This method is generated from the original method."
      Specified by:
      accept in interface Consumer<io.vertx.core.buffer.Buffer>
    • getDelegate

      public io.vertx.core.parsetools.RecordParser getDelegate()
      Get the delegate instance.

      This method returns the instance on which this shim is delegating the calls. And so, give you access to the bare API.

      Specified by:
      getDelegate in interface MutinyDelegate
      Specified by:
      getDelegate in interface ReadStream<io.vertx.core.buffer.Buffer>
      Specified by:
      getDelegate in interface StreamBase
      Returns:
      the delegate instance
    • pipeTo

      @CheckReturnValue public io.smallrye.mutiny.Uni<Void> pipeTo(WriteStream<io.vertx.core.buffer.Buffer> arg0)
      Pipe this ReadStream to the WriteStream.

      Elements emitted by this stream will be written to the write stream until this stream ends or fails.

      Unlike the bare Vert.x variant, this method returns a Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.

      Don't forget to subscribe on it to trigger the operation.

      Specified by:
      pipeTo in interface ReadStream<io.vertx.core.buffer.Buffer>
      Parameters:
      dst - the destination write stream
      Returns:
      A Uni representing the asynchronous result of this operation.
      See Also:
      • io.vertx.core.parsetools.RecordParser#pipeTo(WriteStream)
    • pipeToAndAwait

      public void pipeToAndAwait(WriteStream<io.vertx.core.buffer.Buffer> arg0)
      Pipe this ReadStream to the WriteStream.

      Elements emitted by this stream will be written to the write stream until this stream ends or fails.

      Unlike the bare Vert.x variant, this method returns a Void. This method awaits indefinitely for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

      Parameters:
      dst - the destination write stream
      See Also:
      • io.vertx.core.parsetools.RecordParser#pipeTo(WriteStream)
    • pipeToAndForget

      public RecordParser pipeToAndForget(WriteStream<io.vertx.core.buffer.Buffer> arg0)
      Pipe this ReadStream to the WriteStream.

      Elements emitted by this stream will be written to the write stream until this stream ends or fails.

      Unlike the bare Vert.x variant, this method ignores the Void result or any failure.

      Parameters:
      dst - the destination write stream
      Returns:
      The current instance to chain operations if needed.
      See Also:
      • io.vertx.core.parsetools.RecordParser#pipeTo(WriteStream)
    • setOutput

      public void setOutput(Consumer<io.vertx.core.buffer.Buffer> output)
    • newDelimited

      public static RecordParser newDelimited(String delim, Consumer<io.vertx.core.buffer.Buffer> output)
      Like newDelimited(String) but set the output that will receive whole records which have been parsed.
      Parameters:
      delim - the initial delimiter string
      output - handler that will receive the output
    • newDelimited

      public static RecordParser newDelimited(String delim, ReadStream<io.vertx.core.buffer.Buffer> stream)
      Like newDelimited(String) but wraps the stream. The stream handlers will be set/unset when the #handler(Handler) is set.

      The pause()/resume() operations are propagated to the stream.

      Parameters:
      delim - the initial delimiter string
      stream - the wrapped stream
    • newDelimited

      public static RecordParser newDelimited(String delim, Flow.Publisher<io.vertx.core.buffer.Buffer> stream)
      Like newDelimited(String) but wraps the stream. The stream handlers will be set/unset when the #handler(Handler) is set.

      The pause()/resume() operations are propagated to the stream.

      Parameters:
      delim - the initial delimiter string
      stream - the wrapped stream
    • newDelimited

      public static RecordParser newDelimited(String delim)
      Create a new RecordParser 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

      public static RecordParser newDelimited(io.vertx.core.buffer.Buffer delim)
      Create a new RecordParser instance, initially in delimited mode, and where the delimiter can be represented by the Buffer delim.

      Parameters:
      delim - the initial delimiter buffer
    • newDelimited

      public static RecordParser newDelimited(io.vertx.core.buffer.Buffer delim, Consumer<io.vertx.core.buffer.Buffer> output)
      Like newDelimited(Buffer) but set the output that will receive whole records which have been parsed.
      Parameters:
      delim - the initial delimiter buffer
      output - handler that will receive the output
    • newDelimited

      public static RecordParser newDelimited(io.vertx.core.buffer.Buffer delim, ReadStream<io.vertx.core.buffer.Buffer> stream)
      Like newDelimited(Buffer) but wraps the stream. The stream handlers will be set/unset when the #handler(Handler) is set.

      The pause()/resume() operations are propagated to the stream.

      Parameters:
      delim - the initial delimiter buffer
      stream - the wrapped stream
    • newDelimited

      public static RecordParser newDelimited(io.vertx.core.buffer.Buffer delim, Flow.Publisher<io.vertx.core.buffer.Buffer> stream)
      Like newDelimited(Buffer) but wraps the stream. The stream handlers will be set/unset when the #handler(Handler) is set.

      The pause()/resume() operations are propagated to the stream.

      Parameters:
      delim - the initial delimiter buffer
      stream - the wrapped stream
    • newFixed

      public static RecordParser newFixed(int size)
      Create a new RecordParser instance, initially in fixed size mode, and where the record size is specified by the size parameter.

      output Will receive whole records which have been parsed.

      Parameters:
      size - the initial record size
    • newFixed

      public static RecordParser newFixed(int size, Consumer<io.vertx.core.buffer.Buffer> output)
      Like newFixed(int) but set the output that will receive whole records which have been parsed.
      Parameters:
      size - the initial record size
      output - handler that will receive the output
    • newFixed

      public static RecordParser newFixed(int size, ReadStream<io.vertx.core.buffer.Buffer> stream)
      Like newFixed(int) but wraps the stream. The stream handlers will be set/unset when the #handler(Handler) is set.

      The pause()/resume() operations are propagated to the stream.

      Parameters:
      size - the initial record size
      stream - the wrapped stream
    • newFixed

      public static RecordParser newFixed(int size, Flow.Publisher<io.vertx.core.buffer.Buffer> stream)
      Like newFixed(int) but wraps the stream. The stream handlers will be set/unset when the #handler(Handler) is set.

      The pause()/resume() operations are propagated to the stream.

      Parameters:
      size - the initial record size
      stream - the wrapped stream
    • delimitedMode

      public void delimitedMode(String delim)
      Flip the parser into delimited mode, and where the delimiter can be represented by the String delim 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

      public void delimitedMode(io.vertx.core.buffer.Buffer delim)
      Flip the parser into delimited mode, and where the delimiter can be represented by the delimiter delim.

      This method can be called multiple times with different values of delim while data is being parsed.

      Parameters:
      delim - the new delimiter
    • fixedSizeMode

      public void fixedSizeMode(int size)
      Flip the parser into fixed size mode, where the record size is specified by size 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

      public 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

      public void handle(io.vertx.core.buffer.Buffer buffer)
      This method is called to provide the parser with data.
      Specified by:
      handle in interface io.vertx.core.Handler<io.vertx.core.buffer.Buffer>
      Parameters:
      buffer - a chunk of data
    • exceptionHandler

      public RecordParser exceptionHandler(Consumer<Throwable> handler)
      Description copied from interface: ReadStream
      Set an exception handler on the read stream.
      Specified by:
      exceptionHandler in interface ReadStream<io.vertx.core.buffer.Buffer>
      Specified by:
      exceptionHandler in interface StreamBase
      Parameters:
      handler - the exception handler. Can be null.
      Returns:
      a reference to this, so the API can be used fluently
    • handler

      public RecordParser handler(Consumer<io.vertx.core.buffer.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 interface ReadStream<io.vertx.core.buffer.Buffer>
      Returns:
      a reference to this, so the API can be used fluently
    • pause

      public RecordParser pause()
      Description copied from interface: ReadStream
      Pause the ReadStream, it sets the buffer in fetch mode and clears the actual demand.

      While it's paused, no data will be sent to the data handler.

      Specified by:
      pause in interface ReadStream<io.vertx.core.buffer.Buffer>
      Returns:
      a reference to this, so the API can be used fluently
    • fetch

      public RecordParser fetch(long amount)
      Description copied from interface: ReadStream
      Fetch the specified amount of elements. If the ReadStream has been paused, reading will recommence with the specified amount of items, otherwise the specified amount will be added to the current stream demand.
      Specified by:
      fetch in interface ReadStream<io.vertx.core.buffer.Buffer>
      Returns:
      a reference to this, so the API can be used fluently
    • resume

      public RecordParser resume()
      Description copied from interface: ReadStream
      Resume reading, and sets the buffer in flowing mode.

      If the ReadStream has been paused, reading will recommence on it.

      Specified by:
      resume in interface ReadStream<io.vertx.core.buffer.Buffer>
      Returns:
      a reference to this, so the API can be used fluently
    • endHandler

      public RecordParser endHandler(Runnable 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 interface ReadStream<io.vertx.core.buffer.Buffer>
      Returns:
      a reference to this, so the API can be used fluently
    • pipe

      public Pipe<io.vertx.core.buffer.Buffer> pipe()
      Pause this stream and return a Pipe to transfer the elements of this stream to a destination WriteStream.

      The stream will be resumed when the pipe will be wired to a WriteStream.

      Specified by:
      pipe in interface ReadStream<io.vertx.core.buffer.Buffer>
      Returns:
      a pipe
    • newInstance

      public static RecordParser newInstance(io.vertx.core.parsetools.RecordParser delegate)
      Creates a new instance of the RecordParser.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toMulti

      @CheckReturnValue public io.smallrye.mutiny.Multi<io.vertx.core.buffer.Buffer> toMulti()
    • toBlockingIterable

      public Iterable<io.vertx.core.buffer.Buffer> toBlockingIterable()
    • toBlockingStream

      public Stream<io.vertx.core.buffer.Buffer> toBlockingStream()