Example usage for io.netty.buffer Unpooled directBuffer

List of usage examples for io.netty.buffer Unpooled directBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled directBuffer.

Prototype

public static ByteBuf directBuffer() 

Source Link

Document

Creates a new big-endian direct buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:org.asynchttpclient.ntlm.NtlmTest.java

License:Open Source License

@Test(expectedExceptions = NtlmEngineException.class)
public void testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeBytes("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
    buf.writeByte(0);/*from   w ww  .  j  a va2 s  .c o  m*/
    // type 2 indicator
    buf.writeByte(3).writeByte(0).writeByte(0).writeByte(0);
    buf.writeBytes("challenge".getBytes());
    NtlmEngine engine = new NtlmEngine();
    engine.generateType3Msg("username", "password", "localhost", "workstation",
            Base64.encode(ByteBufUtils.byteBuf2Bytes(buf)));
    fail("An NtlmEngineException must have occurred as type 2 indicator is incorrect");
}

From source file:org.asynchttpclient.ntlm.NtlmTest.java

License:Open Source License

@Test(expectedExceptions = NtlmEngineException.class)
public void testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeBytes("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
    buf.writeByte(0);// w  w w . j  a  va 2  s  .  c om
    // type 2 indicator
    buf.writeByte(2).writeByte(0).writeByte(0).writeByte(0);
    buf.writeLong(1);
    // flags
    buf.writeByte(0);// unicode support indicator
    buf.writeByte(0).writeByte(0).writeByte(0);
    buf.writeLong(1);// challenge
    NtlmEngine engine = new NtlmEngine();
    engine.generateType3Msg("username", "password", "localhost", "workstation",
            Base64.encode(ByteBufUtils.byteBuf2Bytes(buf)));
    fail("An NtlmEngineException must have occurred as unicode support is not indicated");
}

From source file:org.asynchttpclient.ntlm.NtlmTest.java

License:Open Source License

@Test
public void testGenerateType3Msg() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeBytes("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
    buf.writeByte(0);/*w w w . ja  v  a 2 s. co  m*/
    // type 2 indicator
    buf.writeByte(2).writeByte(0).writeByte(0).writeByte(0);
    buf.writeLong(0);
    // flags
    buf.writeByte(1);// unicode support indicator
    buf.writeByte(0).writeByte(0).writeByte(0);
    buf.writeLong(1);// challenge
    NtlmEngine engine = new NtlmEngine();
    String type3Msg = engine.generateType3Msg("username", "password", "localhost", "workstation",
            Base64.encode(ByteBufUtils.byteBuf2Bytes(buf)));
    assertEquals(type3Msg,
            "TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABIAEgB4AAAAEAAQAIoAAAAWABYAmgAAAAAAAACwAAAAAQAAAgUBKAoAAAAP1g6lqqN1HZ0wSSxeQ5riQkyh7/UexwVlCPQm0SHU2vsDQm2wM6NbT2zPonPzLJL0TABPAEMAQQBMAEgATwBTAFQAdQBzAGUAcgBuAGEAbQBlAFcATwBSAEsAUwBUAEEAVABJAE8ATgA=",
            "Incorrect type3 message generated");
}

From source file:org.iotivity.cloud.base.protocols.coap.CoapEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, CoapMessage msg, ByteBuf out) throws Exception {

    CoapMessage coapMessage = (CoapMessage) msg;

    ByteBuf optBuf = Unpooled.directBuffer();

    /**// w ww.  j  a v a2 s.  c  om
     * encode options
     */
    encodeOptions(optBuf, coapMessage);
    long length = optBuf.readableBytes();

    if (coapMessage.getPayloadSize() > 0) {
        // + 1 means 8bits delimiter
        length += 1 + coapMessage.getPayloadSize();
    }

    calcShimHeader(coapMessage, out, length);

    out.writeByte(coapMessage.getCode());

    if (coapMessage.getTokenLength() > 0) {
        out.writeBytes(coapMessage.getToken());
    }

    // Write option
    if (optBuf.readableBytes() > 0) {
        out.writeBytes(optBuf);
    }

    optBuf.release();

    if (coapMessage.getPayloadSize() > 0) {
        out.writeByte(255);
        out.writeBytes(coapMessage.getPayload());
    }
}

From source file:org.lanternpowered.server.network.buffer.UnpooledByteBufferAllocator.java

License:MIT License

@Override
public ByteBuffer directBuffer() {
    return new LanternByteBuffer(Unpooled.directBuffer());
}

From source file:org.royaldev.enterprise.proxy.ProxyBackendHandler.java

License:Apache License

private ByteBuf createMessage(String sender, String message) {
    final ByteBuf motd = Unpooled.directBuffer();
    motd.writeByte(PacketType.Server.CHAT);
    motd.writeBytes(new byte[] { 4, 48, 1, 0, 0, 0, 0, 1 }); // 48 is length
    motd.writeByte((byte) sender.length());
    motd.writeBytes(this.getStringBytes(sender));
    motd.writeByte((byte) message.length());
    motd.writeBytes(this.getStringBytes(message));
    return motd;// ww  w.ja  v  a 2  s .c  o m
}

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

License:Open Source License

@Test
public void byteBufTest() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeInt(1234);//w w  w  . j a v a  2  s  .  c o m
    int val = buf.readInt();
    assertEquals(1234, val);
}

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

License:Open Source License

@Test
public void byteTest() {
    byte[] some = new byte[] { 1, 2, 3, 4 };
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);/*from   w w w  .j  a v a 2 s  .c o m*/
    System.out.println("Bytes: " + ByteBufUtil.hexDump(buf));
    byte[] someRes = (byte[]) Serializers.fromBinary(buf, Optional.absent());
    assertArrayEquals(some, someRes);
}

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

License:Open Source License

@Test
public void nullTest() {
    Object some = null;/*from  w  ww .  j a v  a2 s  . c  o m*/
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);
    System.out.println("Nulls: " + ByteBufUtil.hexDump(buf));
    Object someRes = Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some, someRes);
}

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

License:Open Source License

@Test
public void intTest() {
    Integer some = 1234;//from   www  . j av a 2 s  . c  o m
    ByteBuf buf = Unpooled.directBuffer();
    Serializers.toBinary(some, buf);
    System.out.println("Ints: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    Integer someRes = (Integer) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(some, someRes);

    int someI = 1234;
    Serializers.toBinary(someI, buf);
    System.out.println("Ints2: " + ByteBufUtil.hexDump(buf) + " : " + ByteBufUtil.hexDump(buf).length());
    int someResI = (int) Serializers.fromBinary(buf, Optional.absent());
    assertEquals(someI, someResI);
}