processor/filereader.go GO 55 lines View on github.com → Search inside
1// SPDX-License-Identifier: MIT23package processor45import (6	"bytes"7	"fmt"8	"io"9	"os"10)1112// FileReader is a struct responsible for reading files into its buffer13type FileReader struct {14	Buffer *bytes.Buffer15}1617// NewFileReader creates a new file reader responsible for reading a file18func NewFileReader() FileReader {19	return FileReader{20		Buffer: &bytes.Buffer{},21	}22}2324// ReadFile actually reads the file into a buffer size controlled by LargeByteCount25func (reader *FileReader) ReadFile(path string, size int) ([]byte, error) {26	fd, err := os.Open(path)27	if err != nil {28		return nil, fmt.Errorf("error opening %s: %v", path, err)29	}30	defer func(file *os.File) {31		_ = file.Close()32	}(fd)3334	// Generally, re-using the buffer is best. But, if we end up reading a huge35	// file we would allocate an equally huge buffer. Rather than keep the huge36	// buffer around forever, it's probably worth eating the GC cost of37	// replacing it so that we can release the memory.38	if int64(reader.Buffer.Cap()) > LargeByteCount {39		reader.Buffer = &bytes.Buffer{}40	}4142	// Reset contents, but retain the underlying memory that's already been43	// allocated44	reader.Buffer.Reset()4546	reader.Buffer.Grow(size)4748	_, err = io.Copy(reader.Buffer, fd)49	if err != nil {50		return nil, fmt.Errorf("error reading %s: %v", path, err)51	}5253	return reader.Buffer.Bytes(), nil54}

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.