scoop:scoop.http.client:0.1.0
src/cache/memory_stream.scoop
Selected production source · src/cache/memory_stream.scoop
1
package scoop.http.client3
import scoop.asyncio.*4
import scoop.bytes.ByteArrayBuilder5
import scoop.http.*6
import scoop.platform.memory.NativeByteBuffer8
internal class ClientMemoryCacheWriter(val owner: MemoryHttpCache, val record: HttpCacheRecord, val limit: Long) : HttpCacheWriter {9
val discard: AtomicBoolean = AtomicBoolean(false)10
private val lock: ClientLock = ClientLock()11
private val closed: AtomicBoolean = AtomicBoolean(false)12
private var output: ByteArrayBuilder? = Some(ByteArrayBuilder())14
public override suspend fun writeFrom(buffer: NativeByteBuffer, count: Long): Long = lock.withLock {15
if (closed.load()) throw IoClosedException()16
if (count < 0L) throw IllegalArgumentException(Some("negative cache write count"))17
if (count > buffer.readableBytes) throw IndexOutOfBoundsException()18
if (clientPresent(output) && count > limit - output!!.size) { discard.store(true); output = None }19
if (discard.load()) { output = None; buffer.skip(count) }20
else output!!.append(buffer.readBytes(count))21
count22
}23
public override suspend fun flush() { if (closed.load()) throw IoClosedException() }24
public override suspend fun commit(trailers: List<HttpField>) {25
lock.withLock {26
if (!closed.compareAndSet(false, true)) throw IoClosedException()27
val bytes = output; output = None28
try {29
if (discard.load()) { owner.abandon(this); return@withLock }30
val value = bytes!!.toByteArray()31
val expected = parseHttpContentLength(record.head.fields)32
if (clientPresent(expected) && expected!! != value.size)33
throw IllegalStateException(Some("cached body length does not match Content-Length"))34
owner.publish(this, value, trailers.toArray())35
} catch (error: Throwable) { owner.abandon(this); throw error }36
}37
}38
public override suspend fun close() {39
val first = lock.withLock {40
if (!closed.compareAndSet(false, true)) false41
else { output = None; true }42
}43
if (!first) return44
owner.abandon(this)45
}46
}48
internal class ClientMemoryCacheSnapshot(entry: ClientMemoryCacheEntry) : HttpCacheSnapshot {49
public override val record: HttpCacheRecord = entry.record50
public override val bodyLength: Long = entry.bytes.size51
public override val trailers: Array<HttpField> = entry.trailers52
private val source: ClientMemoryCacheSource = ClientMemoryCacheSource(entry.bytes)53
public override val body: AsyncByteSource get() = source54
public override suspend fun close() { source.close() }55
}57
internal class ClientMemoryCacheSource(private val bytes: Array<Byte>) : AsyncByteSource {58
private val lock: ClientLock = ClientLock()59
private var closed: Boolean = false60
private var position: Long = 0L61
fun close() { lock.withLock { closed = true } }62
public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long = lock.withLock {63
if (closed) throw IoClosedException()64
if (count < 0L) throw IllegalArgumentException(Some("negative cache read count"))65
if (count > buffer.writableBytes) throw IndexOutOfBoundsException()66
val remaining = bytes.size - position67
val actual = if (count < remaining) count else remaining68
buffer.writeBytes(bytes, position, actual)69
position += actual70
actual71
}72
}