diff --git a/pdu/global.go b/pdu/global.go index 6117068..65c7b8e 100644 --- a/pdu/global.go +++ b/pdu/global.go @@ -2,7 +2,7 @@ package pdu import "sync" -var byteBufferPool = &BufferPoolManager{ +var ByteBufferPool = &BufferPoolManager{ pools: make(map[int]*sync.Pool), mu: sync.Mutex{}, } diff --git a/pdu/pdu.go b/pdu/pdu.go index 7f47104..3928bec 100644 --- a/pdu/pdu.go +++ b/pdu/pdu.go @@ -26,9 +26,9 @@ type ( } ) -func (p *PDU_HEADER) Encode() ([]uint8, error) { - buf := make([]uint8, 16) - err := p.EncodeInto(&buf) +func (p *PDU_HEADER) Encode() (*[]uint8, error) { + buf := ByteBufferPool.GetBuffer(int(p.Size())) + err := p.EncodeInto(buf) return buf, err } func (p *PDU_HEADER) EncodeInto(buf *[]uint8) error { diff --git a/pdu/pdu_test.go b/pdu/pdu_test.go index 239eb88..21114dc 100644 --- a/pdu/pdu_test.go +++ b/pdu/pdu_test.go @@ -20,8 +20,8 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) { if err != nil { t.Errorf("Expected no error, got %v", err) } - if len(result) != 16 { - t.Errorf("Expected byte slice of length 16, got %d", len(result)) + if len(*result) != 16 { + t.Errorf("Expected byte slice of length 16, got %d", len(*result)) } } @@ -38,9 +38,9 @@ func TestEncodeHandlesZeroValues(t *testing.T) { t.Errorf("Expected no error, got %v", err) } expected := make([]uint8, 16) - for i, v := range result { + for i, v := range *result { if v != expected[i] { - t.Errorf("Expected byte slice with zero values, got %v", result) + t.Errorf("Expected byte slice with zero values, got %v", *result) break } } @@ -60,9 +60,9 @@ func TestEncodeEncodesProperly(t *testing.T) { } expected := []uint8{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4} - for i, v := range result { + for i, v := range *result { if v != expected[i] { - t.Errorf("Expected byte slice with values %v, got %v", expected, result) + t.Errorf("Expected byte slice with values %v, got %v", expected, *result) break } } @@ -82,9 +82,9 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) { } expected := []uint8{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35} - for i, v := range result { + for i, v := range *result { if v != expected[i] { - t.Errorf("Expected byte slice with values %v, got %v", expected, result) + t.Errorf("Expected byte slice with values %v, got %v", expected, *result) break } }