Files
2026-06-04 11:42:34 +02:00

574 lines
24 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Security;
namespace System.IO
{
/// <summary>Writes primitive types in binary to a stream and supports writing strings in a specific encoding.</summary>
/// <filterpriority>2</filterpriority>
// Token: 0x020001A8 RID: 424
[ComVisible(true)]
[Serializable]
public class BinaryWriter : IDisposable
{
/// <summary>Initializes a new instance of the <see cref="T:System.IO.BinaryWriter" /> class that writes to a stream.</summary>
// Token: 0x060015AA RID: 5546 RVA: 0x00053224 File Offset: 0x00051424
protected BinaryWriter()
: this(Stream.Null, Encoding.UTF8UnmarkedUnsafe)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.BinaryWriter" /> class based on the supplied stream and using UTF-8 as the encoding for strings.</summary>
/// <param name="output">The output stream. </param>
/// <exception cref="T:System.ArgumentException">The stream does not support writing, or the stream is already closed. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="output" /> is null. </exception>
// Token: 0x060015AB RID: 5547 RVA: 0x00053238 File Offset: 0x00051438
public BinaryWriter(Stream output)
: this(output, Encoding.UTF8UnmarkedUnsafe)
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.IO.BinaryWriter" /> class based on the supplied stream and a specific character encoding.</summary>
/// <param name="output">The supplied stream. </param>
/// <param name="encoding">The character encoding. </param>
/// <exception cref="T:System.ArgumentException">The stream does not support writing, or the stream is already closed. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="output" /> or <paramref name="encoding" /> is null. </exception>
// Token: 0x060015AC RID: 5548 RVA: 0x00053248 File Offset: 0x00051448
public BinaryWriter(Stream output, Encoding encoding)
{
if (output == null)
{
throw new ArgumentNullException("output");
}
if (encoding == null)
{
throw new ArgumentNullException("encoding");
}
if (!output.CanWrite)
{
throw new ArgumentException(Locale.GetText("Stream does not support writing or already closed."));
}
this.OutStream = output;
this.m_encoding = encoding;
this.buffer = new byte[16];
}
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.BinaryWriter" /> and optionally releases the managed resources.</summary>
// Token: 0x060015AE RID: 5550 RVA: 0x000532C0 File Offset: 0x000514C0
void IDisposable.Dispose()
{
this.Dispose(true);
}
/// <summary>Gets the underlying stream of the <see cref="T:System.IO.BinaryWriter" />.</summary>
/// <returns>The underlying stream associated with the BinaryWriter.</returns>
/// <filterpriority>1</filterpriority>
// Token: 0x170003D8 RID: 984
// (get) Token: 0x060015AF RID: 5551 RVA: 0x000532CC File Offset: 0x000514CC
public virtual Stream BaseStream
{
get
{
return this.OutStream;
}
}
/// <summary>Closes the current <see cref="T:System.IO.BinaryWriter" /> and the underlying stream.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B0 RID: 5552 RVA: 0x000532D4 File Offset: 0x000514D4
public virtual void Close()
{
this.Dispose(true);
}
/// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.BinaryWriter" /> and optionally releases the managed resources.</summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
// Token: 0x060015B1 RID: 5553 RVA: 0x000532E0 File Offset: 0x000514E0
protected virtual void Dispose(bool disposing)
{
if (disposing && this.OutStream != null)
{
this.OutStream.Close();
}
this.buffer = null;
this.m_encoding = null;
this.disposed = true;
}
/// <summary>Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B2 RID: 5554 RVA: 0x00053314 File Offset: 0x00051514
public virtual void Flush()
{
this.OutStream.Flush();
}
/// <summary>Sets the position within the current stream.</summary>
/// <returns>The position with the current stream.</returns>
/// <param name="offset">A byte offset relative to <paramref name="origin" />. </param>
/// <param name="origin">A field of <see cref="T:System.IO.SeekOrigin" /> indicating the reference point from which the new position is to be obtained. </param>
/// <exception cref="T:System.IO.IOException">The file pointer was moved to an invalid location. </exception>
/// <exception cref="T:System.ArgumentException">The <see cref="T:System.IO.SeekOrigin" /> value is invalid. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B3 RID: 5555 RVA: 0x00053324 File Offset: 0x00051524
public virtual long Seek(int offset, SeekOrigin origin)
{
return this.OutStream.Seek((long)offset, origin);
}
/// <summary>Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.</summary>
/// <param name="value">The Boolean value to write (0 or 1). </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B4 RID: 5556 RVA: 0x00053334 File Offset: 0x00051534
public virtual void Write(bool value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.buffer[0] = ((!value) ? 0 : 1);
this.OutStream.Write(this.buffer, 0, 1);
}
/// <summary>Writes an unsigned byte to the current stream and advances the stream position by one byte.</summary>
/// <param name="value">The unsigned byte to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B5 RID: 5557 RVA: 0x00053388 File Offset: 0x00051588
public virtual void Write(byte value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.OutStream.WriteByte(value);
}
/// <summary>Writes a byte array to the underlying stream.</summary>
/// <param name="buffer">A byte array containing the data to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="buffer" /> is null. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B6 RID: 5558 RVA: 0x000533B4 File Offset: 0x000515B4
public virtual void Write(byte[] buffer)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
this.OutStream.Write(buffer, 0, buffer.Length);
}
/// <summary>Writes a region of a byte array to the current stream.</summary>
/// <param name="buffer">A byte array containing the data to write. </param>
/// <param name="index">The starting point in <paramref name="buffer" /> at which to begin writing. </param>
/// <param name="count">The number of bytes to write. </param>
/// <exception cref="T:System.ArgumentException">The buffer length minus <paramref name="index" /> is less than <paramref name="count" />. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="buffer" /> is null. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="index" /> or <paramref name="count" /> is negative. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B7 RID: 5559 RVA: 0x00053400 File Offset: 0x00051600
public virtual void Write(byte[] buffer, int index, int count)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
this.OutStream.Write(buffer, index, count);
}
/// <summary>Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.</summary>
/// <param name="ch">The non-surrogate, Unicode character to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="ch" /> is a single surrogate character.</exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B8 RID: 5560 RVA: 0x00053448 File Offset: 0x00051648
public virtual void Write(char ch)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
char[] array = new char[] { ch };
byte[] bytes = this.m_encoding.GetBytes(array, 0, 1);
this.OutStream.Write(bytes, 0, bytes.Length);
}
/// <summary>Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.</summary>
/// <param name="chars">A character array containing the data to write. </param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="chars" /> is null. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015B9 RID: 5561 RVA: 0x0005349C File Offset: 0x0005169C
public virtual void Write(char[] chars)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
if (chars == null)
{
throw new ArgumentNullException("chars");
}
byte[] bytes = this.m_encoding.GetBytes(chars, 0, chars.Length);
this.OutStream.Write(bytes, 0, bytes.Length);
}
/// <summary>Writes a section of a character array to the current stream, and advances the current position of the stream in accordance with the Encoding used and perhaps the specific characters being written to the stream.</summary>
/// <param name="chars">A character array containing the data to write. </param>
/// <param name="index">The starting point in <paramref name="chars" /> from which to begin writing. </param>
/// <param name="count">The number of characters to write. </param>
/// <exception cref="T:System.ArgumentException">The buffer length minus <paramref name="index" /> is less than <paramref name="count" />. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="chars" /> is null. </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="index" /> or <paramref name="count" /> is negative. </exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015BA RID: 5562 RVA: 0x000534F8 File Offset: 0x000516F8
public virtual void Write(char[] chars, int index, int count)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
if (chars == null)
{
throw new ArgumentNullException("chars");
}
byte[] bytes = this.m_encoding.GetBytes(chars, index, count);
this.OutStream.Write(bytes, 0, bytes.Length);
}
/// <summary>Writes a decimal value to the current stream and advances the stream position by sixteen bytes.</summary>
/// <param name="value">The decimal value to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015BB RID: 5563 RVA: 0x00053550 File Offset: 0x00051750
public unsafe virtual void Write(decimal value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < 16; i++)
{
if (i < 4)
{
this.buffer[i + 12] = *((ref value) + i);
}
else if (i < 8)
{
this.buffer[i + 4] = *((ref value) + i);
}
else if (i < 12)
{
this.buffer[i - 8] = *((ref value) + i);
}
else
{
this.buffer[i - 8] = *((ref value) + i);
}
}
}
else
{
for (int j = 0; j < 16; j++)
{
if (j < 4)
{
this.buffer[15 - j] = *((ref value) + j);
}
else if (j < 8)
{
this.buffer[15 - j] = *((ref value) + j);
}
else if (j < 12)
{
this.buffer[11 - j] = *((ref value) + j);
}
else
{
this.buffer[19 - j] = *((ref value) + j);
}
}
}
this.OutStream.Write(this.buffer, 0, 16);
}
/// <summary>Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.</summary>
/// <param name="value">The eight-byte floating-point value to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015BC RID: 5564 RVA: 0x00053684 File Offset: 0x00051884
public virtual void Write(double value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
}
/// <summary>Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.</summary>
/// <param name="value">The two-byte signed integer to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015BD RID: 5565 RVA: 0x000536C0 File Offset: 0x000518C0
public virtual void Write(short value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.buffer[0] = (byte)value;
this.buffer[1] = (byte)(value >> 8);
this.OutStream.Write(this.buffer, 0, 2);
}
/// <summary>Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.</summary>
/// <param name="value">The four-byte signed integer to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015BE RID: 5566 RVA: 0x00053714 File Offset: 0x00051914
public virtual void Write(int value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.buffer[0] = (byte)value;
this.buffer[1] = (byte)(value >> 8);
this.buffer[2] = (byte)(value >> 16);
this.buffer[3] = (byte)(value >> 24);
this.OutStream.Write(this.buffer, 0, 4);
}
/// <summary>Writes an eight-byte signed integer to the current stream and advances the stream position by eight bytes.</summary>
/// <param name="value">The eight-byte signed integer to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015BF RID: 5567 RVA: 0x00053780 File Offset: 0x00051980
public virtual void Write(long value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
int i = 0;
int num = 0;
while (i < 8)
{
this.buffer[i] = (byte)(value >> num);
i++;
num += 8;
}
this.OutStream.Write(this.buffer, 0, 8);
}
/// <summary>Writes a signed byte to the current stream and advances the stream position by one byte.</summary>
/// <param name="value">The signed byte to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015C0 RID: 5568 RVA: 0x000537E4 File Offset: 0x000519E4
[CLSCompliant(false)]
public virtual void Write(sbyte value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.buffer[0] = (byte)value;
this.OutStream.Write(this.buffer, 0, 1);
}
/// <summary>Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.</summary>
/// <param name="value">The four-byte floating-point value to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015C1 RID: 5569 RVA: 0x0005382C File Offset: 0x00051A2C
public virtual void Write(float value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
}
/// <summary>Writes a length-prefixed string to this stream in the current encoding of the <see cref="T:System.IO.BinaryWriter" />, and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.</summary>
/// <param name="value">The value to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="value" /> is null. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015C2 RID: 5570 RVA: 0x00053868 File Offset: 0x00051A68
public virtual void Write(string value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
int byteCount = this.m_encoding.GetByteCount(value);
this.Write7BitEncodedInt(byteCount);
if (this.stringBuffer == null)
{
this.stringBuffer = new byte[512];
this.maxCharsPerRound = 512 / this.m_encoding.GetMaxByteCount(1);
}
int num = 0;
int num2;
for (int i = value.Length; i > 0; i -= num2)
{
num2 = ((i <= this.maxCharsPerRound) ? i : this.maxCharsPerRound);
int bytes = this.m_encoding.GetBytes(value, num, num2, this.stringBuffer, 0);
this.OutStream.Write(this.stringBuffer, 0, bytes);
num += num2;
}
}
/// <summary>Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.</summary>
/// <param name="value">The two-byte unsigned integer to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015C3 RID: 5571 RVA: 0x00053938 File Offset: 0x00051B38
[CLSCompliant(false)]
public virtual void Write(ushort value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.buffer[0] = (byte)value;
this.buffer[1] = (byte)(value >> 8);
this.OutStream.Write(this.buffer, 0, 2);
}
/// <summary>Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.</summary>
/// <param name="value">The four-byte unsigned integer to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015C4 RID: 5572 RVA: 0x0005398C File Offset: 0x00051B8C
[CLSCompliant(false)]
public virtual void Write(uint value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
this.buffer[0] = (byte)value;
this.buffer[1] = (byte)(value >> 8);
this.buffer[2] = (byte)(value >> 16);
this.buffer[3] = (byte)(value >> 24);
this.OutStream.Write(this.buffer, 0, 4);
}
/// <summary>Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.</summary>
/// <param name="value">The eight-byte unsigned integer to write. </param>
/// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <filterpriority>1</filterpriority>
// Token: 0x060015C5 RID: 5573 RVA: 0x000539F8 File Offset: 0x00051BF8
[CLSCompliant(false)]
public virtual void Write(ulong value)
{
if (this.disposed)
{
throw new ObjectDisposedException("BinaryWriter", "Cannot write to a closed BinaryWriter");
}
int i = 0;
int num = 0;
while (i < 8)
{
this.buffer[i] = (byte)(value >> num);
i++;
num += 8;
}
this.OutStream.Write(this.buffer, 0, 8);
}
/// <summary>Writes a 32-bit integer in a compressed format.</summary>
/// <param name="value">The 32-bit integer to be written. </param>
/// <exception cref="T:System.IO.EndOfStreamException">The end of the stream is reached. </exception>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed. </exception>
/// <exception cref="T:System.IO.IOException">The stream is closed. </exception>
// Token: 0x060015C6 RID: 5574 RVA: 0x00053A5C File Offset: 0x00051C5C
protected void Write7BitEncodedInt(int value)
{
do
{
int num = (value >> 7) & 33554431;
byte b = (byte)(value & 127);
if (num != 0)
{
b |= 128;
}
this.Write(b);
value = num;
}
while (value != 0);
}
/// <summary>Specifies a <see cref="T:System.IO.BinaryWriter" /> with no backing store.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x0400063B RID: 1595
public static readonly BinaryWriter Null = new BinaryWriter();
/// <summary>Holds the underlying stream.</summary>
// Token: 0x0400063C RID: 1596
protected Stream OutStream;
// Token: 0x0400063D RID: 1597
private Encoding m_encoding;
// Token: 0x0400063E RID: 1598
private byte[] buffer;
// Token: 0x0400063F RID: 1599
private bool disposed;
// Token: 0x04000640 RID: 1600
private byte[] stringBuffer;
// Token: 0x04000641 RID: 1601
private int maxCharsPerRound;
}
}