diff --git a/utils/binary/builder.go b/utils/binary/builder.go index f3d062b6..e89ae5c1 100644 --- a/utils/binary/builder.go +++ b/utils/binary/builder.go @@ -1,6 +1,7 @@ package binary import ( + "bytes" "encoding/binary" "io" "math" @@ -45,6 +46,17 @@ func (b *Builder) ToBytes() (data []byte) { return } +func (b *Builder) Buffer() (buffer *bytes.Buffer) { + b.p(func(ub *pbuf.UserBuffer[teacfg]) { + buffer = &ub.Buffer + }) + return +} + +func (b *Builder) ManualDestroy() { + (*orbyte.Item[pbuf.UserBuffer[teacfg]])(b).ManualDestroy() +} + // Pack TLV with tea encryption if key is set // // GC 安全, 返回的数据在 Builder 被销毁之后仍能被正确读取, diff --git a/utils/binary/pool.go b/utils/binary/pool.go index d73c8cdf..484f8dc8 100644 --- a/utils/binary/pool.go +++ b/utils/binary/pool.go @@ -25,6 +25,11 @@ func NewBuilder(key ...byte) *Builder { return (*Builder)(b) } +// PutBuilder +func PutBuilder(b *Builder) { + b.ManualDestroy() +} + var gzipPool = sync.Pool{ New: func() any { buf := new(bytes.Buffer)