<commit_before>import ceylon.net.http.server { Response, Exception }

import io.undertow.server { HttpServerExchange }
import io.undertow.util { HttpString }
import ceylon.io.charset { Charset, getCharset }
import ceylon.net.http { Header }
import ceylon.collection { MutableList, LinkedList }

import java.io { JIOException=IOException }
import java.lang { arrays, ByteArray }
import java.nio { 
    JByteBuffer=ByteBuffer { wrapByteBuffer=wrap }}
import org.xnio.channels { StreamSinkChannel,
                           Channels { chFlushBlocking=flushBlocking } }
import ceylon.io.buffer { Buffer }

by("<NAME>")
shared class ResponseImpl(HttpServerExchange exchange, Charset defaultCharset) 
        satisfies Response {

    variable StreamSinkChannel? lazyResponse = null;
    StreamSinkChannel response => lazyResponse else (lazyResponse=exchange.responseChannel);

    MutableList<Header> headers = LinkedList<Header>();
    variable value headersSent = false;
    
    //TODO comment encodings and defaults
    shared actual void writeString(String string) {
        applyHeadersToExchange();

        value charset = findCharset();
        Buffer<Integer> buffer = charset.encode(string);
        
        ByteArray bytes = ByteArray(buffer.available);
        variable Integer i = 0;
        while(buffer.hasAvailable) {
            bytes.set(i++, buffer.get());
        }
        value bb = wrapByteBuffer(bytes);
        response.write(bb);
        try {
            response.awaitWritable();
        } catch(JIOException e) {
            //TODO log
            print(e);
        }
    }
    <commit_msg>Do not write empty string.<commit_after>import ceylon.net.http.server { Response, Exception }

import io.undertow.server { HttpServerExchange }
import io.undertow.util { HttpString }
import ceylon.io.charset { Charset, getCharset }
import ceylon.net.http { Header }
import ceylon.collection { MutableList, LinkedList }

import java.io { JIOException=IOException }
import java.lang { arrays, ByteArray }
import java.nio { 
    JByteBuffer=ByteBuffer { wrapByteBuffer=wrap }}
import org.xnio.channels { StreamSinkChannel,
                           Channels { chFlushBlocking=flushBlocking } }
import ceylon.io.buffer { ByteBuffer }

by("<NAME>")
shared class ResponseImpl(HttpServerExchange exchange, Charset defaultCharset) 
        satisfies Response {

    variable StreamSinkChannel? lazyResponse = null;
    StreamSinkChannel response => lazyResponse else (lazyResponse=exchange.responseChannel);

    MutableList<Header> headers = LinkedList<Header>();
    variable value headersSent = false;
    
    //TODO comment encodings and defaults
    shared actual void writeString(String string) {
        applyHeadersToExchange();

        value charset = findCharset();
        ByteBuffer buffer = charset.encode(string);
        //TODO use underlaying java ByteBuffer
        ByteArray bytes = ByteArray(buffer.available);
        variable Integer i = 0;
        while(buffer.hasAvailable) {
            bytes.set(i++, buffer.get());
        }
        if (bytes.size > 0) {
            value bb = wrapByteBuffer(bytes);
            response.write(bb);
            try {
                response.awaitWritable();
            } catch(JIOException e) {
                //TODO log
                print(e);
            }
        }
    }

{'fraction_non_alphanumeric': 0.09278042959427207, 'fraction_numerical': 0.0008949880668257757, 'mean_word_length': 2.7631874298540966, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, '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': '2766228', 'n_tokens_mistral': 919, 'n_tokens_neox': 886, 'n_words': 218}