Example usage for io.netty.buffer ByteBuf copy

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

Introduction

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

Prototype

public abstract ByteBuf copy();

Source Link

Document

Returns a copy of this buffer's readable bytes.

Usage

From source file:org.thingsplode.synapse.endpoint.handlers.HttpRequestIntrospector.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
    try {/*from   w  w  w .java2s  .  com*/
        if (msg == null) {
            logger.warn("Message@Endpoint received: NULL");
        } else if (logger.isDebugEnabled()) {
            final StringBuilder hb = new StringBuilder();
            msg.headers().entries().forEach(e -> {
                hb.append(e.getKey()).append(" : ").append(e.getValue()).append("\n");
            });
            String payloadAsSring = null;
            if (msg instanceof FullHttpRequest) {
                ByteBuf content = ((FullHttpRequest) msg).content();
                byte[] dst = new byte[content.capacity()];
                content.copy().getBytes(0, dst);
                content.retain();
                payloadAsSring = new String(dst, Charset.forName("UTF-8"));
            }
            logger.debug("Message@Endpoint received: \n\n" + "Uri: " + msg.uri() + "\n" + "Method: "
                    + msg.method() + "\n" + hb.toString() + "\n" + "Payload -> "
                    + (!Util.isEmpty(payloadAsSring) ? payloadAsSring + "\n" : "EMPTY\n"));
        }
    } catch (Throwable th) {
        logger.error(th.getClass().getSimpleName() + " caught while introspecting request, with message: "
                + th.getMessage(), th);
    }
    if (msg != null && msg instanceof FullHttpRequest) {
        ((FullHttpRequest) msg).content().retain();
    }
    ctx.fireChannelRead(msg);
}

From source file:org.thingsplode.synapse.proxy.handlers.HttpResponseIntrospector.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpResponse msg) throws Exception {
    if (msg != null) {
        final StringBuilder hb = new StringBuilder();
        msg.headers().entries().forEach(e -> {
            hb.append(e.getKey()).append(" : ").append(e.getValue()).append("\n");
        });/*from   w ww  . j av a2  s. c  o m*/
        String payloadAsSring = null;
        if (msg instanceof FullHttpResponse) {
            ByteBuf content = ((FullHttpResponse) msg).content();
            byte[] dst = new byte[content.capacity()];
            content.copy().getBytes(0, dst);
            content.retain();
            payloadAsSring = new String(dst, Charset.forName("UTF-8"));
        }
        logger.debug("Message@Proxy received [" + msg.getClass().getSimpleName() + "]: \n\n" + "Status: "
                + msg.status() + "\n" + hb.toString() + "\n" + "Payload -> ["
                + (!Util.isEmpty(payloadAsSring) ? payloadAsSring : "EMPTY") + "]\n");
    }
    ctx.fireChannelRead(msg);
}

From source file:ratpack.session.store.internal.LocalMemorySessionStoreAdapter.java

License:Apache License

@Override
public Operation store(SessionId sessionId, ByteBufAllocator bufferAllocator, ByteBuf sessionData) {
    return execControl.operation(() -> {
        ByteBuf oldValue = cache.asMap().put(sessionId.getValue(),
                Unpooled.unmodifiableBuffer(sessionData.copy()));
        if (oldValue != null) {
            oldValue.release();/*from   w  w w.j ava  2 s . c o m*/
        }
    });
}

From source file:ru.jts.authserver.network.clientpackets.CM_AUTH_BY_PASS.java

License:Apache License

@Override
public void runImpl() {
    //Account account = AccountsDAO.getInstance().restoreByLogin(jsonMap.get(JSON_LOGIN));

    getClient().setBlowFishKey(blowFishKey);
    getClient().setRandomKey(Rnd.nextInt());

    ByteBuf buf = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);

    buf.writeBytes(new byte[] { (byte) 192, (byte) 168, (byte) 1, (byte) 100 });
    buf.writeInt(20015);//from   w  ww .j  ava  2  s .  c o m

    buf.writeInt(getClient().getRandomKey());

    Map<String, String> jsonMapp = new HashMap<>();
    //jsonMap.put("security_msg", "old_pass");
    jsonMapp.put("token2", getClient().generateToken2());

    ObjectMapper mapper = new ObjectMapper();
    String json = null;
    try {
        json = mapper.writeValueAsString(jsonMapp);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
        return;
    }
    buf.writeByte(json.length());
    buf.writeBytes(json.getBytes());

    byte[] cryptedData = CryptEngine.getInstance().encrypt(buf.copy().array(), getClient().getBlowFishKey(),
            CryptEngine.ZERO_TRAILING_MODE);

    State state = AccountController.getInstance().accountLogin(jsonMap, password);
    switch (state) {
    case AUTHED: {
        getClient().sendPacket(new SM_AUTH_RESPONSE(sessionId, SM_AUTH_RESPONSE.LOGIN_OK, cryptedData));
        break;
    }
    case ALREADY_AUTHED: {
        getClient().sendPacket(new SM_AUTH_RESPONSE(sessionId,
                SM_AUTH_RESPONSE.LOGIN_REJECTED_ALREADY_LOGGED_IN, cryptedData));
        break;
    }
    default: {
    }
    }
}

From source file:ru.jts_dev.authserver.config.EncoderTest.java

License:Open Source License

@Test
public void testEncWithXor() throws Exception {
    byte[] data = new byte[32];
    random.nextBytes(data);//from  w  w w. ja  va  2 s .c o m
    ByteBuf buf = buffer(32, 48);

    ByteBuf raw = buf.copy(); // create copy of buf, because encoder due direct write to buf

    ByteBuf encoded = encoder.encWithXor(buf);

    // encoded array must be longer for 4 bytes
    assertThat(raw.writerIndex() + 16 == encoded.writerIndex());
}