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
a084eac8
Commit
a084eac8
authored
May 28, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rearrange internal implementations
parent
57b85a35
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
43 additions
and
29 deletions
+43
-29
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/FileCacheStrategy.common.kt
.../kotlin/net.mamoe.mirai/utils/FileCacheStrategy.common.kt
+12
-14
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/internal/ReusableInput.kt
...in/kotlin/net.mamoe.mirai/utils/internal/ReusableInput.kt
+6
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/internal/md5.common.kt
...nMain/kotlin/net.mamoe.mirai/utils/internal/md5.common.kt
+1
-1
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/FileCacheStrategy.jvm.kt
...ain/kotlin/net/mamoe/mirai/utils/FileCacheStrategy.jvm.kt
+8
-14
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/asReusableInput.jvm.kt
...lin/net/mamoe/mirai/utils/internal/asReusableInput.jvm.kt
+15
-0
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/md5.jvm.kt
.../jvmMain/kotlin/net/mamoe/mirai/utils/internal/md5.jvm.kt
+1
-0
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/FileCacheStrategy.common.kt
View file @
a084eac8
package
net.mamoe.mirai.utils
import
kotlinx.io.core.Closeable
import
kotlinx.io.core.Input
import
kotlinx.io.core.use
import
kotlinx.io.errors.IOException
import
net.mamoe.mirai.utils.internal.InputStream
import
kotlin.contracts.InvocationKind
import
kotlin.contracts.contract
/**
* 缓存策略.
...
...
@@ -19,14 +22,6 @@ expect interface FileCacheStrategy {
@Throws
(
IOException
::
class
)
fun
newImageCache
(
input
:
Input
):
ExternalImage
/**
* 将 [input] 缓存为 [ExternalImage].
* 此函数应 close 这个 [InputStream]
*/
@MiraiExperimentalAPI
@Throws
(
IOException
::
class
)
fun
newImageCache
(
input
:
InputStream
):
ExternalImage
/**
* 将 [input] 缓存为 [ExternalImage].
* 此 [input] 的内容应是不变的.
...
...
@@ -49,12 +44,15 @@ expect interface FileCacheStrategy {
@Throws
(
IOException
::
class
)
override
fun
newImageCache
(
input
:
Input
):
ExternalImage
@MiraiExperimentalAPI
@Throws
(
IOException
::
class
)
override
fun
newImageCache
(
input
:
InputStream
):
ExternalImage
@MiraiExperimentalAPI
@Throws
(
IOException
::
class
)
override
fun
newImageCache
(
input
:
ByteArray
):
ExternalImage
}
}
\ No newline at end of file
}
internal
inline
fun
<
I
:
Closeable
,
O
:
Closeable
,
R
>
I
.
withOut
(
output
:
O
,
block
:
I
.(
output
:
O
)
->
R
):
R
{
contract
{
callsInPlace
(
block
,
InvocationKind
.
EXACTLY_ONCE
)
}
return
use
{
output
.
use
{
block
(
this
,
output
)
}
}
}
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/internal/ReusableInput.kt
View file @
a084eac8
package
net.mamoe.mirai.utils.internal
import
io.ktor.utils.io.ByteWriteChannel
import
kotlinx.io.core.Input
internal
interface
ReusableInput
{
val
md5
:
ByteArray
...
...
@@ -8,4 +9,9 @@ internal interface ReusableInput {
fun
chunkedFlow
(
sizePerPacket
:
Int
):
ChunkedFlowSession
<
ChunkedInput
>
suspend
fun
writeTo
(
out
:
ByteWriteChannel
):
Long
/**
* Remember to close.
*/
fun
asInput
():
Input
}
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/internal/md5.common.kt
View file @
a084eac8
...
...
@@ -5,7 +5,7 @@ package net.mamoe.mirai.utils.internal
import
kotlinx.io.pool.DefaultPool
import
kotlinx.io.pool.ObjectPool
expect
abstract
class
InputStream
{
internal
expect
abstract
class
InputStream
{
open
fun
available
():
Int
open
fun
close
()
abstract
fun
read
():
Int
...
...
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/FileCacheStrategy.jvm.kt
View file @
a084eac8
...
...
@@ -2,10 +2,9 @@
package
net.mamoe.mirai.utils
import
kotlinx.io.core.Closeable
import
kotlinx.io.core.Input
import
kotlinx.io.core.readAvailable
import
kotlinx.io.core.readBytes
import
kotlinx.io.core.*
import
kotlinx.io.streams.asInput
import
kotlinx.io.streams.asOutput
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.utils.internal.InputStream
import
net.mamoe.mirai.utils.internal.asReusableInput
...
...
@@ -16,10 +15,11 @@ import java.io.IOException
import
java.io.OutputStream
import
java.net.URL
import
java.security.MessageDigest
import
java.util.*
import
javax.imageio.ImageIO
import
kotlin.contracts.ExperimentalContracts
import
kotlin.contracts.InvocationKind
import
kotlin.contracts.contract
import
kotlin.io.use
/**
* 缓存策略.
...
...
@@ -44,7 +44,7 @@ actual interface FileCacheStrategy {
*/
@MiraiExperimentalAPI
@Throws
(
IOException
::
class
)
actual
fun
newImageCache
(
input
:
InputStream
):
ExternalImage
fun
newImageCache
(
input
:
InputStream
):
ExternalImage
/**
* 将 [input] 缓存为 [ExternalImage].
...
...
@@ -87,7 +87,7 @@ actual interface FileCacheStrategy {
@MiraiExperimentalAPI
@Throws
(
IOException
::
class
)
actual
override
fun
newImageCache
(
input
:
InputStream
):
ExternalImage
{
override
fun
newImageCache
(
input
:
InputStream
):
ExternalImage
{
return
newImageCache
(
input
.
readBytes
())
}
...
...
@@ -186,12 +186,6 @@ actual interface FileCacheStrategy {
}
}
internal
inline
fun
<
I
:
Closeable
,
O
:
Closeable
,
R
>
I
.
withOut
(
output
:
O
,
block
:
I
.(
output
:
O
)
->
R
):
R
{
contract
{
callsInPlace
(
block
,
InvocationKind
.
EXACTLY_ONCE
)
}
return
use
{
output
.
use
{
block
(
this
,
output
)
}
}
}
@Throws
(
IOException
::
class
)
internal
fun
Input
.
copyTo
(
out
:
OutputStream
,
bufferSize
:
Int
=
DEFAULT_BUFFER_SIZE
):
Long
{
...
...
@@ -204,4 +198,4 @@ internal fun Input.copyTo(out: OutputStream, bufferSize: Int = DEFAULT_BUFFER_SI
bytes
=
readAvailable
(
buffer
)
}
return
bytesCopied
}
}
\ No newline at end of file
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/asReusableInput.jvm.kt
View file @
a084eac8
...
...
@@ -4,6 +4,9 @@ import io.ktor.utils.io.ByteWriteChannel
import
kotlinx.coroutines.Dispatchers
import
kotlinx.coroutines.flow.Flow
import
kotlinx.coroutines.withContext
import
kotlinx.io.core.ByteReadPacket
import
kotlinx.io.core.Input
import
kotlinx.io.streams.asInput
import
net.mamoe.mirai.message.data.toLongUnsigned
import
java.io.File
import
java.io.InputStream
...
...
@@ -28,6 +31,10 @@ internal actual fun ByteArray.asReusableInput(): ReusableInput {
out
.
flush
()
return
this
@
asReusableInput
.
size
.
toLongUnsigned
()
}
override
fun
asInput
():
Input
{
return
ByteReadPacket
(
this
@
asReusableInput
)
}
}
}
...
...
@@ -50,6 +57,10 @@ internal fun File.asReusableInput(deleteOnClose: Boolean): ReusableInput {
override
suspend
fun
writeTo
(
out
:
ByteWriteChannel
):
Long
{
return
inputStream
().
use
{
it
.
copyTo
(
out
)
}
}
override
fun
asInput
():
Input
{
return
inputStream
().
asInput
()
}
}
}
...
...
@@ -72,6 +83,10 @@ internal fun File.asReusableInput(deleteOnClose: Boolean, md5: ByteArray): Reusa
override
suspend
fun
writeTo
(
out
:
ByteWriteChannel
):
Long
{
return
inputStream
().
use
{
it
.
copyTo
(
out
)
}
}
override
fun
asInput
():
Input
{
return
inputStream
().
asInput
()
}
}
}
...
...
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/md5.jvm.kt
View file @
a084eac8
...
...
@@ -19,4 +19,5 @@ internal actual fun InputStream.md5(): ByteArray {
return
digest
.
digest
()
}
@Suppress
(
"ACTUAL_WITHOUT_EXPECT"
)
internal
actual
typealias
InputStream
=
java
.
io
.
InputStream
\ 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