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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 三驾马车

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())