class · System.IO

BinaryWriter

public class BinaryWriter
Tipo
class
Espacio de nombres
System.IO
Ensamblado
System.Core
Acceso
public
Archivo fuente
BinaryIO.ant

Herencia

Clases base
Any BinaryWriter

Constructores

new(Stream output)
#

Propiedades

Stream BaseStream { get; }
#

read-only

The stream it writes to.

Métodos

Write(u8 value)
#
Write(i8 value)
#
Write(bool value)
#
Write(i16 value)
#
Write(u16 value)
#
Write(i32 value)
#
Write(u32 value)
#
Write(i64 value)
#
Write(u64 value)
#
Write(f32 value)
#
Write(f64 value)
#
Write(str text)
#

The byte count (7-bit encoded) and the UTF-8 bytes.

Write(u8[] bytes)
#

The bytes, without their count.

Write(u8[] bytes, i32 offset, i32 count)
#
Write7BitEncodedInt(i32 value)
#

7 bits per byte, the high bit set on every byte but the last.

Flush()
#
Close()
#

Miembros privados e internos (4)

Stream stream
#

private

MemoryStream? memory
#

private

The stream when it is a MemoryStream: calls through this type can be direct (the program does not override them).

u8[] scratch
#

private

Put(u64 value, i32 count)
#

private

Miembros heredados (5)

Código fuente

BinaryWriter en Antril, descompilado a partir de su Larva HIR.

System.IO.BinaryWriter.ant
// System.IO.BinaryWriter — System.Core (decompiled from Larva HIR)

import System;

into namespace System.IO
{
	public class BinaryWriter
	{
		Stream stream;

		/// The stream when it is a MemoryStream: calls through this type can be direct (the program does not override them).
		MemoryStream? memory;
		u8[] scratch;

		Put(u64 value, i32 count)
		{
			loop (i32 i = 0..count)
			{
				scratch[i] = (u8)value;
				value = value >> 8;
			}
			if (memory is MemoryStream m)
				m.Write(scratch, 0, count);
			else
				stream.Write(scratch, 0, count);
		}

		public:
			new(Stream output)
			{
				if (!output.CanWrite)
					throw new ArgumentError("BinaryWriter: the stream cannot be written");
				stream = output;
				memory = output is MemoryStream m ? m : null;
				scratch = new u8[16];
			}

			Write(u8 value)
			{
				stream.WriteByte(value);
			}

			Write(i8 value)
			{
				stream.WriteByte((u8)value);
			}

			Write(bool value)
			{
				stream.WriteByte(value ? 1 : 0);
			}

			Write(i16 value)
			{
				Put((u64)value, 2);
			}

			Write(u16 value)
			{
				Put(value, 2);
			}

			Write(i32 value)
			{
				Put((u64)value, 4);
			}

			Write(u32 value)
			{
				Put(value, 4);
			}

			Write(i64 value)
			{
				Put((u64)value, 8);
			}

			Write(u64 value)
			{
				Put(value, 8);
			}

			Write(f32 value)
			{
				Put(Float32.Bits(value), 4);
			}

			Write(f64 value)
			{
				Put((u64)Float64.Bits(value), 8);
			}

			/// The byte count (7-bit encoded) and the UTF-8 bytes.
			Write(str text)
			{
				u8[] bytes = text.Units();
				Write7BitEncodedInt(bytes.Length);
				stream.Write(bytes, 0, bytes.Length);
			}

			/// The bytes, without their count.
			Write(u8[] bytes)
			{
				stream.Write(bytes, 0, bytes.Length);
			}

			Write(u8[] bytes, i32 offset, i32 count)
			{
				stream.Write(bytes, offset, count);
			}

			/// 7 bits per byte, the high bit set on every byte but the last.
			Write7BitEncodedInt(i32 value)
			{
				u32 rest = (u32)value;
				i32 count = 0;
				loop (rest >= 128)
				{
					scratch[count] = (u8)(rest | 128);
					rest = rest >> 7;
					count++;
				}
				scratch[count] = (u8)rest;
				stream.Write(scratch, 0, count + 1);
			}

			Flush()
			{
				stream.Flush();
			}

			Close()
			{
				stream.Close();
			}

			/// The stream it writes to.
			Stream BaseStream
			{
				get => stream;
			}
	}
}
Esc