JOJ3/internal/executor/local/buffer.go
张泊明518370910136 c3f7b0fa2b
Some checks failed
build / trigger-build-image (push) Blocked by required conditions
submodules sync / sync (push) Has been cancelled
build / build (push) Has been cancelled
feat(executor/local): simple local executor without limits
2024-11-28 10:05:44 -05:00

36 lines
782 B
Go

package local
import (
"bytes"
"errors"
)
// LimitedBuffer wraps a bytes.Buffer and limits its size.
type LimitedBuffer struct {
buf *bytes.Buffer
maxSize int
}
// Write writes data to the buffer and checks the size limit.
func (lb *LimitedBuffer) Write(p []byte) (n int, err error) {
if lb.buf.Len()+len(p) > lb.maxSize {
// Truncate to fit within the limit
allowed := lb.maxSize - lb.buf.Len()
if allowed > 0 {
n, _ = lb.buf.Write(p[:allowed])
}
return n, errors.New("buffer size limit exceeded")
}
return lb.buf.Write(p)
}
// Bytes returns the buffer's content.
func (lb *LimitedBuffer) Bytes() []byte {
return lb.buf.Bytes()
}
// String returns the buffer's content as a string.
func (lb *LimitedBuffer) String() string {
return lb.buf.String()
}