Search in sources :

Example 1 with RecordParser

use of io.vertx.core.parsetools.RecordParser in project vert.x by eclipse.

the class RecordParserExamples method example1.

public void example1() {
    final RecordParser parser = RecordParser.newDelimited("\n", h -> {
        System.out.println(h.toString());
    });
    parser.handle(Buffer.buffer("HELLO\nHOW ARE Y"));
    parser.handle(Buffer.buffer("OU?\nI AM"));
    parser.handle(Buffer.buffer("DOING OK"));
    parser.handle(Buffer.buffer("\n"));
}
Also used : RecordParser(io.vertx.core.parsetools.RecordParser)

Example 2 with RecordParser

use of io.vertx.core.parsetools.RecordParser in project vert.x by eclipse.

the class RecordParserTest method testIllegalArguments.

@Test
public void testIllegalArguments() throws Exception {
    assertNullPointerException(() -> RecordParser.newDelimited((Buffer) null, handler -> {
    }));
    assertNullPointerException(() -> RecordParser.newDelimited((String) null, handler -> {
    }));
    RecordParser parser = RecordParser.newDelimited("", handler -> {
    });
    assertNullPointerException(() -> parser.setOutput(null));
    assertNullPointerException(() -> parser.delimitedMode((Buffer) null));
    assertNullPointerException(() -> parser.delimitedMode((String) null));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) RecordParser(io.vertx.core.parsetools.RecordParser) Test(org.junit.Test) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) RecordParser(io.vertx.core.parsetools.RecordParser) Test(org.junit.Test)

Example 3 with RecordParser

use of io.vertx.core.parsetools.RecordParser in project vert.x by eclipse.

the class RecordParserTest method doTestDelimited.

private void doTestDelimited(final Buffer input, Buffer delim, Integer[] chunkSizes, final Buffer... expected) {
    final Buffer[] results = new Buffer[expected.length];
    Handler<Buffer> out = new Handler<Buffer>() {

        int pos;

        public void handle(Buffer buff) {
            results[pos++] = buff;
        }
    };
    RecordParser parser = RecordParser.newDelimited(delim, out);
    feedChunks(input, parser, chunkSizes);
    checkResults(expected, results);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) Handler(io.vertx.core.Handler) RecordParser(io.vertx.core.parsetools.RecordParser)

Example 4 with RecordParser

use of io.vertx.core.parsetools.RecordParser in project vert.x by eclipse.

the class RecordParserTest method testMixed.

@Test
public /*
  Test mixture of fixed and delimited
   */
void testMixed() {
    final int lines = 8;
    final List<Object> types = new ArrayList<Object>();
    class MyHandler implements Handler<Buffer> {

        RecordParser parser = RecordParser.newFixed(10, this);

        int pos;

        public void handle(Buffer buff) {
            if (pos < lines) {
                Object type = types.get(pos);
                if (type instanceof byte[]) {
                    byte[] bytes = (byte[]) type;
                    parser.delimitedMode(Buffer.buffer(bytes));
                } else {
                    int length = (Integer) type;
                    parser.fixedSizeMode(length);
                }
            }
        }
    }
    MyHandler out = new MyHandler();
    Buffer[] expected = new Buffer[lines];
    Buffer input = Buffer.buffer(100);
    expected[0] = TestUtils.randomBuffer(10);
    input.appendBuffer(expected[0]);
    types.add(expected[0].length());
    expected[1] = TestUtils.randomBuffer(100);
    input.appendBuffer(expected[1]);
    types.add(expected[1].length());
    byte[] delim = new byte[] { 23, -120, 100, 3 };
    expected[2] = TestUtils.randomBuffer(50, true, delim[0]);
    input.appendBuffer(expected[2]);
    types.add(delim);
    input.appendBuffer(Buffer.buffer(delim));
    expected[3] = TestUtils.randomBuffer(1000);
    input.appendBuffer(expected[3]);
    types.add(expected[3].length());
    expected[4] = TestUtils.randomBuffer(230, true, delim[0]);
    input.appendBuffer(expected[4]);
    types.add(delim);
    input.appendBuffer(Buffer.buffer(delim));
    delim = new byte[] { 17 };
    expected[5] = TestUtils.randomBuffer(341, true, delim[0]);
    input.appendBuffer(expected[5]);
    types.add(delim);
    input.appendBuffer(Buffer.buffer(delim));
    delim = new byte[] { 54, -32, 0 };
    expected[6] = TestUtils.randomBuffer(1234, true, delim[0]);
    input.appendBuffer(expected[6]);
    types.add(delim);
    input.appendBuffer(Buffer.buffer(delim));
    expected[7] = TestUtils.randomBuffer(100);
    input.appendBuffer(expected[7]);
    types.add(expected[7].length());
    feedChunks(input, out.parser, new Integer[] { 50, 10, 3 });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) RecordParser(io.vertx.core.parsetools.RecordParser) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

RecordParser (io.vertx.core.parsetools.RecordParser)4 Handler (io.vertx.core.Handler)3 Buffer (io.vertx.core.buffer.Buffer)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 TestUtils.assertNullPointerException (io.vertx.test.core.TestUtils.assertNullPointerException)1 Collections (java.util.Collections)1 List (java.util.List)1 Assert.assertEquals (org.junit.Assert.assertEquals)1