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) 

Source Link

Document

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

Usage

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void uuidTest() {
    ByteBuf buf = Unpooled.directBuffer();
    UUID orig = UUID.randomUUID();
    Serializers.toBinary(orig, buf);//www  . j  a v a  2 s.  c  o  m
    System.out.println("UUID: " + ByteBufUtil.hexDump(buf));
    UUID copy = (UUID) Serializers.fromBinary(buf, Optional.absent());
    Assert.assertEquals(orig, copy);
}

From source file:se.sics.kompics.network.netty.serialization.SerializationTest.java

License:Open Source License

@Test
public void avroTest() throws AvroSerializer.KeyExistsException, AvroSerializer.InvalidKeyException {
    Serializers.register(new AvroSerializer(), "avroS");
    AvroSerializer.register(15, SomeAvro.class);
    Serializers.register(ParentSomeAvro.class, "avroS");
    AvroSerializer.register(16, SomeGeneratedAvro.class);
    SomeAvro some = new SomeAvro();
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);//from w  ww .j av a2 s .c o m
    System.out.println(
            "AVRO - SomeAvro: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    SomeAvro someRes = (SomeAvro) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some.getField(), someRes.getField());
    //
    buf.clear();
    //
    ParentSomeAvro someP = new ParentSomeAvro(some);
    Serializers.toBinary(someP, buf);
    System.out.println(
            "AVRO - ParentSomeAvro: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    ParentSomeAvro somePRes = (ParentSomeAvro) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(someP.getMySer().getField(), somePRes.getMySer().getField());
    //
    buf.clear();
    //
    SomeGeneratedAvro sga = SomeGeneratedAvro.newBuilder().setSomeNumber(1234).build();
    Serializers.toBinary(sga, buf);
    System.out.println("AVRO - SomeGeneratedAvro: " + ByteBufUtil.hexDump(buf) + " : "
            + ByteBufUtil.hexDump(buf).length());
    SomeGeneratedAvro sgaR = (SomeGeneratedAvro) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(sga.getSomeNumber(), sgaR.getSomeNumber());
    //
    buf.release();
}

From source file:server.operation.OperationServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;/* www  .jav  a  2s.c  om*/
    System.out.println("? : " + (ByteBufUtil.hexDump(buf)));

    startTm = System.currentTimeMillis();

    /*
     * if (buf.readableBytes() <= 20) {
     * System.out.println("buf.readableBytes() : " + buf.readableBytes() +
     * "?? ? 20? ?. "); return; }
     */

    // 1. ? ?
    headerSize = buf.readInt();
    if (headerSize >= 100 || headerSize < 0) {
        System.out.println("fileNameSize : " + headerSize);
        // System.out.println((ByteBufUtil.hexDump(buf)));
        return;
    }

    headerBody = buf.readBytes(headerSize).toString(CharsetUtil.UTF_8);
    dataSize = buf.readLong();
    System.out.println(
            "headerSize : " + headerSize + ", headerBody : " + headerBody + ", dataSize : " + dataSize);

    headerRead = true;

    // 2. ?  ? ?   : operation or file

    if (headerBody.startsWith(OperationCommand.OP_CODE_JOIN)) {
        operationCode = headerBody;
    } else {
        System.err.println("? Header Body . : " + headerBody);
        return;
    }

    int a = buf.readableBytes();
    int b = (int) Math.min(dataSize - offset, a);
    System.out.println("ReadableBytes : " + a + ", To-Read Bytes : " + b);

    operationData += buf.readBytes(b).toString(CharsetUtil.UTF_8);
    System.out.println("OP Data   : " + operationData);

    offset += b;

    if (offset >= dataSize) {
        offset = 0;
        headerRead = false;

        if (headerBody.startsWith(OperationCommand.OP_CODE_JOIN)) {
            // ?? ? ??
            agentName = operationData + "(" + ctx.channel().id().toString() + ")";

            // WebApplicationContext context =
            // ContextLoader.getCurrentWebApplicationContext();
            // WsHandler wsHandler =
            // (WsHandler)context.getBean("websocket.WsHandler.class");
            // WebApplicationContext wac = BootServer.wac;
            // WsHandler wsHandler = (WsHandler)wac.getBean("wsHandler");
            // WsHandler.sendMessage(agentName + "("
            // +ctx.channel().id().toString() + ")");

        }

        //ChannelManager.map.remove(ctx.channel().id().toString());
        ChannelManager.map.put(agentName, ctx.channel());

        // ?  
        publishEvent(EventType.Connected, agentName, "");

        //   
        // WsHandler ws = (WsHandler)
        // applicationContext.getBean("wsHandler");
        // ws.sendMessage(agentName + "(" + ctx.channel().id().toString() +
        // ") is connected");
        // System.out.println(this.getClass() + agentName + "(" +
        // ctx.channel().id().toString() + ") is connected");

        System.out.println("OP Data : " + operationData);
        System.out.println(
                "OP Data  : " + ((System.currentTimeMillis() - startTm) / 1000.0f) + "");

        operationData = ""; // 

        return;
    }

}