class · System.IO
BinaryReader
public class BinaryReader
- Tipo
- class
- Espacio de nombres
- System.IO
- Ensamblado
- System.Core
- Acceso
- public
- Archivo fuente
BinaryIO.ant
Herencia
- Clases base
-
AnyBinaryReader
Constructores
Propiedades
Métodos
ReadString() : str
#
What Write(str) wrote: a 7-bit encoded byte count and UTF-8 bytes.
Up to count bytes (fewer only at the end of the stream).
Close()
#
Miembros privados e internos (5)
Miembros heredados (5)
GetTypemétodo de AnyUndescribedmétodo de AnyToStringmétodo de AnyHashmétodo de Anydestroymétodo de Any
Código fuente
BinaryReader en Antril, descompilado a partir de su Larva HIR.
// System.IO.BinaryReader — System.Core (decompiled from Larva HIR)
import System;
import System.Text;
into namespace System.IO
{
public class BinaryReader
{
Stream stream;
/// The stream when it is a MemoryStream (see BinaryWriter).
MemoryStream? memory;
u8[] scratch;
/// The rest of a value the stream gave only in part (the rare path, kept out of Take so Take inlines).
[NoInline]
Rest(i32 got, i32 count)
{
stream.ReadExactly(scratch, got, count - got);
}
Take(i32 count) : u64
{
i32 got = memory is MemoryStream m ? m.Read(scratch, 0, count) : stream.Read(scratch, 0, count);
if (got != count)
Rest(got, count);
u64 value = 0;
i32 i = count - 1;
loop (i >= 0)
{
value = value << 8 | (u64)scratch[i];
i--;
}
return value;
}
public:
new(Stream input)
{
if (!input.CanRead)
throw new ArgumentError("BinaryReader: the stream cannot be read");
stream = input;
memory = input is MemoryStream m ? m : null;
scratch = new u8[16];
}
ReadByte() : u8
{
i32 b = stream.ReadByte();
if (b < 0)
throw new EndOfStreamError();
return (u8)b;
}
ReadSByte() : i8 => (i8)ReadByte();
ReadBoolean() : bool => ReadByte() != 0;
ReadInt16() : i16 => (i16)Take(2);
ReadUInt16() : u16 => (u16)Take(2);
ReadInt32() : i32 => (i32)Take(4);
ReadUInt32() : u32 => (u32)Take(4);
ReadInt64() : i64 => (i64)Take(8);
ReadUInt64() : u64 => Take(8);
ReadFloat32() : f32 => Float32.FromBits((u32)Take(4));
ReadFloat64() : f64 => Float64.FromBits((i64)Take(8));
/// What Write(str) wrote: a 7-bit encoded byte count and UTF-8 bytes.
ReadString() : str
{
i32 count = Read7BitEncodedInt();
if (count < 0)
throw new IOError("BinaryReader.ReadString: a negative length");
u8[] bytes = new u8[count];
stream.ReadExactly(bytes, 0, count);
return Encoding.UTF8.GetString(bytes);
}
/// Up to `count` bytes (fewer only at the end of the stream).
ReadBytes(i32 count) : u8[]
{
u8[] bytes = new u8[count];
i32 got = 0;
loop (got < count)
{
i32 read = stream.Read(bytes, got, count - got);
if (read == 0)
return Array.Resize(bytes, got);
got += read;
}
return bytes;
}
Read7BitEncodedInt() : i32
{
u32 value = 0;
i32 shift = 0;
loop (shift < 35)
{
u32 b = ReadByte();
value = value | (b & 127) << (u32)shift;
if (b < 128)
return (i32)value;
shift += 7;
}
throw new IOError("BinaryReader: a 7-bit encoded number is too long");
}
Close()
{
stream.Close();
}
/// The stream it reads from.
Stream BaseStream
{
get => stream;
}
}
}