Example usage for io.netty.buffer ByteBufUtil hexDump

List of usage examples for io.netty.buffer ByteBufUtil hexDump

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil hexDump.

Prototype

public static String hexDump(byte[] array, int fromIndex, int length) 

Source Link

Document

Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the specified byte array's sub-region.

Usage

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkDecoder.java

License:Apache License

private ByteBuf decode(Delegate delegate, SecureChannel channel, List<ByteBuf> chunkBuffers)
        throws UaException {
    CompositeByteBuf composite = BufferUtil.compositeBuffer();

    int signatureSize = delegate.getSignatureSize(channel);
    int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel);

    boolean encrypted = delegate.isEncryptionEnabled(channel);
    boolean signed = delegate.isSigningEnabled(channel);

    for (ByteBuf chunkBuffer : chunkBuffers) {
        char chunkType = (char) chunkBuffer.getByte(3);

        chunkBuffer.skipBytes(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE);

        delegate.readSecurityHeader(channel, chunkBuffer);

        if (encrypted) {
            decryptChunk(delegate, channel, chunkBuffer);
        }//from   w  ww.  ja  v  a 2 s .c om

        int encryptedStart = chunkBuffer.readerIndex();
        chunkBuffer.readerIndex(0);

        if (signed) {
            delegate.verifyChunk(channel, chunkBuffer);
        }

        int paddingSize = encrypted ? getPaddingSize(cipherTextBlockSize, signatureSize, chunkBuffer) : 0;
        int bodyEnd = chunkBuffer.readableBytes() - signatureSize - paddingSize;

        chunkBuffer.readerIndex(encryptedStart);

        SequenceHeader sequenceHeader = SequenceHeader.decode(chunkBuffer);
        long sequenceNumber = sequenceHeader.getSequenceNumber();
        lastRequestId = sequenceHeader.getRequestId();

        if (lastSequenceNumber == -1) {
            lastSequenceNumber = sequenceNumber;
        } else {
            if (lastSequenceNumber + 1 != sequenceNumber) {
                String message = String.format("expected sequence number %s but received %s",
                        lastSequenceNumber + 1, sequenceNumber);

                logger.error(message);
                logger.error(ByteBufUtil.hexDump(chunkBuffer, 0, chunkBuffer.writerIndex()));

                throw new UaException(StatusCodes.Bad_SecurityChecksFailed, message);
            }

            lastSequenceNumber = sequenceNumber;
        }

        ByteBuf bodyBuffer = chunkBuffer.readSlice(bodyEnd - chunkBuffer.readerIndex());

        if (chunkType == 'A') {
            ErrorMessage errorMessage = ErrorMessage.decode(bodyBuffer);

            throw new MessageAbortedException(errorMessage.getError(), errorMessage.getReason());
        }

        composite.addComponent(bodyBuffer);
        composite.writerIndex(composite.writerIndex() + bodyBuffer.readableBytes());
    }

    return composite.order(ByteOrder.LITTLE_ENDIAN);
}

From source file:dorkbox.network.serialization.Serialization.java

License:Apache License

/**
 * Waits until a kryo is available to write, using CAS operations to prevent having to synchronize.
 * <p>//from  w w  w .j ava 2 s .  c o m
 * No crypto and no sequence number
 * <p>
 * There is a small speed penalty if there were no kryo's available to use.
 */
@Override
public final void write(final ByteBuf buffer, final Object message) throws IOException {
    final KryoExtra kryo = kryoPool.take();
    try {
        if (wireWriteLogger.isTraceEnabled()) {
            int start = buffer.writerIndex();
            kryo.write(buffer, message);
            int end = buffer.writerIndex();

            wireWriteLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start));
        } else {
            kryo.write(buffer, message);
        }
    } finally {
        kryoPool.put(kryo);
    }
}

From source file:dorkbox.network.serialization.Serialization.java

License:Apache License

/**
 * Reads an object from the buffer.//from  w w  w  .  j  a  va2  s  . c o m
 * <p>
 * No crypto and no sequence number
 *
 * @param length should ALWAYS be the length of the expected object!
 */
@Override
public final Object read(final ByteBuf buffer, final int length) throws IOException {
    final KryoExtra kryo = kryoPool.take();
    try {
        if (wireReadLogger.isTraceEnabled()) {
            int start = buffer.readerIndex();
            Object object = kryo.read(buffer);
            int end = buffer.readerIndex();

            wireReadLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start));

            return object;
        } else {
            return kryo.read(buffer);
        }
    } finally {
        kryoPool.put(kryo);
    }
}

From source file:dorkbox.network.serialization.Serialization.java

License:Apache License

/**
 * Waits until a kryo is available to write, using CAS operations to prevent having to synchronize.
 * <p>/*w w w  .j  ava 2  s  .  c  o  m*/
 * There is a small speed penalty if there were no kryo's available to use.
 */
@Override
public final void writeWithCrypto(final Connection_ connection, final ByteBuf buffer, final Object message)
        throws IOException {
    final KryoExtra kryo = kryoPool.take();
    try {
        // we only need to encrypt when NOT on loopback, since encrypting on loopback is a waste of CPU
        if (connection.isLoopback()) {
            if (wireWriteLogger.isTraceEnabled()) {
                int start = buffer.writerIndex();
                kryo.writeCompressed(connection, buffer, message);
                int end = buffer.writerIndex();

                wireWriteLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start));
            } else {
                kryo.writeCompressed(connection, buffer, message);
            }
        } else {
            if (wireWriteLogger.isTraceEnabled()) {
                int start = buffer.writerIndex();
                kryo.writeCrypto(connection, buffer, message);
                int end = buffer.writerIndex();

                wireWriteLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start));
            } else {
                kryo.writeCrypto(connection, buffer, message);
            }
        }
    } finally {
        kryoPool.put(kryo);
    }
}

From source file:dorkbox.network.serialization.Serialization.java

License:Apache License

/**
 * Reads an object from the buffer./*from w w w.  j  a  v a  2s  .  c  o m*/
 * <p>
 * Crypto + sequence number
 *
 * @param connection can be NULL
 * @param length should ALWAYS be the length of the expected object!
 */
@SuppressWarnings("Duplicates")
@Override
public final Object readWithCrypto(final Connection_ connection, final ByteBuf buffer, final int length)
        throws IOException {
    final KryoExtra kryo = kryoPool.take();
    try {
        // we only need to encrypt when NOT on loopback, since encrypting on loopback is a waste of CPU
        if (connection.isLoopback()) {
            if (wireReadLogger.isTraceEnabled()) {
                int start = buffer.readerIndex();
                Object object = kryo.readCompressed(connection, buffer, length);
                int end = buffer.readerIndex();

                wireReadLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start));

                return object;
            } else {
                return kryo.readCompressed(connection, buffer, length);
            }
        } else {
            if (wireReadLogger.isTraceEnabled()) {
                int start = buffer.readerIndex();
                Object object = kryo.readCrypto(connection, buffer, length);
                int end = buffer.readerIndex();

                wireReadLogger.trace(ByteBufUtil.hexDump(buffer, start, end - start));

                return object;
            } else {
                return kryo.readCrypto(connection, buffer, length);
            }
        }
    } finally {
        kryoPool.put(kryo);
    }
}

From source file:io.reactivesocket.FrameTest.java

License:Apache License

@Test
@Theory//from  ww  w  .  ja va 2 s  .  c o m
public void shouldReturnCorrectDataPlusMetadataForRequestResponse(final int offset) {
    final ByteBuffer requestData = TestUtil.byteBufferFromUtf8String("request data");
    final ByteBuffer requestMetadata = TestUtil.byteBufferFromUtf8String("request metadata");
    final Payload payload = createPayload(requestMetadata, requestData);

    Frame encodedFrame = Frame.Request.from(1, FrameType.REQUEST_RESPONSE, payload, 1);
    TestUtil.copyFrame(reusableMutableDirectBuffer, offset, encodedFrame);
    reusableFrame.wrap(reusableMutableDirectBuffer, offset);

    assertEquals(FrameType.REQUEST_RESPONSE, reusableFrame.getType());
    assertEquals(1, reusableFrame.getStreamId());
    assertEquals("request data", TestUtil.byteToString(reusableFrame.getData()));
    assertEquals("request metadata", TestUtil.byteToString(reusableFrame.getMetadata()));

    assertEquals("0000002c0004400000000001", ByteBufUtil.hexDump(encodedFrame.getByteBuffer().array(), 0, 12));
}

From source file:io.reactivesocket.FrameTest.java

License:Apache License

@Test
@Theory/*from w w w .j a  v a2  s .  c o  m*/
public void shouldReturnCorrectDataPlusMetadataForFireAndForget(final int offset) {
    final ByteBuffer requestData = TestUtil.byteBufferFromUtf8String("request data");
    final ByteBuffer requestMetadata = TestUtil.byteBufferFromUtf8String("request metadata");
    final Payload payload = createPayload(requestMetadata, requestData);

    Frame encodedFrame = Frame.Request.from(1, FrameType.FIRE_AND_FORGET, payload, 0);
    TestUtil.copyFrame(reusableMutableDirectBuffer, offset, encodedFrame);
    reusableFrame.wrap(reusableMutableDirectBuffer, offset);

    assertEquals("request data", TestUtil.byteToString(reusableFrame.getData()));
    assertEquals("request metadata", TestUtil.byteToString(reusableFrame.getMetadata()));
    assertEquals(FrameType.FIRE_AND_FORGET, reusableFrame.getType());
    assertEquals(1, reusableFrame.getStreamId());

    assertEquals("0000002c0005400000000001", ByteBufUtil.hexDump(encodedFrame.getByteBuffer().array(), 0, 12));
}

From source file:io.reactivesocket.FrameTest.java

License:Apache License

@Test
@Theory/*from   w  ww .ja  v a2  s.co m*/
public void shouldReturnCorrectDataPlusMetadataForRequestStream(final int offset) {
    final ByteBuffer requestData = TestUtil.byteBufferFromUtf8String("request data");
    final ByteBuffer requestMetadata = TestUtil.byteBufferFromUtf8String("request metadata");
    final Payload payload = createPayload(requestMetadata, requestData);

    Frame encodedFrame = Frame.Request.from(1, FrameType.REQUEST_STREAM, payload, 128);
    TestUtil.copyFrame(reusableMutableDirectBuffer, offset, encodedFrame);
    reusableFrame.wrap(reusableMutableDirectBuffer, offset);

    assertEquals("request data", TestUtil.byteToString(reusableFrame.getData()));
    assertEquals("request metadata", TestUtil.byteToString(reusableFrame.getMetadata()));
    assertEquals(FrameType.REQUEST_STREAM, reusableFrame.getType());
    assertEquals(1, reusableFrame.getStreamId());
    assertEquals(128, Frame.Request.initialRequestN(reusableFrame));

    assertEquals("000000300006480000000001", ByteBufUtil.hexDump(encodedFrame.getByteBuffer().array(), 0, 12));
}

From source file:io.reactivesocket.FrameTest.java

License:Apache License

@Test
@Theory/*from  w  ww. java2 s  .com*/
public void shouldReturnCorrectDataPlusMetadataForRequestSubscription(final int offset) {
    final ByteBuffer requestData = TestUtil.byteBufferFromUtf8String("request data");
    final ByteBuffer requestMetadata = TestUtil.byteBufferFromUtf8String("request metadata");
    final Payload payload = createPayload(requestMetadata, requestData);

    Frame encodedFrame = Frame.Request.from(1, FrameType.REQUEST_SUBSCRIPTION, payload, 128);
    TestUtil.copyFrame(reusableMutableDirectBuffer, offset, encodedFrame);
    reusableFrame.wrap(reusableMutableDirectBuffer, offset);

    assertEquals("request data", TestUtil.byteToString(reusableFrame.getData()));
    assertEquals("request metadata", TestUtil.byteToString(reusableFrame.getMetadata()));
    assertEquals(FrameType.REQUEST_SUBSCRIPTION, reusableFrame.getType());
    assertEquals(1, reusableFrame.getStreamId());
    assertEquals(128, Frame.Request.initialRequestN(reusableFrame));

    assertEquals("000000300007480000000001", ByteBufUtil.hexDump(encodedFrame.getByteBuffer().array(), 0, 12));
}

From source file:io.reactivesocket.FrameTest.java

License:Apache License

@Test
@Theory//from  w w  w . ja  va 2  s .c o  m
public void shouldReturnCorrectDataPlusMetadataForResponse(final int offset) {
    final ByteBuffer requestData = TestUtil.byteBufferFromUtf8String("response data");
    final ByteBuffer requestMetadata = TestUtil.byteBufferFromUtf8String("response metadata");
    final Payload payload = createPayload(requestMetadata, requestData);

    Frame encodedFrame = Frame.Response.from(1, FrameType.RESPONSE, payload);
    TestUtil.copyFrame(reusableMutableDirectBuffer, offset, encodedFrame);
    reusableFrame.wrap(reusableMutableDirectBuffer, offset);

    assertEquals("response data", TestUtil.byteToString(reusableFrame.getData()));
    assertEquals("response metadata", TestUtil.byteToString(reusableFrame.getMetadata()));
    assertEquals(FrameType.NEXT, reusableFrame.getType());
    assertEquals(1, reusableFrame.getStreamId());

    assertEquals("0000002e000b400000000001", ByteBufUtil.hexDump(encodedFrame.getByteBuffer().array(), 0, 12));
}