class · System.IO

BinaryReader

public class BinaryReader
Kind
class
Namespace
System.IO
Assembly
System.Core
Access
public
Source file
BinaryIO.ant

Inheritance

Base classes
Any BinaryReader

Constructors

new(Stream input)
#

Properties

Stream BaseStream { get; }
#

read-only

The stream it reads from.

Methods

ReadByte() : u8
#
ReadSByte() : i8
#
ReadBoolean() : bool
#
ReadInt16() : i16
#
ReadUInt16() : u16
#
ReadInt32() : i32
#
ReadUInt32() : u32
#
ReadInt64() : i64
#
ReadUInt64() : u64
#
ReadFloat32() : f32
#
ReadFloat64() : f64
#
ReadString() : str
#

What Write(str) wrote: a 7-bit encoded byte count and UTF-8 bytes.

ReadBytes(i32 count) : u8[]
#

Up to count bytes (fewer only at the end of the stream).

Read7BitEncodedInt() : i32
#
Close()
#

Private and internal members (5)

Stream stream
#

private

MemoryStream? memory
#

private

The stream when it is a MemoryStream (see BinaryWriter).

u8[] scratch
#

private

Rest(i32 got, i32 count)
#

private

The rest of a value the stream gave only in part (the rare path, kept out of Take so Take inlines).

Take(i32 count) : u64
#

private

Inherited members (5)

Source

BinaryReader as Antril, decompiled from its Larva HIR.

System.IO.BinaryReader.ant
// 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;
			}
	}
}
Esc