Example usage for io.netty.buffer ByteBuf capacity

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

Introduction

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

Prototype

public abstract int capacity();

Source Link

Document

Returns the number of bytes (octets) this buffer can contain.

Usage

From source file:org.clitherproject.clither.server.net.PacketDecoder.java

License:Open Source License

@SuppressWarnings("deprecation")
@Override/*w  w w .j a v a 2s.  c  o m*/
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
    ByteBuf buf = frame.content().order(ByteOrder.BIG_ENDIAN);
    if (buf.capacity() < 1) {
        // Discard empty messages
        return;
    }

    buf.resetReaderIndex();
    int packetId = buf.readUnsignedByte();
    Packet packet = PacketRegistry.SERVERBOUND.constructPacket(packetId);

    if (packet == null) {
        return;
    }

    ClitherServer.log.finest("Received packet " + " (" + packet.getClass().getSimpleName() + ") from "
            + ctx.channel().remoteAddress());

    packet.readData(buf);
    out.add(packet);
}

From source file:org.code_house.ebus.netty.codec.OutboundEBusHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // receive/*from   w ww. jav  a 2s  .  co m*/
    if (msg instanceof MasterHeader) {
        this.masterHeader = (MasterHeader) msg;
    } else if (msg instanceof MasterData) {
        this.masterData = (MasterData) msg;
    }

    if (msg instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) msg;
        if (writeInProgress && buffer.capacity() == 1) {
            byte aByte = buffer.getByte(0);
            if (device.getMaster().getAddress() == aByte) {
                // we got access to bus, lets try to flush something
                accessGranted = true;
                flush(ctx);
                return;
            }
        }
        writeInProgress = false;

        if (buffer.isReadable()) {
            byte lastByte = buffer.getByte(buffer.readerIndex());
            if (lastByte == Constants.SYN) {
                // we received SYN symbol, try to write own address
                writeInProgress = true;
                ctx.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { device.getMaster().getAddress() })); // force writing master address to bus
            }
        }
    }

    ctx.fireChannelRead(msg);
}

From source file:org.graylog2.inputs.transports.netty.LenientDelimiterBasedFrameDecoder.java

License:Open Source License

/**
 * Returns true if the delimiters are "\n" and "\r\n".
 *///from   www  . j  a  v  a2s .c om
private static boolean isLineBased(final ByteBuf[] delimiters) {
    if (delimiters.length != 2) {
        return false;
    }
    ByteBuf a = delimiters[0];
    ByteBuf b = delimiters[1];
    if (a.capacity() < b.capacity()) {
        a = delimiters[1];
        b = delimiters[0];
    }
    return a.capacity() == 2 && b.capacity() == 1 && a.getByte(0) == '\r' && a.getByte(1) == '\n'
            && b.getByte(0) == '\n';
}

From source file:org.graylog2.inputs.transports.netty.LenientDelimiterBasedFrameDecoder.java

License:Open Source License

/**
 * Create a frame out of the {@link ByteBuf} and return it.
 *
 * @param ctx    the {@link ChannelHandlerContext} which this {@link ByteToMessageDecoder} belongs to
 * @param buffer the {@link ByteBuf} from which to read data
 * @return frame           the {@link ByteBuf} which represent the frame or {@code null} if no frame could
 * be created.//from  www. j a  v  a  2 s.  co  m
 */
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    if (lineBasedDecoder != null) {
        return lineBasedDecoder.decode(ctx, buffer);
    }
    // Try all delimiters and choose the delimiter which yields the shortest frame.
    int minFrameLength = Integer.MAX_VALUE;
    ByteBuf minDelim = null;
    for (ByteBuf delim : delimiters) {
        int frameLength = indexOf(buffer, delim);
        if (frameLength >= 0 && frameLength < minFrameLength) {
            minFrameLength = frameLength;
            minDelim = delim;
        }
    }

    if (minDelim != null) {
        int minDelimLength = minDelim.capacity();
        ByteBuf frame;

        if (discardingTooLongFrame) {
            // We've just finished discarding a very large frame.
            // Go back to the initial state.
            discardingTooLongFrame = false;
            buffer.skipBytes(minFrameLength + minDelimLength);

            int tooLongFrameLength = this.tooLongFrameLength;
            this.tooLongFrameLength = 0;
            if (!failFast) {
                fail(tooLongFrameLength);
            }
            return null;
        }

        if (minFrameLength > maxFrameLength) {
            // Discard read frame.
            buffer.skipBytes(minFrameLength + minDelimLength);
            fail(minFrameLength);
            return null;
        }

        if (stripDelimiter) {
            frame = buffer.readRetainedSlice(minFrameLength);
            buffer.skipBytes(minDelimLength);
        } else {
            frame = buffer.readRetainedSlice(minFrameLength + minDelimLength);
        }

        return frame;
    } else if (emitLastLineWithoutDelimiter && !ctx.channel().isActive()) {
        minFrameLength = buffer.readableBytes();
        ByteBuf frame;

        if (discardingTooLongFrame) {
            // We've just finished discarding a very large frame.
            // Go back to the initial state.
            discardingTooLongFrame = false;
            buffer.skipBytes(minFrameLength);

            int tooLongFrameLength = this.tooLongFrameLength;
            this.tooLongFrameLength = 0;
            if (!failFast) {
                fail(tooLongFrameLength);
            }
            return null;
        }

        if (minFrameLength > maxFrameLength) {
            // Discard read frame.
            buffer.skipBytes(minFrameLength);
            fail(minFrameLength);
            return null;
        }

        frame = buffer.readRetainedSlice(minFrameLength);

        return frame;
    } else {
        if (!discardingTooLongFrame) {
            if (buffer.readableBytes() > maxFrameLength) {
                // Discard the content of the buffer until a delimiter is found.
                tooLongFrameLength = buffer.readableBytes();
                buffer.skipBytes(buffer.readableBytes());
                discardingTooLongFrame = true;
                if (failFast) {
                    fail(tooLongFrameLength);
                }
            }
        } else {
            // Still discarding the buffer since a delimiter is not found.
            tooLongFrameLength += buffer.readableBytes();
            buffer.skipBytes(buffer.readableBytes());
        }
        return null;
    }
}

From source file:org.hornetq.tests.integration.transports.netty.HornetQFrameDecoder2Test.java

License:Apache License

@Test
public void testOrdinaryFragmentation() throws Exception {
    final EmbeddedChannel decoder = new EmbeddedChannel(new HornetQFrameDecoder2());
    final byte[] data = new byte[HornetQFrameDecoder2Test.MSG_LEN];
    HornetQFrameDecoder2Test.rand.nextBytes(data);

    ByteBuf src = Unpooled.buffer(HornetQFrameDecoder2Test.MSG_CNT * (HornetQFrameDecoder2Test.MSG_LEN + 4));
    while (src.writerIndex() < src.capacity()) {
        src.writeInt(HornetQFrameDecoder2Test.MSG_LEN);
        src.writeBytes(data);/* w  ww  . j  a va  2  s.  c  o  m*/
    }

    List<ByteBuf> packets = new ArrayList<ByteBuf>();
    while (src.isReadable()) {
        int length = Math.min(HornetQFrameDecoder2Test.rand.nextInt(HornetQFrameDecoder2Test.FRAGMENT_MAX_LEN),
                src.readableBytes());
        packets.add(src.readBytes(length));
    }

    int cnt = 0;
    for (ByteBuf p : packets) {
        decoder.writeInbound(p);
        for (;;) {
            ByteBuf frame = (ByteBuf) decoder.readInbound();
            if (frame == null) {
                break;
            }
            Assert.assertEquals(4, frame.readerIndex());
            Assert.assertEquals(HornetQFrameDecoder2Test.MSG_LEN, frame.readableBytes());
            Assert.assertEquals(Unpooled.wrappedBuffer(data), frame);
            cnt++;
            frame.release();
        }
    }
    Assert.assertEquals(HornetQFrameDecoder2Test.MSG_CNT, cnt);
}

From source file:org.jboss.aerogear.simplepush.server.netty.SimplePushSockJSServiceTest.java

License:Apache License

private Object readOutboundDiscardEmpty(final EmbeddedChannel ch) {
    final Object obj = ch.readOutbound();
    if (obj instanceof ByteBuf) {
        final ByteBuf buf = (ByteBuf) obj;
        if (buf.capacity() == 0) {
            ReferenceCountUtil.release(buf);
            return ch.readOutbound();
        }/* w  w w .java2  s.c o  m*/
    }
    return obj;
}

From source file:org.lmdbjava.ByteBufProxy.java

License:Apache License

@Override
protected ByteBuf allocate() {
    final ArrayDeque<ByteBuf> queue = BUFFERS.get();
    final ByteBuf buffer = queue.poll();

    if (buffer != null && buffer.capacity() >= 0) {
        return buffer;
    } else {// ww w  . j a  va2 s  . c om
        return DEFAULT.directBuffer(0);
    }
}

From source file:org.lmdbjava.ByteBufProxy.java

License:Apache License

@Override
protected byte[] getBytes(final ByteBuf buffer) {
    final byte[] dest = new byte[buffer.capacity()];
    buffer.getBytes(0, dest);//from  w  w w  . j ava  2  s .c  o  m
    return dest;
}

From source file:org.mobicents.protocols.ss7.m3ua.impl.message.AspFactoryPayloadTest.java

License:Open Source License

@Test
public void testReceive() throws Exception {
    byte[] data = new byte[] { 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x02, 0x00, 0x00, 0x08, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x08, 0x00, 0x00, 0x00, 0x19, 0x02, 0x10, 0x00, 0x21, 0x00,
            0x00, 0x17, (byte) 0x9d, 0x00, 0x00, 0x18, 0x1c, 0x03, 0x03, 0x00, 0x02, 0x09, 0x00, 0x03, 0x05,
            0x07, 0x02, 0x42, 0x01, 0x02, 0x42, 0x01, 0x05, 0x03, (byte) 0xd5, 0x1c, 0x18, 0x00, 0x00, 0x00,
            0x00 };/*from   w w  w.  j ava 2  s. c  om*/

    byte[] plData = new byte[] { 9, 0, 3, 5, 7, 2, 66, 1, 2, 66, 1, 5, 3, -43, 28, 24, 0 };

    // SCTP - SCTP layer netty support
    AspFactoryImplProxy aspFactory = new AspFactoryImplProxy(true);
    ByteBuf byteBuf = Unpooled.wrappedBuffer(data);
    org.mobicents.protocols.api.PayloadData pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(),
            byteBuf, true, false, 0, 0);
    AssociationImpl association = new AssociationImpl("hostAddress", 1111, "peerAddress", 1112, "assocName",
            IpChannelType.SCTP, null);
    aspFactory.onPayload(association, pd);

    assertEquals(aspFactory.lstReadMessage.size(), 1);
    M3UAMessage messageImpl = aspFactory.lstReadMessage.get(0);

    assertEquals(MessageType.PAYLOAD, messageImpl.getMessageType());
    PayloadData payloadData = (PayloadData) messageImpl;
    assertEquals(0l, payloadData.getNetworkAppearance().getNetApp());
    assertEquals(1, payloadData.getRoutingContext().getRoutingContexts().length);
    assertEquals(25l, payloadData.getRoutingContext().getRoutingContexts()[0]);
    ProtocolData protocolData = payloadData.getData();
    assertNotNull(protocolData);
    assertEquals(6045, protocolData.getOpc());
    assertEquals(6172, protocolData.getDpc());
    assertEquals(3, protocolData.getSI());
    assertEquals(2, protocolData.getSLS());
    assertEquals(3, protocolData.getNI());
    assertEquals(0, protocolData.getMP());
    assertEquals(plData, protocolData.getData());

    // TCP - SCTP layer netty support
    aspFactory = new AspFactoryImplProxy(true);
    byteBuf = Unpooled.wrappedBuffer(data);
    pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(), byteBuf, true, false, 0, 0);
    association = new AssociationImpl("hostAddress", 1111, "peerAddress", 1112, "assocName", IpChannelType.TCP,
            null);
    aspFactory.onPayload(association, pd);

    assertEquals(aspFactory.lstReadMessage.size(), 1);
    messageImpl = aspFactory.lstReadMessage.get(0);

    assertEquals(MessageType.PAYLOAD, messageImpl.getMessageType());
    payloadData = (PayloadData) messageImpl;
    assertEquals(0l, payloadData.getNetworkAppearance().getNetApp());
    assertEquals(1, payloadData.getRoutingContext().getRoutingContexts().length);
    assertEquals(25l, payloadData.getRoutingContext().getRoutingContexts()[0]);
    protocolData = payloadData.getData();
    assertNotNull(protocolData);
    assertEquals(6045, protocolData.getOpc());
    assertEquals(6172, protocolData.getDpc());
    assertEquals(3, protocolData.getSI());
    assertEquals(2, protocolData.getSLS());
    assertEquals(3, protocolData.getNI());
    assertEquals(0, protocolData.getMP());

    // SCTP - SCTP layer netty NOT support
    aspFactory = new AspFactoryImplProxy(false);
    byteBuf = Unpooled.wrappedBuffer(data);
    pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(), byteBuf, true, false, 0, 0);
    association = new AssociationImpl("hostAddress", 1111, "peerAddress", 1112, "assocName", IpChannelType.SCTP,
            null);
    aspFactory.onPayload(association, pd);

    assertEquals(aspFactory.lstReadMessage.size(), 1);
    messageImpl = aspFactory.lstReadMessage.get(0);

    assertEquals(MessageType.PAYLOAD, messageImpl.getMessageType());
    payloadData = (PayloadData) messageImpl;
    assertEquals(0l, payloadData.getNetworkAppearance().getNetApp());
    assertEquals(1, payloadData.getRoutingContext().getRoutingContexts().length);
    assertEquals(25l, payloadData.getRoutingContext().getRoutingContexts()[0]);
    protocolData = payloadData.getData();
    assertNotNull(protocolData);
    assertEquals(6045, protocolData.getOpc());
    assertEquals(6172, protocolData.getDpc());
    assertEquals(3, protocolData.getSI());
    assertEquals(2, protocolData.getSLS());
    assertEquals(3, protocolData.getNI());
    assertEquals(0, protocolData.getMP());

    // TCP - SCTP layer netty NOT support
    aspFactory = new AspFactoryImplProxy(false);
    byteBuf = Unpooled.wrappedBuffer(data);
    pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(), byteBuf, true, false, 0, 0);
    association = new AssociationImpl("hostAddress", 1111, "peerAddress", 1112, "assocName", IpChannelType.TCP,
            null);
    aspFactory.onPayload(association, pd);

    assertEquals(aspFactory.lstReadMessage.size(), 1);
    messageImpl = aspFactory.lstReadMessage.get(0);

    assertEquals(MessageType.PAYLOAD, messageImpl.getMessageType());
    payloadData = (PayloadData) messageImpl;
    assertEquals(0l, payloadData.getNetworkAppearance().getNetApp());
    assertEquals(1, payloadData.getRoutingContext().getRoutingContexts().length);
    assertEquals(25l, payloadData.getRoutingContext().getRoutingContexts()[0]);
    protocolData = payloadData.getData();
    assertNotNull(protocolData);
    assertEquals(6045, protocolData.getOpc());
    assertEquals(6172, protocolData.getDpc());
    assertEquals(3, protocolData.getSI());
    assertEquals(2, protocolData.getSLS());
    assertEquals(3, protocolData.getNI());
    assertEquals(0, protocolData.getMP());
}

From source file:org.mobicents.protocols.ss7.m3ua.impl.message.AspFactoryPayloadTest.java

License:Open Source License

@Test
public void test2TCPPartial() throws Exception {
    // Only header
    byte[] header = new byte[] { 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x08 };

    byte[] bodyStart = new byte[] { 0x00, 0x06, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x02, 0x10, 0x00,
            (byte) 0xf8, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x03, 0x02, 0x00, 0x01, 0x09, 0x01,
            0x03, 0x10, 0x1d, 0x0d, 0x53, 0x01, 0x00, (byte) 0x91, 0x00, 0x12, 0x04, 0x19, 0x09, 0x31,
            (byte) 0x91, 0x39, 0x08, 0x0d, 0x53, 0x02, 0x00, (byte) 0x92, 0x00, 0x12, 0x04, 0x19, 0x09, 0x31,
            (byte) 0x91, 0x39, 0x09, (byte) 0xc6, 0x62, (byte) 0x81, (byte) 0xc3, 0x48, 0x04, 0x00, 0x08, 0x00,
            0x10, 0x6b, 0x1a, 0x28, 0x18, 0x06, 0x07, 0x00, 0x11, (byte) 0x86, 0x05, 0x01, 0x01, 0x01,
            (byte) 0xa0, 0x0d, 0x60, 0x0b, (byte) 0xa1, 0x09, 0x06, 0x07, 0x04, 0x00, 0x00, 0x01, 0x00, 0x32,
            0x01, 0x6c, (byte) 0x81, (byte) 0x9e, (byte) 0xa1, (byte) 0x81, (byte) 0x9b, 0x02, 0x01, 0x01, 0x02,
            0x01, 0x00, 0x30, (byte) 0x81, (byte) 0x92, (byte) 0x80, 0x01, 0x0c, (byte) 0x82, 0x09, 0x03, 0x10,
            0x13, 0x60, (byte) 0x99, (byte) 0x86, 0x00, 0x00, 0x02, (byte) 0x83, 0x08, 0x04, 0x13, 0x19,
            (byte) 0x89, 0x17, (byte) 0x97, 0x31, 0x72, (byte) 0x85, 0x01, 0x0a, (byte) 0x88, 0x01, 0x01,
            (byte) 0x8a, 0x05, 0x04, 0x13, 0x19, (byte) 0x89, (byte) 0x86, (byte) 0xbb, 0x04, (byte) 0x80, 0x02,
            (byte) 0x80, };

    byte[] bodyEndWithOtherMessage = { (byte) 0x90, (byte) 0x9c, 0x01, 0x0c, (byte) 0x9f, 0x32, 0x08, 0x04,
            0x64, 0x58, 0x05, (byte) 0x94, 0x74, 0x34, (byte) 0xf3, (byte) 0xbf, 0x33, 0x02, (byte) 0x80, 0x00,
            (byte) 0xbf, 0x34, 0x2b, 0x02, 0x01, 0x23, (byte) 0x80, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, (byte) 0x81, 0x07, (byte) 0x91, 0x19, (byte) 0x89, (byte) 0x86, (byte) 0x91, 0x01,
            (byte) 0x82, (byte) 0x82, 0x08, 0x04, (byte) 0x97, 0x19, (byte) 0x89, (byte) 0x86, (byte) 0x91,
            0x01, (byte) 0x82, (byte) 0xa3, 0x09, (byte) 0x80, 0x07, 0x04, (byte) 0xf4, (byte) 0x86, 0x00, 0x65,
            0x18, (byte) 0xd1, (byte) 0xbf, 0x35, 0x03, (byte) 0x83, 0x01, 0x11, (byte) 0x9f, 0x36, 0x08,
            (byte) 0xd2, 0x25, 0x00, 0x00, 0x0d, 0x62, 0x0b, (byte) 0x88, (byte) 0x9f, 0x37, 0x07, (byte) 0x91,
            0x19, (byte) 0x89, (byte) 0x86, (byte) 0x95, (byte) 0x99, (byte) 0x89, (byte) 0x9f, 0x39, 0x08,
            0x02, 0x11, 0x20, 0x10, (byte) 0x91, 0x45, 0x51, 0x23, 0x01, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01,
            0x08, 0x00, 0x06, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x02, 0x10, 0x00, (byte) 0xf8, 0x00, 0x00,
            0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x03, 0x02, 0x00, 0x01, 0x09, 0x01, 0x03, 0x10, 0x1d, 0x0d,
            0x53, 0x01, 0x00, (byte) 0x91, 0x00, 0x12, 0x04, 0x19, 0x09, 0x31, (byte) 0x91, 0x39, 0x08, 0x0d,
            0x53, 0x02, 0x00, (byte) 0x92, 0x00, 0x12, 0x04, 0x19, 0x09, 0x31, (byte) 0x91, 0x39, 0x09,
            (byte) 0xc6, 0x62, (byte) 0x81, (byte) 0xc3, 0x48, 0x04, 0x00, 0x08, 0x00, 0x10, 0x6b, 0x1a, 0x28,
            0x18, 0x06, 0x07, 0x00, 0x11, (byte) 0x86, 0x05, 0x01, 0x01, 0x01, (byte) 0xa0, 0x0d, 0x60, 0x0b,
            (byte) 0xa1, 0x09, 0x06, 0x07, 0x04, 0x00, 0x00, 0x01, 0x00, 0x32, 0x01, 0x6c, (byte) 0x81,
            (byte) 0x9e, (byte) 0xa1, (byte) 0x81, (byte) 0x9b, 0x02, 0x01, 0x01, 0x02, 0x01, 0x00, 0x30,
            (byte) 0x81, (byte) 0x92, (byte) 0x80, 0x01, 0x0c, (byte) 0x82, 0x09, 0x03, 0x10, 0x13, 0x60,
            (byte) 0x99, (byte) 0x86, 0x00, 0x00, 0x02, (byte) 0x83, 0x08, 0x04, 0x13, 0x19, (byte) 0x89, 0x17,
            (byte) 0x97, 0x31, 0x72, (byte) 0x85, 0x01, 0x0a, (byte) 0x88, 0x01, 0x01, (byte) 0x8a, 0x05, 0x04,
            0x13, 0x19, (byte) 0x89, (byte) 0x86, (byte) 0xbb, 0x04, (byte) 0x80, 0x02, (byte) 0x80,
            (byte) 0x90, (byte) 0x9c, 0x01, 0x0c, (byte) 0x9f, 0x32, 0x08, 0x04, 0x64, 0x58, 0x05, (byte) 0x94,
            0x74, 0x34, (byte) 0xf3, (byte) 0xbf, 0x33, 0x02, (byte) 0x80, 0x00, (byte) 0xbf, 0x34, 0x2b, 0x02,
            0x01, 0x23, (byte) 0x80, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x81, 0x07,
            (byte) 0x91, 0x19, (byte) 0x89, (byte) 0x86, (byte) 0x91, 0x01, (byte) 0x82, (byte) 0x82, 0x08,
            0x04, (byte) 0x97, 0x19, (byte) 0x89, (byte) 0x86, (byte) 0x91, 0x01, (byte) 0x82, (byte) 0xa3,
            0x09, (byte) 0x80, 0x07, 0x04, (byte) 0xf4, (byte) 0x86, 0x00, 0x65, 0x18, (byte) 0xd1, (byte) 0xbf,
            0x35, 0x03, (byte) 0x83, 0x01, 0x11, (byte) 0x9f, 0x36, 0x08, (byte) 0xd2, 0x25, 0x00, 0x00, 0x0d,
            0x62, 0x0b, (byte) 0x88, (byte) 0x9f, 0x37, 0x07, (byte) 0x91, 0x19, (byte) 0x89, (byte) 0x86,
            (byte) 0x95, (byte) 0x99, (byte) 0x89, (byte) 0x9f, 0x39, 0x08, 0x02, 0x11, 0x20, 0x10, (byte) 0x91,
            0x45, 0x51, 0x23 };/*from   www .  j  a v  a  2  s.  c om*/

    AspFactoryImplProxy aspFactory = new AspFactoryImplProxy(true);
    AssociationImpl association = new AssociationImpl("hostAddress", 1111, "peerAddress", 1112, "assocName",
            IpChannelType.TCP, null);

    ByteBuf byteBuf = Unpooled.wrappedBuffer(header);
    org.mobicents.protocols.api.PayloadData pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(),
            byteBuf, true, false, 0, 0);
    aspFactory.onPayload(association, pd);

    assertEquals(aspFactory.lstReadMessage.size(), 0);

    // Now lets read only first half of body and yet we have null
    // messageImpl
    byteBuf = Unpooled.wrappedBuffer(bodyStart);
    pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(), byteBuf, true, false, 0, 0);
    aspFactory.onPayload(association, pd);
    assertEquals(aspFactory.lstReadMessage.size(), 0);

    // Now lets read other half
    byteBuf = Unpooled.wrappedBuffer(bodyEndWithOtherMessage);
    pd = new org.mobicents.protocols.api.PayloadData(byteBuf.capacity(), byteBuf, true, false, 0, 0);
    aspFactory.onPayload(association, pd);
    assertEquals(aspFactory.lstReadMessage.size(), 2);
    M3UAMessage messageImpl = aspFactory.lstReadMessage.get(0);

    assertEquals(MessageType.PAYLOAD, messageImpl.getMessageType());
    PayloadData payloadData = (PayloadData) messageImpl;
    assertNull(payloadData.getNetworkAppearance());
    assertEquals(1, payloadData.getRoutingContext().getRoutingContexts().length);
    assertEquals(1l, payloadData.getRoutingContext().getRoutingContexts()[0]);
    ProtocolData protocolData = payloadData.getData();
    assertNotNull(protocolData);
    assertEquals(2, protocolData.getOpc());
    assertEquals(1, protocolData.getDpc());
    assertEquals(3, protocolData.getSI());
    assertEquals(1, protocolData.getSLS());
    assertEquals(2, protocolData.getNI());
    assertEquals(0, protocolData.getMP());

    messageImpl = aspFactory.lstReadMessage.get(1);

    assertEquals(MessageType.PAYLOAD, messageImpl.getMessageType());
    payloadData = (PayloadData) messageImpl;
    assertNull(payloadData.getNetworkAppearance());
    assertEquals(1, payloadData.getRoutingContext().getRoutingContexts().length);
    assertEquals(1l, payloadData.getRoutingContext().getRoutingContexts()[0]);
    protocolData = payloadData.getData();
    assertNotNull(protocolData);
    assertEquals(2, protocolData.getOpc());
    assertEquals(1, protocolData.getDpc());
    assertEquals(3, protocolData.getSI());
    assertEquals(1, protocolData.getSLS());
    assertEquals(2, protocolData.getNI());
    assertEquals(0, protocolData.getMP());

}