<commit_before>import C7

/**
    Buffers receive and send calls to a Stream.
 
    Receive calls are buffered by the size used to initialize
    the buffer.
 
    Send calls are buffered until `flush()` is called.
*/
public final class StreamBuffer: Stream {
    public var closed: Bool {
        return stream.closed
    }
    public func close() throws {
        try stream.close()
    }

    private let stream: Stream
    private let size: Int


    public func setTimeout(_ timeout: Double) throws {
        try stream.setTimeout(timeout)
    }

    private var receiveIterator: IndexingIterator<[Byte]>
    private var sendBuffer: Bytes

    public init(_ stream: Stream, size: Int = 2048) {
        self.size = size
        self.stream = stream

        self.receiveIterator = Data().makeIterator()
        self.sendBuffer = []
    }

    public func receive() throws -> Byte? {
        guard let next = receiveIterator.next() else {
            receiveIterator = try stream.receive(max: size).makeIterator()
            return receiveIterator.next()
        }
        return next
    }

    public func receive(max: Int) throws -> Bytes {
        var bytes: Bytes = []

        for _ in 0 ..< max {
            guard let byte = try receive() else {
                break
            }

            bytes += byte
        }

        return bytes
    }

    public func send(_ bytes: Bytes) throws {
        sendBuffer += bytes
    }

    public func flush() throws {
        try stream.send(sendBuffer)
        sendBuffer = []
    }

    /**
         Sometimes we let sockets queue things up before flushing, but in situations like web sockets,
         we may want to skip that functionality
     */
    public func send(_ bytes: Bytes, flushing: Bool) throws {
        guard flushing else {
            try send(bytes)<commit_msg>adding additional empty check in buffer flush<commit_after>import C7

/**
    Buffers receive and send calls to a Stream.
 
    Receive calls are buffered by the size used to initialize
    the buffer.
 
    Send calls are buffered until `flush()` is called.
*/
public final class StreamBuffer: Stream {
    public var closed: Bool {
        return stream.closed
    }
    public func close() throws {
        try stream.close()
    }

    private let stream: Stream
    private let size: Int


    public func setTimeout(_ timeout: Double) throws {
        try stream.setTimeout(timeout)
    }

    private var receiveIterator: IndexingIterator<[Byte]>
    private var sendBuffer: Bytes

    public init(_ stream: Stream, size: Int = 2048) {
        self.size = size
        self.stream = stream

        self.receiveIterator = Data().makeIterator()
        self.sendBuffer = []
    }

    public func receive() throws -> Byte? {
        guard let next = receiveIterator.next() else {
            receiveIterator = try stream.receive(max: size).makeIterator()
            return receiveIterator.next()
        }
        return next
    }

    public func receive(max: Int) throws -> Bytes {
        var bytes: Bytes = []

        for _ in 0 ..< max {
            guard let byte = try receive() else {
                break
            }

            bytes += byte
        }

        return bytes
    }

    public func send(_ bytes: Bytes) throws {
        sendBuffer += bytes
    }

    public func flush() throws {
        guard !sendBuffer.isEmpty else { return }
        try stream.send(sendBuffer)
        sendBuffer = []
    }

    /**
         Sometimes we let sockets queue things up before flushing, but in situations like web sockets,
         we may want to skip that functionality
     */
    public func send(_ bytes: Bytes, flushing: Bool) throws {
        guard flushing else {
            try send(bytes)
{'fraction_non_alphanumeric': 0.07925531914893617, 'fraction_numerical': 0.0031914893617021275, 'mean_word_length': 1.9521193092621665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '1353212', 'n_tokens_mistral': 1005, 'n_tokens_neox': 937, 'n_words': 363}