Example usage for io.netty.buffer ByteBuf nioBuffers

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

Introduction

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

Prototype

public abstract ByteBuffer[] nioBuffers();

Source Link

Document

Exposes this buffer's readable bytes as an NIO ByteBuffer 's.

Usage

From source file:io.urmia.proxy.HttpProxyFrontendHandler.java

License:Open Source License

private void digestUpdate(HttpContent httpContent) {
    ByteBuf content = httpContent.content();
    receivedSize.addAndGet(content.readableBytes());

    for (ByteBuffer bb : content.nioBuffers()) {
        if (bb.isDirect())
            DirectDigest.md5_update(md5ctx, bb);
        else//from   www.ja  va  2 s. c o  m
            DirectDigest.md5_update(md5ctx, bb.array());
    }
}

From source file:io.urmia.st.StorageServerHandler.java

License:Open Source License

private void writeToFile(ByteBuf buf) throws IOException {
    if (fileChannel == null)
        return;//w  w  w . j av  a 2  s . c  o  m
    fileChannel.write(buf.nioBuffers());
}

From source file:io.vertx.core.file.impl.AsyncFileImpl.java

License:Open Source License

private synchronized AsyncFile doWrite(Buffer buffer, long position, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(buffer, "buffer");
    Arguments.require(position >= 0, "position must be >= 0");
    check();// w  w  w . jav  a2 s.  c o  m
    Handler<AsyncResult<Void>> wrapped = ar -> {
        if (ar.succeeded()) {
            checkContext();
            if (writesOutstanding == 0 && closedDeferred != null) {
                closedDeferred.run();
            } else {
                checkDrained();
            }
            if (handler != null) {
                handler.handle(ar);
            }
        } else {
            if (handler != null) {
                handler.handle(ar);
            } else {
                handleException(ar.cause());
            }
        }
    };
    ByteBuf buf = buffer.getByteBuf();
    if (buf.nioBufferCount() > 1) {
        doWrite(buf.nioBuffers(), position, wrapped);
    } else {
        ByteBuffer bb = buf.nioBuffer();
        doWrite(bb, position, bb.limit(), wrapped);
    }
    return this;
}

From source file:net.tomp2p.connection.DefaultSignatureFactory.java

License:Apache License

@Override
public SHA1Signature sign(PrivateKey privateKey, ByteBuf buf)
        throws InvalidKeyException, SignatureException, IOException {
    Signature signature = signatureInstance();
    signature.initSign(privateKey);/*from   www . j av  a2s  .  co m*/
    ByteBuffer[] byteBuffers = buf.nioBuffers();
    int len = byteBuffers.length;
    for (int i = 0; i < len; i++) {
        ByteBuffer buffer = byteBuffers[i];
        signature.update(buffer);
    }
    byte[] signatureData = signature.sign();

    SHA1Signature decodedSignature = new SHA1Signature();
    decodedSignature.decode(signatureData);
    return decodedSignature;
}

From source file:net.tomp2p.connection.DefaultSignatureFactory.java

License:Apache License

@Override
public boolean verify(PublicKey publicKey, ByteBuf buf, SHA1Signature signatureEncoded)
        throws SignatureException, InvalidKeyException, IOException {
    Signature signature = signatureInstance();
    signature.initVerify(publicKey);// w  w w .j a v  a  2 s. co  m
    ByteBuffer[] byteBuffers = buf.nioBuffers();
    int len = byteBuffers.length;
    for (int i = 0; i < len; i++) {
        ByteBuffer buffer = byteBuffers[i];
        signature.update(buffer);
    }
    byte[] signatureReceived = signatureEncoded.encode();
    return signature.verify(signatureReceived);
}

From source file:org.asynchttpclient.netty.util.Utf8ByteBufCharsetDecoder.java

License:Open Source License

public String decode(ByteBuf buf) throws CharacterCodingException {
    if (buf.isDirect()) {
        return buf.toString(UTF_8);
    }//from w w  w  . j  av a  2 s  .c o m

    int length = buf.readableBytes();
    ensureCapacity(length);

    if (buf.nioBufferCount() == 1) {
        decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate(), length);
    } else {
        decode(buf.nioBuffers(), buf.readableBytes());
    }

    return charBuffer.flip().toString();
}

From source file:org.asynchttpclient.netty.util.Utf8ByteBufCharsetDecoder.java

License:Open Source License

public String decode(ByteBuf... bufs) throws CharacterCodingException {
    if (bufs.length == 1) {
        return decode(bufs[0]);
    }/*  w ww.  j av a  2 s .c  om*/

    int totalSize = 0;
    int totalNioBuffers = 0;
    boolean direct = false;
    for (ByteBuf buf : bufs) {
        if (buf.isDirect()) {
            direct = true;
            break;
        }
        totalSize += buf.readableBytes();
        totalNioBuffers += buf.nioBufferCount();
    }

    if (direct) {
        ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bufs);
        try {
            return wrappedBuffer.toString(UTF_8);
        } finally {
            wrappedBuffer.release();
        }

    } else {
        ByteBuffer[] nioBuffers = new ByteBuffer[totalNioBuffers];
        int i = 0;
        for (ByteBuf buf : bufs) {
            for (ByteBuffer nioBuffer : buf.nioBuffers()) {
                nioBuffers[i++] = nioBuffer;
            }
        }

        ensureCapacity(totalSize);
        decode(nioBuffers, totalSize);

        return charBuffer.flip().toString();
    }
}

From source file:org.asynchttpclient.request.body.multipart.part.MultipartPart.java

License:Open Source License

protected long transfer(ByteBuf source, WritableByteChannel target, MultipartState sourceFullyWrittenState)
        throws IOException {

    int transferred = 0;
    if (target instanceof GatheringByteChannel) {
        transferred = source.readBytes((GatheringByteChannel) target, (int) source.readableBytes());
    } else {//from   w ww.  j av a 2 s.c om
        for (ByteBuffer byteBuffer : source.nioBuffers()) {
            int len = byteBuffer.remaining();
            int written = target.write(byteBuffer);
            transferred += written;
            if (written != len) {
                // couldn't write full buffer, exit loop
                break;
            }
        }
        // assume this is a basic single ByteBuf
        source.readerIndex(source.readerIndex() + transferred);
    }

    if (source.isReadable()) {
        slowTarget = true;
    } else {
        state = sourceFullyWrittenState;
    }
    return transferred;
}

From source file:org.asynchttpclient.util.ByteBufUtils.java

License:Open Source License

public static String byteBuf2String(ByteBuf buf, Charset charset)
        throws UTFDataFormatException, IndexOutOfBoundsException, CharacterCodingException {

    int byteLen = buf.readableBytes();

    if (charset.equals(StandardCharsets.US_ASCII)) {
        return Utf8Reader.readUtf8(buf, byteLen);
    } else if (charset.equals(StandardCharsets.UTF_8)) {
        try {/*from www.j a  va 2s .c  o m*/
            return Utf8Reader.readUtf8(buf.duplicate(), (int) (byteLen * 1.4));
        } catch (IndexOutOfBoundsException e) {
            // try again with 3 bytes per char
            return Utf8Reader.readUtf8(buf, byteLen * 3);
        }
    } else {
        return byteBuffersToString(buf.nioBuffers(), charset);
    }
}

From source file:org.elasticsearch.http.nio.NettyAdaptor.java

License:Apache License

NettyAdaptor(ChannelHandler... handlers) {
    nettyChannel = new EmbeddedChannel();
    nettyChannel.pipeline().addLast("write_captor", new ChannelOutboundHandlerAdapter() {

        @Override//from w ww  .jav a2 s. c om
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            // This is a little tricky. The embedded channel will complete the promise once it writes the message
            // to its outbound buffer. We do not want to complete the promise until the message is sent. So we
            // intercept the promise and pass a different promise back to the rest of the pipeline.

            try {
                ByteBuf message = (ByteBuf) msg;
                promise.addListener((f) -> message.release());
                NettyListener listener = NettyListener.fromChannelPromise(promise);
                flushOperations.add(new FlushOperation(message.nioBuffers(), listener));
            } catch (Exception e) {
                promise.setFailure(e);
            }
        }
    });
    nettyChannel.pipeline().addLast(handlers);
}