Example usage for io.netty.buffer ByteBuf setByte

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

Introduction

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

Prototype

public abstract ByteBuf setByte(int index, int value);

Source Link

Document

Sets the specified byte at the specified absolute index in this buffer.

Usage

From source file:com.quavo.osrs.network.protocol.codec.update.encrypt.XOREncryptionEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, XOREncryptionResponse msg, ByteBuf out) throws Exception {
    if (msg.getKey() != 0) {
        for (int i = 0; i < out.writerIndex(); i++) {
            out.setByte(i, out.getByte(i) ^ msg.getKey());
        }//from   w  w w  . ja  v a 2s.co  m
    }

    ctx.pipeline().remove(this);
}

From source file:com.talent.mysql.packet.request.AuthPacket.java

License:Open Source License

@Override
public void encodeBody(ByteBuf byteBuf) {
    int index = byteBuf.readerIndex();
    String xx = Long.toBinaryString(clientFlags);
    byteBuf.setLong(index, clientFlags);
    index += 4;//from   w w  w .jav  a 2  s .  c  o m

    byteBuf.setLong(index, maxPacketSize);
    index += 4;

    byteBuf.setByte(index, charsetIndex);
    index++;

    byteBuf.setBytes(index, extra);
    index += extra.length;

    byteBuf.setBytes(index, user);
    index += user.length;

    byteBuf.setByte(index, 0);
    index++;

    byteBuf.setByte(index, passwordLen);
    index++;

    byteBuf.setBytes(index, password);
    index += password.length;

    byteBuf.setBytes(index, database);
    index += database.length;

    byteBuf.setByte(index, 0);
    index++;
}

From source file:com.tcy.app.netty4.Ne4ClientHandler.java

License:Apache License

public void sendMessage(Object object) {
    ByteBuf buf = ctx.alloc().buffer();
    buf.setByte(0, 123456);
    ctx.writeAndFlush(buf.duplicate().retain()).addListener(trafficGenerator);

}

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

License:Open Source License

String processDecimalValue(ByteBuf cb, int valueLen) throws PEException {
    String value = StringUtils.EMPTY;

    byte precision = cb.readByte();
    byte scale = cb.readByte();

    int intg = (int) precision - (int) scale;
    int intg0 = intg / DIG_PER_DEC1;
    int frac0 = (int) scale / DIG_PER_DEC1;
    int intg0x = intg - intg0 * DIG_PER_DEC1;
    int frac0x = (int) scale - frac0 * DIG_PER_DEC1;

    int firstValue = intg0 * 4 + dig2bytes[intg0x];
    int secondValue = frac0 * 4 + dig2bytes[frac0x];

    int binSize = firstValue + secondValue;

    int readableBytes = cb.readableBytes();
    if ((firstValue < 1 && secondValue < 1) || readableBytes < binSize) {
        throw new PEException("Cannot decode binary decimal");
    }// w w  w.  ja  v  a2s.c o m

    ByteBuf chunk = PooledByteBufAllocator.DEFAULT.heapBuffer(binSize);
    cb.readBytes(chunk);

    // 1st byte is special cause it determines the sign
    byte firstByte = chunk.getByte(0);
    int sign = (firstByte & 0x80) == 0x80 ? 1 : -1;
    // invert sign
    chunk.setByte(0, (firstByte ^ 0x80));

    if (sign == -1) {
        // invert all the bytes
        for (int i = 0; i < binSize; i++) {
            chunk.setByte(i, ~chunk.getByte(i));
        }
    }

    BigDecimal integerPortion = decodeBinDecimal(chunk, firstValue, true);
    BigDecimal fractionPortion = decodeBinDecimal(chunk, secondValue, false);

    value = ((sign == -1) ? "-" : StringUtils.EMPTY) + integerPortion.toPlainString() + "."
            + fractionPortion.toPlainString();

    return value;
}

From source file:com.uber.tchannel.frames.CallFrame.java

License:Open Source License

public final ByteBuf encodePayload(ByteBufAllocator allocator, List<ByteBuf> args) {
    ByteBuf payload = CodecUtils.writeArgs(allocator, encodeHeader(allocator), args);

    if (args.isEmpty()) {
        this.flags = 0;
        payload.setByte(0, 0);
    } else {//from w w  w . j a v  a  2 s .c  o  m
        this.flags = 1;
        payload.setByte(0, 1);
    }

    this.payload = payload;
    return payload;
}

From source file:com.vethrfolnir.game.network.mu.crypt.MuDecoder.java

License:Open Source License

private static void DecXor32(ByteBuf buff, int SizeOfHeader, int Len) {

    for (int i = Len - 1; i > 0; i--) {
        int Buff = buff.getUnsignedByte(buff.readerIndex() + i);
        Buff ^= (Xor32Keys[(i + SizeOfHeader) & 31] ^ buff.getUnsignedByte((buff.readerIndex() + i) - 1));
        buff.setByte(buff.readerIndex() + i, Buff);
    }/*from   w  w w .j  av a 2  s . c  o  m*/
}

From source file:com.vethrfolnir.game.network.mu.crypt.MuEncoder.java

License:Open Source License

public static ByteBuf EncodePacket(ByteBuf buff, int serial) {
    int header = GetHeaderSize(buff);
    int packetSize = GetPacketSize(buff);
    int contentSize = packetSize - header;

    int encodedSize = (((contentSize / 8) + (((contentSize % 8) > 0) ? 1 : 0)) * 11) + header;

    int size = header;
    int originalHead = buff.getUnsignedByte(0);

    ByteBuf out = alloc.heapBuffer(encodedSize, encodedSize);

    //buff.writerIndex(buff.writerIndex() + 1);
    short[] Contents = new short[contentSize + 1];
    Contents[0] = (short) serial; // XXX:  Check this

    buff.readerIndex(header - 1);//from w  ww  .  ja  v a  2  s.  co  m

    buff.setByte(header - 1, serial);

    MuCryptUtils.readAsUByteArray(buff, Contents);

    //System.out.println("Encoding: "+PrintData.printData(Contents));

    size += EncodeBuffer(Contents, out, header, (contentSize + 1));

    out.writerIndex(0);

    // Header
    out.writeByte(originalHead);

    // Size write
    switch (originalHead) {
    case 0xc3:
        out.writeByte(size);
        break;
    case 0xC4:
        out.writeByte(size >> 8);
        out.writeByte(size & 0xFF);
        break;
    }

    out.writerIndex(size);

    return out;
}

From source file:com.vethrfolnir.network.WritePacket.java

License:Open Source License

public void markLength(ByteBuf buff) {
    int lenght = buff.writerIndex();
    switch (buff.getUnsignedByte(0)) {
    case 0xC1:/* w w w  . j  av a 2s.com*/
    case 0xC3:
        buff.setByte(1, lenght);
        break;
    case 0xC2:
    case 0xC4:
        buff.setByte(1, lenght >> 8);
        buff.setByte(2, lenght & 0xFF);
        break;
    }
}

From source file:com.yahoo.pulsar.utils.NumberFormat.java

License:Apache License

static void format(ByteBuf out, long num) {
    if (num == 0) {
        out.writeByte('0');
        return;//from   w w  w . j a  v a2s . com
    }

    // Long.MIN_VALUE needs special handling since abs(Long.MIN_VALUE) = abs(Long.MAX_VALUE) + 1
    boolean encounteredMinValue = (num == Long.MIN_VALUE);
    if (num < 0) {
        out.writeByte('-');
        num += encounteredMinValue ? 1 : 0;
        num *= -1;
    }

    // Putting the number in bytebuf in reverse order
    int start = out.writerIndex();
    formatHelper(out, num);
    int end = out.writerIndex();

    if (encounteredMinValue) {
        out.setByte(start, out.getByte(start) + 1);
    }

    // Reversing the digits
    end--;
    for (int i = 0; i <= (end - start) / 2; i++) {
        byte tmp = out.getByte(end - i);
        out.setByte(end - i, out.getByte(start + i));
        out.setByte(start + i, tmp);
    }
}

From source file:com.yea.remote.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null) {
        throw new Exception("The encode message is null");
    }//from w  ww.ja v  a  2s.c om
    ISerializer serializer = serializePool.borrow();
    try {
        long basedate = new Date().getTime();
        sendBuf.writeInt((msg.getHeader().getCrcCode()));// 4
        sendBuf.writeInt((msg.getHeader().getLength()));// 4Header+BodyHeader18
        sendBuf.writeBytes((msg.getHeader().getSessionID()));// 16
        sendBuf.writeByte((msg.getHeader().getType()));// 1
        sendBuf.writeByte((msg.getHeader().getPriority()));// 1
        sendBuf.writeByte((msg.getHeader().getResult()));// 1
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            // 
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
            sendBuf.writeByte(compressionAlgorithm.ordinal());
        } else {
            sendBuf.writeByte(RemoteConstants.CompressionAlgorithm.NONE.ordinal());
        }
        sendBuf.writeLong(basedate);// 8??
        if (msg.getHeader().getAttachment() != null && !msg.getHeader().getAttachment().isEmpty()) {
            sendBuf.writeByte(msg.getHeader().getAttachment().size());
            Map<String, Number> mapDateType = new HashMap<String, Number>();// Date
            Map<String, String> mapStringType = new HashMap<String, String>();// String

            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(new byte[1]);// 1?Date?
            byte[] keyArray = null;
            byte[] valueArray = null;
            for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
                if (param.getValue() instanceof Date) {
                    //Date???byte
                    long time = basedate - ((Date) param.getValue()).getTime();
                    if (time > Integer.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Long(time));
                    } else if (time > Short.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Integer((int) time));
                    } else if (time > Byte.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Short((short) time));
                    } else {
                        mapDateType.put(param.getKey(), new Byte((byte) time));
                    }
                } else if (param.getValue() instanceof String) {
                    //??getBytes()?
                    mapStringType.put(param.getKey(), (String) param.getValue());
                } else {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = serializer.serialize(param.getValue());
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
            sendBuf.setByte(lengthPos,
                    msg.getHeader().getAttachment().size() - mapDateType.size() - mapStringType.size());

            if (mapDateType.isEmpty()) {
                sendBuf.writeByte(0);// 1Date?0
            } else {
                sendBuf.writeByte(mapDateType.size());// 1Date?
                for (Map.Entry<String, Number> param : mapDateType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    if (param.getValue() instanceof Long) {
                        sendBuf.writeByte(8);
                        sendBuf.writeLong((Long) param.getValue());
                    } else if (param.getValue() instanceof Integer) {
                        sendBuf.writeByte(4);
                        sendBuf.writeInt((Integer) param.getValue());
                    } else if (param.getValue() instanceof Short) {
                        sendBuf.writeByte(2);
                        sendBuf.writeShort((Short) param.getValue());
                    } else {
                        sendBuf.writeByte(1);
                        sendBuf.writeByte((Byte) param.getValue());
                    }
                }
            }

            if (mapStringType.isEmpty()) {
                sendBuf.writeByte(0);// 1String?0
            } else {
                sendBuf.writeByte(mapStringType.size());// 1String?
                for (Map.Entry<String, String> param : mapStringType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = param.getValue().getBytes("ISO-8859-1");
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
        } else {
            sendBuf.writeByte(0);// 20
        }

        if (msg.getBody() != null) {
            byte[] objArray = serializer.serialize(msg.getBody());
            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(LENGTH_PLACEHOLDER);// 4Body
            sendBuf.writeBytes(objArray);
            sendBuf.setInt(lengthPos, sendBuf.writerIndex() - lengthPos - 4);// Body
        } else {
            sendBuf.writeInt(0);
        }
        sendBuf.setInt(4, sendBuf.readableBytes() - 8);// Header+Body
    } finally {
        serializePool.restore(serializer);
    }
}