github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/encoding/gob/decoder.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gob
     6  
     7  import (
     8  	"github.com/shogo82148/std/io"
     9  	"github.com/shogo82148/std/reflect"
    10  	"github.com/shogo82148/std/sync"
    11  )
    12  
    13  // Decoderは、接続のリモート側から読み取られた型とデータ情報の受信を管理します。
    14  // 複数のゴルーチンによる並行使用が安全です。
    15  //
    16  // Decoderは、デコードされた入力サイズに対して基本的な健全性チェックのみを行い、
    17  // その制限は設定可能ではありません。信頼できないソースからのgobデータをデコードする際は注意が必要です。
    18  type Decoder struct {
    19  	mutex        sync.Mutex
    20  	r            io.Reader
    21  	buf          decBuffer
    22  	wireType     map[typeId]*wireType
    23  	decoderCache map[reflect.Type]map[typeId]**decEngine
    24  	ignorerCache map[typeId]**decEngine
    25  	freeList     *decoderState
    26  	countBuf     []byte
    27  	err          error
    28  }
    29  
    30  // NewDecoderは、[io.Reader] から読み取る新しいデコーダを返します。
    31  // もしrが [io.ByteReader] も実装していない場合、それは [bufio.Reader] でラップされます。
    32  func NewDecoder(r io.Reader) *Decoder
    33  
    34  // Decodeは、入力ストリームから次の値を読み取り、
    35  // 空のインターフェース値で表されるデータに格納します。
    36  // もしeがnilの場合、値は破棄されます。それ以外の場合、
    37  // eの下にある値は、受け取った次のデータ項目の
    38  // 正しい型へのポインタでなければなりません。
    39  // 入力がEOFにある場合、Decodeは [io.EOF] を返し、
    40  // eを変更しません。
    41  func (dec *Decoder) Decode(e any) error
    42  
    43  // DecodeValueは、入力ストリームから次の値を読み取ります。
    44  // もしvがゼロのreflect.Value(v.Kind() == Invalid)の場合、DecodeValueは値を破棄します。
    45  // それ以外の場合、値はvに格納されます。その場合、vは
    46  // 非nilのデータへのポインタを表すか、または代入可能なreflect.Value(v.CanSet())でなければなりません。
    47  // 入力がEOFにある場合、DecodeValueは [io.EOF] を返し、
    48  // vを変更しません。
    49  func (dec *Decoder) DecodeValue(v reflect.Value) error