scoop:scoop.http.asyncio:0.1.0
src/multipart/discard.scoop
Selected production source · src/multipart/discard.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*4
import scoop.platform.memory.NativeByteBuffer6
internal suspend fun MultipartReader.discard(body: MultipartBody, maxBytes: Long): Long {7
try {8
return io.run({9
requireBody(body)10
if (maxBytes < 0L) throw IllegalArgumentException(Some("negative multipart discard limit"))11
}) suspend { source ->12
val capacity = if (maxBytes >= 8192L) 8192L else maxBytes + 1L13
NativeByteBuffer.allocate(capacity).use suspend { buffer ->14
var total = 0L15
while (true) {16
buffer.clear()17
val remaining = maxBytes - total18
val count = if (remaining < capacity) remaining + 1L else capacity19
val read = readBody(source, body, buffer, count)20
if (read == 0L) break21
if (read > remaining) throw MultipartDiscardLimitException(maxBytes)22
total += read23
}24
total25
}26
}27
} catch (error: MultipartDiscardLimitException) {28
try { io.close() }29
catch (cleanup: Throwable) { throw ResourceUseException(error, cleanup) }30
throw error31
}32
}34
internal suspend fun MultipartReader.closeBody(body: MultipartBody) {35
if (body.closed || io.isClosed) { body.closed = true; body.clear(); return }36
httpJob(io.loop, false)37
if (body.complete) { body.closed = true; body.clear(); return }38
try { io.close() }39
finally { body.closed = true; body.clear() }40
}