github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/stl/adaptors/buffer.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package adaptors
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/stl"
    19  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/stl/containers"
    20  )
    21  
    22  func NewBuffer(buf []byte) *Buffer {
    23  	b := &Buffer{
    24  		storage: containers.NewStdVector[byte](),
    25  	}
    26  	if len(buf) > 0 {
    27  		bs := stl.NewFixedTypeBytes[byte]()
    28  		bs.Storage = buf
    29  		b.storage.ReadBytes(bs, true)
    30  	}
    31  	return b
    32  }
    33  
    34  func (b *Buffer) Reset()         { b.storage.Reset() }
    35  func (b *Buffer) Close()         { b.storage.Close() }
    36  func (b *Buffer) String() string { return b.storage.String() }
    37  
    38  func (b *Buffer) Write(p []byte) (n int, err error) {
    39  	n = len(p)
    40  	b.storage.AppendMany(p...)
    41  	return
    42  }
    43  
    44  // WriteString TODO: avoid string to []byte copy
    45  func (b *Buffer) WriteString(s string) (n int, err error) {
    46  	n = len(s)
    47  	b.storage.AppendMany([]byte(s)...)
    48  	return
    49  }
    50  
    51  func (b *Buffer) Bytes() []byte  { return b.storage.Data() }
    52  func (b *Buffer) Len() int       { return b.storage.Length() }
    53  func (b *Buffer) Cap() int       { return b.storage.Capacity() }
    54  func (b *Buffer) Allocated() int { return b.storage.Allocated() }