Example usage for io.netty.buffer ByteBuf writeShort

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

Introduction

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

Prototype

public abstract ByteBuf writeShort(int value);

Source Link

Document

Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

@Test
public void testIllegalHttpSettingsFrame() throws Exception {
    int length = 6;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; // illegal stream identifier
    int id = RANDOM.nextInt() & 0xFFFF;
    int value = RANDOM.nextInt();

    ByteBuf frame = settingsFrame(length, flags, streamId);
    frame.writeShort(id);
    frame.writeInt(value);// w  w w  .j  av a2s . c o  m
    decoder.decode(frame);

    verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 SETTINGS Frame/*from  w  w w . j  a v a 2s .c  om*/
 */
public ByteBuf encodeSettingsFrame(HttpSettingsFrame httpSettingsFrame) {
    Set<Integer> ids = httpSettingsFrame.getIds();
    int length = ids.size() * 6;
    byte flags = httpSettingsFrame.isAck() ? HTTP_FLAG_ACK : 0;
    int streamId = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_SETTINGS_FRAME, flags, streamId);
    for (int id : ids) {
        frame.writeShort(id);
        frame.writeInt(httpSettingsFrame.getValue(id));
    }
    return frame;
}

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);
    buffer.writeBytes(raw);//from   w w  w  .j  av a2s .co  m
}

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

License:Open Source License

public static void encodeHeaders(Map<String, String> headers, ByteBuf buffer) {

    buffer.writeShort(headers.size());

    for (Map.Entry<String, String> header : headers.entrySet()) {
        CodecUtils.encodeString(header.getKey(), buffer);
        CodecUtils.encodeString(header.getValue(), buffer);
    }/*from  ww  w .  java  2  s  .c o  m*/

}

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

License:Open Source License

public static int writeArg(ByteBufAllocator allocator, ByteBuf arg, int writableBytes, List<ByteBuf> bufs) {
    if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
        throw new UnsupportedOperationException(
                "writableBytes must be larger than " + TFrame.FRAME_SIZE_LENGTH);
    }// ww  w  .ja  v a 2s.co m

    int readableBytes = arg.readableBytes();
    int headerSize = TFrame.FRAME_SIZE_LENGTH;
    int chunkLength = Math.min(readableBytes + headerSize, writableBytes);
    ByteBuf sizeBuf = allocator.buffer(TFrame.FRAME_SIZE_LENGTH);
    bufs.add(sizeBuf);

    // Write the size of the `arg`
    sizeBuf.writeShort(chunkLength - headerSize);
    if (readableBytes == 0) {
        return TFrame.FRAME_SIZE_LENGTH;
    } else {
        bufs.add(arg.readSlice(chunkLength - headerSize).retain());
        return chunkLength;
    }
}

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

License:Open Source License

public static ByteBuf writeArgCopy(ByteBufAllocator allocator, ByteBuf payload, ByteBuf arg,
        int writableBytes) {
    if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
        throw new UnsupportedOperationException(
                "writableBytes must be larger than " + TFrame.FRAME_SIZE_LENGTH);
    }//from  w  w w  . j  a  v a2s  .  com

    int readableBytes = arg.readableBytes();
    int headerSize = TFrame.FRAME_SIZE_LENGTH;
    int chunkLength = Math.min(readableBytes + headerSize, writableBytes);

    // Write the size of the `arg`
    payload.writeShort(chunkLength - headerSize);
    if (readableBytes == 0) {
        return payload;
    } else {
        return payload.writeBytes(arg, chunkLength - headerSize);
    }
}

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//ww  w  .  j av  a  2 s . co  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.codecs.TFrameCodec.java

License:Open Source License

public static ByteBuf encode(ByteBufAllocator allocator, TFrame frame) {
    ByteBuf buffer = allocator.buffer(TFrame.FRAME_HEADER_LENGTH, TFrame.FRAME_HEADER_LENGTH);

    // size:2/*  w ww .ja  v a2  s  . c om*/
    buffer.writeShort(frame.size + TFrame.FRAME_HEADER_LENGTH);

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

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

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

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

    // TODO: refactor
    if (frame.payload instanceof CompositeByteBuf) {
        CompositeByteBuf cbf = (CompositeByteBuf) frame.payload;
        cbf.addComponent(0, buffer);
        cbf.writerIndex(cbf.writerIndex() + TFrame.FRAME_HEADER_LENGTH);
        return cbf;
    }

    return Unpooled.wrappedBuffer(buffer, frame.payload);
}

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    // Allocate new ByteBuf
    ByteBuf buffer = allocator.buffer(256);

    // version:2/*  w  w  w . j a va  2s  .  co  m*/
    buffer.writeShort(getVersion());

    // headers -> nh:2 (key~2 value~2){nh}
    CodecUtils.encodeHeaders(getHeaders(), buffer);

    return buffer;
}

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

License:Open Source License

/**
 * @param decrypted/*  w  ww  .  j a  v  a  2 s.  c o  m*/
 * @param encrypted
 * @param decServerKeys
 * @return
 */
private static int BlockDecode(ByteBuf decrypted, short[] InBuf, ByteBuf converter, long[] Keys) {

    long[] Ring = new long[4];
    short[] Shift = new short[4];

    ShiftBytes(Shift, 0x00, InBuf, 0x00, 0x10);
    ShiftBytes(Shift, 0x16, InBuf, 0x10, 0x02);

    writeByteArray(converter, Shift);
    flushArray(Shift, 0, 4);

    ShiftBytes(Shift, 0x00, InBuf, 0x12, 0x10);
    ShiftBytes(Shift, 0x16, InBuf, 0x22, 0x02);

    writeByteArray(converter, Shift);
    flushArray(Shift, 0, 4);

    ShiftBytes(Shift, 0x00, InBuf, 0x24, 0x10);
    ShiftBytes(Shift, 0x16, InBuf, 0x34, 0x02);

    writeByteArray(converter, Shift);
    flushArray(Shift, 0, 4);

    ShiftBytes(Shift, 0x00, InBuf, 0x36, 0x10);
    ShiftBytes(Shift, 0x16, InBuf, 0x46, 0x02);

    writeByteArray(converter, Shift);
    flushArray(Shift, 0, 4);

    // for (int i = 0; i < Ring.length; i++) {
    // System.err.print(Integer.toHexString((int) Ring[i])+" ");;
    // }

    // System.err.println();

    for (int i = 0; i < Ring.length; i++) {
        Ring[i] = converter.readInt();
    }
    converter.clear();

    Ring[2] = Ring[2] ^ Keys[10] ^ (Ring[3] & 0xFFFF);
    Ring[1] = Ring[1] ^ Keys[9] ^ (Ring[2] & 0xFFFF);
    Ring[0] = Ring[0] ^ Keys[8] ^ (Ring[1] & 0xFFFF);

    //       System.err.println("Finished Ring: ");
    //       for (int i = 0; i < Ring.length; i++) {
    //       System.err.print(Integer.toHexString((int) Ring[i])+" ");;
    //       }
    //       System.err.println();
    int[] CryptBuf = new int[4];

    // Had ushort cast here.
    CryptBuf[0] = (int) (Keys[8] ^ ((Ring[0] * Keys[4]) % Keys[0]));

    CryptBuf[1] = (int) (Keys[9] ^ ((Ring[1] * Keys[5]) % Keys[1]) ^ (Ring[0] & 0xFFFF));
    CryptBuf[2] = (int) (Keys[10] ^ ((Ring[2] * Keys[6]) % Keys[2]) ^ (Ring[1] & 0xFFFF));
    CryptBuf[3] = (int) (Keys[11] ^ ((Ring[3] * Keys[7]) % Keys[3]) ^ (Ring[2] & 0xFFFF));

    //      System.err.println("Pre done: " + PrintData.printData(CryptBuf));

    short[] Finale = new short[2];
    ShiftBytes(Finale, 0x00, InBuf, 0x48, 0x10);
    Finale[0] ^= Finale[1];
    Finale[0] ^= 0x3D;

    converter.clear();
    for (int i = 0; i < CryptBuf.length; i++) {
        converter.writeShort(CryptBuf[i]);
    }

    decrypted.writeBytes(converter, Finale[0]);
    converter.clear();

    //System.out.println(PrintData.printData(decrypted.nioBuffer()));

    // System.err.println("Finale: "+ Finale[0]);

    short Check = 0xF8;
    for (int i = 0; i < Finale[0]; ++i)
        Check = (short) (Check ^ decrypted.getUnsignedByte(i));

    if (Finale[1] == Check)
        return Finale[0];

    //System.err.println("Finale["+Finale[0]+"] And done: "+PrintData.printData(decrypted.nioBuffer()));

    return Finale[0];
}