binaryio

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlignOffset

func AlignOffset(n int64, alignment int64) int64

AlignOffset 将 n 向上取整到下一个对齐边界 / AlignOffset returns n rounded up to the next alignment boundary.

func Get7BitEncodedIntSize

func Get7BitEncodedIntSize(value int32) int64

Get7BitEncodedIntSize 计算编码一个 int32 值所需的字节数 与 Write7BitEncodedInt 保持一致的编码逻辑

func PeekByte

func PeekByte(r io.Reader) (byte, error)

PeekByte 偷看下一个字节,不移动读取指针。 要求 reader 实现 Peeker 接口,例如 bufio.Reader。

func PeekString

func PeekString(r io.Reader) (string, error)

PeekString 读取下一个字符串(7BitEncode + UTF-8),但不消耗它。 要求 reader 实现 Peeker 接口,例如 bufio.Reader。

注意:如果字符串长度超过了 bufio.Reader 的缓冲区大小(默认 4KB), Peek 操作会返回 bufio.ErrBufferFull。 如果需要处理超大字符串,请在创建 bufio.Reader 时指定更大的缓冲区大小

func Read7BitEncodedInt

func Read7BitEncodedInt(reader io.Reader) (int32, error)

Read7BitEncodedInt 读取 C# 格式的 7-bit encoded int 匹配 .NET 4.8 的实现逻辑

func ReadBool

func ReadBool(r io.Reader) (bool, error)

ReadBool 读取一个字节,返回 bool,如果字节非 0 则返回 true,否则返回 false 对应 C# 中的 BinaryReader.ReadBoolean

func ReadByte

func ReadByte(r io.Reader) (byte, error)

ReadByte 读 1 字节

func ReadBytes

func ReadBytes(r io.Reader, n int64) ([]byte, error)

ReadBytes 读取 n 个字节

func ReadFloat2

func ReadFloat2(r io.Reader) ([2]float32, error)

func ReadFloat3

func ReadFloat3(r io.Reader) ([3]float32, error)

func ReadFloat4

func ReadFloat4(r io.Reader) ([4]float32, error)

func ReadFloat4x4

func ReadFloat4x4(r io.Reader) ([16]float32, error)

func ReadFloat32

func ReadFloat32(r io.Reader) (float32, error)

ReadFloat32 从 r 中读取 4 个字节,以 little-endian 解码成 float32 在 C# 中是 BinaryReader.ReadSingle

func ReadFloat64

func ReadFloat64(r io.Reader) (float64, error)

ReadFloat64 读取 8 字节浮点数 (little-endian)

func ReadInt8

func ReadInt8(r io.Reader) (int8, error)

ReadInt8 读取 1 字节有符号整数

func ReadInt16

func ReadInt16(r io.Reader) (int16, error)

ReadInt16 读取 2 字节有符号整数 (little-endian)

func ReadInt32

func ReadInt32(r io.Reader) (int32, error)

ReadInt32 读取 4 字节 int32(little-endian)

func ReadInt64

func ReadInt64(r io.Reader) (int64, error)

ReadInt64 读取 8 字节有符号整数 (little-endian)

func ReadNullString

func ReadNullString(r io.Reader) (string, error)

ReadNullString 从 io.Reader 读取 null 结尾字符串 / ReadNullString reads a null-terminated string from an io.Reader.

func ReadString

func ReadString(r io.Reader) (string, error)

ReadString 读取 C# BinaryWriter.WriteString 格式的字符串 完全匹配 .NET 4.8 的实现逻辑 需要注意这个 7BitEncode 虽然与 LEB128 类似,但不是完全相同

func ReadUInt16

func ReadUInt16(r io.Reader) (uint16, error)

ReadUInt16 读取 2 字节无符号整数 (little-endian)

func ReadUInt32

func ReadUInt32(r io.Reader) (uint32, error)

ReadUInt32 读取 4 字节无符号整数 (little-endian)

func ReadUInt64

func ReadUInt64(r io.Reader) (uint64, error)

ReadUInt64 读取 8 字节无符号整数 (little-endian)

func Write7BitEncodedInt

func Write7BitEncodedInt(writer io.Writer, value int32) error

Write7BitEncodedInt 写入 C# 格式的 7-bit encoded int 完全匹配 .NET 4.8 的实现:支持负数,使用无符号转换

func WriteBool

func WriteBool(w io.Writer, v bool) error

WriteBool 写一个字节,如果 b 为 true 则写入 1,否则写入 0

func WriteByte

func WriteByte(w io.Writer, b byte) error

WriteByte 写单字节

func WriteBytes

func WriteBytes(w io.Writer, bs []byte) error

WriteBytes 写多个字节

func WriteFloat2

func WriteFloat2(w io.Writer, arr [2]float32) error

func WriteFloat3

func WriteFloat3(w io.Writer, arr [3]float32) error

func WriteFloat4

func WriteFloat4(w io.Writer, arr [4]float32) error

func WriteFloat4x4

func WriteFloat4x4(w io.Writer, arr [16]float32) error

func WriteFloat32

func WriteFloat32(w io.Writer, val float32) error

WriteFloat32 写一个 float32 (4 bytes, little-endian) 在 C# 中是 BinaryWriter.WriteSingle

func WriteFloat64

func WriteFloat64(w io.Writer, value float64) error

WriteFloat64 写入 8 字节浮点数 (little-endian)

func WriteInt8

func WriteInt8(w io.Writer, value int8) error

WriteInt8 写入 1 字节 int8

func WriteInt16

func WriteInt16(w io.Writer, value int16) error

WriteInt16 写入 2 字节有符号整数 (little-endian)

func WriteInt32

func WriteInt32(w io.Writer, v int32) error

WriteInt32 写一个 4 字节 int32(little-endian)

func WriteInt64

func WriteInt64(w io.Writer, value int64) error

WriteInt64 写入 8 字节有符号整数 (little-endian)

func WriteString

func WriteString(w io.Writer, s string) error

WriteString 写入 C# BinaryWriter.WriteString 格式的字符串 完全匹配 .NET 4.8 的实现逻辑 需要注意这个 7BitEncode 虽然与 LEB128 类似,但不是完全相同

func WriteUInt16

func WriteUInt16(w io.Writer, val uint16) error

WriteUInt16 写入一个16位无符号整数(little-endian)

func WriteUInt32

func WriteUInt32(w io.Writer, val uint32) error

WriteUInt32 写入一个 4 字节 uint32(little-endian)

func WriteUInt64

func WriteUInt64(w io.Writer, value uint64) error

WriteUInt64 写入 8 字节无符号整数 (little-endian)

Types

type EndianReader

type EndianReader struct {
	// contains filtered or unexported fields
}

EndianReader 以可配置字节序从内存字节切片读取基础类型 / EndianReader reads primitive values from an in-memory byte slice with a configurable byte order.

func NewEndianReader

func NewEndianReader(data []byte, order binary.ByteOrder) *EndianReader

NewEndianReader 创建从偏移 0 开始读取的 EndianReader / NewEndianReader creates an EndianReader starting at offset 0.

func NewEndianReaderAt

func NewEndianReaderAt(data []byte, pos int64, order binary.ByteOrder) *EndianReader

NewEndianReaderAt 创建从指定偏移开始读取的 EndianReader / NewEndianReaderAt creates an EndianReader starting at the requested offset.

func (*EndianReader) Align

func (r *EndianReader) Align(alignment int64)

Align 将读取偏移前移到下一个对齐边界 / Align advances the read offset to the next alignment boundary.

func (*EndianReader) Align4

func (r *EndianReader) Align4()

Align4 将读取偏移前移到下一个 4 字节边界 / Align4 advances the read offset to the next 4-byte boundary.

func (*EndianReader) ByteOrder

func (r *EndianReader) ByteOrder() binary.ByteOrder

ByteOrder 返回当前数值字节序 / ByteOrder returns the current numeric byte order.

func (*EndianReader) Len

func (r *EndianReader) Len() int64

Len 返回底层数据长度 / Len returns the length of the backing data.

func (*EndianReader) Pos

func (r *EndianReader) Pos() int64

Pos 返回当前读取偏移 / Pos returns the current read offset.

func (*EndianReader) ReadAlignedString

func (r *EndianReader) ReadAlignedString() (string, error)

ReadAlignedString 读取 Unity 对齐字符串布局 / ReadAlignedString reads Unity's aligned string layout: int32 字节长度、原始字节,然后跳过到 4 字节边界 / int32 byte length, raw bytes, then padding to a 4-byte boundary.

func (*EndianReader) ReadBool

func (r *EndianReader) ReadBool() (bool, error)

ReadBool 将单个字节读取为 bool / ReadBool reads one byte as a boolean.

func (*EndianReader) ReadByte

func (r *EndianReader) ReadByte() (byte, error)

ReadByte 读取单个字节 / ReadByte reads one byte.

func (*EndianReader) ReadBytes

func (r *EndianReader) ReadBytes(n int64) ([]byte, error)

ReadBytes 读取 n 个字节并返回新切片 / ReadBytes reads n bytes into a new slice.

func (*EndianReader) ReadFloat32

func (r *EndianReader) ReadFloat32() (float32, error)

ReadFloat32 按当前字节序读取 float32 / ReadFloat32 reads a float32 using the configured byte order.

func (*EndianReader) ReadFloat64

func (r *EndianReader) ReadFloat64() (float64, error)

ReadFloat64 按当前字节序读取 float64 / ReadFloat64 reads a float64 using the configured byte order.

func (*EndianReader) ReadFull

func (r *EndianReader) ReadFull(buf []byte) error

ReadFull 将 len(buf) 个字节复制到 buf / ReadFull copies len(buf) bytes into buf.

func (*EndianReader) ReadInt8

func (r *EndianReader) ReadInt8() (int8, error)

ReadInt8 读取单字节有符号整数 / ReadInt8 reads one signed byte.

func (*EndianReader) ReadInt16

func (r *EndianReader) ReadInt16() (int16, error)

ReadInt16 按当前字节序读取 int16 / ReadInt16 reads an int16 using the configured byte order.

func (*EndianReader) ReadInt32

func (r *EndianReader) ReadInt32() (int32, error)

ReadInt32 按当前字节序读取 int32 / ReadInt32 reads an int32 using the configured byte order.

func (*EndianReader) ReadInt64

func (r *EndianReader) ReadInt64() (int64, error)

ReadInt64 按当前字节序读取 int64 / ReadInt64 reads an int64 using the configured byte order.

func (*EndianReader) ReadNullString

func (r *EndianReader) ReadNullString() (string, error)

ReadNullString 从当前偏移读取 null 结尾字符串 / ReadNullString reads a null-terminated string from the current offset.

func (*EndianReader) ReadUInt16

func (r *EndianReader) ReadUInt16() (uint16, error)

ReadUInt16 按当前字节序读取 uint16 / ReadUInt16 reads a uint16 using the configured byte order.

func (*EndianReader) ReadUInt32

func (r *EndianReader) ReadUInt32() (uint32, error)

ReadUInt32 按当前字节序读取 uint32 / ReadUInt32 reads a uint32 using the configured byte order.

func (*EndianReader) ReadUInt64

func (r *EndianReader) ReadUInt64() (uint64, error)

ReadUInt64 按当前字节序读取 uint64 / ReadUInt64 reads a uint64 using the configured byte order.

func (*EndianReader) Remaining

func (r *EndianReader) Remaining() int64

Remaining 返回从当前偏移开始仍可读取的字节数 / Remaining returns the number of readable bytes from the current offset.

func (*EndianReader) SetByteOrder

func (r *EndianReader) SetByteOrder(order binary.ByteOrder)

SetByteOrder 修改后续读取使用的数值字节序 / SetByteOrder changes the numeric byte order used by subsequent reads.

func (*EndianReader) Skip

func (r *EndianReader) Skip(n int64)

Skip 前移读取偏移,并在底层数据末尾截断 / Skip advances the read offset and clamps at the end of the backing data.

type EndianWriter

type EndianWriter struct {
	W io.Writer
	// contains filtered or unexported fields
}

EndianWriter 以可配置字节序向底层 writer 写入基础类型 / EndianWriter writes primitive values into an underlying writer with a configurable byte order.

func NewEndianWriter

func NewEndianWriter(w io.Writer, order binary.ByteOrder) *EndianWriter

NewEndianWriter 使用指定字节序创建 EndianWriter / NewEndianWriter creates an EndianWriter using the requested byte order.

func (*EndianWriter) Align

func (w *EndianWriter) Align(alignment int64) error

Align 填充缓冲区到下一个对齐边界 / Align pads the buffer to the next alignment boundary.

func (*EndianWriter) ByteOrder

func (w *EndianWriter) ByteOrder() binary.ByteOrder

ByteOrder 返回当前数值字节序 / ByteOrder returns the current numeric byte order.

func (*EndianWriter) Bytes

func (w *EndianWriter) Bytes() []byte

Bytes 在底层 writer 是 bytes.Buffer 时返回其字节,否则返回 nil / Bytes returns underlying bytes when the writer is a bytes.Buffer, otherwise nil.

func (*EndianWriter) Len

func (w *EndianWriter) Len() int64

Len 返回当前已写入偏移 / Len returns the current written offset.

func (*EndianWriter) SetByteOrder

func (w *EndianWriter) SetByteOrder(order binary.ByteOrder)

SetByteOrder 修改后续写入使用的数值字节序 / SetByteOrder changes the numeric byte order used by subsequent writes.

func (*EndianWriter) WriteAlignedString

func (w *EndianWriter) WriteAlignedString(value string) error

WriteAlignedString 写入 Unity 对齐字符串布局 / WriteAlignedString writes Unity's aligned string layout: int32 字节长度、原始字节,然后填充到 4 字节边界 / int32 byte length, raw bytes, then padding to a 4-byte boundary.

func (*EndianWriter) WriteBool

func (w *EndianWriter) WriteBool(value bool) error

WriteBool 将 bool 写为单字节 / WriteBool writes a bool as one byte.

func (*EndianWriter) WriteByte

func (w *EndianWriter) WriteByte(value byte) error

WriteByte 写入单个字节 / WriteByte writes one byte.

func (*EndianWriter) WriteBytes

func (w *EndianWriter) WriteBytes(value []byte) error

WriteBytes 写入原始字节 / WriteBytes writes raw bytes.

func (*EndianWriter) WriteFloat32

func (w *EndianWriter) WriteFloat32(value float32) error

WriteFloat32 按当前字节序写入 float32 / WriteFloat32 writes a float32 using the configured byte order.

func (*EndianWriter) WriteFloat64

func (w *EndianWriter) WriteFloat64(value float64) error

WriteFloat64 按当前字节序写入 float64 / WriteFloat64 writes a float64 using the configured byte order.

func (*EndianWriter) WriteInt16

func (w *EndianWriter) WriteInt16(value int16) error

WriteInt16 按当前字节序写入 int16 / WriteInt16 writes an int16 using the configured byte order.

func (*EndianWriter) WriteInt32

func (w *EndianWriter) WriteInt32(value int32) error

WriteInt32 按当前字节序写入 int32 / WriteInt32 writes an int32 using the configured byte order.

func (*EndianWriter) WriteInt64

func (w *EndianWriter) WriteInt64(value int64) error

WriteInt64 按当前字节序写入 int64 / WriteInt64 writes an int64 using the configured byte order.

func (*EndianWriter) WriteNullString

func (w *EndianWriter) WriteNullString(value string) error

WriteNullString 写入字符串原始字节并追加 null 结尾 / WriteNullString writes raw string bytes followed by a null byte.

func (*EndianWriter) WriteUInt16

func (w *EndianWriter) WriteUInt16(value uint16) error

WriteUInt16 按当前字节序写入 uint16 / WriteUInt16 writes a uint16 using the configured byte order.

func (*EndianWriter) WriteUInt32

func (w *EndianWriter) WriteUInt32(value uint32) error

WriteUInt32 按当前字节序写入 uint32 / WriteUInt32 writes a uint32 using the configured byte order.

func (*EndianWriter) WriteUInt64

func (w *EndianWriter) WriteUInt64(value uint64) error

WriteUInt64 按当前字节序写入 uint64 / WriteUInt64 writes a uint64 using the configured byte order.

func (*EndianWriter) WriteZeroes

func (w *EndianWriter) WriteZeroes(count int64) error

WriteZeroes 写入 count 个零字节 / WriteZeroes writes count zero bytes.

type Peeker

type Peeker interface {
	Peek(n int) ([]byte, error)
	io.Reader
}

Peeker 定义了实现了 Peek 方法的接口,bufio.Reader 实现了该接口。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL