Documentation ¶
Index ¶
- Constants
- Variables
- func GetVarSize(value any) int
- func MakeDirForFile(filePath string, creator string) error
- func PutVarUint(data []byte, val uint64) int
- type BinReader
- func (r *BinReader) Len() int
- func (r *BinReader) ReadArray(t any, maxSize ...int)
- func (r *BinReader) ReadB() byte
- func (r *BinReader) ReadBool() bool
- func (r *BinReader) ReadBytes(buf []byte)
- func (r *BinReader) ReadString(maxSize ...int) string
- func (r *BinReader) ReadU16BE() uint16
- func (r *BinReader) ReadU16LE() uint16
- func (r *BinReader) ReadU32LE() uint32
- func (r *BinReader) ReadU64LE() uint64
- func (r *BinReader) ReadVarBytes(maxSize ...int) []byte
- func (r *BinReader) ReadVarUint() uint64
- type BinWriter
- func (w *BinWriter) Grow(n int)
- func (w *BinWriter) WriteArray(arr any)
- func (w *BinWriter) WriteB(u8 byte)
- func (w *BinWriter) WriteBool(b bool)
- func (w *BinWriter) WriteBytes(b []byte)
- func (w *BinWriter) WriteString(s string)
- func (w *BinWriter) WriteU16BE(u16 uint16)
- func (w *BinWriter) WriteU16LE(u16 uint16)
- func (w *BinWriter) WriteU32LE(u32 uint32)
- func (w *BinWriter) WriteU64LE(u64 uint64)
- func (w *BinWriter) WriteVarBytes(b []byte)
- func (w *BinWriter) WriteVarUint(val uint64)
- type BufBinWriter
- type Serializable
Constants ¶
const MaxArraySize = 0x1000000
MaxArraySize is the maximum size of an array which can be decoded. It is taken from https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs#L130
Variables ¶
var ErrDrained = errors.New("buffer already drained")
ErrDrained is returned on an attempt to use an already drained write buffer.
Functions ¶
func GetVarSize ¶
GetVarSize returns the number of bytes in a serialized variable. It supports ints/uints (estimating them with variable-length encoding that is used in Neo), strings, pointers to Serializable structures, slices and arrays of ints/uints or Serializable structures. It's similar to GetVarSize<T>(this T[] value) used in C#, but differs in that it also supports things like Uint160 or Uint256.
func MakeDirForFile ¶
MakeDirForFile creates a directory provided in the filePath.
func PutVarUint ¶ added in v0.96.0
PutVarUint puts a val in the varint form to the pre-allocated buffer.
Types ¶
type BinReader ¶
type BinReader struct { Err error // contains filtered or unexported fields }
BinReader is a convenient wrapper around an io.Reader and err object. Used to simplify error handling when reading into a struct with many fields.
func NewBinReaderFromBuf ¶
NewBinReaderFromBuf makes a BinReader from a byte buffer.
func NewBinReaderFromIO ¶
NewBinReaderFromIO makes a BinReader from io.Reader.
func (*BinReader) Len ¶ added in v0.97.3
Len returns the number of bytes of the unread portion of the buffer if reading from bytes.Reader or -1 otherwise.
func (*BinReader) ReadArray ¶
ReadArray reads an array into a value which must be a pointer to a slice.
func (*BinReader) ReadB ¶
ReadB reads a byte from the underlying io.Reader. On read failures it returns zero.
func (*BinReader) ReadBool ¶
ReadBool reads a boolean value encoded in a zero/non-zero byte from the underlying io.Reader. On read failures it returns false.
func (*BinReader) ReadBytes ¶
ReadBytes copies a fixed-size buffer from the reader to the provided slice.
func (*BinReader) ReadString ¶
ReadString calls ReadVarBytes and casts the results as a string.
func (*BinReader) ReadU16BE ¶
ReadU16BE reads a big-endian encoded uint16 value from the underlying io.Reader. On read failures it returns zero.
func (*BinReader) ReadU16LE ¶
ReadU16LE reads a little-endian encoded uint16 value from the underlying io.Reader. On read failures it returns zero.
func (*BinReader) ReadU32LE ¶
ReadU32LE reads a little-endian encoded uint32 value from the underlying io.Reader. On read failures it returns zero.
func (*BinReader) ReadU64LE ¶
ReadU64LE reads a little-endian encoded uint64 value from the underlying io.Reader. On read failures it returns zero.
func (*BinReader) ReadVarBytes ¶
ReadVarBytes reads the next set of bytes from the underlying reader. ReadVarUInt() is used to determine how large that slice is.
func (*BinReader) ReadVarUint ¶
ReadVarUint reads a variable-length-encoded integer from the underlying reader.
type BinWriter ¶
type BinWriter struct { Err error // contains filtered or unexported fields }
BinWriter is a convenient wrapper around an io.Writer and err object. Used to simplify error handling when writing into an io.Writer from a struct with many fields.
func NewBinWriterFromIO ¶
NewBinWriterFromIO makes a BinWriter from io.Writer.
func (*BinWriter) Grow ¶ added in v0.97.2
Grow tries to increase the 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 (*BinWriter) WriteArray ¶
WriteArray writes a slice or an array arr into w. Note that nil slices and empty slices are gonna be treated the same resulting in an equal zero-length array encoded.
func (*BinWriter) WriteBool ¶
WriteBool writes a boolean value into the underlying io.Writer encoded as a byte with values of 0 or 1.
func (*BinWriter) WriteBytes ¶
WriteBytes writes a variable byte into the underlying io.Writer without prefix.
func (*BinWriter) WriteString ¶
WriteString writes a variable length string into the underlying io.Writer.
func (*BinWriter) WriteU16BE ¶
WriteU16BE writes a uint16 value into the underlying io.Writer in big-endian format.
func (*BinWriter) WriteU16LE ¶
WriteU16LE writes a uint16 value into the underlying io.Writer in little-endian format.
func (*BinWriter) WriteU32LE ¶
WriteU32LE writes a uint32 value into the underlying io.Writer in little-endian format.
func (*BinWriter) WriteU64LE ¶
WriteU64LE writes a uint64 value into the underlying io.Writer in little-endian format.
func (*BinWriter) WriteVarBytes ¶
WriteVarBytes writes a variable length byte array into the underlying io.Writer.
func (*BinWriter) WriteVarUint ¶
WriteVarUint writes a uint64 into the underlying writer using variable-length encoding.
type BufBinWriter ¶
type BufBinWriter struct { *BinWriter // contains filtered or unexported fields }
BufBinWriter is an additional layer on top of BinWriter that automatically creates a buffer to write into that you can get after all writes via Bytes().
func NewBufBinWriter ¶
func NewBufBinWriter() *BufBinWriter
NewBufBinWriter makes a BufBinWriter with an empty byte buffer.
func (*BufBinWriter) Bytes ¶
func (bw *BufBinWriter) Bytes() []byte
Bytes returns the resulting buffer and makes future writes return an error. Subsequent calls to it will return nil. You can reuse this instance of BufBinWriter after BufBinWriter.Reset.
func (*BufBinWriter) Len ¶
func (bw *BufBinWriter) Len() int
Len returns the number of bytes of the unread portion of the buffer.
func (*BufBinWriter) Reset ¶
func (bw *BufBinWriter) Reset()
Reset resets the state of the buffer, making it usable again. It can make buffer usage somewhat more efficient because you don't need to create it again. But beware, the buffer is gonna be the same as the one returned by Bytes(), so if you need that data after Reset() you have to copy it yourself.
type Serializable ¶
Serializable defines the binary encoding/decoding interface. Errors are returned via BinReader/BinWriter Err field. These functions must have safe behavior when the passed BinReader/BinWriter with Err is already set. Invocations to these functions tend to be nested, with this mechanism only the top-level caller should handle an error once and all the other code should just not panic while there is an error.