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:ratpack.session.clientside.internal.DefaultCrypto.java

License:Apache License

@Override
public byte[] encrypt(ByteBuf message) {
    return Exceptions.uncheck(() -> {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        ByteBuf messageBuf = Unpooled.wrappedBuffer(new byte[cipher.getOutputSize(message.readableBytes())]);
        cipher.update(message.nioBuffer(), messageBuf.nioBuffer());

        byte[] payload = cipher.doFinal();
        if (isInitializationVectorRequired) {
            byte[] ivBytes = cipher.getIV();
            messageBuf.release();//www .jav a  2 s  . c  om

            int outputLength = 1 + ivBytes.length + payload.length;
            ByteBuf output = Unpooled.wrappedBuffer(new byte[outputLength]).resetWriterIndex()
                    .writeByte(ivBytes.length).writeBytes(ivBytes).writeBytes(payload);

            payload = output.array();

            output.release();
        }

        return payload;
    });
}

From source file:ratpack.session.clientside.internal.DefaultCrypto.java

License:Apache License

@Override
public byte[] decrypt(ByteBuf message) {
    return Exceptions.uncheck(() -> {
        Cipher cipher = Cipher.getInstance(algorithm);

        if (isInitializationVectorRequired) {
            int ivByteLength = message.readByte();
            ByteBuf ivBytes = message.readBytes(ivByteLength);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes.array());
            ivBytes.release();/*from   ww  w.ja va  2 s  . co  m*/

            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        }

        int messageLength = message.readableBytes();
        ByteBuf output = Unpooled.wrappedBuffer(new byte[cipher.getOutputSize(messageLength)]);
        cipher.update(message.readBytes(messageLength).nioBuffer(), output.nioBuffer());

        byte[] decrypted = cipher.doFinal();
        output.release();

        return decrypted;
    });
}

From source file:ratpack.session.clientside.internal.DefaultSigner.java

License:Apache License

@Override
public byte[] sign(ByteBuf message) {
    return Exceptions.uncheck(() -> {
        Mac mac = Mac.getInstance(secretKeySpec.getAlgorithm());
        mac.init(secretKeySpec);/*ww w . j a va 2 s .  c  o m*/
        mac.update(message.nioBuffer());
        return mac.doFinal();
    });
}

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

License:Apache License

@Override
public ByteBuffer encodeValue(ByteBuf value) {
    return value.nioBuffer();
}

From source file:reactor.io.net.impl.netty.NettyChannelHandlerBridge.java

License:Apache License

private void passToConnection(ByteBuf data) {
    Buffer b = new Buffer(data.nioBuffer());
    int start = b.position();
    if (null != channelStream.getDecoder() && null != b.byteBuffer()) {
        IN read = channelStream.getDecoder().apply(b);
        if (read != null) {
            channelSubscription.onNext(read);
        }/*from w w  w.  j a va 2 s  .co  m*/
    }

    //data.remaining() > 0;
    data.skipBytes(b.position() - start);
}

From source file:reactor.io.net.netty.NettyNetChannelInboundHandler.java

License:Apache License

private void passToConnection(ByteBuf data) {
    Buffer b = new Buffer(data.nioBuffer());
    int start = b.position();
    netChannel.read(b);/*  w ww  .java2 s.  c  o m*/
    data.skipBytes(b.position() - start);
}

From source file:reactor.tcp.netty.NettyTcpConnectionChannelInboundHandler.java

License:Open Source License

private void passToConnection(ByteBuf data) {
    Buffer b = new Buffer(data.nioBuffer());
    int start = b.position();
    conn.read(b);//  w  w w .  j  a v a2  s. c  o m
    data.skipBytes(b.position() - start);
}

From source file:rxweb.engine.server.netty.NettyServerCodecHandlerAdapter.java

License:Apache License

/**
 * Create a {@link NettyServerRequestAdapter} when a {@link HttpRequest} is received, and use
 * @ {@link UnicastContentSubject} to send the content as a request stream.
 *//*from w  ww  .  ja va  2  s  .c o m*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Class<?> messageClass = msg.getClass();
    if (HttpRequest.class.isAssignableFrom(messageClass)) {
        this.request = new NettyServerRequestAdapter((HttpRequest) msg, this.requestContent);
        super.channelRead(ctx, this.request);
    } else if (HttpContent.class.isAssignableFrom(messageClass)) {
        Assert.notNull(this.request);
        ByteBuf content = ((ByteBufHolder) msg).content();
        ByteBuffer nioBuffer = content.nioBuffer();
        this.requestContent.onNext(nioBuffer);
        if (LastHttpContent.class.isAssignableFrom(messageClass)) {
            this.requestContent.onCompleted();
        }
        // FIXME I need to make it works without that ...
        super.channelRead(ctx, this.request);
    } else {
        super.channelRead(ctx, msg);
    }
}