Example usage for io.netty.buffer Unpooled copyLong

List of usage examples for io.netty.buffer Unpooled copyLong

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled copyLong.

Prototype

public static ByteBuf copyLong(long... values) 

Source Link

Document

Create a new big-endian buffer that holds a sequence of the specified 64-bit integers.

Usage

From source file:com.github.sparkfy.network.protocol.MessageWithHeaderSuite.java

License:Apache License

@Test
public void testByteBufBody() throws Exception {
    ByteBuf header = Unpooled.copyLong(42);
    ByteBuf bodyPassedToNettyManagedBuffer = Unpooled.copyLong(84);
    assertEquals(1, header.refCnt());//w w w.j a  v a 2 s  .c o  m
    assertEquals(1, bodyPassedToNettyManagedBuffer.refCnt());
    ManagedBuffer managedBuf = new NettyManagedBuffer(bodyPassedToNettyManagedBuffer);

    Object body = managedBuf.convertToNetty();
    assertEquals(2, bodyPassedToNettyManagedBuffer.refCnt());
    assertEquals(1, header.refCnt());

    MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, managedBuf.size());
    ByteBuf result = doWrite(msg, 1);
    assertEquals(msg.count(), result.readableBytes());
    assertEquals(42, result.readLong());
    assertEquals(84, result.readLong());

    assert (msg.release());
    assertEquals(0, bodyPassedToNettyManagedBuffer.refCnt());
    assertEquals(0, header.refCnt());
}

From source file:com.github.sparkfy.network.protocol.MessageWithHeaderSuite.java

License:Apache License

private void testFileRegionBody(int totalWrites, int writesPerCall) throws Exception {
    ByteBuf header = Unpooled.copyLong(42);
    int headerLength = header.readableBytes();
    TestFileRegion region = new TestFileRegion(totalWrites, writesPerCall);
    MessageWithHeader msg = new MessageWithHeader(null, header, region, region.count());

    ByteBuf result = doWrite(msg, totalWrites / writesPerCall);
    assertEquals(headerLength + region.count(), result.readableBytes());
    assertEquals(42, result.readLong());
    for (long i = 0; i < 8; i++) {
        assertEquals(i, result.readLong());
    }//from w  w w  .  j  av a  2 s . c  om
    assert (msg.release());
}

From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java

License:Apache License

@Test
public void testByteBufBody() throws Exception {
    ByteBuf header = Unpooled.copyLong(42);
    ByteBuf bodyPassedToNettyManagedBuffer = Unpooled.copyLong(84);
    assertEquals(1, header.refCnt());/*from  w  w w.  java  2 s  .c o  m*/
    assertEquals(1, bodyPassedToNettyManagedBuffer.refCnt());
    ManagedBuffer managedBuf = new NettyManagedBuffer(bodyPassedToNettyManagedBuffer);

    Object body = managedBuf.convertToNetty();
    assertEquals(2, bodyPassedToNettyManagedBuffer.refCnt());
    assertEquals(1, header.refCnt());

    MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, managedBuf.size());
    ByteBuf result = doWrite(msg, 1);
    assertEquals(msg.count(), result.readableBytes());
    assertEquals(42, result.readLong());
    assertEquals(84, result.readLong());

    assertTrue(msg.release());
    assertEquals(0, bodyPassedToNettyManagedBuffer.refCnt());
    assertEquals(0, header.refCnt());
}

From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java

License:Apache License

@Test
public void testDeallocateReleasesManagedBuffer() throws Exception {
    ByteBuf header = Unpooled.copyLong(42);
    ManagedBuffer managedBuf = Mockito.spy(new TestManagedBuffer(84));
    ByteBuf body = (ByteBuf) managedBuf.convertToNetty();
    assertEquals(2, body.refCnt());//from   www . ja  va2  s .  c  o m
    MessageWithHeader msg = new MessageWithHeader(managedBuf, header, body, body.readableBytes());
    assertTrue(msg.release());
    Mockito.verify(managedBuf, Mockito.times(1)).release();
    assertEquals(0, body.refCnt());
}

From source file:org.apache.spark.network.protocol.MessageWithHeaderSuite.java

License:Apache License

private void testFileRegionBody(int totalWrites, int writesPerCall) throws Exception {
    ByteBuf header = Unpooled.copyLong(42);
    int headerLength = header.readableBytes();
    TestFileRegion region = new TestFileRegion(totalWrites, writesPerCall);
    MessageWithHeader msg = new MessageWithHeader(null, header, region, region.count());

    ByteBuf result = doWrite(msg, totalWrites / writesPerCall);
    assertEquals(headerLength + region.count(), result.readableBytes());
    assertEquals(42, result.readLong());
    for (long i = 0; i < 8; i++) {
        assertEquals(i, result.readLong());
    }// ww w .  ja  v  a2 s  .  c om
    assertTrue(msg.release());
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

@Test
public void testInterception() throws Exception {
    int interceptedReads = 3;
    TransportFrameDecoder decoder = new TransportFrameDecoder();
    TransportFrameDecoder.Interceptor interceptor = spy(new MockInterceptor(interceptedReads));
    ChannelHandlerContext ctx = mockChannelHandlerContext();

    byte[] data = new byte[8];
    ByteBuf len = Unpooled.copyLong(8 + data.length);
    ByteBuf dataBuf = Unpooled.wrappedBuffer(data);

    try {/*from   w w  w.  j av a 2 s . c o  m*/
        decoder.setInterceptor(interceptor);
        for (int i = 0; i < interceptedReads; i++) {
            decoder.channelRead(ctx, dataBuf);
            assertEquals(0, dataBuf.refCnt());
            dataBuf = Unpooled.wrappedBuffer(data);
        }
        decoder.channelRead(ctx, len);
        decoder.channelRead(ctx, dataBuf);
        verify(interceptor, times(interceptedReads)).handle(any(ByteBuf.class));
        verify(ctx).fireChannelRead(any(ByteBuffer.class));
        assertEquals(0, len.refCnt());
        assertEquals(0, dataBuf.refCnt());
    } finally {
        release(len);
        release(dataBuf);
    }
}

From source file:org.apache.spark.network.util.TransportFrameDecoderSuite.java

License:Apache License

private void testInvalidFrame(long size) throws Exception {
    TransportFrameDecoder decoder = new TransportFrameDecoder();
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ByteBuf frame = Unpooled.copyLong(size);
    try {/*from   w w w.  j a v a 2  s  . c  om*/
        decoder.channelRead(ctx, frame);
    } finally {
        release(frame);
    }
}

From source file:org.opendaylight.protocol.bgp.linkstate.attribute.LinkAttributesParser.java

License:Open Source License

static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
    final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
    LOG.trace("Started serializing Link Attributes");
    if (linkAttributes.getLocalIpv4RouterId() != null) {
        TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID,
                Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
    }/*from  w w w.j  ava 2s  . co  m*/
    if (linkAttributes.getLocalIpv6RouterId() != null) {
        TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID,
                Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
    }
    if (linkAttributes.getRemoteIpv4RouterId() != null) {
        TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID,
                Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
    }
    if (linkAttributes.getRemoteIpv6RouterId() != null) {
        TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID,
                Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
    }
    if (linkAttributes.getAdminGroup() != null) {
        TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()),
                byteAggregator);
    }
    if (linkAttributes.getMaxLinkBandwidth() != null) {
        TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()),
                byteAggregator);
    }
    if (linkAttributes.getMaxReservableBandwidth() != null) {
        TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH,
                Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
    }
    serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), byteAggregator);
    if (linkAttributes.getTeMetric() != null) {
        TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()),
                byteAggregator);
    }
    if (linkAttributes.getLinkProtection() != null) {
        TlvUtil.writeTLV(LINK_PROTECTION_TYPE,
                Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
    }
    serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
    if (linkAttributes.getMetric() != null) {
        // size of metric can be 1,2 or 3 depending on the protocol
        TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()),
                byteAggregator);
    }
    serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), byteAggregator);
    if (linkAttributes.getLinkName() != null) {
        TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())),
                byteAggregator);
    }
    if (linkAttributes.getSrAdjIds() != null) {
        SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getSrAdjIds(), SR_ADJ_ID,
                byteAggregator);
    }
    if (linkAttributes.getSrLanAdjIds() != null) {
        SrLinkAttributesParser.serializeLanAdjacencySegmentIdentifiers(linkAttributes.getSrLanAdjIds(),
                SR_LAN_ADJ_ID, byteAggregator);
    }
    if (linkAttributes.getPeerNodeSid() != null) {
        TlvUtil.writeTLV(PEER_NODE_SID_CODE,
                SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerNodeSid()),
                byteAggregator);
    }
    if (linkAttributes.getPeerAdjSid() != null) {
        TlvUtil.writeTLV(PEER_ADJ_SID_CODE,
                SrLinkAttributesParser.serializeAdjacencySegmentIdentifier(linkAttributes.getPeerAdjSid()),
                byteAggregator);
    }
    if (linkAttributes.getPeerSetSids() != null) {
        SrLinkAttributesParser.serializeAdjacencySegmentIdentifiers(linkAttributes.getPeerSetSids(),
                PEER_SET_SID_CODE, byteAggregator);
    }
    LOG.trace("Finished serializing Link Attributes");
}

From source file:org.opendaylight.protocol.bgp.linkstate.impl.attribute.LinkAttributesParser.java

License:Open Source License

static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf output) {
    final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
    LOG.trace("Started serializing Link Attributes");
    ifPresentApply(linkAttributes.getLocalIpv4RouterId(), value -> TlvUtil
            .writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress((Ipv4Address) value), output));
    ifPresentApply(linkAttributes.getLocalIpv6RouterId(), value -> TlvUtil
            .writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress((Ipv6Address) value), output));
    ifPresentApply(linkAttributes.getRemoteIpv4RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID,
            Ipv4Util.byteBufForAddress((Ipv4Address) value), output));
    ifPresentApply(linkAttributes.getRemoteIpv6RouterId(), value -> TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID,
            Ipv6Util.byteBufForAddress((Ipv6Address) value), output));
    ifPresentApply(linkAttributes.getAdminGroup(), value -> TlvUtil.writeTLV(ADMIN_GROUP,
            Unpooled.copyInt((((AdministrativeGroup) value).getValue()).intValue()), output));
    ifPresentApply(linkAttributes.getMaxLinkBandwidth(), value -> TlvUtil.writeTLV(MAX_BANDWIDTH,
            Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
    ifPresentApply(linkAttributes.getMaxReservableBandwidth(),
            value -> TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH,
                    Unpooled.wrappedBuffer(((Bandwidth) value).getValue()), output));
    serializeUnreservedBw(linkAttributes.getUnreservedBandwidth(), output);
    ifPresentApply(linkAttributes.getTeMetric(),
            value -> TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(((TeMetric) value).getValue()), output));
    ifPresentApply(linkAttributes.getLinkProtection(), value -> TlvUtil.writeTLV(LINK_PROTECTION_TYPE,
            Unpooled.copyShort(((LinkProtectionType) value).getIntValue()), output));
    serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), output);
    ifPresentApply(linkAttributes.getMetric(), value -> TlvUtil.writeTLV(METRIC,
            Unpooled.copyMedium(((Metric) value).getValue().intValue()), output));
    serializeSrlg(linkAttributes.getSharedRiskLinkGroups(), output);
    ifPresentApply(linkAttributes.getLinkName(), value -> TlvUtil.writeTLV(LINK_NAME,
            Unpooled.wrappedBuffer(StandardCharsets.UTF_8.encode((String) value)), output));
    ifPresentApply(linkAttributes.getSrAdjIds(), value -> SrLinkAttributesParser
            .serializeAdjacencySegmentIdentifiers((List<SrAdjIds>) value, SR_ADJ_ID, output));
    ifPresentApply(linkAttributes.getSrLanAdjIds(), value -> SrLinkAttributesParser
            .serializeLanAdjacencySegmentIdentifiers((List<SrLanAdjIds>) value, output));
    ifPresentApply(linkAttributes.getPeerNodeSid(), value -> TlvUtil.writeTLV(PEER_NODE_SID_CODE,
            SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerNodeSid) value), output));
    ifPresentApply(linkAttributes.getPeerAdjSid(), value -> TlvUtil.writeTLV(PEER_ADJ_SID_CODE,
            SrLinkAttributesParser.serializeAdjacencySegmentIdentifier((PeerAdjSid) value), output));
    ifPresentApply(linkAttributes.getPeerSetSids(), value -> SrLinkAttributesParser
            .serializeAdjacencySegmentIdentifiers((List<PeerSetSids>) value, PEER_SET_SID_CODE, output));
    LOG.trace("Finished serializing Link Attributes");
}