Example usage for io.netty.buffer ByteBufAllocator heapBuffer

List of usage examples for io.netty.buffer ByteBufAllocator heapBuffer

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator heapBuffer.

Prototype

ByteBuf heapBuffer(int initialCapacity);

Source Link

Document

Allocate a heap ByteBuf with the given initial capacity.

Usage

From source file:com.minetats.mw.NamedPipe.java

License:Apache License

@Override
public ByteBuf readChunk(ByteBufAllocator bba) throws Exception {
    chunks++;/*from   w ww .jav  a2 s .c  o  m*/
    ByteBuf buf = bba.heapBuffer(chunkSize);
    boolean release = false;
    int read = 0;
    try {
        do {
            buf.writerIndex(buf.writerIndex() + read);
            read = file.read(buf.array(), buf.arrayOffset() + read, chunkSize - read);
        } while (read > 0);
        int index = buf.writerIndex() - 1;
        if (buf.getByte(index) == '\n' && buf.getByte(index - 1) == '\n') {
            endOfInput = true;
            System.out.println("endOfInput=" + endOfInput + ", read " + chunks + " chunks");
        }
        return buf;
    } finally {
        if (release) {
            buf.release();
        }
    }
}

From source file:io.advantageous.conekt.net.impl.ConektHandler.java

License:Open Source License

protected static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }/*from  w  w  w . j  a v a  2s.co m*/
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf);
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}

From source file:io.vertx.core.http.impl.Http2ConnectionBase.java

License:Open Source License

/**
 * Return a buffer from HTTP/2 codec that Vert.x can use:
 *
 * - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version
 * - if it's a composite buffer we do the same
 * - otherwise we increase the ref count
 *///from   ww  w. j  a  v  a 2  s. c o m
static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        if (buf.isReadable()) {
            ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
            buffer.writeBytes(buf);
            return buffer;
        } else {
            return Unpooled.EMPTY_BUFFER;
        }
    }
    return buf.retain();
}

From source file:io.vertx.core.net.impl.VertxHandler.java

License:Open Source License

public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
    if (buf == Unpooled.EMPTY_BUFFER) {
        return buf;
    }/*w w  w . j  av  a 2s  . c o m*/
    if (buf.isDirect() || buf instanceof CompositeByteBuf) {
        try {
            if (buf.isReadable()) {
                ByteBuf buffer = allocator.heapBuffer(buf.readableBytes());
                buffer.writeBytes(buf);
                return buffer;
            } else {
                return Unpooled.EMPTY_BUFFER;
            }
        } finally {
            buf.release();
        }
    }
    return buf;
}

From source file:mysql.client.Session_Old.java

/**
 * ?mysql server ?/*  www  . ja v a 2s  .  c  o  m*/
 * @return
 * @throws IOException
 */
private ByteBuf readPacket() throws IOException {
    try {
        //mysqlio?DataInputStream.readFully,?
        //?read(bytes)readFully(bytes),????,??
        io.readFully(packetHeaderBuf);
    } catch (EOFException e) {
        //mysql ?,
        io.close();
        return null;
    }
    //        if(count < packetHeaderBuf.length){
    //            //mysql ?,
    //            io.close();
    //            return null;
    //        }

    int packetLength = (this.packetHeaderBuf[0] & 0xff) + ((this.packetHeaderBuf[1] & 0xff) << 8)
            + ((this.packetHeaderBuf[2] & 0xff) << 16);

    byte[] bytes = new byte[packetLength + 1];
    int realReadCount = io.read(bytes);
    bytes[packetLength] = 0;//??packetLength, 0, c++ /0 ?
    if (realReadCount != packetLength) {
        io.close();
        throw new IllegalStateException("mysql ??,length??");
    }
    ByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
    ByteBuf byteBuf = allocator.heapBuffer(bytes.length);
    byteBuf.writeBytes(bytes);
    byteBuf = byteBuf.order(ByteOrder.LITTLE_ENDIAN);//mysql ????
    return byteBuf;
}