Example usage for io.netty.buffer ByteBuf readSlice

List of usage examples for io.netty.buffer ByteBuf readSlice

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readSlice.

Prototype

public abstract ByteBuf readSlice(int length);

Source Link

Document

Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessage.java

License:Apache License

/**
 * Convenience method for reading a {@link ZMTPMessage} from a {@link ByteBuf}.
 *///from  w  w  w  . ja va 2 s. c o  m
public static ZMTPMessage read(final ByteBuf in, final ZMTPVersion version) throws ZMTPParsingException {
    final int mark = in.readerIndex();
    final ZMTPWireFormat wireFormat = ZMTPWireFormats.wireFormat(version);
    final ZMTPWireFormat.Header header = wireFormat.header();
    final RecyclableArrayList frames = RecyclableArrayList.newInstance();
    while (true) {
        final boolean read = header.read(in);
        if (!read) {
            frames.recycle();
            in.readerIndex(mark);
            return null;
        }
        if (in.readableBytes() < header.length()) {
            frames.recycle();
            in.readerIndex(mark);
            return null;
        }
        if (header.length() > Integer.MAX_VALUE) {
            throw new ZMTPParsingException("frame is too large: " + header.length());
        }
        final ByteBuf frame = in.readSlice((int) header.length());
        frame.retain();
        frames.add(frame);
        if (!header.more()) {
            @SuppressWarnings("unchecked")
            final ZMTPMessage message = ZMTPMessage.from((List<ByteBuf>) (Object) frames);
            frames.recycle();
            return message;
        }
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPMessageDecoder.java

License:Apache License

@Override
public void content(final ChannelHandlerContext ctx, final ByteBuf data, final List<Object> out) {
    // Wait for more data?
    if (data.readableBytes() < frameLength) {
        return;/*from   w  w  w. java2s  .  c om*/
    }

    if (frameLength == 0) {
        frames.add(DELIMITER);
        return;
    }

    final ByteBuf frame = data.readSlice(frameLength);
    frame.retain();
    frames.add(frame);
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

License:Open Source License

public static String getLengthCodedString(ByteBuf cb, Charset decoder) {

    if (isLengthCodedLongNull(cb))
        return null;

    int length = (int) getLengthCodedLong(cb);
    if (length == 0)
        return StringUtils.EMPTY;

    return cb.readSlice((int) length).toString(decoder);
}

From source file:com.tesora.dve.db.mysql.libmy.MyHandshakeV10.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    protocolVersion = cb.readByte();/*w  w  w .  ja v a  2  s . c  o m*/
    serverVersion = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
    cb.skipBytes(1); // skip the NULL terminator
    threadID = cb.readInt();
    scrambleBuffer1st = MysqlAPIUtils.readBytesAsString(cb, 8, CharsetUtil.ISO_8859_1);
    cb.skipBytes(1);
    long sc1 = cb.readUnsignedShort();
    serverCharset = cb.readByte();
    serverStatus = cb.readShort();
    long sc2 = Long.rotateLeft(cb.readUnsignedShort(), 16);
    setServerCapabilities(sc1 + sc2);
    scrambleBufferSize = new Integer(cb.readByte());
    cb.skipBytes(10); //unused bytes
    scrambleBuffer2nd = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.ISO_8859_1);
    cb.skipBytes(1);
    if ((serverCapabilitiesasLong
            & ClientCapabilities.CLIENT_PLUGIN_AUTH) == ClientCapabilities.CLIENT_PLUGIN_AUTH) {
        plugInProvidedData = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
    }
}

From source file:com.tesora.dve.db.mysql.libmy.MyLoginRequest.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    clientCapabilities = cb.readUnsignedInt();
    boolean hasConnectDatabase = ((clientCapabilities
            & ClientCapabilities.CLIENT_CONNECT_WITH_DB) == ClientCapabilities.CLIENT_CONNECT_WITH_DB);
    maxPacketSize = cb.readInt();//  w w  w .j  av  a 2 s  . c o m
    clientCharset = cb.readByte();

    cb.skipBytes(23); // login request has a 23 byte filler
    username = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
    cb.skipBytes(1); // skip the NULL terminator
    byte passwordLength = cb.readByte();
    byte[] passwordBytes = new byte[passwordLength];
    cb.getBytes(cb.readerIndex(), passwordBytes, 0, passwordLength);
    password = new String(passwordBytes, CharsetUtil.ISO_8859_1);
    cb.skipBytes(passwordLength);
    // if the clientCapabilities flag has the CLIENT_CONNECT_WITH_DB bit set,
    // then this message contains an initial database to connect to
    if (hasConnectDatabase) {
        database = cb.readSlice(cb.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
        if (database.length() < 1) {
            database = null;
        }
    }
}

From source file:com.tesora.dve.db.mysql.portal.protocol.MSPAuthenticateV10MessageMessage.java

License:Open Source License

@Override
protected ParsedData unmarshall(ByteBuf source) {
    ParsedData parseValues = new ParsedData();
    parseValues.caps = new ClientCapabilities(source.readUnsignedInt());
    parseValues.maxPacketSize = source.readInt();
    parseValues.charsetID = source.readByte();

    source.skipBytes(23); // login request has a 23 byte filler
    parseValues.username = source.readSlice(source.bytesBefore((byte) 0)).toString(CharsetUtil.UTF_8);
    source.skipBytes(1); // skip the NULL terminator
    byte passwordLength = source.readByte();
    parseValues.password = source.readSlice(passwordLength).toString(CharsetUtil.ISO_8859_1);

    // if the clientCapabilities flag has the CLIENT_CONNECT_WITH_DB bit set,
    // then this message contains an initial database to connect to
    if (parseValues.caps.connectWithDB()) {
        parseValues.initialDatabase = source.readSlice(source.bytesBefore((byte) 0))
                .toString(CharsetUtil.UTF_8);
        source.skipBytes(1); // skip the NULL terminator
    } else {/*from  w  w w  . java 2 s .c o m*/
        parseValues.initialDatabase = "";
    }
    return parseValues;
}

From source file:com.tesora.dve.db.mysql.portal.protocol.Packet.java

License:Open Source License

public boolean decodeMore(ByteBuf input) {
    if (!sealed) {
        int transferToHeader = Math.min(header.writableBytes(), input.readableBytes());
        input.readBytes(header, transferToHeader);

        if (header.readableBytes() < HEADER_LENGTH) {
            return false;//we don't have enough to read the header.
        }/*from ww  w. j a  v  a 2 s .c o m*/

        int chunkLength = header.getUnsignedMedium(0);
        lastDecodedSeq = header.getUnsignedByte(3);

        int chunkAlreadyReceived = payload.writerIndex() - totalFullBytes;
        int chunkExpecting = chunkLength - chunkAlreadyReceived;
        int payloadTransfer = Math.min(chunkExpecting, input.readableBytes());

        ByteBuf slice = input.readSlice(payloadTransfer).retain();

        payload.addComponent(slice);
        payload.writerIndex(payload.writerIndex() + slice.readableBytes()); //need to move the writer index, doesn't happen automatically.

        //recalculate how much we are expecting for this chunk.
        chunkAlreadyReceived = payload.writerIndex() - totalFullBytes;
        chunkExpecting = chunkLength - chunkAlreadyReceived;

        if (chunkExpecting == 0) {
            if (lastDecodedSeq != expectedSequence) {
                String message = context + " , sequence problem decoding packet, expected=" + expectedSequence
                        + " , decoded=" + lastDecodedSeq;
                logger.warn(message);
            }
            expectedSequence++;
            //finished this packet, mark how many full packet bytes we've read.
            totalFullBytes = payload.writerIndex();
            if (chunkLength < MAX_PAYLOAD) {
                sealed = true;
            } else {
                //an extended packet was indicated, prepare the read of the next packet.
                header.clear();
            }
        }
    }
    return sealed;
}

From source file:com.tesora.dve.db.mysql.portal.protocol.Packet.java

License:Open Source License

public static int encodeFullPayload(int sequenceStart, ByteBuf payloadHolder, ByteBuf destination) {
    ByteBuf leBuf = destination.order(ByteOrder.LITTLE_ENDIAN);

    //TODO: this loop is identical to the one below, except it doesn't consume the source or update the destination.  Consolidate?
    //calculate the size of the final encoding, so we resize the destination at most once.
    int payloadRemaining = payloadHolder.readableBytes();
    int outputSize = 0;
    do {//from  ww w.  j a v  a  2 s  .  c o m
        outputSize += 4; //header
        int chunkLength = Math.min(payloadRemaining, MAX_PAYLOAD);
        outputSize += chunkLength;
        payloadRemaining -= chunkLength;
        if (chunkLength == MAX_PAYLOAD)
            outputSize += 4; //need one more packet if last fragment is exactly 0xFFFF long.
    } while (payloadRemaining > 0);

    leBuf.ensureWritable(outputSize);

    int sequenceIter = sequenceStart;
    boolean lastChunkWasMaximumLength;
    do {
        int initialSize = payloadHolder.readableBytes();
        int maxSlice = MAX_PAYLOAD;
        int sendingPayloadSize = Math.min(maxSlice, initialSize);
        lastChunkWasMaximumLength = (sendingPayloadSize == maxSlice); //need to send a zero length payload if last fragment was exactly 0xFFFF long.

        ByteBuf nextChunk = payloadHolder.readSlice(sendingPayloadSize);
        leBuf.writeMedium(sendingPayloadSize);
        leBuf.writeByte(sequenceIter);
        leBuf.writeBytes(nextChunk);

        sequenceIter++;
    } while (payloadHolder.readableBytes() > 0 || lastChunkWasMaximumLength);
    return sequenceIter; //returns the next usable/expected sequence number.
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyComBinLogDumpRequest.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    binlogPosition = cb.readUnsignedInt();
    cb.skipBytes(2);//from  w ww.j  a v a2 s . c  o  m
    slaveServerID = cb.readInt();
    if (cb.readableBytes() > 0) {
        binlogFileName = cb.readSlice(cb.readableBytes()).toString(CharsetUtil.UTF_8);
    }
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyStatusVariables.java

License:Open Source License

public void parseStatusVariables(ByteBuf cb, int svLen) throws PEException {
    if (svLen > 0) {
        ByteBuf statsVarsBuf = cb.readBytes(svLen);
        while (statsVarsBuf.isReadable()) {
            byte code = statsVarsBuf.readByte();
            MyQueryEventCode mqec = MyQueryEventCode.fromByte(code);
            if (mqec == null) {
                throw new PEException("Replication could not decode query event code: '" + code + "' (0x"
                        + Integer.toHexString(code) + ")");
            }/*from  ww w .j  a v a 2 s .c  o  m*/

            switch (mqec) {
            case Q_FLAGS2_CODE:
                int flags = statsVarsBuf.readInt();
                suppliedEventCodes.add(new QueryFlags2Event(flags));
                break;

            case Q_SQL_MODE_CODE:
                long sqlMode = statsVarsBuf.readLong();
                suppliedEventCodes.add(new QuerySQLModeEvent(sqlMode));
                break;

            case Q_CATALOG_CODE: {
                byte len = statsVarsBuf.readByte();
                String catalog = MysqlAPIUtils.readBytesAsString(statsVarsBuf, len, CharsetUtil.UTF_8);
                statsVarsBuf.readByte(); // null terminated byte

                suppliedEventCodes.add(new QueryCatalogEvent(catalog));
                break;
            }
            case Q_AUTO_INCREMENT:
                int autoIncrementIncrement = statsVarsBuf.readUnsignedShort();
                int autoIncrementOffset = statsVarsBuf.readUnsignedShort();

                suppliedEventCodes
                        .add(new QueryAutoIncrementEvent(autoIncrementIncrement, autoIncrementOffset));
                break;

            case Q_CHARSET_CODE:
                int charSetClient = statsVarsBuf.readUnsignedShort();
                int collationConnection = statsVarsBuf.readUnsignedShort();
                int collationServer = statsVarsBuf.readUnsignedShort();

                suppliedEventCodes
                        .add(new QueryCharSetCodeEvent(charSetClient, collationConnection, collationServer));
                break;

            case Q_TIME_ZONE_CODE: {
                byte len = statsVarsBuf.readByte();
                String timeZone = MysqlAPIUtils.readBytesAsString(statsVarsBuf, len, CharsetUtil.UTF_8);

                suppliedEventCodes.add(new QueryTimeZoneCodeEvent(timeZone));
                break;
            }
            case Q_CATALOG_NZ_CODE: {
                byte catalogLen = statsVarsBuf.readByte();
                String catalog = MysqlAPIUtils.readBytesAsString(statsVarsBuf, catalogLen, CharsetUtil.UTF_8);

                suppliedEventCodes.add(new QueryCatalogNZEvent(catalog));
                break;
            }
            case Q_LC_TIME_NAMES_CODE:
                short monthDayNames = statsVarsBuf.readShort();

                suppliedEventCodes.add(new QueryTimeNamesEvent(monthDayNames));
                break;

            case Q_CHARSET_DATABASE_CODE:
                short collationDatabase = statsVarsBuf.readShort();

                suppliedEventCodes.add(new QueryCollationDatabaseEvent(collationDatabase));
                break;

            case Q_TABLE_MAP_FOR_UPDATE_CODE:
                long tableMapForUpdate = statsVarsBuf.readLong();

                suppliedEventCodes.add(new QueryTableMapEvent(tableMapForUpdate));
                break;

            case Q_MASTER_DATA_WRITTEN_CODE:
                int originalLength = statsVarsBuf.readInt();

                suppliedEventCodes.add(new QueryMasterDataWrittenEvent(originalLength));
                break;

            case Q_INVOKER:
                int userLen = statsVarsBuf.readByte();
                String user = MysqlAPIUtils.readBytesAsString(statsVarsBuf, userLen, CharsetUtil.UTF_8);
                int hostLen = statsVarsBuf.readByte();
                String host = MysqlAPIUtils.readBytesAsString(statsVarsBuf, hostLen, CharsetUtil.UTF_8);

                suppliedEventCodes.add(new QueryInvokerEvent(user, host));
                break;

            case Q_UPDATED_DB_NAMES:
                List<String> dbNames = new ArrayList<String>();
                int numDbs = statsVarsBuf.readByte();
                if (numDbs > 0) {
                    for (int i = 0; i < numDbs; i++) {
                        dbNames.add(statsVarsBuf.readSlice(statsVarsBuf.bytesBefore((byte) 0))
                                .toString(CharsetUtil.UTF_8));
                        statsVarsBuf.readByte(); //read null byte
                    }
                }
                suppliedEventCodes.add(new QueryUpdatedDBNamesEvent(dbNames));
                break;

            case Q_MICROSECONDS:
                int microseconds = statsVarsBuf.readMedium();

                suppliedEventCodes.add(new QueryMicrosecondsEvent(microseconds));
                break;

            case Q_HRNOW:
                //TODO: this was apparently added for MariaDB, but I can't find a lot of info on it. skip for now.
                suppliedEventCodes.add(new QueryMicrosecondsEvent(statsVarsBuf.readUnsignedMedium()));
                break;

            default:
                throw new PEException("Replication encountered an unknown query event code: '" + code + "' (0x"
                        + Integer.toHexString(code) + ")");
            }
        }
    }
}