1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

Added asBytes method. Used InputStream.copyTo

This commit is contained in:
slesh 2020-08-23 01:02:50 +03:00
parent e2c7125f6c
commit a1b471d2ca

View File

@ -13,16 +13,20 @@ fun PathedFile.asStream(
fun PathedFile.asFile( fun PathedFile.asFile(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
dest: File = File.createTempFile(this.fileUniqueId, this.filename), dest: File = File.createTempFile(this.fileUniqueId, this.filename),
defaultBufferSize: Int = 1024, defaultBufferSize: Int = 1024
buffer: ByteArray = ByteArray(defaultBufferSize)
): File { ): File {
this.asStream(telegramAPIUrlsKeeper).use { input -> this.asStream(telegramAPIUrlsKeeper).use { input ->
FileOutputStream(dest).use { out -> FileOutputStream(dest).use { out ->
var read: Int input.copyTo(out, defaultBufferSize)
while (input.read(buffer, 0, defaultBufferSize).also { read = it } >= 0) {
out.write(buffer, 0, read)
}
} }
} }
return dest return dest
} }
fun PathedFile.asBytes(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper
): ByteArray {
return this.asStream(telegramAPIUrlsKeeper).use { input ->
input.readBytes()
}
}