Example usage for io.netty.buffer ByteBuf nioBuffer

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

Introduction

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

Prototype

public abstract ByteBuffer nioBuffer();

Source Link

Document

Exposes this buffer's readable bytes as an NIO ByteBuffer .

Usage

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;
    for (int i = 0; i < 1; i++) {
        buff.writeBytes(data);/* w ww .  j  av a 2s  .c  o  m*/

        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.vmware.dcp.common.http.netty.NettyHttpClientRequestHandler.java

License:Open Source License

private void decodeRequestBody(ChannelHandlerContext ctx, Operation request, ByteBuf content) {
    if (!content.isReadable()) {
        // skip body decode, request had no body
        request.setContentLength(0);/*from   ww w  .j ava2s  .  c om*/
        submitRequest(ctx, request);
        return;
    }

    request.nestCompletion((o, e) -> {
        if (e != null) {
            request.setStatusCode(Operation.STATUS_CODE_BAD_REQUEST);
            request.setBody(ServiceErrorResponse.create(e, request.getStatusCode()));
            sendResponse(ctx, request);
            return;
        }
        submitRequest(ctx, request);
    });

    Utils.decodeBody(request, content.nioBuffer());
}

From source file:com.vmware.dcp.common.http.netty.NettyHttpServerResponseHandler.java

License:Open Source License

private void decodeResponseBody(Operation request, ByteBuf content) {
    if (!content.isReadable()) {
        if (checkResponseForError(request)) {
            return;
        }//from w w  w.  java 2 s . c  om
        // skip body decode, request had no body
        request.setContentLength(0).setContentType(Operation.MEDIA_TYPE_APPLICATION_JSON);
        request.complete();
        return;
    }

    request.nestCompletion((o, e) -> {
        if (e != null) {
            request.fail(e);
            return;
        }
        completeRequest(request);
    });

    Utils.decodeBody(request, content.nioBuffer());
}

From source file:com.vmware.xenon.common.http.netty.NettyHttpClientRequestHandler.java

License:Open Source License

private void decodeRequestBody(ChannelHandlerContext ctx, Operation request, ByteBuf content, Integer streamId,
        URI callbackUri) {/*  ww  w. j  av  a 2s  .com*/
    if (!content.isReadable()) {
        // skip body decode, request had no body
        request.setContentLength(0);
        submitRequest(ctx, request, streamId, callbackUri);
        return;
    }

    request.nestCompletion((o, e) -> {
        if (e != null) {
            request.setStatusCode(Operation.STATUS_CODE_BAD_REQUEST);
            request.setBody(ServiceErrorResponse.create(e, request.getStatusCode()));
            sendResponse(ctx, request, streamId);
            return;
        }

        submitRequest(ctx, request, streamId, callbackUri);
    });

    Utils.decodeBody(request, content.nioBuffer());
}

From source file:com.vmware.xenon.common.http.netty.NettyHttpServerResponseHandler.java

License:Open Source License

private void decodeResponseBody(Operation request, ByteBuf content) {
    if (!content.isReadable()) {
        if (checkResponseForError(request)) {
            return;
        }//from  w w  w. j ava2s .c  om
        // skip body decode, request had no body
        request.setContentLength(0).complete();
        return;
    }

    request.nestCompletion((o, e) -> {
        if (e != null) {
            request.fail(e);
            return;
        }
        if (checkResponseForError(request)) {
            return;
        }
        completeRequest(request);
    });

    Utils.decodeBody(request, content.nioBuffer());
}

From source file:com.yahoo.pulsar.broker.service.PersistentMessageFinderTest.java

License:Apache License

public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
    PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
    messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
    messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
    messageMetadataBuilder.setSequenceId(1);
    PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
    ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());

    int msgMetadataSize = messageMetadata.getSerializedSize();
    int payloadSize = data.readableBytes();
    int totalSize = 4 + msgMetadataSize + payloadSize;

    ByteBuf headers = PooledByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
    headers.writeInt(msgMetadataSize);/*from  w  ww. j a va 2 s . co  m*/
    messageMetadata.writeTo(outStream);
    ByteBuf headersAndPayload = DoubleByteBuf.get(headers, data);
    byte[] byteMessage = headersAndPayload.nioBuffer().array();
    headersAndPayload.release();
    return byteMessage;
}

From source file:com.yahoo.pulsar.common.compression.CommandsTest.java

License:Apache License

@Test
public void testChecksumSendCommand() throws Exception {

    // test checksum in send command
    String producerName = "prod-name";
    int sequenceId = 0;
    ByteBuf data = Unpooled.buffer(1024);
    MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis())
            .setProducerName(producerName).setSequenceId(sequenceId).build();
    int expectedChecksum = computeChecksum(messageMetadata, data);
    ByteBuf clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data);
    clientCommand.retain();//w  w  w  .j  av  a  2 s  .c  om
    ByteBuffer inputBytes = clientCommand.nioBuffer();
    ByteBuf receivedBuf = Unpooled.wrappedBuffer(inputBytes);
    receivedBuf.skipBytes(4); //skip [total-size]
    int cmdSize = (int) receivedBuf.readUnsignedInt();
    receivedBuf.readerIndex(8 + cmdSize);
    int startMessagePos = receivedBuf.readerIndex();

    /*** 1. verify checksum and metadataParsing ***/
    boolean hasChecksum = Commands.hasChecksum(receivedBuf);
    int checksum = Commands.readChecksum(receivedBuf).intValue();

    // verify checksum is present
    assertTrue(hasChecksum);
    // verify checksum value
    assertEquals(expectedChecksum, checksum);
    MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

    /** 2. parseMessageMetadata should skip checksum if present **/
    receivedBuf.readerIndex(startMessagePos);
    metadata = Commands.parseMessageMetadata(receivedBuf);
    // verify metadata parsing
    assertEquals(metadata.getProducerName(), producerName);

}

From source file:de.ocarthon.core.network.codec.flatbuffer.FlatBufferCodec.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    out.add(method.invoke(null, msg.nioBuffer()));
}

From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureChecker.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {/*w ww  .  jav a2s. c  o  m*/
        if (msg instanceof ByteBuf) {
            final ByteBuf in = (ByteBuf) msg;
            final int dataLength = in.readInt();
            final ByteBuf data = in.readSlice(dataLength);
            final int signatureLength = in.readInt();
            final byte[] signature = new byte[signatureLength];
            in.readBytes(signature);

            verifySignature.update(data.nioBuffer());
            final boolean valid = verifySignature.verify(signature);
            //Log.v(TAG, "Read " + dataLength + "b of data with " + signatureLength + "b " +
            //        (valid ? "valid" : "invalid") + " signature" +
            //        (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : ""));
            if (valid) {
                data.retain();
                ctx.fireChannelRead(data);
            } else {
                throw new SignatureException("Message has a broken signature, closing connection");
            }
        } else {
            throw new SignatureException("Can't check signature of message of type "
                    + (msg != null ? msg.getClass() : "null") + ", closing connection");
        }
    } catch (SignatureException | RuntimeException e) {
        ctx.close();
        throw e;
    }
}

From source file:de.unipassau.isl.evs.ssh.core.network.handler.SignatureGenerator.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    final int dataLength = msg.readableBytes();
    msg.markReaderIndex();//from w w w  .  java  2s  .  c  o m
    out.writeInt(dataLength);
    out.writeBytes(msg);
    msg.resetReaderIndex();

    signSignature.update(msg.nioBuffer());
    msg.readerIndex(msg.writerIndex());

    final byte[] signature = signSignature.sign();
    final int signatureLength = signature.length;
    out.writeInt(signatureLength);
    out.writeBytes(signature);

    //Log.v(TAG, "Signed " + dataLength + "b of data with " + signatureLength + "b signature" +
    //        (Log.isLoggable(TAG, Log.VERBOSE) ? ": " + Arrays.toString(signature) : ""));
}