NOTE

Usage

Historical example of allocating, reading, writing, and releasing a pooled direct ByteBuf

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

package com.example.demo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;

/**
 * @description:
 * @author: <AUTHOR>
 * @create: 2019-12-13 19:34
 **/
public class ByteBufDemo
{
    public static void main(String[] args)
    {
        ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;

        // tiny-size memory allocation is normalized to an integer multiple of 16
        // that is greater than or equal to the requested size:
        // 254 is normalized to 256 here.
        ByteBuf byteBuf = alloc.directBuffer(254);

        // Read and write ByteBuf.
        byteBuf.writeInt(126);
        System.out.println(byteBuf.readInt());

        // Very important: release the memory.
        byteBuf.release();
    }
}

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub