Example usage for io.netty.buffer Unpooled copiedBuffer

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

Introduction

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

Prototype

public static ByteBuf copiedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new buffer whose content is a merged copy of the specified buffers ' slices.

Usage

From source file:com.streamsets.pipeline.lib.parser.net.BaseNetworkMessageDataParser.java

License:Apache License

private void decodeMessages() throws IOException, DataParserException {
    byte[] bytes;
    if (inputStream != null) {
        bytes = IOUtils.toByteArray(inputStream);
    } else if (reader != null) {
        bytes = IOUtils.toByteArray(reader, charset);
    } else {/*  ww w .java2 s . c om*/
        throw new IllegalStateException(
                "Both inputStream and reader were null in BaseNetworkMessageDataParser#decodeMessages");
    }

    ByteBuf buffer = Unpooled.copiedBuffer(bytes);
    if (maxObjectLen > 0) {
        buffer.capacity(maxObjectLen);
    }
    if (readerOffset != null && readerOffset > 0) {
        buffer.readerIndex(readerOffset.intValue());
    }
    try {
        performDecode(buffer);
        decodedMessagesIter = decodedMessages.listIterator();
        decoded = true;
    } catch (OnRecordErrorException e) {
        throw new DataParserException(Errors.SYSLOG_20, e.getMessage(), e);
    }
}

From source file:com.streamsets.pipeline.lib.parser.net.netflow.TestNetflowDecoder.java

License:Apache License

@Test
public void senderAndReceiver() throws IOException, OnRecordErrorException {
    final NetflowCommonDecoder decoder = makeNetflowDecoder();

    final byte[] bytes = getV9MessagesBytes7Flows();
    final List<BaseNetflowMessage> messages = new LinkedList<>();
    final InetSocketAddress senderAddr = InetSocketAddress.createUnresolved("hostA", 1234);
    final InetSocketAddress recipientAddr = InetSocketAddress.createUnresolved("hostB", 5678);
    decoder.decodeStandaloneBuffer(Unpooled.copiedBuffer(bytes), messages, senderAddr, recipientAddr);

    assertThat(messages, hasSize(7));//  w  w w .jav  a 2  s. com
    final BaseNetflowMessage firstBaseMsg = messages.get(0);
    assertThat(firstBaseMsg, instanceOf(NetflowV9Message.class));
    final NetflowV9Message firstMsg = (NetflowV9Message) firstBaseMsg;
    assertThat(firstMsg.getSender(), notNullValue());
    assertThat(firstMsg.getRecipient(), notNullValue());
    assertThat(firstMsg.getSender().toString(), equalTo(senderAddr.toString()));
    assertThat(firstMsg.getRecipient().toString(), equalTo(recipientAddr.toString()));

    Record record = RecordCreator.create();
    firstMsg.populateRecord(record);

    assertThat(record.get("/" + NetflowV9Message.FIELD_SENDER), fieldWithValue(senderAddr.toString()));
    assertThat(record.get("/" + NetflowV9Message.FIELD_RECIPIENT), fieldWithValue(recipientAddr.toString()));
}

From source file:com.streamsets.pipeline.lib.parser.net.TestDelimitedLengthFieldBasedFrameDecoder.java

License:Apache License

private void writeStringAndAssert(EmbeddedChannel channel, String value, Charset charset,
        boolean randomlyPartition, boolean expectFrameTooLarge) {
    String frame = makeFrame(value, charset);

    try {/* w w  w.j a  va2s . c o  m*/
        if (randomlyPartition) {
            for (List<Byte> chunk : getRandomByteSlices(frame.getBytes())) {
                channel.writeInbound(Unpooled.copiedBuffer(Bytes.toArray(chunk)));
            }
        } else {
            channel.writeInbound(Unpooled.copiedBuffer(frame, charset));
        }
    } catch (TooLongFrameException e) {
        if (!expectFrameTooLarge) {
            Assert.fail("TooLongFrameException unexpectedly thrown");
        } else {
            Assert.assertNull(channel.readInbound());
        }
    }
    if (!expectFrameTooLarge) {
        ByteBuf in = (ByteBuf) channel.readInbound();
        Assert.assertEquals(value, in.toString(charset));
        in.release();
    }
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TCPServerSource.java

License:Apache License

private DelimiterBasedFrameDecoder buildDelimiterBasedFrameDecoder(String recordSeparatorStr) {
    final byte[] delimiterBytes = StringEscapeUtils.unescapeJava(recordSeparatorStr).getBytes();
    return new DelimiterBasedFrameDecoder(config.maxMessageSize, true, Unpooled.copiedBuffer(delimiterBytes));
}

From source file:com.stremebase.examples.todomvc.HttpRouter.java

License:Apache License

private static HttpResponse createResponse(HttpRequest req, Router<Integer> router) {
    RouteResult<Integer> routeResult = router.route(req.getMethod(), req.getUri());

    Integer request = routeResult.target();

    String data = "";
    String mimeType = "";

    if (request == CSS) {
        data = Todo.getCss();//from   w ww.ja v  a  2  s. co m
        mimeType = "text/css";
    } else if (request == ICON) {
        mimeType = "image/x-icon";
    } else if (request == GET) {
        data = Todo.get();
        mimeType = "text/html";
    } else if (request == FILTER) {
        data = Todo.filter(routeResult.pathParams().get("filtertype"));
        mimeType = "text/html";
    } else if (req.getMethod().equals(HttpMethod.POST)) {
        HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req);

        Attribute attribute;

        String item_text = null;
        InterfaceHttpData httpData = decoder.getBodyHttpData("item-text");
        if (httpData != null) {
            attribute = (Attribute) httpData;
            try {
                item_text = attribute.getValue();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String item_id = null;
        httpData = decoder.getBodyHttpData("item-id");
        if (httpData != null) {
            attribute = (Attribute) httpData;
            try {
                item_id = attribute.getValue();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (request == POST) {
            if (item_id == null)
                data = Todo.create(item_text);
            else
                data = Todo.save(Long.valueOf(item_id), item_text);
        } else if (request == DELETE) {
            data = Todo.delete(Long.valueOf(item_id));
        } else if (request == DELETECOMPLETED) {
            data = Todo.clearCompleted();
        } else if (request == TOGGLESTATUS)
            data = Todo.toggleStatus(Long.valueOf(item_id));

        mimeType = "text/html";
        decoder.destroy();
    }

    FullHttpResponse res;

    if (request == NOTFOUND) {
        res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.TEMPORARY_REDIRECT);
        res.headers().add(HttpHeaders.Names.LOCATION, "/");
        return res;
    }

    if (request == ICON)
        res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
                Unpooled.copiedBuffer(Todo.favicon));
    else
        res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
                Unpooled.copiedBuffer(data, CharsetUtil.UTF_8));

    res.headers().set(HttpHeaders.Names.CONTENT_TYPE, mimeType);
    res.headers().set(HttpHeaders.Names.CONTENT_LENGTH, res.content().readableBytes());
    if (request == CSS || request == ICON)
        setDateAndCacheHeaders(res);

    return res;
}

From source file:com.talent.balance.backend.decode.BackendResponseDecoder.java

License:Open Source License

@Override
public PacketWithMeta<Packet> decode(ByteBuf buffer, ChannelContext channelContext) throws DecodeException {
    BalancePacket backendResponsePacket = new BalancePacket();

    if (MysqlExt.getHandshakePacket(channelContext) == null
            && BackendExt.PROTOCOL_MYSQL.equals(channelContext.getProtocol())) {
        HandshakePacket handshakePacket = new HandshakePacket();
        PacketWithMeta<Packet> pwm = handshakePacket.decode(buffer);
        return pwm;
    }//w  ww  .jav  a  2 s . c o m

    backendResponsePacket.setBuffer(Unpooled.copiedBuffer(buffer));

    List<Packet> packets = new ArrayList<Packet>();
    packets.add(backendResponsePacket);

    PacketWithMeta<Packet> packetWithMeta = new PacketWithMeta<Packet>();
    buffer.readerIndex(buffer.capacity());
    packetWithMeta.setPacketLenght(buffer.capacity());
    packetWithMeta.setPackets(packets);
    return packetWithMeta;
}

From source file:com.talent.balance.frontend.decode.FrontendRequestDecoder.java

License:Open Source License

@Override
public PacketWithMeta decode(ByteBuf buffer, ChannelContext channelContext) throws DecodeException {
    BalancePacket frontendRequestPacket = new BalancePacket();
    frontendRequestPacket.setBuffer(Unpooled.copiedBuffer(buffer));

    List<Packet> ret = new ArrayList<Packet>();
    ret.add(frontendRequestPacket);//from  w  ww .j  a va  2s.  c  o m

    PacketWithMeta packetWithMeta = new PacketWithMeta();
    buffer.readerIndex(buffer.capacity());
    packetWithMeta.setPacketLenght(buffer.capacity());
    packetWithMeta.setPackets(ret);
    return packetWithMeta;
}

From source file:com.talent.nio.communicate.receive.ChannelReader.java

License:Open Source License

public static ByteBuf read(SocketChannel socketChannel, ChannelContext channelContext) throws IOException {
    byteBuffer.clear(); // ??buffer?

    if (log.isDebugEnabled()) {
        log.debug("{},buffer before read:{}", channelContext, byteBuffer);
    }//w  ww  . ja  v  a 2  s . c o m

    int countOfRead = socketChannel.read(byteBuffer);

    long receivedBytes = channelContext.getStatVo().getReceivedBytes() + countOfRead;
    channelContext.getStatVo().setReceivedBytes(receivedBytes);
    if (log.isDebugEnabled()) {
        log.debug("{} has received {} bytes", channelContext.getId(), receivedBytes);

        log.debug("{},buffer after read:{}", channelContext, byteBuffer);
    }

    if (countOfRead == -1) {
        String string = "count of read is -1, " + channelContext.getId();
        log.error(string);
        throw new EOFException(string);
        //            NioUtils.remove(channelContext, string);
        //            channelContext.getReadIOErrorHandler().handle(socketChannel, (IOException) null, channelContext, "count of read is -1");
        //            return null;
    }
    if (countOfRead == 0) {
        log.warn("0 byte read {}", channelContext.getId());
        return null;
    }

    channelContext.getStatVo().setCurrentReceivedTime(SystemTimer.currentTimeMillis());

    receivedByteCount.addAndGet(countOfRead);
    if (log.isDebugEnabled()) {
        log.debug(("received {} bytes, all received {} bytes"), countOfRead, receivedByteCount.get());
    }

    // byte[] datas = new byte[byteBuffer.position()];
    byteBuffer.flip(); // ?
    // byteBuffer.get(datas); // ?buffer

    ByteBuf buf1 = Unpooled.copiedBuffer(byteBuffer);

    readCount.incrementAndGet();
    channelContext.getStatVo().getReadCount().incrementAndGet();

    return buf1;

}

From source file:com.talent.nio.communicate.receive.DecodeRunnable.java

License:Open Source License

/**
 * @param args/*from w ww  .java2 s.co  m*/
 */
public static void main(String[] args) {
    byte[] bs1 = new byte[] { 1, 10, 11, 12 };
    byte[] bs2 = new byte[] { 2, 2, 2, 2 };
    byte[] bs3 = new byte[] { 3, 3, 3, 3 };
    byte[] bs4 = new byte[] { 4, 4, 4, 4 };
    byte[] bs5 = new byte[] { 5, 5, 5, 5 };
    byte[] bs6 = new byte[] { 6, 6, 6, 6 };

    ByteBuffer buffer1 = ByteBuffer.allocate(1024);
    buffer1.put(bs1);
    buffer1.flip();

    ByteBuf buf1 = Unpooled.copiedBuffer(buffer1);// .copiedBuffer(bs1);

    buffer1.put(bs3);

    ByteBuf buf2 = Unpooled.copiedBuffer(bs2);
    ByteBuf buf3 = Unpooled.copiedBuffer(bs3);
    ByteBuf buf4 = Unpooled.copiedBuffer(bs4);
    ByteBuf buf5 = Unpooled.copiedBuffer(bs5);
    ByteBuf buf6 = Unpooled.copiedBuffer(bs6);

    CompositeByteBuf cb = Unpooled.compositeBuffer();
    cb.addComponents(buf1, buf2, buf3);

    byte dd = cb.getByte(0);

    CompositeByteBuf cb2 = Unpooled.compositeBuffer();
    cb.addComponents(buf4, buf5, buf6);

    // cb.c
    // cb2.writerIndex(128 * 1024);

    cb.addComponent(cb2);

    Long number = cb2.readLong(); // causes IllegalBufferAccessException
                                  // here!

}

From source file:com.talent.nio.utils.ByteUtils.java

License:Open Source License

public static void main(String[] args) throws IOException {
    byte[] bs = "1hello world\r\nhehe".getBytes();
    System.out.println(ArrayUtils.toString(bs));
    ByteBuf buf = Unpooled.copiedBuffer(bs);
    String xString = buf.toString();
    ByteUtils.toLinesList(buf);/*  w  w w  . java  2 s  .  c om*/

    bs = "2hello world hehe\r\n".getBytes();
    buf = Unpooled.copiedBuffer(bs);
    ByteUtils.toLinesList(buf);

    bs = "3hello world\r\nhehe\r\n\r\n".getBytes();
    buf = Unpooled.copiedBuffer(bs);
    ByteUtils.toLinesList(buf);

    bs = "4\rhe\nllo world\r\nhehe".getBytes();
    buf = Unpooled.copiedBuffer(bs);
    ByteUtils.toLinesList(buf);
}