<commit_before>package io

import (
	"io"
	"sync"

	"fmt"
	"v2ray.com/core/common/alloc"
)

type BufferedWriter struct {
	sync.Mutex
	writer io.Writer
	buffer *alloc.Buffer
	cached bool
}

func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
	return &BufferedWriter{
		writer: rawWriter,
		buffer: alloc.NewBuffer().Clear(),
		cached: true,
	}
}

func (this *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
	this.Lock()
	defer this.Unlock()

	if this.writer == nil {
		return 0, io.ErrClosedPipe
	}

	totalBytes := int64(0)
	for {
		nBytes, err := this.buffer.FillFrom(reader)
		totalBytes += int64(nBytes)
		if err != nil {
			if err == io.EOF {
				return totalBytes, nil
			}
			return totalBytes, err
		}
		this.FlushWithoutLock()
	}
}

func (this *BufferedWriter) Write(b []byte) (int, error) {
	this.Lock()
	defer this.Unlock()

	if this.writer == nil {
		return 0, io.ErrClosedPipe
	}

	fmt.Printf("BufferedWriter writing: %v\n", b)

	if !this.cached {
		return this.writer.Write(b)
	}
	nBytes, _ := this.buffer.Write(b)
	if this.buffer.IsFull() {
		this.FlushWithoutLock()
	}
	fmt.Printf("BufferedWriter content: %v\n", this.buffer.Value)
	return nBytes, nil
}

func (this *BufferedWriter) Flush() error {
	this.Lock()
	defer this.Unlock()

	if this.writer == nil {
		return io.ErrClosedPipe
	}

	return this.FlushWithoutLock()
}

func (this *BufferedWriter) FlushWithoutLock() error {
	defer this.buffer.Clear()
	for !this.buffer.IsEmpty() {
		nBytes, err := this.writer.Write(this.buffer.Value)<commit_msg>more log<commit_after>package io

import (
	"io"
	"sync"

	"fmt"
	"v2ray.com/core/common/alloc"
)

type BufferedWriter struct {
	sync.Mutex
	writer io.Writer
	buffer *alloc.Buffer
	cached bool
}

func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
	return &BufferedWriter{
		writer: rawWriter,
		buffer: alloc.NewBuffer().Clear(),
		cached: true,
	}
}

func (this *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
	this.Lock()
	defer this.Unlock()

	if this.writer == nil {
		return 0, io.ErrClosedPipe
	}

	totalBytes := int64(0)
	for {
		nBytes, err := this.buffer.FillFrom(reader)
		totalBytes += int64(nBytes)
		if err != nil {
			if err == io.EOF {
				return totalBytes, nil
			}
			return totalBytes, err
		}
		this.FlushWithoutLock()
	}
}

func (this *BufferedWriter) Write(b []byte) (int, error) {
	this.Lock()
	defer this.Unlock()

	if this.writer == nil {
		return 0, io.ErrClosedPipe
	}

	fmt.Printf("BufferedWriter writing: %v\n", b)

	if !this.cached {
		return this.writer.Write(b)
	}
	nBytes, _ := this.buffer.Write(b)
	if this.buffer.IsFull() {
		this.FlushWithoutLock()
	}
	fmt.Printf("BufferedWriter content: %v\n", this.buffer.Value)
	return nBytes, nil
}

func (this *BufferedWriter) Flush() error {
	this.Lock()
	defer this.Unlock()

	if this.writer == nil {
		return io.ErrClosedPipe
	}

	return this.FlushWithoutLock()
}

func (this *BufferedWriter) FlushWithoutLock() error {
	fmt.Println("BufferedWriter flushing")
	defer this.buffer.Clear()
	for !this.buffer.IsEmpty() {
		nBytes, err := this.writer.Write(this.buffer.Value)
		fmt.Printf("BufferedWriting flushed %d bytes.\n", nBytes)
{'fraction_non_alphanumeric': 0.14073840328179235, 'fraction_numerical': 0.006311139160618492, 'mean_word_length': 3.953125, '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': '3368508', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1181, 'n_words': 265}