using System;
using System.Runtime.InteropServices;
using System.Text;
using Mono.Security;
namespace System.IO
{
/// Writes primitive types in binary to a stream and supports writing strings in a specific encoding.
/// 2
// Token: 0x020001A8 RID: 424
[ComVisible(true)]
[Serializable]
public class BinaryWriter : IDisposable
{
/// Initializes a new instance of the class that writes to a stream.
// Token: 0x060015AA RID: 5546 RVA: 0x00053224 File Offset: 0x00051424
protected BinaryWriter()
: this(Stream.Null, Encoding.UTF8UnmarkedUnsafe)
{
}
/// Initializes a new instance of the class based on the supplied stream and using UTF-8 as the encoding for strings.
/// The output stream.
/// The stream does not support writing, or the stream is already closed.
///
/// is null.
// Token: 0x060015AB RID: 5547 RVA: 0x00053238 File Offset: 0x00051438
public BinaryWriter(Stream output)
: this(output, Encoding.UTF8UnmarkedUnsafe)
{
}
/// Initializes a new instance of the class based on the supplied stream and a specific character encoding.
/// The supplied stream.
/// The character encoding.
/// The stream does not support writing, or the stream is already closed.
///
/// or is null.
// 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];
}
/// Releases the unmanaged resources used by the and optionally releases the managed resources.
// Token: 0x060015AE RID: 5550 RVA: 0x000532C0 File Offset: 0x000514C0
void IDisposable.Dispose()
{
this.Dispose(true);
}
/// Gets the underlying stream of the .
/// The underlying stream associated with the BinaryWriter.
/// 1
// Token: 0x170003D8 RID: 984
// (get) Token: 0x060015AF RID: 5551 RVA: 0x000532CC File Offset: 0x000514CC
public virtual Stream BaseStream
{
get
{
return this.OutStream;
}
}
/// Closes the current and the underlying stream.
/// 1
// Token: 0x060015B0 RID: 5552 RVA: 0x000532D4 File Offset: 0x000514D4
public virtual void Close()
{
this.Dispose(true);
}
/// Releases the unmanaged resources used by the and optionally releases the managed resources.
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
// 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;
}
/// Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
/// 1
// Token: 0x060015B2 RID: 5554 RVA: 0x00053314 File Offset: 0x00051514
public virtual void Flush()
{
this.OutStream.Flush();
}
/// Sets the position within the current stream.
/// The position with the current stream.
/// A byte offset relative to .
/// A field of indicating the reference point from which the new position is to be obtained.
/// The file pointer was moved to an invalid location.
/// The value is invalid.
/// 1
// Token: 0x060015B3 RID: 5555 RVA: 0x00053324 File Offset: 0x00051524
public virtual long Seek(int offset, SeekOrigin origin)
{
return this.OutStream.Seek((long)offset, origin);
}
/// Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.
/// The Boolean value to write (0 or 1).
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes an unsigned byte to the current stream and advances the stream position by one byte.
/// The unsigned byte to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a byte array to the underlying stream.
/// A byte array containing the data to write.
/// An I/O error occurs.
/// The stream is closed.
///
/// is null.
/// 1
// 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);
}
/// Writes a region of a byte array to the current stream.
/// A byte array containing the data to write.
/// The starting point in at which to begin writing.
/// The number of bytes to write.
/// The buffer length minus is less than .
///
/// is null.
///
/// or is negative.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// 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.
/// The non-surrogate, Unicode character to write.
/// An I/O error occurs.
/// The stream is closed.
///
/// is a single surrogate character.
/// 1
// 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);
}
/// 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.
/// A character array containing the data to write.
///
/// is null.
/// The stream is closed.
/// An I/O error occurs.
/// 1
// 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);
}
/// 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.
/// A character array containing the data to write.
/// The starting point in from which to begin writing.
/// The number of characters to write.
/// The buffer length minus is less than .
///
/// is null.
///
/// or is negative.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a decimal value to the current stream and advances the stream position by sixteen bytes.
/// The decimal value to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.
/// The eight-byte floating-point value to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
/// The two-byte signed integer to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.
/// The four-byte signed integer to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes an eight-byte signed integer to the current stream and advances the stream position by eight bytes.
/// The eight-byte signed integer to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a signed byte to the current stream and advances the stream position by one byte.
/// The signed byte to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.
/// The four-byte floating-point value to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a length-prefixed string to this stream in the current encoding of the , and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.
/// The value to write.
/// An I/O error occurs.
///
/// is null.
/// The stream is closed.
/// 1
// 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;
}
}
/// Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.
/// The two-byte unsigned integer to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.
/// The four-byte unsigned integer to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.
/// The eight-byte unsigned integer to write.
/// An I/O error occurs.
/// The stream is closed.
/// 1
// 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);
}
/// Writes a 32-bit integer in a compressed format.
/// The 32-bit integer to be written.
/// The end of the stream is reached.
/// The stream is closed.
/// The stream is closed.
// 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);
}
/// Specifies a with no backing store.
/// 1
// Token: 0x0400063B RID: 1595
public static readonly BinaryWriter Null = new BinaryWriter();
/// Holds the underlying stream.
// 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;
}
}