Example usage for io.netty.buffer ByteBuf getBytes

List of usage examples for io.netty.buffer ByteBuf getBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf getBytes.

Prototype

public abstract ByteBuf getBytes(int index, ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination's position reaches its limit.

Usage

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.GetStateInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link HealthCheckInputMessageFactory} for correct translation from POJO
 * @throws Exception//  w  w w  . java  2 s .c  om
 */
@Test
public void testElementsSet() throws Exception {
    GetStateInputBuilder hib = new GetStateInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("GETSTATEREQ"));
    boolean testEvent = true;
    String testObjId = "exampleObj:0";
    String testStateType = "ALL";

    //set xid
    Method m2_t = hib.getClass().getMethod("setXid", Long.class);
    m2_t.invoke(hib, new Long(0));

    //set eventDrivenReporting
    Method m3_t = hib.getClass().getMethod("setEventDrivenReporting", Boolean.class);
    m3_t.invoke(hib, new Boolean(testEvent));

    //set ObjId
    Method m4_t = hib.getClass().getMethod("setObjId", ObjId.class);
    m4_t.invoke(hib, new ObjId(testObjId));

    //set State Type
    Method m5_t = hib.getClass().getMethod("setStateType", StateAllType.class);
    m5_t.invoke(hib, StateAllType.valueOf(testStateType));

    GetStateInput hi = hib.build();
    LOG.debug("GetStateInputFactoryTest - hi objId value = {}", hi.getObjId());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    getStateInputFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("GetStateInputFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    String buf = new String(bytes, "UTF-8");
    StringBuilder seq = new StringBuilder("");
    seq.append("<obj objID=\"");
    seq.append(testObjId);
    seq.append("\">");
    boolean checkVal = buf.contains(seq);

    StringBuilder seq2 = new StringBuilder("");
    seq2.append("<eventDrivenReporting>");
    seq2.append(String.valueOf(testEvent));
    seq2.append("</eventDrivenReporting>");
    boolean checkVal2 = buf.contains(seq2);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
    Assert.assertEquals("Wrong length", true, checkVal2);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.HealthCheckInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link HealthCheckInputMessageFactory} for correct translation from POJO
 * @throws Exception/*from ww w.  j a  v a2  s .c  o  m*/
 */
@Test
public void testElementsSet() throws Exception {
    HealthCheckInputBuilder hib = new HealthCheckInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("HEALTHCHECKREQ"));
    int testTimeout = 16;

    //set tcpLinkMonTimeout
    Method m_t = hib.getClass().getMethod("setTcpLinkMonTimeout", XsdUnsignedShort.class);
    m_t.invoke(hib, new XsdUnsignedShort(testTimeout));

    //set xid
    Method m2_t = hib.getClass().getMethod("setXid", Long.class);
    m2_t.invoke(hib, new Long(0));

    HealthCheckInput hi = hib.build();
    LOG.debug("HealthCheckInputMessageFactoryTest - hi testTimeout value = {}",
            hi.getTcpLinkMonTimeout().getValue());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    healthCheckFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("HealthCheckInputMessageFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    StringBuilder seq = new StringBuilder("");
    seq.append("<tcpLinkMonTimeout>");
    seq.append(testTimeout);
    seq.append("</tcpLinkMonTimeout>");
    String buf = new String(bytes, "UTF-8");
    boolean checkVal = buf.contains(seq);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.HelloInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link HealthCheckInputMessageFactory} for correct translation from POJO
 * @throws Exception// w  w  w.  ja v  a 2s .com
 */
@Test
public void testElementsSet() throws Exception {
    HelloInputBuilder hib = new HelloInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("HELLOACK"));
    String testResult = "FAILOCPVERSION";

    //set result
    Method m_t = hib.getClass().getMethod("setResult", OriHelloAckRes.class);
    m_t.invoke(hib, OriHelloAckRes.valueOf(testResult));

    //set xid
    Method m2_t = hib.getClass().getMethod("setXid", Long.class);
    m2_t.invoke(hib, new Long(0));

    HelloInput hi = hib.build();
    LOG.debug("HealthCheckInputMessageFactoryTest - hi result value = {}", hi.getResult());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    helloInputFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("HealthCheckInputMessageFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    StringBuilder seq = new StringBuilder("");
    seq.append("<result>");
    seq.append(testResult);
    seq.append("</result>");
    String buf = new String(bytes, "UTF-8");
    boolean checkVal = buf.contains(seq);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.ModifyParamInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link ModifyParamInputFactory} for correct translation from POJO
 * @throws Exception//w  w w  . ja v a  2s  .  co m
 */
@Test
public void testElementsSet() throws Exception {
    ModifyParamInputBuilder hib = new ModifyParamInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("MODIFYPARAMREQ"));
    boolean testEvent = true;
    String testObjId = "ALL";
    //set xid
    Method m_t = hib.getClass().getMethod("setXid", Long.class);
    m_t.invoke(hib, new Long(0));

    //set ObjId
    Method m2_t = hib.getClass().getMethod("setObjId", ObjId.class);
    m2_t.invoke(hib, new ObjId(testObjId));

    //set Params
    List<Param> plist = new ArrayList();
    ParamBuilder parambuilder1 = new ParamBuilder();
    parambuilder1.setName("parameter1");
    parambuilder1.setValue("value1");
    plist.add(parambuilder1.build());
    ParamBuilder parambuilder2 = new ParamBuilder();
    parambuilder2.setName("parameter2");
    parambuilder2.setValue("value2");
    plist.add(parambuilder2.build());
    ParamBuilder parambuilder3 = new ParamBuilder();
    parambuilder3.setName("parameter3");
    parambuilder3.setValue("a wrong value");
    plist.add(parambuilder3.build());

    Method m3_t = hib.getClass().getMethod("setParam", List.class);
    m3_t.invoke(hib, plist);

    ModifyParamInput hi = hib.build();
    LOG.debug("ModifyParamInputFactoryTest - hi objId value = {}", hi.getObjId());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    modifyParamInputFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("ModifyParamInputFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    String buf = new String(bytes, "UTF-8");
    StringBuilder seq = new StringBuilder("");
    seq.append("<obj objID=\"");
    seq.append(testObjId);
    seq.append("\">");
    boolean checkVal = buf.contains(seq);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.ModifyStateInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link HealthCheckInputMessageFactory} for correct translation from POJO
 * @throws Exception//from w  ww  .ja  v  a 2  s.  c  o  m
 */
@Test
public void testElementsSet() throws Exception {
    ModifyStateInputBuilder hib = new ModifyStateInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("MODIFYSTATEREQ"));
    boolean testEvent = true;
    String testObjId = "ALL";
    String testType = "AST";
    String testVal = "UNLOCKED";

    //set xid
    Method m2_t = hib.getClass().getMethod("setXid", Long.class);
    m2_t.invoke(hib, new Long(0));

    //set ObjId
    Method m3_t = hib.getClass().getMethod("setObjId", ObjId.class);
    m3_t.invoke(hib, new ObjId(testObjId));

    //set stateType
    Method m4_t = hib.getClass().getMethod("setStateType", StateType.class);
    m4_t.invoke(hib, StateType.valueOf(testType));

    //set stateValue
    Method m5_t = hib.getClass().getMethod("setStateValue", StateVal.class);
    m5_t.invoke(hib, StateVal.valueOf(testVal));

    ModifyStateInput hi = hib.build();
    LOG.debug("ModifyStateInputFactoryTest - hi objId value = {}", hi.getObjId());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    modifyStateInputFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("ModifyStateInputFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    String buf = new String(bytes, "UTF-8");
    StringBuilder seq = new StringBuilder("");
    seq.append("<obj objID=\"");
    seq.append(testObjId);
    seq.append("\">");
    boolean checkVal = buf.contains(seq);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.ReResetInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link ReResetInputFactory} for correct translation from POJO
 * @throws Exception//from  w w w  . j  a v a  2 s  .c om
 */
@Test
public void testElementsSet() throws Exception {
    ReResetInputBuilder hib = new ReResetInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("RESETREQ"));
    String msgTag = "resetReq";

    //set xid
    Method m_t = hib.getClass().getMethod("setXid", Long.class);
    m_t.invoke(hib, new Long(0));

    ReResetInput hi = hib.build();
    LOG.debug("ReResetInputFactoryTest - hi Xid value = {}", hi.getXid());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    reResetFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("ReResetInputFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    StringBuilder seq = new StringBuilder("");
    seq.append("resetReq");
    String buf = new String(bytes, "UTF-8");
    boolean checkVal = buf.contains(seq);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
}

From source file:org.opendaylight.ocpjava.protocol.impl.serialization.factories.SetTimeInputFactoryTest.java

License:Open Source License

/**
 * Testing of {@link SetTimeInputFactory} for correct translation from POJO
 * @throws Exception/*from  w ww. j a  v a 2s .  co m*/
 */
@Test
public void testElementsSet() throws Exception {
    SetTimeInputBuilder hib = new SetTimeInputBuilder();
    BufferHelper.setupHeader(hib, OcpMsgType.valueOf("SETTIMEREQ"));
    String testNewTime = "2016-04-26T10:23:00-05:00";

    //set tcpLinkMonTimeout
    Method m_t = hib.getClass().getMethod("setNewTime", XsdDateTime.class);
    m_t.invoke(hib, new XsdDateTime(testNewTime));

    //set xid
    Method m2_t = hib.getClass().getMethod("setXid", Long.class);
    m2_t.invoke(hib, new Long(0));

    SetTimeInput hi = hib.build();
    LOG.debug("HealthCheckInputMessageFactoryTest - hi testNewTime value = {}",
            hi.getNewTime().getValue().toString());

    ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
    setTimeInputFactory.serialize(hi, out);

    //Verify and check the bytebuf info
    LOG.debug("SetTimeInputFactoryTest - out = {}", out.readableBytes());
    byte[] bytes = new byte[out.readableBytes()];
    int readerIndex = out.readerIndex();
    out.getBytes(readerIndex, bytes);

    StringBuilder seq = new StringBuilder("");
    seq.append("<newTime>");
    seq.append(testNewTime);
    seq.append("</newTime>");
    String buf = new String(bytes, "UTF-8");
    boolean checkVal = buf.contains(seq);

    //Check and compare elements
    Assert.assertEquals("Wrong length", true, checkVal);
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {

    logger.trace("readable bytes {}, records read {}, incomplete record bytes {}", buf.readableBytes(),
            recordsRead, lastRecordBytes);

    if (lastRecordBytes == 0) {
        if (buf.readableBytes() < 4) {
            return; //wait for more data
        }/* w  w  w.jav  a2  s  .c o m*/

        skipSpaces(buf);

        byte[] buff = new byte[4];
        buf.getBytes(buf.readerIndex(), buff);
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(jacksonIOContext, buff, 0, 4);
        JsonEncoding jsonEncoding = strapper.detectEncoding();
        if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
            throw new InvalidEncodingException(jsonEncoding.getJavaName(), "currently only UTF-8 is supported");
        }
    }

    int index = lastRecordBytes + buf.readerIndex();

    for (; index < buf.writerIndex(); index++) {
        switch (buf.getByte(index)) {
        case '{':
            if (!inS) {
                leftCurlies++;
            }
            break;
        case '}':
            if (!inS) {
                rightCurlies++;
            }
            break;
        case '"':
            if (buf.getByte(index - 1) != '\\') {
                inS = !inS;
            }
            break;
        default:
            break;
        }

        if (leftCurlies != 0 && leftCurlies == rightCurlies && !inS) {
            ByteBuf slice = buf.readSlice(1 + index - buf.readerIndex());
            JsonParser jp = jacksonJsonFactory.createParser(new ByteBufInputStream(slice));
            JsonNode root = jp.readValueAsTree();
            out.add(root);
            leftCurlies = rightCurlies = lastRecordBytes = 0;
            recordsRead++;
            break;
        }

        if (index - buf.readerIndex() >= maxFrameLength) {
            fail(ctx, index - buf.readerIndex());
        }
    }

    // end of stream, save the incomplete record index to avoid reexamining the whole on next run
    if (index >= buf.writerIndex()) {
        lastRecordBytes = buf.readableBytes();
        return;
    }
}

From source file:org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder.java

License:Open Source License

private void print(ByteBuf buf, int startPos, int chars, String message) {
    if (null == message) {
        message = "";
    }/*  ww  w  .j  a  v  a  2s .c o  m*/
    if (startPos > buf.writerIndex()) {
        logger.trace("startPos out of bounds");
    }
    byte[] bytes = new byte[startPos + chars <= buf.writerIndex() ? chars : buf.writerIndex() - startPos];
    buf.getBytes(startPos, bytes);
    logger.trace("{} ={}", message, new String(bytes));
}

From source file:org.opendaylight.protocol.pcep.ietf.stateful07.PathBindingTlvParser.java

License:Open Source License

private void backwardsParser(final int type, final ByteBuf buffer, final PathBindingBuilder builder) {
    builder.setBindingType((short) type);
    final byte[] value = new byte[buffer.readableBytes()];
    //codec will do the reading from buffer
    buffer.getBytes(0, value);
    builder.setBindingValue(value);/*from   w w w  .ja  va2  s.  c o m*/
}