class · System.IO

StreamWriter

public class StreamWriter : TextWriter

Text written to a stream as UTF-8, through a buffer.

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

Herencia

Clases base
Any TextWriter StreamWriter

Constructores

new(Stream stream)
#
new(Stream stream, i32 bufferSize)
#
new(str path)
#

A new file (or the file emptied).

new(str path, bool append)
#

A file written from its end when append, else emptied first.

Propiedades

bool AutoFlush { get; set; }
#

Sends the text to the stream after every Write.

Stream BaseStream { get; }
#

read-only

The stream it writes to.

Métodos

override Write(str text)
#

override

override Write(u16 c)
#

override

override Flush()
#

override

override Close()
#

override

Miembros privados e internos (4)

Stream stream
#

private

u8[] buffer
#

private

i32 used
#

private

Drain()
#

private

Writes what is buffered (without flushing the stream).

Miembros heredados (42)

Código fuente

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

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

import System;

into namespace System.IO
{
	/// Text written to a stream as UTF-8, through a buffer.
	public class StreamWriter : TextWriter
	{
		Stream stream;
		u8[] buffer;
		i32 used;

		/// Writes what is buffered (without flushing the stream).
		Drain()
		{
			if (used > 0)
			{
				stream.Write(buffer, 0, used);
				used = 0;
			}
		}

		public:
			new(Stream stream) : (stream, 8192)
			{
			}

			new(Stream stream, i32 bufferSize)
			{
				if (!stream.CanWrite)
					throw new ArgumentError("StreamWriter: the stream cannot be written");
				this.stream = stream;
				buffer = new u8[bufferSize < 16 ? 16 : bufferSize];
			}

			/// A new file (or the file emptied).
			new(str path) : (path, false)
			{
			}

			/// A file written from its end when `append`, else emptied first.
			new(str path, bool append) : (new FileStream(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write))
			{
			}

			override Write(str text)
			{
				u8[] bytes = text.Units();
				i32 count = bytes.Length;
				if (used + count > buffer.Length)
				{
					Drain();
					if (count > buffer.Length)
					{
						stream.Write(bytes, 0, count);
						if (AutoFlush)
							stream.Flush();
						return;
					}
				}
				Array.Copy(bytes, 0, buffer, used, count);
				used += count;
				if (AutoFlush)
					Flush();
			}

			override Write(u16 c)
			{
				if (c < 128)
				{
					if (used == buffer.Length)
						Drain();
					buffer[used] = (u8)c;
					used++;
					if (AutoFlush)
						Flush();
					return;
				}
				Write(Unicode.ToString(c));
			}

			override Flush()
			{
				Drain();
				stream.Flush();
			}

			override Close()
			{
				Drain();
				stream.Close();
			}

			/// Sends the text to the stream after every Write.
			bool AutoFlush { get; set; }

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