Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
55427ba5
Commit
55427ba5
authored
Feb 22, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add channel utils
parent
d99e2afe
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
114 additions
and
0 deletions
+114
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt
...e/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt
+114
-0
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/channels.kt
0 → 100644
View file @
55427ba5
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@
file
:
JvmName
(
"Utils"
)
@
file
:
JvmMultifileClass
package
net.mamoe.mirai.utils
import
io.ktor.utils.io.ByteReadChannel
import
io.ktor.utils.io.readAvailable
import
kotlinx.coroutines.io.close
import
kotlinx.io.OutputStream
import
kotlinx.io.core.Output
import
kotlinx.io.pool.useInstance
import
net.mamoe.mirai.utils.io.ByteArrayPool
import
kotlin.jvm.JvmMultifileClass
import
kotlin.jvm.JvmName
/**
* 从接收者管道读取所有数据并写入 [dst]. 不会关闭 [dst]
*/
suspend
fun
ByteReadChannel
.
copyTo
(
dst
:
OutputStream
)
{
ByteArrayPool
.
useInstance
{
do
{
val
size
=
this
.
readAvailable
(
it
)
dst
.
write
(
it
,
0
,
size
)
}
while
(
size
!=
0
)
}
}
/**
* 从接收者管道读取所有数据并写入 [dst]. 不会关闭 [dst]
*/
suspend
fun
ByteReadChannel
.
copyTo
(
dst
:
Output
)
{
ByteArrayPool
.
useInstance
{
do
{
val
size
=
this
.
readAvailable
(
it
)
dst
.
writeFully
(
it
,
0
,
size
)
}
while
(
size
!=
0
)
}
}
/**
* 从接收者管道读取所有数据并写入 [dst]. 不会关闭 [dst]
*/
suspend
fun
ByteReadChannel
.
copyTo
(
dst
:
kotlinx
.
coroutines
.
io
.
ByteWriteChannel
)
{
ByteArrayPool
.
useInstance
{
do
{
val
size
=
this
.
readAvailable
(
it
)
dst
.
writeFully
(
it
,
0
,
size
)
}
while
(
size
!=
0
)
}
}
// copyAndClose
/**
* 从接收者管道读取所有数据并写入 [dst], 最终关闭 [dst]
*/
suspend
fun
ByteReadChannel
.
copyAndClose
(
dst
:
OutputStream
)
{
try
{
ByteArrayPool
.
useInstance
{
do
{
val
size
=
this
.
readAvailable
(
it
)
dst
.
write
(
it
,
0
,
size
)
}
while
(
size
!=
0
)
}
}
finally
{
dst
.
close
()
}
}
/**
* 从接收者管道读取所有数据并写入 [dst], 最终关闭 [dst]
*/
suspend
fun
ByteReadChannel
.
copyAndClose
(
dst
:
Output
)
{
try
{
ByteArrayPool
.
useInstance
{
do
{
val
size
=
this
.
readAvailable
(
it
)
dst
.
writeFully
(
it
,
0
,
size
)
}
while
(
size
!=
0
)
}
}
finally
{
dst
.
close
()
}
}
/**
* 从接收者管道读取所有数据并写入 [dst], 最终关闭 [dst]
*/
suspend
fun
ByteReadChannel
.
copyAndClose
(
dst
:
kotlinx
.
coroutines
.
io
.
ByteWriteChannel
)
{
try
{
ByteArrayPool
.
useInstance
{
do
{
val
size
=
this
.
readAvailable
(
it
)
dst
.
writeFully
(
it
,
0
,
size
)
}
while
(
size
!=
0
)
}
}
finally
{
dst
.
close
()
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment