Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static void encodeString(String value, ByteBuf buffer) {
    byte[] raw = value.getBytes();
    buffer.writeShort(raw.length);// ww  w. j ava2s .c  om
    buffer.writeBytes(raw);
}

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static void encodeSmallString(String value, ByteBuf buffer) {
    byte[] raw = value.getBytes();
    buffer.writeByte(raw.length);//  w w  w  .j  a va 2  s .  c  o m
    buffer.writeBytes(raw);
}

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static ByteBuf writeArgsCopy(ByteBufAllocator allocator, ByteBuf header, List<ByteBuf> args) {
    ByteBuf payload = allocator.buffer(header.readableBytes(), TFrame.MAX_FRAME_PAYLOAD_LENGTH);
    payload.writeBytes(header);
    header.release();//from   ww  w .java2s  . c o  m
    int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - payload.readableBytes();

    while (!args.isEmpty()) {
        ByteBuf arg = args.get(0);
        writeArgCopy(allocator, payload, arg, writableBytes);
        writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - payload.readableBytes();
        if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
            break;
        }

        if (arg.readableBytes() == 0) {
            args.remove(0);
        }
    }

    return payload;
}

From source file:com.uber.tchannel.codecs.TFrameCodec.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, TFrame frame, ByteBuf out) throws Exception {
    // size:2//  www . j  ava  2 s  .c o m
    out.writeShort(frame.size + TFrame.FRAME_HEADER_LENGTH);

    // type:1
    out.writeByte(frame.type);

    // reserved:1
    out.writeZero(1);

    // id:4
    out.writeInt((int) frame.id);

    // reserved:8
    out.writeZero(8);

    // payload:16+
    out.writeBytes(frame.payload);
}

From source file:com.uber.tchannel.tracing.TracingPropagationTest.java

License:Open Source License

private static TraceResponse callDownstreamThrift(String remainingEncodings) throws Exception {
    ThriftRequest<Example> request = new ThriftRequest.Builder<Example>("tchannel-name", "Behavior::trace")
            .setTimeout(1, TimeUnit.MINUTES).setBody(new Example(remainingEncodings, 0)).build();

    TFuture<ThriftResponse<Example>> responsePromise = subChannel.send(request, tchannel.getHost(),
            tchannel.getListeningPort());

    ThriftResponse<Example> thriftResponse = responsePromise.get();
    assertNull(thriftResponse.getError());
    String json = thriftResponse.getBody(Example.class).getAString();
    thriftResponse.release();/*from  w w  w .ja  va  2s. co  m*/
    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(json.length());
    byteBuf.writeBytes(json.getBytes());
    TraceResponse response = new JSONSerializer().decodeBody(byteBuf, TraceResponse.class);
    byteBuf.release();
    return response;
}

From source file:com.uber.tchannel.tracing.TracingPropagationTest.java

License:Open Source License

private static TraceResponse callDownstreamThriftAsync(String remainingEncodings) throws Exception {
    ThriftRequest<Example> request = new ThriftRequest.Builder<Example>("tchannel-name", "Behavior::asynctrace")
            .setTimeout(1, TimeUnit.MINUTES).setBody(new Example(remainingEncodings, 0)).build();

    TFuture<ThriftResponse<Example>> responsePromise = subChannel.send(request, tchannel.getHost(),
            tchannel.getListeningPort());

    ThriftResponse<Example> thriftResponse = responsePromise.get();
    assertNull(thriftResponse.getError());
    String json = thriftResponse.getBody(Example.class).getAString();
    thriftResponse.release();/*  w  w w.ja  va  2s.  c  om*/
    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(json.length());
    byteBuf.writeBytes(json.getBytes());
    TraceResponse response = new JSONSerializer().decodeBody(byteBuf, TraceResponse.class);
    byteBuf.release();
    return response;
}

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

License:Open Source License

private static int DecodeBlock(ByteBuf buff, ByteBuf outBuff, int offset, int size) {
    // decripted size
    int index = 0;

    if ((size % 11) != 0) {
        log.warn("Cannot decrypt packet, it's already decrypted!: Size " + size + " = " + ((size % 11)));
        log.warn(PrintData.printData(buff.nioBuffer()));
        return -1;
    }/* w w  w  . ja  v  a  2 s .c  om*/

    ByteBuf encrypted = alloc.heapBuffer(11, 11).order(ByteOrder.LITTLE_ENDIAN);
    short[] uByteArray = new short[encrypted.capacity()];

    ByteBuf decrypted = alloc.heapBuffer(8, 8).order(ByteOrder.LITTLE_ENDIAN);
    ByteBuf converter = alloc.heapBuffer(4).order(ByteOrder.LITTLE_ENDIAN);

    for (int i = 0; i < size; i += 11) {
        buff.readBytes(encrypted);

        //System.out.println("ENC: "+PrintData.printData(encrypted.nioBuffer()));
        int Result = BlockDecode(decrypted, getAsUByteArray(encrypted, uByteArray), converter,
                MuKeyFactory.getClientToServerPacketDecKeys());
        if (Result != -1) {
            //Buffer.BlockCopy(Decrypted, 0, m_DecryptResult, (OffSet - 1) + DecSize, Result);

            outBuff.writerIndex((offset - 1) + index);
            outBuff.writeBytes(decrypted);

            //outBuff.writeBytes(decrypted);

            decrypted.clear();
            encrypted.clear();
            converter.clear();
            //System.arraycopy(Decrypted, 0, m_DecryptResult, (OffSet - 1) + DecSize, Result);

            index += Result;
        }
    }

    return index;
}

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

License:Open Source License

public static void main(String[] args) {
    //byte[] data = PacketUtils.hex2Bytes("C1 04 F3 0D d3");
    //byte[] data = PacketUtils.hex2Bytes("C1 0F F3 7B 85 58 A8 B9 E9 41 BF 09 40 1D 74");
    byte[] data = PacketUtils.hex2Bytes(
            "C3 5A 9E 4D 18 56 28 FB 20 E5 2D A7 92 5A 01 33 CB 50 BA F0 10 69 76 43 16 D1 65 36 64 13 F1 45 CC 1A 2F B1 B7 4E 24 49 1F F2 2F 54 A7 92 F8 04 7E 8A A7 A7 73 EF 00 C9 FC E2 CF 35 E4 58 21 A0 5C 89 16 23 B2 8C 5C 4C 62 23 B2 8B 90 27 12 61 07 5D 00 4D C3 F3 5A 31 99 A7");

    ByteBuf buff = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);

    ByteBuf out = null;/*  w  w  w. j ava 2  s .c  o  m*/
    for (int i = 0; i < 1; i++) {
        buff.writeBytes(data);

        long t1 = System.currentTimeMillis();
        //DecodeXor32(buff);
        out = DecodePacket(buff);
        long t2 = System.currentTimeMillis();

        System.out.println("Process time: " + (t2 - t1) + " milis");

    }
    System.out.println(PrintData.printData(out.nioBuffer()));

}

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

License:Open Source License

public static void main(String[] args) {
    Corax.Install(new CoraxBuilder() {

        @Override//  ww w  .j av a  2  s  . c  om
        protected void build(Corax corax) {
            CorvusConfig.WorkingDirectory = new File("./dist/GameServer");
            Corax.config().loadDirectory("/config");
            bind(AssetManager.class).as(Scope.Singleton);
            ;
        }
    });

    MuKeyFactory.parse();
    System.out.println("");

    //byte[] data = PacketUtils.hex2Bytes("C3 44 F3 03 42 E6 33 00 00 00 00 00 00 00 FA 00 00 00 00 00 00 01 25 84 0A 00 2D 00 32 00 12 00 28 00 56 00 56 00 59 00 59 00 C8 00 C8 00 10 00 20 00 12 00 B0 B4 1D 00 03 00 00 00 04 00 00 00 00 00 04 00");
    byte[] data = PacketUtils.hex2Bytes(
            "C4 00 2D 01 F3 10 03 00 00 00 12 00 00 10 00 FF FF FF FF FF 0C 14 08 1E 00 00 D0 00 FF FF FF FF FF 0D 14 10 1E 00 00 D0 00 FF FF FF FF");
    ByteBuf buff = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);
    buff.writeBytes(data);

    ByteBuf out = EncodePacket(buff, 0x02);

    byte[] arr = new byte[out.readableBytes()];
    out.readBytes(arr);

    //System.out.println(PrintData.printData(out.nioBuffer()));
    //System.out.println("C3-65-22-EF-31-62-4B-D5-32-2F-B1-AA-9F-45-1B-08-45-D2-96-92-B1-51-37-02-45-1B-1F-83-13-D1-F3-9C-E6-67-52-9C-F7-0F-7C-CD-9B-E1-EC-B4-E8-DD-3D-33-0D-44-C2-73-52-48-4E-CD-F8-98-77-44-8B-99-FA-64-71-A6-FD-C8-35-92-35-B9-95-3D-A2-3C-81-C5-F0-0F-4E-1D-F0-81-34-72-2B-E8-C9-FC-36-05-31-36-80-D0-80-BB-0C-C2-FC");
    System.out.println("C3-0D-FE-53-65-66-18-AB-51-01-C1-4D-77");
    System.out.println(PacketUtils.byteArrayToHex(arr).toUpperCase());
}

From source file:com.vethrfolnir.game.network.mu.packets.MuWritePacket.java

License:Open Source License

public void writeS(ByteBuf buff, String value, int max) {
    if (value == null)
        throw new RuntimeException("Value is null!");

    try {//from   w  w  w . j av  a  2  s.com
        int l = value.length();
        buff.writeBytes(value.getBytes("ISO-8859-1"));
        for (int i = l; i < max; i++)
            buff.writeByte(0x00);
    } catch (UnsupportedEncodingException e) {
        log.warn("Failed writing string!", e);
    }
}