Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
16c40b32
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看板
提交
16c40b32
编写于
4月 27, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:数据写入
上级
77c126bf
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
305 addition
and
0 deletion
+305
-0
src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java
...n/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java
+1
-0
src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/TestEmbeddedChannel.java
...an/shuyu/heima/netty_06_pipeline/TestEmbeddedChannel.java
+80
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_01.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_01.java
+68
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_02.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_02.java
+38
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_03.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_03.java
+48
-0
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_04.java
...ava/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_04.java
+66
-0
src/main/java/com/kwan/shuyu/until/ByteBufferUtil.java
src/main/java/com/kwan/shuyu/until/ByteBufferUtil.java
+4
-0
target/classes/com/kwan/shuyu/until/ByteBufferUtil.class
target/classes/com/kwan/shuyu/until/ByteBufferUtil.class
+0
-0
未找到文件。
src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java
浏览文件 @
16c40b32
...
@@ -52,6 +52,7 @@ public class Server {
...
@@ -52,6 +52,7 @@ public class Server {
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
log
.
info
(
"3"
);
log
.
info
(
"3"
);
log
.
info
(
"h3结果{},class={}"
,
msg
,
msg
.
getClass
());
log
.
info
(
"h3结果{},class={}"
,
msg
,
msg
.
getClass
());
//入栈处理器最后的写入,供出栈处理器读取
ch
.
writeAndFlush
(
ctx
.
alloc
().
buffer
().
writeBytes
(
"server..."
.
getBytes
()));
ch
.
writeAndFlush
(
ctx
.
alloc
().
buffer
().
writeBytes
(
"server..."
.
getBytes
()));
}
}
});
});
...
...
src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/TestEmbeddedChannel.java
0 → 100644
浏览文件 @
16c40b32
package
com.kwan.shuyu.heima.netty_06_pipeline
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
import
io.netty.channel.ChannelHandlerContext
;
import
io.netty.channel.ChannelInboundHandlerAdapter
;
import
io.netty.channel.ChannelOutboundHandlerAdapter
;
import
io.netty.channel.ChannelPromise
;
import
io.netty.channel.embedded.EmbeddedChannel
;
import
lombok.extern.slf4j.Slf4j
;
import
java.nio.charset.Charset
;
/**
* netty提供的 EmbeddedChannel 测试方法
* 可以 绑定 很多的 handler 进行测试
* <p>
* <p>
* 入站:服务端 处理 客户端 write事件
*/
@Slf4j
public
class
TestEmbeddedChannel
{
public
static
void
main
(
String
[]
args
)
{
// 1. 入站
ChannelInboundHandlerAdapter
h1
=
new
ChannelInboundHandlerAdapter
()
{
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
log
.
info
(
"1"
);
super
.
channelRead
(
ctx
,
msg
);
}
};
// 2. 入站
ChannelInboundHandlerAdapter
h2
=
new
ChannelInboundHandlerAdapter
()
{
// 当前handler起名 h2
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
log
.
info
(
"2"
);
super
.
channelRead
(
ctx
,
msg
);
}
};
// 3. 入站
ChannelInboundHandlerAdapter
h3
=
new
ChannelInboundHandlerAdapter
()
{
@Override
public
void
channelRead
(
ChannelHandlerContext
ctx
,
Object
msg
)
throws
Exception
{
log
.
info
(
"3333333333333333"
);
ctx
.
channel
().
writeAndFlush
(
msg
);
// 【最后一个handler往前找】
// ctx.writeAndFlush(msg); // 【当前节点往上找 出站处理器】
}
};
// 4. 出站
ChannelOutboundHandlerAdapter
h4
=
new
ChannelOutboundHandlerAdapter
()
{
@Override
public
void
write
(
ChannelHandlerContext
ctx
,
Object
msg
,
ChannelPromise
promise
)
throws
Exception
{
log
.
info
(
"4"
);
super
.
write
(
ctx
,
msg
,
promise
);
}
};
// 5. 出站
ChannelOutboundHandlerAdapter
h5
=
new
ChannelOutboundHandlerAdapter
()
{
@Override
public
void
write
(
ChannelHandlerContext
ctx
,
Object
msg
,
ChannelPromise
promise
)
throws
Exception
{
log
.
info
(
"5"
);
super
.
write
(
ctx
,
msg
,
promise
);
}
};
final
EmbeddedChannel
channel
=
new
EmbeddedChannel
(
h1
,
h2
,
h3
,
h4
,
h5
);
// 模拟 入站
channel
.
writeInbound
(
ByteBufAllocator
.
DEFAULT
.
buffer
().
writeBytes
(
"hello"
.
getBytes
()));
System
.
out
.
println
(((
ByteBuf
)
channel
.
readOutbound
()).
toString
(
Charset
.
defaultCharset
()));
/**
* 输出结果
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 1
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 2
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 3333333333333333
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 5
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 4
* hello
*/
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_01.java
0 → 100644
浏览文件 @
16c40b32
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
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
*/
public
class
ByteBuf_01
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buf
=
ByteBufAllocator
.
DEFAULT
.
buffer
();
log
(
buf
);
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
300
;
i
++)
{
sb
.
append
(
"a"
);
}
buf
.
writeBytes
(
sb
.
toString
().
getBytes
());
log
(
buf
);
/**
* PooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 256)
* read index:0 write index:300 capacity:512
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000010| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000020| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000030| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000040| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000050| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000060| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000070| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000080| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000090| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000a0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000b0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000c0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000d0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000e0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000f0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000100| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000110| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000120| 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaa |
* +--------+-------------------------------------------------+----------------+
*/
}
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_02.java
0 → 100644
浏览文件 @
16c40b32
package
com.kwan.shuyu.heima.netty_07_bytebuf
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.buffer.ByteBufAllocator
;
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
*/
public
class
ByteBuf_02
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buffer
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
10
);
log
(
buffer
);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
* read index:0 write index:0 capacity:10
*/
}
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_03.java
0 → 100644
浏览文件 @
16c40b32
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_03
{
public
static
void
main
(
String
[]
args
)
{
ByteBuf
buffer
=
ByteBufAllocator
.
DEFAULT
.
buffer
(
10
);
ByteBuf
buffer1
=
ByteBufAllocator
.
DEFAULT
.
heapBuffer
(
10
);
ByteBuf
buffer2
=
ByteBufAllocator
.
DEFAULT
.
directBuffer
(
10
);
log
.
info
(
buffer
.
getClass
().
getSimpleName
());
log
.
info
(
buffer1
.
getClass
().
getSimpleName
());
log
.
info
(
buffer2
.
getClass
().
getSimpleName
());
log
(
buffer
);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
[main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeDirectByteBuf
[main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeHeapByteBuf
[main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeDirectByteBuf
read index:0 write index:0 capacity:10
*/
}
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_04.java
0 → 100644
浏览文件 @
16c40b32
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_04
{
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
.
setInt
(
0
,
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| 00 00 00 05 00 00 00 05 |........ |
* +--------+-------------------------------------------------+----------------+
*/
}
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/until/ByteBufferUtil.java
浏览文件 @
16c40b32
package
com.kwan.shuyu.until
;
package
com.kwan.shuyu.until
;
import
io.netty.buffer.ByteBuf
;
import
io.netty.util.internal.StringUtil
;
import
io.netty.util.internal.StringUtil
;
import
java.nio.ByteBuffer
;
import
java.nio.ByteBuffer
;
import
static
io
.
netty
.
util
.
internal
.
MathUtil
.
isOutOfBounds
;
import
static
io
.
netty
.
util
.
internal
.
MathUtil
.
isOutOfBounds
;
import
static
io
.
netty
.
util
.
internal
.
StringUtil
.
NEWLINE
;
/**
/**
* ByteBuffer工具类
* ByteBuffer工具类
...
@@ -167,4 +169,6 @@ public class ByteBufferUtil {
...
@@ -167,4 +169,6 @@ public class ByteBufferUtil {
public
static
short
getUnsignedByte
(
ByteBuffer
buffer
,
int
index
)
{
public
static
short
getUnsignedByte
(
ByteBuffer
buffer
,
int
index
)
{
return
(
short
)
(
buffer
.
get
(
index
)
&
0xFF
);
return
(
short
)
(
buffer
.
get
(
index
)
&
0xFF
);
}
}
}
}
\ No newline at end of file
target/classes/com/kwan/shuyu/until/ByteBufferUtil.class
浏览文件 @
16c40b32
无法预览此类型文件
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录