Example usage for io.netty.buffer ByteBuf array

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

Introduction

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

Prototype

public abstract byte[] array();

Source Link

Document

Returns the backing byte array of this buffer.

Usage

From source file:ratpack.util.internal.BufferUtil.java

License:Apache License

public static byte[] getBytes(ByteBuf byteBuf) {
    if (byteBuf.hasArray()) {
        return byteBuf.array();
    } else {/*from   w  w  w  .j ava2  s.  c  om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream(byteBuf.writerIndex());
        try {
            writeTo(byteBuf, baos);
        } catch (IOException e) {
            throw uncheck(e);
        }
        return baos.toByteArray();
    }
}

From source file:ru.jts.authserver.network.crypt.CryptEngine.java

License:Apache License

private byte[] randomBytesPadding(byte[] data) {
    byte additional = (byte) (8 - (data.length) % 8);
    byte[] addBytes = new byte[additional - 1];
    random.nextBytes(addBytes);//from   w w  w.ja v  a2  s .  co  m
    ByteBuf buf = Unpooled.buffer(data.length + additional).order(ByteOrder.LITTLE_ENDIAN);
    buf.writeBytes(data, 0, data.length - 4);
    buf.writeBytes(addBytes);
    buf.writeBytes(data, data.length - 4, 4);
    buf.writeByte(additional);
    return buf.array();
}

From source file:ru.jts.authserver.network.crypt.RSAEngine.java

License:Apache License

/**
 * @param data   -  ??  //ww  w  .j a va 2 s  .  c om
 * @param from
 * @param length
 * @return - ?? ? 
 */
private byte[] decrypt0(byte[] data, int from, int length) {
    ByteBuf buf = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);
    PrivateKey privateKey = KeyStore.getInstance().getPrivateKey();
    try {
        Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding",
                BouncyCastleProvider.PROVIDER_NAME);
        rsa.init(Cipher.DECRYPT_MODE, privateKey);

        final int blockSize = rsa.getBlockSize();
        for (int i = from; i < length; i += blockSize) {
            if (i + blockSize > length) {
                byte[] tempData = rsa.doFinal(data, i, length - i);
                buf.writeBytes(tempData);
            } else {
                byte[] tempData = rsa.doFinal(data, i, blockSize);
                buf.writeBytes(tempData);
            }
        }
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | NoSuchProviderException e) {
        e.printStackTrace();
    }
    return buf.array();
}

From source file:ru.jts.authserver.network.handler.GameServersChannelHandler.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf buf = (ByteBuf) super.decode(ctx, in);
    System.out.println(ArrayUtils.bytesToHexString(buf.array()));
    return buf;//from w  w  w.ja v  a 2 s .c  o m
}

From source file:ru.jts.authserver.network.handler.packet.Client2AuthPacketHandler.java

License:Apache License

private ByteBuf decryptBuffer(ByteBuf buf) {
    byte[] data = RSAEngine.getInstance().decrypt(buf.array(), 0, buf.array().length);
    return Unpooled.copiedBuffer(data).order(ByteOrder.LITTLE_ENDIAN);
}

From source file:ru.jts.authserver.network.serverpackets.SM_AUTH_RESPONSE.java

License:Apache License

private byte[] makeDataFromString(String data) {
    ByteBuf buf = Unpooled.buffer().capacity(data.length() + 1);
    buf.writeByte(data.length());/*ww w  .j a  va 2s  .  c  o m*/
    buf.writeBytes(data.getBytes());
    return buf.array();
}

From source file:ru.jts.gameserver.network.crypt.CryptEngine.java

License:Apache License

private byte[] randomBytesPadding(byte[] data) {
    byte additional = (byte) (8 - (data.length) % 8);
    byte[] addBytes = new byte[additional - 1];
    random.nextBytes(addBytes);/*w w w . j av  a 2  s .  c  om*/
    ByteBuf buf = Unpooled.buffer(data.length + ENCRYPT_MAGIC.length + additional)
            .order(ByteOrder.LITTLE_ENDIAN);
    buf.writeBytes(data);
    buf.writeBytes(ENCRYPT_MAGIC);
    buf.writeBytes(addBytes);
    buf.writeByte(additional);
    return buf.array();
}

From source file:ru.jts_dev.gameserver.service.GameSessionService.java

License:Open Source License

private GameSession createSession(TcpConnection connection) {
    ByteBuf encryptKey = buffer(16, 16).order(ByteOrder.LITTLE_ENDIAN);
    // randomize first 8 bytes of key
    random.nextBytes(encryptKey.array());
    encryptKey.writerIndex(8);//from w  w w. j a v a  2  s  .c o  m

    // and add 8 byte as static key part
    encryptKey.writeBytes(STATIC_KEY_PART);

    // then copy encrypt key to decrypt key
    ByteBuf decryptKey = copiedBuffer(encryptKey).order(ByteOrder.LITTLE_ENDIAN);

    // and pass keys to a game session object
    return new GameSession(connection, encryptKey, decryptKey);
}

From source file:sas.systems.imflux.test.packet.DataPacketTest.java

License:Apache License

@Test
public void testEncode() {
    DataPacket packet = new DataPacket();
    packet.setVersion(RtpVersion.V2);/*from w  w w  .  j a v a  2  s . com*/
    packet.setMarker(true);
    packet.setPayloadType(8);
    packet.setSequenceNumber(6515);
    packet.setTimestamp(103700);
    packet.setSsrc(0x1fcc779a);
    packet.setData(new byte[] { (byte) 0xd5, (byte) 0xd5, (byte) 0xd5, (byte) 0xd5, (byte) 0xd5, (byte) 0xd5 });
    ByteBuf buffer = packet.encode();
    assertTrue(Arrays.equals(ALAW_RTP_PACKET_SAMPLE, buffer.array()));
}

From source file:sas.systems.imflux.test.packet.DataPacketTest.java

License:Apache License

@Test
public void testEncodeDecodeWithFixedBlockSize() {
    DataPacket packet = new DataPacket();
    packet.setMarker(true);//from  w w w  . j a  v a 2s .c  o  m
    packet.setSsrc(0x45);
    packet.setSequenceNumber(2);
    packet.setPayloadType(8);
    packet.setTimestamp(69);
    packet.setData(new byte[] { 0x45, 0x45, 0x45, 0x45, 0x45 });
    System.out.println("packet = " + packet);

    ByteBuf encoded = packet.encode(64);
    System.out.println(ByteUtils.writeArrayAsHex(encoded.array(), true));
    assertEquals(64, encoded.readableBytes());

    DataPacket decoded = DataPacket.decode(encoded);
    assertEquals(0, encoded.readableBytes());

    assertEquals(packet.hasMarker(), decoded.hasMarker());
    assertEquals(packet.getSsrc(), decoded.getSsrc());
    assertEquals(packet.getSequenceNumber(), decoded.getSequenceNumber());
    assertEquals(packet.getPayloadType(), decoded.getPayloadType());
    assertEquals(packet.getTimestamp(), decoded.getTimestamp());
    assertNotNull(decoded.getData());
    assertEquals(packet.getDataSize(), decoded.getDataSize());
    assertTrue(Arrays.equals(packet.getDataAsArray(), decoded.getDataAsArray()));
    System.out.println("decoded = " + decoded);
}