<commit_before>package org.jetbrains.ktor.websocket

import org.jetbrains.ktor.cio.*
import java.nio.*
import java.util.concurrent.atomic.*

internal class WebSocketWriter(val writeChannel: WriteChannel) {
    private val serializer = Serializer()
    private val buffer = ByteBuffer.allocate(8192)

    private val writeInProgress = AtomicBoolean()

    var masking: Boolean
        get() = serializer.masking
        set(newValue) { serializer.masking = newValue }

    /**
     * enqueue frame for future sending, may block if too many enqueued frames
     */
    fun enqueue(frame: Frame) {
        serializer.enqueue(frame)
    }

    /**
     * Send a frame and write it and all outstanding frames in the queue
     */
    suspend fun send(frame: Frame) {
        enqueue(frame)
        flush()
    }

    /**
     * Ensures all enqueued messages has been written
     */
    suspend fun flush() {
        while (serializer.hasOutstandingBytes) {
            writeLoop()
        }
    }

    private suspend fun writeLoop() {
        if (writeInProgress.compareAndSet(false, true)) {
            try {
                serializer.serialize(buffer)
                buffer.flip()
                while (buffer.hasRemaining()) {
                    writeChannel.write(buffer)

                    buffer.compact()
                    serializer.serialize(buffer)
                    buffer.flip()
                }
                buffer.compact()

                writeChannel.flush()
            } finally {
                writeInProgress.set(false)
            }
        }
    }

}<commit_msg>WebSockets: eliminate null checks in write loop<commit_after>package org.jetbrains.ktor.websocket

import org.jetbrains.ktor.cio.*
import java.nio.*
import java.util.concurrent.atomic.*

internal class WebSocketWriter(val writeChannel: WriteChannel) {
    private val serializer = Serializer()
    private val buffer: ByteBuffer = ByteBuffer.allocate(8192)

    private val writeInProgress = AtomicBoolean()

    var masking: Boolean
        get() = serializer.masking
        set(newValue) { serializer.masking = newValue }

    /**
     * enqueue frame for future sending, may block if too many enqueued frames
     */
    fun enqueue(frame: Frame) {
        serializer.enqueue(frame)
    }

    /**
     * Send a frame and write it and all outstanding frames in the queue
     */
    suspend fun send(frame: Frame) {
        enqueue(frame)
        flush()
    }

    /**
     * Ensures all enqueued messages has been written
     */
    suspend fun flush() {
        while (serializer.hasOutstandingBytes) {
            writeLoop()
        }
    }

    private suspend fun writeLoop() {
        if (writeInProgress.compareAndSet(false, true)) {
            try {
                serializer.serialize(buffer)
                buffer.flip()
                while (buffer.hasRemaining()) {
                    writeChannel.write(buffer)

                    buffer.compact()
                    serializer.serialize(buffer)
                    buffer.flip()
                }
                buffer.compact()

                writeChannel.flush()
            } finally {
                writeInProgress.set(false)
            }
        }
    }

}
{'fraction_non_alphanumeric': 0.08816276202219482, 'fraction_numerical': 0.002466091245376079, 'mean_word_length': 1.7593537414965987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637049', 'n_tokens_mistral': 887, 'n_tokens_neox': 831, 'n_words': 209}