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:com.seagate.kinetic.simulator.io.provider.nio.http.HttpMessageServiceHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

    int contentLength = 0;

    if (msg instanceof HttpRequest) {
        HttpRequest request = (HttpRequest) msg;

        logger.finest("protocol version: " + request.getProtocolVersion());

        logger.finest("host: " + getHost(request, "unknown"));

        logger.finest("REQUEST_URI: " + request.getUri());

        List<Map.Entry<String, String>> headers = request.headers().entries();

        String lenstr = request.headers().get(HttpHeaders.Names.CONTENT_LENGTH);
        contentLength = Integer.parseInt(lenstr);

        logger.finest("content length=" + contentLength);

        if (!headers.isEmpty()) {
            for (Map.Entry<String, String> h : request.headers().entries()) {
                String key = h.getKey();
                String value = h.getValue();
                logger.finest("HEADER: " + key + " = " + value);
            }/*  w  w w  .jav  a  2s.  c om*/
        }

        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
        Map<String, List<String>> params = queryStringDecoder.parameters();
        if (!params.isEmpty()) {
            for (Entry<String, List<String>> p : params.entrySet()) {
                String key = p.getKey();
                List<String> vals = p.getValue();
                for (String val : vals) {
                    logger.finest("PARAM: " + key + " = " + val);
                }
            }

        }

    }

    // create extended builder
    ExtendedMessage.Builder extendedMessage = ExtendedMessage.newBuilder();

    if (msg instanceof HttpContent) {

        HttpContent httpContent = (HttpContent) msg;

        ByteBuf content = httpContent.content();
        if (content.isReadable()) {

            byte[] body = new byte[contentLength];
            content.getBytes(0, body);

            // read from serialized bytes
            extendedMessage.mergeFrom(body);
        }

        // build message
        ExtendedMessage extended = extendedMessage.build();

        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("received request: " + extended);
        }

        // create kinetic message for processing
        KineticMessage km = new KineticMessage();

        // set interface message
        km.setMessage(extended.getInterfaceMessage());

        // get command bytes
        ByteString commandBytes = extendedMessage.getInterfaceMessage().getCommandBytes();

        // build command
        Command.Builder commandBuilder = Command.newBuilder();

        try {
            commandBuilder.mergeFrom(commandBytes);
            km.setCommand(commandBuilder.build());
        } catch (InvalidProtocolBufferException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }

        // set value
        if (extended.hasValue()) {
            km.setValue(extended.getValue().toByteArray());
        }

        // process request
        KineticMessage kmresp = this.lcservice.processRequest(km);

        // construct response message
        ExtendedMessage.Builder extendedResponse = ExtendedMessage.newBuilder();

        // set interface message
        extendedResponse.setInterfaceMessage((Message.Builder) kmresp.getMessage());

        // set value
        if (kmresp.getValue() != null) {
            extendedResponse.setValue(ByteString.copyFrom(kmresp.getValue()));
        }

        // get serialized value
        ByteBuf data = Unpooled.copiedBuffer(extendedResponse.build().toByteArray());

        FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
                data);

        httpResponse.headers().set(CONTENT_TYPE, "application/octet-stream");

        httpResponse.headers().set(HttpHeaders.Names.CONTENT_ENCODING, HttpHeaders.Values.BINARY);

        httpResponse.headers().set(CONTENT_LENGTH, httpResponse.content().readableBytes());

        httpResponse.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);

        // send response message
        ctx.writeAndFlush(httpResponse);

        if (logger.isLoggable(Level.FINEST)) {
            logger.finest("wrote and flushed response: " + kmresp);
        }
    }
}

From source file:com.streamsets.pipeline.lib.parser.udp.separated.SeparatedDataParser.java

License:Apache License

@Override
public List<Record> parse(ByteBuf buf, InetSocketAddress recipient, InetSocketAddress sender)
        throws OnRecordErrorException {

    List<Record> records = new LinkedList<>();

    final byte[] data = new byte[buf.readableBytes()];
    buf.getBytes(0, data);

    for (byte[] chunk : split(data, separatorBytes)) {
        final Record record = context.createRecord(String.format("SeparatedDataParser_%d", recordId++));

        record.set(Field.create(Collections.emptyMap()));
        switch (rawDataMode) {
        case CHARACTER:
            record.set(outputFieldPath, Field.create(new String(chunk, charset)));
            break;
        case BINARY:
            record.set(outputFieldPath, Field.create(chunk));
            break;
        default://  w w w . ja  va  2s  .c  o m
            throw new IllegalStateException(String.format("Unrecognized rawDataMode: %s", rawDataMode.name()));
        }

        if (sender != null) {
            record.getHeader().setAttribute(SENDER_HEADER_ATTR_NAME, sender.toString());
        }
        if (recipient != null) {
            record.getHeader().setAttribute(RECIPIENT_HEADER_ATTR_NAME, recipient.toString());
        }
        records.add(record);
    }

    return records;
}

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

License:Open Source License

/**
 * //from www  . ja v a2 s.com
 * @param buffer
 * @return
 * @throws IOException
 */
public static List<String> toLinesList(ByteBuf buffer) throws IOException {
    List<String> retList = new ArrayList<String>(20);

    int lastPosition = 0;
    int byteCountInOneLine = 0; // 

    byte lastByte = 0; // 
    int length = buffer.capacity();
    for (int i = 0; i < length; i++) {
        byte b = buffer.getByte(i);// .get();
        boolean isLastByte = (length - 1 == i); // ??
        byteCountInOneLine++;

        if (b == '\n') {
            if ((i > 0 && lastByte == '\r')) {
                if (byteCountInOneLine == 2) // ?????
                {
                    retList.add("\r\n"); // 
                } else {
                    byte[] bs1 = new byte[byteCountInOneLine];
                    buffer.getBytes(lastPosition, bs1);
                    String line1 = new String(bs1, "utf-8");
                    retList.add(line1);
                }
            } else {
                if (byteCountInOneLine == 1) // ?????
                {
                    retList.add("\n"); // 
                } else {
                    byte[] bs1 = new byte[byteCountInOneLine];
                    buffer.getBytes(lastPosition, bs1);
                    String line1 = new String(bs1, "utf-8");
                    retList.add(line1);
                }
            }

            byteCountInOneLine = 0;
            lastPosition = i + 1;
        } else if (isLastByte) {
            byte[] bs1 = new byte[byteCountInOneLine];
            buffer.getBytes(lastPosition, bs1);
            String line1 = new String(bs1, "utf-8");
            retList.add(line1);
        }

        lastByte = b;
    }
    return retList;
}

From source file:com.vethrfolnir.game.network.mu.received.ExRequestAuth.java

License:Open Source License

@Override
public void read(MuClient client, ByteBuf buff, Object... params) {

    if (MuNetworkServer.onlineClients() >= capacity) {
        invalidate(buff);//ww w .j av  a 2 s  .com

        client.sendPacket(MuPackets.ExAuthAnswer, AuthResult.ServerIsFull);
        return;
    }

    byte[] data1 = new byte[10];
    byte[] data2 = new byte[10];

    buff.getBytes(4, data1);
    buff.getBytes(14, data2);

    MuCryptUtils.Dec3bit(data1, 0, 10);
    MuCryptUtils.Dec3bit(data2, 0, 10);

    buff.setBytes(4, data1);
    buff.setBytes(14, data2);

    buff.readerIndex(4);

    String user = readS(buff, 10);
    String password = readS(buff, 10);

    buff.readerIndex(38);

    String version = readS(buff, 5);
    String mainSerial = readS(buff, 16);
    System.out.println("user: [" + user + "] pass[" + password + "] - " + " Version:[" + version + "] "
            + " Serial: [" + mainSerial + "]");

    enqueue(client, user, password, version, mainSerial);
}

From source file:com.weibo.api.motan.protocol.grpc.http.NettyHttpRequestHandler.java

License:Apache License

protected DefaultRequest buildRpcRequest(FullHttpRequest httpRequest) throws UnsupportedEncodingException {
    String uri = httpRequest.uri();
    String[] uriInfo = uri.split("\\?");
    String[] serviceInfo = uriInfo[0].split("/");
    if (serviceInfo.length != 4) {
        throw new MotanServiceException("invalid request uri! uri like '/${group}/${service}/${method}'");
    }/*from   w ww.j a  v a  2 s.  c  o m*/
    DefaultRequest rpcRequest = new DefaultRequest();
    rpcRequest.setAttachment(URLParamType.group.getName(), serviceInfo[1]);
    rpcRequest.setInterfaceName(serviceInfo[2]);
    rpcRequest.setMethodName(serviceInfo[3]);

    HashMap<String, String> params = new HashMap<String, String>();
    if (uriInfo.length == 2) {
        addParams(params, uriInfo[1]);
    }
    ByteBuf buf = httpRequest.content();
    final byte[] contentBytes = new byte[buf.readableBytes()];
    buf.getBytes(0, contentBytes);
    String body = new String(contentBytes, "UTF-8");
    addParams(params, body);

    MethodInfo mi = methodDescMap.get(rpcRequest.getMethodName());
    if (mi != null && mi.isDuplicate()) {
        mi = null;
        String paramDesc = params.get("paramDesc");
        if (StringUtils.isBlank(paramDesc)) {
            throw new MotanServiceException(
                    "request method name conflict! paramDesc is required!" + rpcRequest.getMethodName());
        }
        mi = methodDescMap.get(rpcRequest.getMethodName() + paramDesc);

    }
    if (mi == null) {
        throw new MotanServiceException("request method name not found" + rpcRequest.getMethodName());
    }
    rpcRequest.setParamtersDesc(mi.getMethodDesc());
    // TODO other info
    addAttachment(rpcRequest, httpRequest.headers());
    rpcRequest.setArguments(parseArguments(params.get("params"), mi));
    return rpcRequest;
}

From source file:com.weibo.api.motan.transport.netty4.yar.YarMessageHandlerWarpper.java

License:Apache License

@Override
public Object handle(Channel channel, Object message) {
    FullHttpRequest httpRequest = (FullHttpRequest) message;
    String uri = httpRequest.getUri();
    int index = uri.indexOf("?");// should not be null
    String requestPath = uri;/*w  ww . j  a  va  2 s .  com*/
    Map<String, String> attachments = null;
    if (index > -1) {
        requestPath = uri.substring(0, index);
        if (index != uri.length() - 1) {
            attachments = getAttachMents(uri.substring(index + 1, uri.length()));
        }
    }
    YarResponse yarResponse = null;
    String packagerName = "JSON";
    try {
        ByteBuf buf = httpRequest.content();
        final byte[] contentBytes = new byte[buf.readableBytes()];
        buf.getBytes(0, contentBytes);
        YarRequest yarRequest = new AttachmentRequest(YarProtocol.buildRequest(contentBytes), attachments);
        yarRequest.setRequestPath(requestPath);
        yarResponse = (YarResponse) orgHandler.handle(channel, yarRequest);

    } catch (Exception e) {
        LoggerUtil.error("YarMessageHandlerWarpper handle yar request fail.", e);
        yarResponse = YarProtocolUtil.buildDefaultErrorResponse(e.getMessage(), packagerName);
    }
    byte[] responseBytes;
    try {
        responseBytes = YarProtocol.toProtocolBytes(yarResponse);
    } catch (IOException e) {
        throw new MotanFrameworkException("convert yar response to bytes fail.", e);
    }
    FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            Unpooled.wrappedBuffer(responseBytes));
    httpResponse.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
    httpResponse.headers().set(HttpHeaders.Names.CONTENT_LENGTH, httpResponse.content().readableBytes());

    if (HttpHeaders.isKeepAlive(httpRequest)) {
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
    } else {
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, Values.CLOSE);
    }

    return httpResponse;
}

From source file:com.weibo.api.motan.transport.netty4.yar.YarMessageHandlerWarpperTest.java

License:Apache License

private YarResponse getYarResponse(FullHttpResponse httpResponse) throws Exception {
    ByteBuf buf = httpResponse.content();
    final byte[] contentBytes = new byte[buf.readableBytes()];
    buf.getBytes(0, contentBytes);
    YarResponse yarResponse = YarProtocol.buildResponse(contentBytes);
    return yarResponse;
}

From source file:com.weibo.yar.yarserver.HttpServerHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {

    ByteBuf buf = msg.content();
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(0, bytes);

    YarRequest yarRequest = YarProtocol.buildRequest(bytes);
    YarResponse yarResponse = process(yarRequest);

    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
            Unpooled.wrappedBuffer(YarProtocol.toProtocolBytes(yarResponse)));
    response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/x-www-form-urlencoded");
    response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
    if (HttpHeaders.isKeepAlive(msg)) {
        response.headers().set(HttpHeaders.Names.CONNECTION, Values.KEEP_ALIVE);
    }/*  www  .  ja  v  a  2 s  .  c o  m*/
    ctx.write(response);
    ctx.flush();
    ctx.close();
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

@Override
public ByteBuf encode(ByteBuf source) {
    byte[] array;
    int length = source.readableBytes();

    int sizeEstimate = (int) Math.ceil(source.readableBytes() * 1.001) + 14;
    ByteBuf compressed = PooledByteBufAllocator.DEFAULT.heapBuffer(sizeEstimate);

    int offset = 0;
    if (source.hasArray()) {
        array = source.array();/*ww  w  . j  a va  2 s .c  o m*/
        offset = source.arrayOffset() + source.readerIndex();
    } else {
        // If it's a direct buffer, we need to copy it
        array = new byte[length];
        source.getBytes(source.readerIndex(), array);
    }

    synchronized (deflater) {
        deflater.setInput(array, offset, length);
        while (!deflater.needsInput()) {
            deflate(compressed);
        }

        deflater.reset();
    }

    return compressed;
}

From source file:com.yahoo.pulsar.common.compression.CompressionCodecZLib.java

License:Apache License

@Override
public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException {
    ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedLength, uncompressedLength);

    int len = encoded.readableBytes();

    byte[] array;
    int offset;/*  ww w. j ava  2  s  .c  om*/
    if (encoded.hasArray()) {
        array = encoded.array();
        offset = encoded.arrayOffset() + encoded.readerIndex();
    } else {
        array = new byte[len];
        encoded.getBytes(encoded.readerIndex(), array);
        offset = 0;
    }

    int resultLength;
    synchronized (inflater) {
        inflater.setInput(array, offset, len);
        try {
            resultLength = inflater.inflate(uncompressed.array(), uncompressed.arrayOffset(),
                    uncompressedLength);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        inflater.reset();
    }

    checkArgument(resultLength == uncompressedLength);

    uncompressed.writerIndex(uncompressedLength);
    return uncompressed;
}