@@ -104,10 +104,7 @@ internal class TeeSink(
104104 * [requested] and the remaining [tapLimit] budget, clamped to never go negative. The actual
105105 * copy and [mirrored] advancement stay at each call site.
106106 */
107- private fun tapAllowance (requested : Long ): Long {
108- val remaining = (tapLimit - mirrored).coerceAtLeast(0L )
109- return if (requested < remaining) requested else remaining
110- }
107+ private fun tapAllowance (requested : Long ): Long = minOf(requested, (tapLimit - mirrored).coerceAtLeast(0L ))
111108
112109 @Throws(IOException ::class )
113110 override fun flush () {
@@ -119,23 +116,28 @@ internal class TeeSink(
119116 primary.close()
120117 }
121118
122- @Throws(IOException ::class )
123- override fun write (source : ByteArray ): BufferedSink {
124- scratch.write(source)
119+ /* *
120+ * Stages one typed write into [scratch] then tees it into the tap and drains it into the
121+ * primary in a single pass, returning `this` for chaining. [encode] receives [scratch]
122+ * explicitly as its `it` argument so the staged write always targets the staging [Buffer] —
123+ * never one of this `TeeSink`'s own `write*` overrides, which a `Buffer.()` receiver lambda
124+ * could silently rebind to (and self-recurse) if a `Buffer` overload were ever added.
125+ */
126+ private inline fun staged (encode : (Buffer ) -> Unit ): BufferedSink {
127+ encode(scratch)
125128 drainScratch()
126129 return this
127130 }
128131
132+ @Throws(IOException ::class )
133+ override fun write (source : ByteArray ): BufferedSink = staged { it.write(source) }
134+
129135 @Throws(IOException ::class )
130136 override fun write (
131137 source : ByteArray ,
132138 offset : Int ,
133139 byteCount : Int ,
134- ): BufferedSink {
135- scratch.write(source, offset, byteCount)
136- drainScratch()
137- return this
138- }
140+ ): BufferedSink = staged { it.write(source, offset, byteCount) }
139141
140142 @Throws(IOException ::class )
141143 override fun writeAll (source : Source ): Long {
@@ -157,32 +159,20 @@ internal class TeeSink(
157159 }
158160
159161 @Throws(IOException ::class )
160- override fun writeUtf8 (string : String ): BufferedSink {
161- scratch.writeUtf8(string)
162- drainScratch()
163- return this
164- }
162+ override fun writeUtf8 (string : String ): BufferedSink = staged { it.writeUtf8(string) }
165163
166164 @Throws(IOException ::class )
167165 override fun writeUtf8 (
168166 string : String ,
169167 beginIndex : Int ,
170168 endIndex : Int ,
171- ): BufferedSink {
172- scratch.writeUtf8(string, beginIndex, endIndex)
173- drainScratch()
174- return this
175- }
169+ ): BufferedSink = staged { it.writeUtf8(string, beginIndex, endIndex) }
176170
177171 @Throws(IOException ::class )
178172 override fun writeString (
179173 string : String ,
180174 charset : Charset ,
181- ): BufferedSink {
182- scratch.writeString(string, charset)
183- drainScratch()
184- return this
185- }
175+ ): BufferedSink = staged { it.writeString(string, charset) }
186176
187177 @Throws(IOException ::class )
188178 override fun outputStream (): OutputStream {
0 commit comments