Java Code Examples for org.jboss.netty.buffer.ChannelBuffers#copiedBuffer()

The following examples show how to use org.jboss.netty.buffer.ChannelBuffers#copiedBuffer() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: onos   File: OspfMessageReaderTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tests readFromBuffer() method.
 */
@Test
public void testReadFromBuffer() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet1));
    ospfMessageReader.readFromBuffer(channelBuffer);

    channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet2));
    ospfMessageReader.readFromBuffer(channelBuffer);

    channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet3));
    ospfMessageReader.readFromBuffer(channelBuffer);

    channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet4));
    ospfMessageReader.readFromBuffer(channelBuffer);

    channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet5));
    ospfMessageReader.readFromBuffer(channelBuffer);
    assertThat(ospfMessageReader, is(notNullValue()));
}
 
Example 2
Source Project: onos   File: L1L2HelloPduTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests l1l2HelloPduBody() method.
 */
@Test
public void testL1l2HelloPduBody() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(helloL1L2);
    l1L2HelloPdu.readFrom(channelBuffer);
    result = l1L2HelloPdu.l1l2HelloPduBody();
    assertThat(result, is(notNullValue()));
}
 
Example 3
Source Project: onos   File: PsnpTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests readFrom() method.
 */
@Test
public void testReadFrom() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(psnpPkt);
    psnp.readFrom(channelBuffer);
    assertThat(psnp, is(notNullValue()));
}
 
Example 4
/**
 * Tests asBytes() method.
 */
@Test
public void testAsBytes() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(internalReachability);
    reachability.readFrom(channelBuffer);
    result3 = reachability.asBytes();
    assertThat(result3, is(notNullValue()));
}
 
Example 5
Source Project: hadoop   File: XDR.java    License: Apache License 2.0 5 votes vote down vote up
/** Write an XDR message to a TCP ChannelBuffer */
public static ChannelBuffer writeMessageTcp(XDR request, boolean last) {
  Preconditions.checkState(request.state == XDR.State.WRITING);
  ByteBuffer b = request.buf.duplicate();
  b.flip();
  byte[] fragmentHeader = XDR.recordMark(b.limit(), last);
  ByteBuffer headerBuf = ByteBuffer.wrap(fragmentHeader);

  // TODO: Investigate whether making a copy of the buffer is necessary.
  return ChannelBuffers.copiedBuffer(headerBuf, b);
}
 
Example 6
/**
 * Tests asBytes() method.
 */
@Test
public void testAsBytes() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(tlv);
    ipExtendedReachabilityTlv.readFrom(channelBuffer);
    ipExtendedReachabilityTlv.setPrefix(prefix);
    result3 = ipExtendedReachabilityTlv.asBytes();
    assertThat(result3, is(notNullValue()));
}
 
Example 7
Source Project: onos   File: OspfUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests readLsaHeader() method.
 */
@Test
public void testReadLsaHeader() throws Exception {
    byte[] header = {0, 10, 2, 1, 7, 7, 7, 7, 7, 7, 7, 7, -128, 0, 0, 2, 46, -126, 0,
            48, 0, 0, 0, 2, 1, 1, 1, 1, 10, 10, 10, 7, 1, 0, 0, 10, 10, 10, 10, 0, -1, -1, -1,
            0, 3, 0, 0, 10, 0, 10, 66, 10, 1, 0, 0, 1, 7, 7, 7, 7, -128, 0, 0, 1, -64, 79, 0,
            116, 0, 1, 0, 4, 0, 0, 0, 0, 0, 2, 0, 84, 0, 1, 0, 1, 1, 0, 0, 0, 0, 2, 0, 4, 10,
            10, 10, 0, 0, 5, 0, 4, 0, 0, 0, 0, 0, 6, 0, 4, 73, -104, -106, -128, 0, 7, 0, 4, 73,
            -104, -106, -128, 0, 8, 0, 32, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106,
            -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128, 73, -104, -106, -128,
            73, -104, -106, -128, 0, 9, 0, 4, 0, 0, 0, 0};
    channelBuffer = ChannelBuffers.copiedBuffer(header);
    lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
    assertThat(lsaHeader, is(notNullValue()));
}
 
Example 8
Source Project: onos   File: PsnpTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests lasBytes() method.
 */
@Test
public void testAsBytes() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(psnpPkt);
    psnp.readFrom(channelBuffer);
    result = psnp.asBytes();
    assertThat(result, is(notNullValue()));
}
 
Example 9
/**
 * Tests readFrom() method.
 */
@Test
public void testReadFrom() throws Exception {
    header = new TlvHeader();
    header.setTlvLength(4);
    header.setTlvType(5);
    channelBuffer = ChannelBuffers.copiedBuffer(packet);
    trafficEngineeringMetric = new TrafficEngineeringMetric(header);
    trafficEngineeringMetric.readFrom(channelBuffer);
    assertThat(trafficEngineeringMetric, is(notNullValue()));
}
 
Example 10
Source Project: onos   File: OspfUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests to readLsaHeader method.
 */
@Test
public void testReadLsaHeader1() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(opaqueheader);
    lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
    assertThat(lsaHeader, is(notNullValue()));
}
 
Example 11
Source Project: onos   File: PaddingTlvTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests readFrom() method.
 */
@Test
public void testReadFrom() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(tlv);
    paddingTlv.readFrom(channelBuffer);
    assertThat(paddingTlv, is(notNullValue()));
}
 
Example 12
Source Project: tajo   File: TajoPullServerService.java    License: Apache License 2.0 5 votes vote down vote up
private void sendError(ChannelHandlerContext ctx, String message,
    HttpResponseStatus status) {
  HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
  response.setHeader(Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
  // Put shuffle version into http header
  ChannelBuffer content = ChannelBuffers.copiedBuffer(message, CharsetUtil.UTF_8);
  response.setContent(content);
  response.setHeader(Names.CONTENT_LENGTH, content.writerIndex());

  // Close the connection as soon as the error message is sent.
  ctx.getChannel().write(response).addListener(ChannelFutureListener.CLOSE);
}
 
Example 13
Source Project: restcommander   File: PlayHandler.java    License: Apache License 2.0 5 votes vote down vote up
protected static void writeResponse(ChannelHandlerContext ctx, Response response, HttpResponse nettyResponse, HttpRequest nettyRequest) {
    if (Logger.isTraceEnabled()) {
        Logger.trace("writeResponse: begin");
    }

    byte[] content = null;

    final boolean keepAlive = isKeepAlive(nettyRequest);
    if (nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
        content = new byte[0];
    } else {
        content = response.out.toByteArray();
    }

    ChannelBuffer buf = ChannelBuffers.copiedBuffer(content);
    nettyResponse.setContent(buf);

    if (Logger.isTraceEnabled()) {
        Logger.trace("writeResponse: content length [" + response.out.size() + "]");
    }

    setContentLength(nettyResponse, response.out.size());

    ChannelFuture f = ctx.getChannel().write(nettyResponse);

    // Decide whether to close the connection or not.
    if (!keepAlive) {
        // Close the connection when the whole content is written out.
        f.addListener(ChannelFutureListener.CLOSE);
    }
    if (Logger.isTraceEnabled()) {
        Logger.trace("writeResponse: end");
    }
}
 
Example 14
Source Project: onos   File: OspfUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests readLsaHeader() method.
 */
@Test
public void testReadLsaHeader2() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(packet);
    lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
    assertThat(lsaHeader, is(notNullValue()));
}
 
Example 15
Source Project: onos   File: LsPduTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests asBytes()  method.
 */
@Test
public void testAsBytes() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(l1Lsp);
    lsPdu.readFrom(channelBuffer);
    result = lsPdu.asBytes();
    assertThat(result, is(notNullValue()));
}
 
Example 16
Source Project: onos   File: NetworkLsaTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests readFrom() method.
 */
@Test(expected = Exception.class)
public void testReadFrom1() throws Exception {
    byte[] temp = {0, 0, 0};
    inputByteArray = temp;
    lsaHeader = createLsaHeader();
    networkLsa = new NetworkLsa(lsaHeader);
    channelBuffer = ChannelBuffers.copiedBuffer(inputByteArray);
    networkLsa.readFrom(channelBuffer);
    assertThat(networkLsa, is(notNullValue()));
}
 
Example 17
Source Project: onos   File: OspfUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests readLsaHeader() method.
 */
@Test
public void testReadreadLsaHeader() throws Exception {
    byte[] header = {0, 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, 0, 1, 58, -100, 0, 48};
    channelBuffer = ChannelBuffers.copiedBuffer(header);
    lsaHeader = OspfUtil.readLsaHeader(channelBuffer);
    assertThat(lsaHeader, is(notNullValue()));
}
 
Example 18
Source Project: onos   File: PsnpTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests partialSequenceNumberPduBody() method.
 */
@Test
public void testPartialSequenceNumberPduBody() throws Exception {
    channelBuffer = ChannelBuffers.copiedBuffer(psnpPkt);
    psnp.readFrom(channelBuffer);
    result = psnp.partialSequenceNumberPduBody();
    assertThat(result, is(notNullValue()));
}
 
Example 19
/**
 * Tests messageReceived() method.
 */
@Test
public void testMessageReceived() throws Exception {
    ospfInterface.setIpAddress(Ip4Address.valueOf("11.11.11.11"));
    ospfInterface.setInterfaceType(2);
    ospfArea.setAreaId(Ip4Address.valueOf("13.13.13.13"));
    channelHandlerContext = EasyMock.createMock(ChannelHandlerContext.class);
    MessageEvent messageEvent = new MessageEvent() {
        @Override
        public Object getMessage() {
            helloPacket = new HelloPacket();
            helloPacket.setSourceIp(Ip4Address.valueOf("1.1.1.1"));
            helloPacket.setRouterId(Ip4Address.valueOf("10.10.10.10"));
            helloPacket.setOspfVer(2);
            helloPacket.setAreaId(Ip4Address.valueOf("12.12.12.12"));
            helloPacket.setOptions(2);
            helloPacket.setAreaId(Ip4Address.valueOf("5.5.5.5"));
            helloPacket.setNetworkMask(Ip4Address.valueOf("3.3.3.3"));
            helloPacket.setOspftype(1);
            helloPacket.setAuthType(0);
            helloPacket.setAuthentication(0);
            checksumCalculator = new ChecksumCalculator();
            byteArray = helloPacket.asBytes();
            helloPacket.setOspfPacLength(byteArray.length);
            checkArray = checksumCalculator.calculateOspfCheckSum(byteArray, 12, 13);
            buf = ChannelBuffers.copiedBuffer(checkArray);
            helloPacket.setChecksum(buf.readUnsignedShort());
            List<HelloPacket> messPackets = new ArrayList<>();
            messPackets.add(helloPacket);
            return messPackets;
        }

        @Override
        public SocketAddress getRemoteAddress() {
            return null;
        }

        @Override
        public Channel getChannel() {
            return null;
        }

        @Override
        public ChannelFuture getFuture() {
            return null;
        }
    };
    ospfInterfaceChannelHandler.messageReceived(channelHandlerContext, messageEvent);
    assertThat(ospfInterfaceChannelHandler, is(notNullValue()));
}
 
Example 20
Source Project: onos   File: LinkTlvTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests readFrom() method.
 */
@Test
public void testReadFrom() throws Exception {

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(9);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet1);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(1);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet2);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(2);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet3);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(3);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet4);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(4);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet5);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(5);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet1);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(6);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet6);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(7);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet7);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));

    header = new TlvHeader();
    header.setTlvLength(8);
    header.setTlvType(8);
    linkTlv = new LinkTlv(header);
    channelBuffer = ChannelBuffers.copiedBuffer(packet8);
    linkTlv.readFrom(channelBuffer);
    assertThat(linkTlv, is(notNullValue()));
    assertThat(linkTlv, instanceOf(LinkTlv.class));
}