github.com/cloudwego/hertz@v0.9.3/pkg/common/bytebufferpool/bytebuffer.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * The MIT License (MIT)
    17   *
    18   * Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors
    19   *
    20   * Permission is hereby granted, free of charge, to any person obtaining a copy
    21   * of this software and associated documentation files (the "Software"), to deal
    22   * in the Software without restriction, including without limitation the rights
    23   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    24   * copies of the Software, and to permit persons to whom the Software is
    25   * furnished to do so, subject to the following conditions:
    26   *
    27   * The above copyright notice and this permission notice shall be included in
    28   * all copies or substantial portions of the Software.
    29   *
    30   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    31   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    32   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    33   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    34   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    35   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    36   * THE SOFTWARE.
    37   *
    38   * This file may have been modified by CloudWeGo authors. All CloudWeGo
    39   * Modifications are Copyright 2022 CloudWeGo Authors.
    40   */
    41  
    42  package bytebufferpool
    43  
    44  import "io"
    45  
    46  // ByteBuffer provides byte buffer, which can be used for minimizing
    47  // memory allocations.
    48  //
    49  // ByteBuffer may be used with functions appending data to the given []byte
    50  // slice. See example code for details.
    51  //
    52  // Use Get for obtaining an empty byte buffer.
    53  type ByteBuffer struct {
    54  	// B is a byte buffer to use in append-like workloads.
    55  	// See example code for details.
    56  	B []byte
    57  }
    58  
    59  // Len returns the size of the byte buffer.
    60  func (b *ByteBuffer) Len() int {
    61  	return len(b.B)
    62  }
    63  
    64  // ReadFrom implements io.ReaderFrom.
    65  //
    66  // The function appends all the data read from r to b.
    67  func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error) {
    68  	p := b.B
    69  	nStart := int64(len(p))
    70  	nMax := int64(cap(p))
    71  	n := nStart
    72  	if nMax == 0 {
    73  		nMax = 64
    74  		p = make([]byte, nMax)
    75  	} else {
    76  		p = p[:nMax]
    77  	}
    78  	for {
    79  		if n == nMax {
    80  			nMax *= 2
    81  			bNew := make([]byte, nMax)
    82  			copy(bNew, p)
    83  			p = bNew
    84  		}
    85  		nn, err := r.Read(p[n:])
    86  		n += int64(nn)
    87  		if err != nil {
    88  			b.B = p[:n]
    89  			n -= nStart
    90  			if err == io.EOF {
    91  				return n, nil
    92  			}
    93  			return n, err
    94  		}
    95  	}
    96  }
    97  
    98  // WriteTo implements io.WriterTo.
    99  func (b *ByteBuffer) WriteTo(w io.Writer) (int64, error) {
   100  	n, err := w.Write(b.B)
   101  	return int64(n), err
   102  }
   103  
   104  // Bytes returns b.B, i.e. all the bytes accumulated in the buffer.
   105  //
   106  // The purpose of this function is bytes.Buffer compatibility.
   107  func (b *ByteBuffer) Bytes() []byte {
   108  	return b.B
   109  }
   110  
   111  // Write implements io.Writer - it appends p to ByteBuffer.B
   112  func (b *ByteBuffer) Write(p []byte) (int, error) {
   113  	b.B = append(b.B, p...)
   114  	return len(p), nil
   115  }
   116  
   117  // WriteByte appends the byte c to the buffer.
   118  //
   119  // The purpose of this function is bytes.Buffer compatibility.
   120  //
   121  // The function always returns nil.
   122  func (b *ByteBuffer) WriteByte(c byte) error {
   123  	b.B = append(b.B, c)
   124  	return nil
   125  }
   126  
   127  // WriteString appends s to ByteBuffer.B.
   128  func (b *ByteBuffer) WriteString(s string) (int, error) {
   129  	b.B = append(b.B, s...)
   130  	return len(s), nil
   131  }
   132  
   133  // Set sets ByteBuffer.B to p.
   134  func (b *ByteBuffer) Set(p []byte) {
   135  	b.B = append(b.B[:0], p...)
   136  }
   137  
   138  // SetString sets ByteBuffer.B to s.
   139  func (b *ByteBuffer) SetString(s string) {
   140  	b.B = append(b.B[:0], s...)
   141  }
   142  
   143  // String returns string representation of ByteBuffer.B.
   144  func (b *ByteBuffer) String() string {
   145  	return string(b.B)
   146  }
   147  
   148  // Reset makes ByteBuffer.B empty.
   149  func (b *ByteBuffer) Reset() {
   150  	b.B = b.B[:0]
   151  }