Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
netty-demo
提交
a716d09d
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看板
提交
a716d09d
编写于
4月 19, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:文件处理
上级
09577b2b
变更
14
显示空白变更内容
内联
并排
Showing
14 changed file
with
374 addition
and
15 deletion
+374
-15
src/main/java/com/kwan/shuyu/heima/ByteBuffer_03_split.java
src/main/java/com/kwan/shuyu/heima/ByteBuffer_03_split.java
+0
-13
src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_12_Test_Write.java
...u/heima/netty_01_bytebuffer/ByteBuffer_12_Test_Write.java
+2
-2
src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_13_Test_split.java
...u/heima/netty_01_bytebuffer/ByteBuffer_13_Test_split.java
+40
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_02_Test_create.java
...m/kwan/shuyu/heima/netty_02_file/File_02_Test_create.java
+22
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_03_Test_create_more.java
...n/shuyu/heima/netty_02_file/File_03_Test_create_more.java
+22
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_04_Test_transferTo.java
...an/shuyu/heima/netty_02_file/File_04_Test_transferTo.java
+31
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_05_Test_transferTo.java
...an/shuyu/heima/netty_02_file/File_05_Test_transferTo.java
+29
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_06_Test_copy.java
...com/kwan/shuyu/heima/netty_02_file/File_06_Test_copy.java
+26
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_07_Test_walkFileTree.java
.../shuyu/heima/netty_02_file/File_07_Test_walkFileTree.java
+35
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_08_Test_jar.java
.../com/kwan/shuyu/heima/netty_02_file/File_08_Test_jar.java
+28
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_09_Test_delete.java
...m/kwan/shuyu/heima/netty_02_file/File_09_Test_delete.java
+28
-0
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_10_Test_copy.java
...com/kwan/shuyu/heima/netty_02_file/File_10_Test_copy.java
+32
-0
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServerAttachment.java
...ima/netty_03_nio/nio_03_selector/NioServerAttachment.java
+78
-0
to.txt
to.txt
+1
-0
未找到文件。
src/main/java/com/kwan/shuyu/heima/ByteBuffer_03_split.java
已删除
100644 → 0
浏览文件 @
09577b2b
package
com.kwan.shuyu.heima
;
/**
* 分割数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
ByteBuffer_03_split
{
public
static
void
main
(
String
[]
args
)
{
}
}
src/main/java/com/kwan/shuyu/heima/
ByteBuffer_01
_Write.java
→
src/main/java/com/kwan/shuyu/heima/
netty_01_bytebuffer/ByteBuffer_12_Test
_Write.java
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima
;
package
com.kwan.shuyu.heima
.netty_01_bytebuffer
;
import
java.io.RandomAccessFile
;
import
java.nio.ByteBuffer
;
...
...
@@ -12,7 +12,7 @@ import java.nio.charset.StandardCharsets;
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
ByteBuffer_
01
_Write
{
public
class
ByteBuffer_
12_Test
_Write
{
public
static
void
main
(
String
[]
args
)
{
final
ByteBuffer
b1
=
StandardCharsets
.
UTF_8
.
encode
(
"hello"
);
final
ByteBuffer
b2
=
StandardCharsets
.
UTF_8
.
encode
(
"world"
);
...
...
src/main/java/com/kwan/shuyu/heima/netty_01_bytebuffer/ByteBuffer_13_Test_split.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_01_bytebuffer
;
import
java.nio.ByteBuffer
;
import
static
com
.
kwan
.
shuyu
.
until
.
ByteBufferUtil
.
debugAll
;
/**
* 分割数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
ByteBuffer_13_Test_split
{
public
static
void
main
(
String
[]
args
)
{
final
ByteBuffer
source
=
ByteBuffer
.
allocate
(
32
);
source
.
put
(
"Hello, world\nI'm zhangsan \nho"
.
getBytes
());
split
(
source
);
source
.
put
(
"w are you?\n"
.
getBytes
());
split
(
source
);
}
private
static
void
split
(
ByteBuffer
source
)
{
source
.
flip
();
final
int
limit
=
source
.
limit
();
for
(
int
i
=
0
;
i
<
limit
;
i
++)
{
//找到完整的消息
if
(
source
.
get
(
i
)
==
'\n'
)
{
final
int
len
=
i
+
1
-
source
.
position
();
final
ByteBuffer
target
=
ByteBuffer
.
allocate
(
len
);
for
(
int
j
=
0
;
j
<
len
;
j
++)
{
//从source读取,向target写入
target
.
put
(
source
.
get
());
}
debugAll
(
target
);
}
}
source
.
compact
();
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_02_Test_create.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
/**
* 创建目录
* 如果目录已存在,会抛异常FileAlreadyExistsException
* 不能一次创建多级目录,否则会抛异常NoSuchFileException
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_02_Test_create
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
Path
path
=
Paths
.
get
(
"/Users/qinyingjie/Documents/idea-workspace/netty-demo/src/main/java/com/kwan/shuyu/heima/1111"
);
Files
.
createDirectories
(
path
);
}
}
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_03_Test_create_more.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
/**
* 创建多级目录
* 如果目录已存在,会抛异常FileAlreadyExistsException
* 不能一次创建多级目录,否则会抛异常NoSuchFileException
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_03_Test_create_more
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
Path
path
=
Paths
.
get
(
"/Users/qinyingjie/Documents/idea-workspace/netty-demo/src/main/java/com/kwan/shuyu/heima/222/333"
);
Files
.
createDirectories
(
path
);
}
}
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_04_Test_transferTo.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.nio.channels.FileChannel
;
/**
* 将一个文件中的数据复制到另一个文件,只传一次
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_04_Test_transferTo
{
public
static
void
main
(
String
[]
args
)
{
long
start
=
System
.
nanoTime
();
try
(
final
FileChannel
from
=
new
FileInputStream
(
"data.txt"
).
getChannel
();
final
FileChannel
to
=
new
FileOutputStream
(
"to.txt"
).
getChannel
();
)
{
final
long
size
=
from
.
size
();
//效率高,底层会利用操作系统的季零拷贝进行优化
from
.
transferTo
(
0
,
size
,
to
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
long
end
=
System
.
nanoTime
();
System
.
out
.
println
(
"transferTo 用时:"
+
(
end
-
start
)
/
1000_000.0
);
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_05_Test_transferTo.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.nio.channels.FileChannel
;
/**
* 将一个文件中的数据复制到另一个文件 传多次
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_05_Test_transferTo
{
public
static
void
main
(
String
[]
args
)
{
try
(
final
FileChannel
from
=
new
FileInputStream
(
"data.txt"
).
getChannel
();
final
FileChannel
to
=
new
FileOutputStream
(
"to.txt"
).
getChannel
();
)
{
final
long
size
=
from
.
size
();
//left代表还剩多少字节
for
(
long
left
=
size
;
left
>
0
;
)
{
left
-=
from
.
transferTo
((
size
-
left
),
left
,
to
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_06_Test_copy.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
/**
* 将一个文件中的数据复制到另一个文件 传多次
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_06_Test_copy
{
public
static
void
main
(
String
[]
args
)
{
try
{
Path
source
=
Paths
.
get
(
"data.txt"
);
Path
target
=
Paths
.
get
(
"to.txt"
);
Files
.
copy
(
source
,
target
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_07_Test_walkFileTree.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.*
;
import
java.nio.file.attribute.BasicFileAttributes
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_07_Test_walkFileTree
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
AtomicInteger
dirCount
=
new
AtomicInteger
();
final
AtomicInteger
fileCount
=
new
AtomicInteger
();
Files
.
walkFileTree
(
Paths
.
get
(
"/Users/qinyingjie/Documents/idea-workspace/netty-demo"
),
new
SimpleFileVisitor
<
Path
>()
{
@Override
public
FileVisitResult
preVisitDirectory
(
Path
dir
,
BasicFileAttributes
attrs
)
throws
IOException
{
System
.
out
.
println
(
"==========>>>"
+
dir
);
dirCount
.
incrementAndGet
();
return
super
.
preVisitDirectory
(
dir
,
attrs
);
}
@Override
public
FileVisitResult
visitFile
(
Path
file
,
BasicFileAttributes
attrs
)
throws
IOException
{
System
.
out
.
println
(
"==========>>>"
+
file
);
fileCount
.
incrementAndGet
();
return
super
.
visitFile
(
file
,
attrs
);
}
});
System
.
out
.
println
(
"文件夹数量="
+
dirCount
);
System
.
out
.
println
(
"文件数量="
+
fileCount
);
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_08_Test_jar.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.*
;
import
java.nio.file.attribute.BasicFileAttributes
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_08_Test_jar
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
AtomicInteger
jarCount
=
new
AtomicInteger
();
Files
.
walkFileTree
(
Paths
.
get
(
"/Users/qinyingjie/Documents/idea-workspace/netty-demo"
),
new
SimpleFileVisitor
<
Path
>()
{
@Override
public
FileVisitResult
visitFile
(
Path
file
,
BasicFileAttributes
attrs
)
throws
IOException
{
if
(
file
.
toString
().
endsWith
(
".jar"
))
{
System
.
out
.
println
(
"==========>>>"
+
file
);
jarCount
.
incrementAndGet
();
}
return
super
.
visitFile
(
file
,
attrs
);
}
});
System
.
out
.
println
(
"jar包数量="
+
jarCount
);
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_09_Test_delete.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.*
;
import
java.nio.file.attribute.BasicFileAttributes
;
/**
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_09_Test_delete
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
Files
.
walkFileTree
(
Paths
.
get
(
"/Users/qinyingjie/Downloads/ConcurrntHashMap扩容源码分析_副本"
),
new
SimpleFileVisitor
<
Path
>()
{
@Override
public
FileVisitResult
visitFile
(
Path
file
,
BasicFileAttributes
attrs
)
throws
IOException
{
Files
.
delete
(
file
);
return
super
.
visitFile
(
file
,
attrs
);
}
@Override
public
FileVisitResult
postVisitDirectory
(
Path
dir
,
IOException
exc
)
throws
IOException
{
Files
.
delete
(
dir
);
return
super
.
postVisitDirectory
(
dir
,
exc
);
}
});
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_02_file/File_10_Test_copy.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_02_file
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
/**
* 拷贝一个文件夹中的数据到另一个文件夹(有子文件夹)
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public
class
File_10_Test_copy
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
String
from
=
"/Users/qinyingjie/Documents/idea-workspace/netty-demo"
;
String
to
=
"/Users/qinyingjie/Downloads/未命名文件夹"
;
Files
.
walk
(
Paths
.
get
(
from
)).
forEach
(
path
->
{
try
{
String
targetName
=
path
.
toString
().
replace
(
from
,
to
);
if
(
Files
.
isDirectory
(
path
))
{
Files
.
createDirectories
(
Paths
.
get
(
targetName
));
}
if
(
Files
.
isRegularFile
(
path
))
{
Files
.
copy
(
path
,
Paths
.
get
(
targetName
));
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
});
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/heima/netty_03_nio/nio_03_selector/NioServerAttachment.java
0 → 100644
浏览文件 @
a716d09d
package
com.kwan.shuyu.heima.netty_03_nio.nio_03_selector
;
import
com.kwan.shuyu.until.ByteBufferUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.IOException
;
import
java.net.InetSocketAddress
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.SelectionKey
;
import
java.nio.channels.Selector
;
import
java.nio.channels.ServerSocketChannel
;
import
java.nio.channels.SocketChannel
;
import
java.util.Iterator
;
/**
* NioServer 服务端 NioServerAttachment 附件传递
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
@Slf4j
public
class
NioServerAttachment
{
public
static
void
main
(
String
[]
args
)
throws
IOException
{
final
Selector
selector
=
Selector
.
open
();
//预设ByteBuffer,并分配空间,为了读取数据
final
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
16
);
//创建ServerSocketChannel
final
ServerSocketChannel
ssc
=
ServerSocketChannel
.
open
();
//绑定端口号
ssc
.
configureBlocking
(
false
);
//ServerSocketChannel注册到Selector中
final
SelectionKey
sscKey
=
ssc
.
register
(
selector
,
0
,
null
);
sscKey
.
interestOps
(
SelectionKey
.
OP_ACCEPT
);
log
.
info
(
"sscKey={}"
,
sscKey
);
ssc
.
bind
(
new
InetSocketAddress
(
8080
));
while
(
true
)
{
//select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行
//select在事件未处理时,它不会阻塞,会一直请求处理
selector
.
select
();
final
Iterator
<
SelectionKey
>
iterator
=
selector
.
selectedKeys
().
iterator
();
while
(
iterator
.
hasNext
())
{
final
SelectionKey
key
=
iterator
.
next
();
iterator
.
remove
();
log
.
info
(
"key={}"
,
key
);
if
(
key
.
isAcceptable
())
{
final
ServerSocketChannel
channel
=
(
ServerSocketChannel
)
key
.
channel
();
final
SocketChannel
sc
=
channel
.
accept
();
sc
.
configureBlocking
(
false
);
ByteBuffer
bf
=
ByteBuffer
.
allocate
(
16
);
//将一个ByteBuffer作为附件绑定到SelectionKey
final
SelectionKey
scKey
=
sc
.
register
(
selector
,
0
,
bf
);
scKey
.
interestOps
(
SelectionKey
.
OP_READ
);
log
.
info
(
"sc={}"
,
sc
);
log
.
info
(
"scKey={}"
,
scKey
);
}
else
if
(
key
.
isReadable
())
{
try
{
final
SocketChannel
channel
=
(
SocketChannel
)
key
.
channel
();
//获取附件的ByteBuffer
final
ByteBuffer
bf
=
(
ByteBuffer
)
key
.
attachment
();
final
int
read
=
channel
.
read
(
bf
);
if
(
read
==
-
1
){
}
else
{
}
bf
.
flip
();
ByteBufferUtil
.
debugRead
(
bf
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
key
.
cancel
();
//取消事件处理
}
}
}
}
}
}
\ No newline at end of file
to.txt
0 → 100644
浏览文件 @
a716d09d
1234567890abc
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录