Scoop API

scoop:scoop.http.client:0.1.0

src/cache/memory_stream.scoop

Selected production source · src/cache/memory_stream.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.bytes.ByteArrayBuilder
5import scoop.http.*
6import scoop.platform.memory.NativeByteBuffer
8internal 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 count
22 }
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 = None
28 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)) false
41 else { output = None; true }
42 }
43 if (!first) return
44 owner.abandon(this)
45 }
48internal class ClientMemoryCacheSnapshot(entry: ClientMemoryCacheEntry) : HttpCacheSnapshot {
49 public override val record: HttpCacheRecord = entry.record
50 public override val bodyLength: Long = entry.bytes.size
51 public override val trailers: Array<HttpField> = entry.trailers
52 private val source: ClientMemoryCacheSource = ClientMemoryCacheSource(entry.bytes)
53 public override val body: AsyncByteSource get() = source
54 public override suspend fun close() { source.close() }
57internal class ClientMemoryCacheSource(private val bytes: Array<Byte>) : AsyncByteSource {
58 private val lock: ClientLock = ClientLock()
59 private var closed: Boolean = false
60 private var position: Long = 0L
61 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 - position
67 val actual = if (count < remaining) count else remaining
68 buffer.writeBytes(bytes, position, actual)
69 position += actual
70 actual
71 }