Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
1b3ea55d
N
netty-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
netty-demo
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
netty-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
1b3ea55d
编写于
4月 27, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:bytebuf数据合并
上级
16c40b32
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
480 addition
and
0 deletion
+480
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_05.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_05.java
+66
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_06.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_06.java
+83
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_07.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_07.java
+102
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_08.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_08.java
+99
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_09.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_09.java
+65
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_10.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_10.java
+65
-0
未找到文件。
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_05.java
0 → 100644
浏览文件 @
1b3ea55d
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
lombok.extern.slf4j.Slf4j
;
import
static
io
.
netty
.
buffer
.
ByteBufUtil
.
appendPrettyHexDump
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
* ByteBuf 写入扩容
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_05
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buffer
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
10
);
buffer
.
writeBytes
(
new
byte
[]{
1
,
2
,
3
,
4
});
log
(
buffer
);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
* read index:0 write index:4 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 |.... |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
writeInt
(
5
);
//int占四个字节
log
(
buffer
);
/**
* read index:0 write index:8 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 |........ |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
writeInt
(
7
);
//int占四个字节
log
(
buffer
);
/**
* read index:0 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 00 00 00 07 |............ |
* +--------+-------------------------------------------------+----------------+
*/
}
private
static
void
log
(
ByteBuf
buffer
)
{
int
length
=
buffer
.
readableBytes
();
int
rows
=
length
/
16
+
(
length
%
15
==
0
?
0
:
1
)
+
4
;
StringBuilder
buf
=
new
StringBuilder
(
rows
*
80
*
2
)
.
append
(
"read index:"
).
append
(
buffer
.
readerIndex
())
.
append
(
" write index:"
).
append
(
buffer
.
writerIndex
())
.
append
(
" capacity:"
).
append
(
buffer
.
capacity
())
.
append
(
NEWLINE
);
appendPrettyHexDump
(
buf
,
buffer
);
System
.
out
.
println
(
buf
.
toString
());
}
}
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_06.java
0 → 100644
浏览文件 @
1b3ea55d
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
lombok.extern.slf4j.Slf4j
;
import
static
io
.
netty
.
buffer
.
ByteBufUtil
.
appendPrettyHexDump
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
* ByteBuf 读数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_06
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buffer
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
10
);
buffer
.
writeBytes
(
new
byte
[]{
1
,
2
,
3
,
4
});
log
(
buffer
);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
* read index:0 write index:4 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 |.... |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
writeInt
(
5
);
//int占四个字节
log
(
buffer
);
/**
* read index:0 write index:8 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 |........ |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
writeInt
(
7
);
//int占四个字节
log
(
buffer
);
/**
* read index:0 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 00 00 00 07 |............ |
* +--------+-------------------------------------------------+----------------+
*/
System
.
out
.
println
(
buffer
.
readByte
());
System
.
out
.
println
(
buffer
.
readByte
());
System
.
out
.
println
(
buffer
.
readByte
());
System
.
out
.
println
(
buffer
.
readByte
());
log
(
buffer
);
/**
* 1
* 2
* 3
* 4
* read index:4 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 07 |........ |
* +--------+-------------------------------------------------+----------------+
*/
}
private
static
void
log
(
ByteBuf
buffer
)
{
int
length
=
buffer
.
readableBytes
();
int
rows
=
length
/
16
+
(
length
%
15
==
0
?
0
:
1
)
+
4
;
StringBuilder
buf
=
new
StringBuilder
(
rows
*
80
*
2
)
.
append
(
"read index:"
).
append
(
buffer
.
readerIndex
())
.
append
(
" write index:"
).
append
(
buffer
.
writerIndex
())
.
append
(
" capacity:"
).
append
(
buffer
.
capacity
())
.
append
(
NEWLINE
);
appendPrettyHexDump
(
buf
,
buffer
);
System
.
out
.
println
(
buf
.
toString
());
}
}
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_07.java
0 → 100644
浏览文件 @
1b3ea55d
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
lombok.extern.slf4j.Slf4j
;
import
static
io
.
netty
.
buffer
.
ByteBufUtil
.
appendPrettyHexDump
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
* ByteBuf 重复读数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_07
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buffer
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
10
);
buffer
.
writeBytes
(
new
byte
[]{
1
,
2
,
3
,
4
});
log
(
buffer
);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
* read index:0 write index:4 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 |.... |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
writeInt
(
5
);
//int占四个字节
log
(
buffer
);
/**
* read index:0 write index:8 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 |........ |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
writeInt
(
7
);
//int占四个字节
log
(
buffer
);
/**
* read index:0 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 00 00 00 07 |............ |
* +--------+-------------------------------------------------+----------------+
*/
System
.
out
.
println
(
buffer
.
readByte
());
System
.
out
.
println
(
buffer
.
readByte
());
System
.
out
.
println
(
buffer
.
readByte
());
System
.
out
.
println
(
buffer
.
readByte
());
log
(
buffer
);
/**
* 1
* 2
* 3
* 4
* read index:4 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 07 |........ |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
markReaderIndex
();
buffer
.
readInt
();
log
(
buffer
);
buffer
.
resetReaderIndex
();
//读之前读过的数据
log
(
buffer
);
/**
* read index:8 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 07 |.... |
* +--------+-------------------------------------------------+----------------+
* read index:4 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 07 |........ |
* +--------+-------------------------------------------------+----------------+
*/
}
private
static
void
log
(
ByteBuf
buffer
)
{
int
length
=
buffer
.
readableBytes
();
int
rows
=
length
/
16
+
(
length
%
15
==
0
?
0
:
1
)
+
4
;
StringBuilder
buf
=
new
StringBuilder
(
rows
*
80
*
2
)
.
append
(
"read index:"
).
append
(
buffer
.
readerIndex
())
.
append
(
" write index:"
).
append
(
buffer
.
writerIndex
())
.
append
(
" capacity:"
).
append
(
buffer
.
capacity
())
.
append
(
NEWLINE
);
appendPrettyHexDump
(
buf
,
buffer
);
System
.
out
.
println
(
buf
.
toString
());
}
}
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_08.java
0 → 100644
浏览文件 @
1b3ea55d
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
lombok.extern.slf4j.Slf4j
;
import
static
io
.
netty
.
buffer
.
ByteBufUtil
.
appendPrettyHexDump
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
* ByteBuf的slice
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_08
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buffer
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
10
);
buffer
.
writeBytes
(
new
byte
[]{
'a'
,
'b'
,
'c'
,
'd'
,
'e'
,
'f'
,
'g'
,
'h'
,
'i'
,
'j'
});
log
(
buffer
);
final
ByteBuf
slice1
=
buffer
.
slice
(
0
,
5
);
slice1
.
retain
();
final
ByteBuf
slice2
=
buffer
.
slice
(
0
,
5
);
slice2
.
retain
();
log
(
slice1
);
log
(
slice2
);
/**
* read index:0 write index:10 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 62 63 64 65 66 67 68 69 6a |abcdefghij |
* +--------+-------------------------------------------------+----------------+
* read index:0 write index:5 capacity:5
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 62 63 64 65 |abcde |
* +--------+-------------------------------------------------+----------------+
* read index:0 write index:5 capacity:5
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 62 63 64 65 |abcde |
* +--------+-------------------------------------------------+----------------+
*/
slice1
.
setByte
(
0
,
'b'
);
log
(
buffer
);
log
(
slice1
);
/**
* read index:0 write index:10 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 62 62 63 64 65 66 67 68 69 6a |bbcdefghij |
* +--------+-------------------------------------------------+----------------+
* read index:0 write index:5 capacity:5
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 62 62 63 64 65 |bbcde |
* +--------+-------------------------------------------------+----------------+
*/
buffer
.
release
();
log
(
slice1
);
/**
* Exception in thread "main" io.netty.util.IllegalReferenceCountException: refCnt: 0
* at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1454)
* at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1383)
* at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1379)
* at io.netty.buffer.AbstractByteBuf.getByte(AbstractByteBuf.java:355)
* at io.netty.buffer.AbstractUnpooledSlicedByteBuf.getByte(AbstractUnpooledSlicedByteBuf.java:120)
* at io.netty.buffer.AbstractByteBuf.getUnsignedByte(AbstractByteBuf.java:368)
* at io.netty.buffer.ByteBufUtil$HexUtil.appendPrettyHexDump(ByteBufUtil.java:1580)
* at io.netty.buffer.ByteBufUtil$HexUtil.access$500(ByteBufUtil.java:1420)
* at io.netty.buffer.ByteBufUtil.appendPrettyHexDump(ByteBufUtil.java:1416)
* at io.netty.buffer.ByteBufUtil.appendPrettyHexDump(ByteBufUtil.java:1407)
* at com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_08.log(ByteBuf_08.java:79)
* at com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_08.main(ByteBuf_08.java:67)
*/
}
private
static
void
log
(
ByteBuf
buffer
)
{
int
length
=
buffer
.
readableBytes
();
int
rows
=
length
/
16
+
(
length
%
15
==
0
?
0
:
1
)
+
4
;
StringBuilder
buf
=
new
StringBuilder
(
rows
*
80
*
2
)
.
append
(
"read index:"
).
append
(
buffer
.
readerIndex
())
.
append
(
" write index:"
).
append
(
buffer
.
writerIndex
())
.
append
(
" capacity:"
).
append
(
buffer
.
capacity
())
.
append
(
NEWLINE
);
appendPrettyHexDump
(
buf
,
buffer
);
System
.
out
.
println
(
buf
.
toString
());
}
}
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_09.java
0 → 100644
浏览文件 @
1b3ea55d
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
io.netty.buffer.ByteBufUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
static
io
.
netty
.
buffer
.
ByteBufUtil
.
appendPrettyHexDump
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
* ByteBuf普通方式合并
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_09
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buf1
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
5
);
buf1
.
writeBytes
(
new
byte
[]{
1
,
2
,
3
,
4
,
5
});
ByteBuf
buf2
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
5
);
buf2
.
writeBytes
(
new
byte
[]{
6
,
7
,
8
,
9
,
10
});
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf1
));
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf2
));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 |..... |
* +--------+-------------------------------------------------+----------------+
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 06 07 08 09 0a |..... |
* +--------+-------------------------------------------------+----------------+
*/
ByteBuf
buf3
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
buf1
.
readableBytes
()
+
buf2
.
readableBytes
());
buf3
.
writeBytes
(
buf1
);
buf3
.
writeBytes
(
buf2
);
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf3
));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 06 07 08 09 0a |.......... |
* +--------+-------------------------------------------------+----------------+
*/
}
private
static
void
log
(
ByteBuf
buffer
)
{
int
length
=
buffer
.
readableBytes
();
int
rows
=
length
/
16
+
(
length
%
15
==
0
?
0
:
1
)
+
4
;
StringBuilder
buf
=
new
StringBuilder
(
rows
*
80
*
2
)
.
append
(
"read index:"
).
append
(
buffer
.
readerIndex
())
.
append
(
" write index:"
).
append
(
buffer
.
writerIndex
())
.
append
(
" capacity:"
).
append
(
buffer
.
capacity
())
.
append
(
NEWLINE
);
appendPrettyHexDump
(
buf
,
buffer
);
System
.
out
.
println
(
buf
.
toString
());
}
}
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_10.java
0 → 100644
浏览文件 @
1b3ea55d
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
io.netty.buffer.ByteBufUtil
;
import
io.netty.buffer.CompositeByteBuf
;
import
lombok.extern.slf4j.Slf4j
;
import
static
io
.
netty
.
buffer
.
ByteBufUtil
.
appendPrettyHexDump
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
* ByteBuf使用CompositeByteBuf方式合并
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public
class
ByteBuf_10
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buf1
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
5
);
buf1
.
writeBytes
(
new
byte
[]{
1
,
2
,
3
,
4
,
5
});
ByteBuf
buf2
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
5
);
buf2
.
writeBytes
(
new
byte
[]{
6
,
7
,
8
,
9
,
10
});
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf1
));
System
.
out
.
println
(
ByteBufUtil
.
prettyHexDump
(
buf2
));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 |..... |
* +--------+-------------------------------------------------+----------------+
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 06 07 08 09 0a |..... |
* +--------+-------------------------------------------------+----------------+
*/
CompositeByteBuf
buf4
=
ByteBufAllocator
.
DEFAULT
.
compositeBuffer
();
// true 表示增加新的 ByteBuf 自动递增 write index, 否则 write index 会始终为 0
buf4
.
addComponents
(
true
,
buf1
,
buf2
);
log
(
buf4
);
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 06 07 08 09 0a |.......... |
* +--------+-------------------------------------------------+----------------+
*/
}
private
static
void
log
(
ByteBuf
buffer
)
{
int
length
=
buffer
.
readableBytes
();
int
rows
=
length
/
16
+
(
length
%
15
==
0
?
0
:
1
)
+
4
;
StringBuilder
buf
=
new
StringBuilder
(
rows
*
80
*
2
)
.
append
(
"read index:"
).
append
(
buffer
.
readerIndex
())
.
append
(
" write index:"
).
append
(
buffer
.
writerIndex
())
.
append
(
" capacity:"
).
append
(
buffer
.
capacity
())
.
append
(
NEWLINE
);
appendPrettyHexDump
(
buf
,
buffer
);
System
.
out
.
println
(
buf
.
toString
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录