From dacf025dd90320a14ab859a3d5a28e376e0c9052 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Fri, 6 Aug 2021 10:52:24 +0300 Subject: [PATCH] io: add `Grow` to `BinWriter` Signed-off-by: Evgeniy Stratonikov --- pkg/io/binaryWriter.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/io/binaryWriter.go b/pkg/io/binaryWriter.go index 46915725e..5f234c1b4 100644 --- a/pkg/io/binaryWriter.go +++ b/pkg/io/binaryWriter.go @@ -1,6 +1,7 @@ package io import ( + "bytes" "encoding/binary" "io" "reflect" @@ -144,3 +145,11 @@ func (w *BinWriter) WriteVarBytes(b []byte) { func (w *BinWriter) WriteString(s string) { w.WriteVarBytes([]byte(s)) } + +// Grow tries to increase underlying buffer capacity so that at least n bytes +// can be written without reallocation. If the writer is not a buffer, this is a no-op. +func (w *BinWriter) Grow(n int) { + if b, ok := w.w.(*bytes.Buffer); ok { + b.Grow(n) + } +}