惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 三驾马车

Claude Code 官宣:可以在 IDEA 用了! idea gitee 更新已取消 解决方案 ByteBuffer和ByteBuf区别 Marshalling.getProvidedMarshallerFactory("serial") 参数有那些 ProtobufVarint32FrameDecoder和ProtobufDecoder区别 protobuf 的 Varint 编码规范 netty initChannel ch.pipeline().addLast 先后顺序很重要 ChannelInboundHandlerAdapter 的channelRead和channelReadComplete的区别 ServerBootstrap 和Bootstrap 区别 childhandler 和 handler 区别 ChannelInitializer<SocketChannel> 的作用详解 ChannelHandlerAdapter 和 ChannelInboundHandlerAdapter 的区别 SimpleChannelInboundHandler 中的 messageReceived 和 channelRead0 ChannelHandlerAdapter 与 ChannelInboundHandler 的区别 Application run failed .ParserException: while parsing a block mapping in 'reader' openssl genrsa 自签名ssl证书 上传本地项目到新建git项目 save download pdf
Unpooled.buffer()和Unpooled.copiedBuffer区别
三驾马车 · 2025-05-14 · via 博客园 - 三驾马车

// 创建默认大小的堆缓冲区(初始256字节,最大Integer.MAX_VALUE)
ByteBuf heapBuffer = Unpooled.buffer();

// 创建指定容量的堆缓冲区
public static ByteBuf buffer(int initialCapacity);

// 创建指定大小的堆缓冲区
ByteBuf sizedHeapBuffer = Unpooled.buffer(1024);

// 创建直接内存缓冲区
ByteBuf directBuffer = Unpooled.directBuffer();

------------------------------------------------------

// 从字节数组创建(深拷贝)
public static ByteBuf copiedBuffer(byte[] array);

// 从字符串创建(指定字符集)
public static ByteBuf copiedBuffer(CharSequence string, Charset charset);

// 从现有数据创建
ByteBuf copiedBuffer = Unpooled.copiedBuffer("Hello", CharsetUtil.UTF_8);
ByteBuf buf2 = Unpooled.copiedBuffer(new byte[]{1, 2, 3});

// 包装现有字节数组
byte[] bytes = new byte[128];
ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(bytes);

特性Unpooled.buffer()Unpooled.copiedBuffer()
缓冲区初始化 创建空缓冲区(内容未初始化) 创建包含指定数据的缓冲区(深拷贝)
内存来源 新分配的空白内存区域 新分配内存并拷贝传入数据
数据所有权 后续由用户写入数据 立即拥有数据的独立副本
典型用途 需要逐步写入数据的场景 快速包装现有数据的场景
线程安全性 非线程安全 非线程安全(但初始数据独立)
方法数据来源是否拷贝可变性
buffer() - 可变
copiedBuffer() 外部数据 可变
wrappedBuffer() 外部数据 不可变
directBuffer() - 可变
firstMessage = Unpooled.buffer(req.length);
firstMessage.writeBytes(req);

等同如下效果

firstMessage =  Unpooled.copiedBuffer("QUERY TIME ORDER".getBytes())