Example usage for io.vertx.core.buffer Buffer getByteBuf

List of usage examples for io.vertx.core.buffer Buffer getByteBuf

Introduction

In this page you can find the example usage for io.vertx.core.buffer Buffer getByteBuf.

Prototype

@GenIgnore(GenIgnore.PERMITTED_TYPE)
ByteBuf getByteBuf();

Source Link

Document

Returns the Buffer as a Netty ByteBuf .

The returned buffer is a duplicate.

The returned ByteBuf might have its readerIndex > 0 This method is meant for internal use only.

Usage

From source file:com.groupon.vertx.memcache.stream.MemcacheInputStream.java

License:Apache License

/**
 * This method handles processing the incoming Buffer from the NetSocket.  The Buffer
 * is not guaranteed to contain a whole message so this method tracks the current state
 * of the incoming data and notifies the pending commands when enough data has been sent
 * for a response./*  ww w . j  a va  2 s  .  co  m*/
 *
 * @param processBuffer - The Buffer containing the current set of bytes.
 */
public void processBuffer(Buffer processBuffer) {
    if (processBuffer == null || processBuffer.length() == 0) {
        return;
    }

    byte first;
    byte second;

    ByteBuf byteBuf = processBuffer.getByteBuf();

    while (byteBuf.isReadable()) {
        first = byteBuf.readByte();
        if (first == '\r') {
            if (byteBuf.isReadable()) {
                second = byteBuf.readByte();
                if (second == '\n') {
                    addCompletedLine();
                }
                previous = second;
            } else {
                previous = first;
            }
        } else if (first == '\n' && previous == '\r') {
            addCompletedLine();
            previous = first;
        } else {
            buffer.write(first);
            previous = first;
        }
    }
}

From source file:com.groupon.vertx.redis.RedisInputStream.java

License:Apache License

/**
 * This method handles processing the incoming Buffer from the NetSocket.  The Buffer
 * is not guaranteed to contain a whole message so this method tracks the current state
 * of the incoming data and notifies the pending commands when enough data has been sent
 * for a response.//from w w  w.  j  a v a 2 s.c  om
 *
 * @param processBuffer - The Buffer containing the current set of bytes.
 */
public void processBuffer(Buffer processBuffer) {
    if (processBuffer == null || processBuffer.length() == 0) {
        return;
    }

    byte first;
    byte second;

    ByteBuf byteBuf = processBuffer.getByteBuf();

    while (byteBuf.isReadable()) {
        first = byteBuf.readByte();
        if (first == '\r' && byteBuf.isReadable()) {
            second = byteBuf.readByte();
            if (second == '\n') {
                byte[] line = new byte[bufferPosition];
                System.arraycopy(buffer, 0, line, 0, line.length);
                addCompletedLine(line);
            } else {
                buffer[bufferPosition++] = first;
                buffer[bufferPosition++] = second;
            }
        } else if (first == '\n' && buffer[bufferPosition - 1] == '\r') {
            byte[] line = new byte[bufferPosition - 1];
            System.arraycopy(buffer, 0, line, 0, line.length);
            addCompletedLine(line);
        } else {
            buffer[bufferPosition++] = first;
        }
    }
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static void binaryEncodeBYTEA(Buffer value, ByteBuf buff) {
    ByteBuf byteBuf = value.getByteBuf();
    buff.writeBytes(byteBuf);
}

From source file:io.servicecomb.codec.protobuf.utils.WrapSchema.java

License:Apache License

@SuppressWarnings("unchecked")
default <T> T readObject(Buffer buffer, ProtobufFeature protobufFeature) throws Exception {
    if (buffer == null || buffer.length() == 0) {
        // void??null
        // ,protobuf?1
        return (T) readFromEmpty();
    }//from ww  w .ja  va 2  s .  com

    ByteBuffer nioBuffer = buffer.getByteBuf().nioBuffer();
    Input input = new ByteBufferInput(nioBuffer, false);

    ProtobufFeatureUtils.setProtobufFeature(protobufFeature);
    try {
        return (T) readObject(input);
    } finally {
        ProtobufFeatureUtils.removeProtobufFeature();
    }
}

From source file:io.servicecomb.common.rest.codec.produce.ProduceProcessor.java

License:Apache License

default Object decodeResponse(Buffer buffer, JavaType type) throws Exception {
    if (buffer.length() == 0) {
        return null;
    }// w w  w.j a v  a2 s  .com

    try (BufferInputStream input = new BufferInputStream(buffer.getByteBuf())) {
        return doDecodeResponse(input, type);
    }
}

From source file:io.servicecomb.foundation.vertx.server.TcpParser.java

License:Apache License

protected void onParse(Buffer buffer) {
    switch (status) {
    case TCP_HEADER:
        ByteBuf buf = buffer.getByteBuf();
        if (!firstNEqual(TCP_MAGIC, buf.array(), TCP_MAGIC.length)) {
            reset();//from w w w  . j  a  v a2 s .  c  o m
            return;
        }

        buf.skipBytes(TCP_MAGIC.length);
        msgId = buf.readLong();
        totalLen = buf.readInt();
        headerLen = buf.readInt();

        if (totalLen == 0) {
            onReadOnePackage(null, null);
            return;
        }

        parser.fixedSizeMode(totalLen);
        status = ParseStatus.TCP_PAYLOAD;
        break;

    case TCP_PAYLOAD:
        Buffer headerBuffer = buffer.slice(0, headerLen);
        Buffer bodyBuffer = buffer.slice(headerLen, buffer.length());
        onReadOnePackage(headerBuffer, bodyBuffer);
        break;

    default:
        break;
    }
}

From source file:io.servicecomb.foundation.vertx.VertxUtils.java

License:Apache License

public static byte[] getBytesFast(Buffer buffer) {
    ByteBuf byteBuf = buffer.getByteBuf();
    return getBytesFast(byteBuf);
}

From source file:io.servicecomb.transport.highway.HighwayServerInvoke.java

License:Apache License

private void sendResponse(Map<String, String> context, Response response) {
    ResponseHeader header = new ResponseHeader();
    header.setStatusCode(response.getStatusCode());
    header.setReasonPhrase(response.getReasonPhrase());
    header.setContext(context);/*from   ww  w.ja va2 s  .co  m*/
    header.setHeaders(response.getHeaders());

    WrapSchema bodySchema = operationProtobuf.findResponseSchema(response.getStatusCode());
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }

    try {
        Buffer respBuffer = HighwayCodec.encodeResponse(msgId, header, bodySchema, body, protobufFeature);
        connection.write(respBuffer.getByteBuf());
    } catch (Exception e) {
        // 
        String msg = String.format("encode response failed, %s, msgId=%d",
                operationProtobuf.getOperationMeta().getMicroserviceQualifiedName(), msgId);
        LOGGER.error(msg, e);
    }
}

From source file:io.techcode.logbulk.pipeline.output.SyslogOutput.java

License:Open Source License

@Override
public Handler<JsonObject> encoder() {
    return data -> {
        Buffer buf = Buffer.buffer();

        // Add facility
        buf.appendString("<");
        String facility = mapping.get(SyslogHeader.FACILITY);
        String severity = mapping.get(SyslogHeader.SEVERITY);
        if (mapping.containsKey(SyslogHeader.FACILITY) && mapping.containsKey(SyslogHeader.SEVERITY)
                && data.containsKey(facility) && data.containsKey(severity)) {
            int acc = data.getInteger(facility) * 8 + data.getInteger(severity);
            buf.appendString(String.valueOf(acc));
        }//from  w w w  .  ja v a  2s .c  o  m
        buf.appendString(">1 ");

        // Add timestamp
        populate(buf, SyslogHeader.TIMESTAMP, data);

        // Add hostname
        populate(buf, SyslogHeader.HOST, data);

        // Add application
        populate(buf, SyslogHeader.APPLICATION, data);

        // Add processus
        populate(buf, SyslogHeader.PROCESSUS, data);

        // Add msg id
        populate(buf, SyslogHeader.ID, data);

        // Add structured data
        populate(buf, SyslogHeader.DATA, data);

        // Add message
        String message = mapping.get(SyslogHeader.MESSAGE);
        if (mapping.containsKey(SyslogHeader.MESSAGE)) {
            if (data.containsKey(message)) {
                buf.appendString(String.valueOf(data.getValue(message)));
            } else {
                try {
                    Json.mapper.writeValue(new ByteBufOutputStream(buf.getByteBuf()), data.getMap());
                } catch (Exception e) {
                    throw new EncodeException("Failed to encode as JSON: " + e.getMessage());
                }
            }
        }

        getConnection().write(framing(buf));
    };
}

From source file:org.apache.servicecomb.codec.protobuf.utils.WrapSchema.java

License:Apache License

@SuppressWarnings("unchecked")
default <T> T readObject(Buffer buffer) throws Exception {
    if (buffer == null || buffer.length() == 0) {
        // void??null
        // ,protobuf?1
        return (T) readFromEmpty();
    }/*from  w w  w .j  ava  2 s.c o  m*/

    ByteBuffer nioBuffer = buffer.getByteBuf().nioBuffer();
    Input input = new ByteBufferInput(nioBuffer, false);

    return (T) readObject(input);
}