processor/filereader.go GO 53 lines View on github.com → Search inside
1// SPDX-License-Identifier: MIT23package processor45import (6	"bytes"7	"fmt"8	"os"9)1011// FileReader is a struct responsible for reading files into its buffer12type FileReader struct {13	Buffer *bytes.Buffer14}1516// NewFileReader creates a new file reader responsible for reading a file17func NewFileReader() FileReader {18	return FileReader{19		Buffer: &bytes.Buffer{},20	}21}2223// ReadFile actually reads the file into a buffer size controlled by LargeByteCount24func (reader *FileReader) ReadFile(path string, size int) ([]byte, error) {25	fd, err := os.Open(path)26	if err != nil {27		return nil, fmt.Errorf("error opening %s: %v", path, err)28	}29	defer func(file *os.File) {30		_ = file.Close()31	}(fd)3233	// Generally, re-using the buffer is best. But, if we end up reading a huge34	// file we would allocate an equally huge buffer. Rather than keep the huge35	// buffer around forever, it's probably worth eating the GC cost of36	// replacing it so that we can release the memory.37	if int64(reader.Buffer.Cap()) > LargeByteCount {38		reader.Buffer = &bytes.Buffer{}39	}4041	// Reset contents, but retain the underlying memory that's already been allocated.42	reader.Buffer.Reset()43	// Leave room for ReadFrom's final EOF probe to avoid an extra buffer growth.44	reader.Buffer.Grow(size + bytes.MinRead)4546	_, err = reader.Buffer.ReadFrom(fd)47	if err != nil {48		return nil, fmt.Errorf("error reading %s: %w", path, err)49	}5051	return reader.Buffer.Bytes(), nil52}

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.