mscorlib(4.0.0.0) API with additions
BinaryWriter.cs
3 using System.Security;
4 using System.Text;
5 
6 namespace System.IO
7 {
10  [ComVisible(true)]
11  [__DynamicallyInvokable]
12  public class BinaryWriter : IDisposable
13  {
15  [__DynamicallyInvokable]
16  public static readonly BinaryWriter Null = new BinaryWriter();
17 
19  [__DynamicallyInvokable]
20  protected Stream OutStream;
21 
22  private byte[] _buffer;
23 
24  private Encoding _encoding;
25 
26  private Encoder _encoder;
27 
28  [OptionalField]
29  private bool _leaveOpen;
30 
31  [OptionalField]
32  private char[] _tmpOneCharBuffer;
33 
34  private byte[] _largeByteBuffer;
35 
36  private int _maxChars;
37 
38  private const int LargeByteBufferSize = 256;
39 
42  [__DynamicallyInvokable]
43  public virtual Stream BaseStream
44  {
45  [__DynamicallyInvokable]
46  get
47  {
48  Flush();
49  return OutStream;
50  }
51  }
52 
54  [__DynamicallyInvokable]
55  protected BinaryWriter()
56  {
58  _buffer = new byte[16];
59  _encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
60  _encoder = _encoding.GetEncoder();
61  }
62 
68  [__DynamicallyInvokable]
69  public BinaryWriter(Stream output)
70  : this(output, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), leaveOpen: false)
71  {
72  }
73 
80  [__DynamicallyInvokable]
81  public BinaryWriter(Stream output, Encoding encoding)
82  : this(output, encoding, leaveOpen: false)
83  {
84  }
85 
94  [__DynamicallyInvokable]
95  public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
96  {
97  if (output == null)
98  {
99  throw new ArgumentNullException("output");
100  }
101  if (encoding == null)
102  {
103  throw new ArgumentNullException("encoding");
104  }
105  if (!output.CanWrite)
106  {
107  throw new ArgumentException(Environment.GetResourceString("Argument_StreamNotWritable"));
108  }
109  OutStream = output;
110  _buffer = new byte[16];
111  _encoding = encoding;
112  _encoder = _encoding.GetEncoder();
113  _leaveOpen = leaveOpen;
114  }
115 
117  public virtual void Close()
118  {
119  Dispose(disposing: true);
120  }
121 
125  [__DynamicallyInvokable]
126  protected virtual void Dispose(bool disposing)
127  {
128  if (disposing)
129  {
130  if (_leaveOpen)
131  {
132  OutStream.Flush();
133  }
134  else
135  {
136  OutStream.Close();
137  }
138  }
139  }
140 
142  [__DynamicallyInvokable]
143  public void Dispose()
144  {
145  Dispose(disposing: true);
146  }
147 
149  [__DynamicallyInvokable]
150  public virtual void Flush()
151  {
152  OutStream.Flush();
153  }
154 
161  [__DynamicallyInvokable]
162  public virtual long Seek(int offset, SeekOrigin origin)
163  {
164  return OutStream.Seek(offset, origin);
165  }
166 
171  [__DynamicallyInvokable]
172  public virtual void Write(bool value)
173  {
174  _buffer[0] = (byte)(value ? 1 : 0);
175  OutStream.Write(_buffer, 0, 1);
176  }
177 
182  [__DynamicallyInvokable]
183  public virtual void Write(byte value)
184  {
185  OutStream.WriteByte(value);
186  }
187 
192  [CLSCompliant(false)]
193  [__DynamicallyInvokable]
194  public virtual void Write(sbyte value)
195  {
196  OutStream.WriteByte((byte)value);
197  }
198 
205  [__DynamicallyInvokable]
206  public virtual void Write(byte[] buffer)
207  {
208  if (buffer == null)
209  {
210  throw new ArgumentNullException("buffer");
211  }
212  OutStream.Write(buffer, 0, buffer.Length);
213  }
214 
226  [__DynamicallyInvokable]
227  public virtual void Write(byte[] buffer, int index, int count)
228  {
229  OutStream.Write(buffer, index, count);
230  }
231 
238  [SecuritySafeCritical]
239  [__DynamicallyInvokable]
240  public unsafe virtual void Write(char ch)
241  {
242  if (char.IsSurrogate(ch))
243  {
244  throw new ArgumentException(Environment.GetResourceString("Arg_SurrogatesNotAllowedAsSingleChar"));
245  }
246  int num = 0;
247  byte[] buffer = _buffer;
248  fixed (byte* bytes = buffer)
249  {
250  num = _encoder.GetBytes(&ch, 1, bytes, _buffer.Length, flush: true);
251  }
252  OutStream.Write(_buffer, 0, num);
253  }
254 
261  [__DynamicallyInvokable]
262  public virtual void Write(char[] chars)
263  {
264  if (chars == null)
265  {
266  throw new ArgumentNullException("chars");
267  }
268  byte[] bytes = _encoding.GetBytes(chars, 0, chars.Length);
269  OutStream.Write(bytes, 0, bytes.Length);
270  }
271 
283  [__DynamicallyInvokable]
284  public virtual void Write(char[] chars, int index, int count)
285  {
286  byte[] bytes = _encoding.GetBytes(chars, index, count);
287  OutStream.Write(bytes, 0, bytes.Length);
288  }
289 
294  [SecuritySafeCritical]
295  [__DynamicallyInvokable]
296  public unsafe virtual void Write(double value)
297  {
298  ulong num = (ulong)(*(long*)(&value));
299  _buffer[0] = (byte)num;
300  _buffer[1] = (byte)(num >> 8);
301  _buffer[2] = (byte)(num >> 16);
302  _buffer[3] = (byte)(num >> 24);
303  _buffer[4] = (byte)(num >> 32);
304  _buffer[5] = (byte)(num >> 40);
305  _buffer[6] = (byte)(num >> 48);
306  _buffer[7] = (byte)(num >> 56);
307  OutStream.Write(_buffer, 0, 8);
308  }
309 
314  [__DynamicallyInvokable]
315  public virtual void Write(decimal value)
316  {
317  decimal.GetBytes(value, _buffer);
318  OutStream.Write(_buffer, 0, 16);
319  }
320 
325  [__DynamicallyInvokable]
326  public virtual void Write(short value)
327  {
328  _buffer[0] = (byte)value;
329  _buffer[1] = (byte)(value >> 8);
330  OutStream.Write(_buffer, 0, 2);
331  }
332 
337  [CLSCompliant(false)]
338  [__DynamicallyInvokable]
339  public virtual void Write(ushort value)
340  {
341  _buffer[0] = (byte)value;
342  _buffer[1] = (byte)(value >> 8);
343  OutStream.Write(_buffer, 0, 2);
344  }
345 
350  [__DynamicallyInvokable]
351  public virtual void Write(int value)
352  {
353  _buffer[0] = (byte)value;
354  _buffer[1] = (byte)(value >> 8);
355  _buffer[2] = (byte)(value >> 16);
356  _buffer[3] = (byte)(value >> 24);
357  OutStream.Write(_buffer, 0, 4);
358  }
359 
364  [CLSCompliant(false)]
365  [__DynamicallyInvokable]
366  public virtual void Write(uint value)
367  {
368  _buffer[0] = (byte)value;
369  _buffer[1] = (byte)(value >> 8);
370  _buffer[2] = (byte)(value >> 16);
371  _buffer[3] = (byte)(value >> 24);
372  OutStream.Write(_buffer, 0, 4);
373  }
374 
379  [__DynamicallyInvokable]
380  public virtual void Write(long value)
381  {
382  _buffer[0] = (byte)value;
383  _buffer[1] = (byte)(value >> 8);
384  _buffer[2] = (byte)(value >> 16);
385  _buffer[3] = (byte)(value >> 24);
386  _buffer[4] = (byte)(value >> 32);
387  _buffer[5] = (byte)(value >> 40);
388  _buffer[6] = (byte)(value >> 48);
389  _buffer[7] = (byte)(value >> 56);
390  OutStream.Write(_buffer, 0, 8);
391  }
392 
397  [CLSCompliant(false)]
398  [__DynamicallyInvokable]
399  public virtual void Write(ulong value)
400  {
401  _buffer[0] = (byte)value;
402  _buffer[1] = (byte)(value >> 8);
403  _buffer[2] = (byte)(value >> 16);
404  _buffer[3] = (byte)(value >> 24);
405  _buffer[4] = (byte)(value >> 32);
406  _buffer[5] = (byte)(value >> 40);
407  _buffer[6] = (byte)(value >> 48);
408  _buffer[7] = (byte)(value >> 56);
409  OutStream.Write(_buffer, 0, 8);
410  }
411 
416  [SecuritySafeCritical]
417  [__DynamicallyInvokable]
418  public unsafe virtual void Write(float value)
419  {
420  uint num = *(uint*)(&value);
421  _buffer[0] = (byte)num;
422  _buffer[1] = (byte)(num >> 8);
423  _buffer[2] = (byte)(num >> 16);
424  _buffer[3] = (byte)(num >> 24);
425  OutStream.Write(_buffer, 0, 4);
426  }
427 
434  [SecuritySafeCritical]
435  [__DynamicallyInvokable]
436  public unsafe virtual void Write(string value)
437  {
438  if (value == null)
439  {
440  throw new ArgumentNullException("value");
441  }
442  int byteCount = _encoding.GetByteCount(value);
443  Write7BitEncodedInt(byteCount);
444  if (_largeByteBuffer == null)
445  {
446  _largeByteBuffer = new byte[256];
447  _maxChars = _largeByteBuffer.Length / _encoding.GetMaxByteCount(1);
448  }
449  if (byteCount <= _largeByteBuffer.Length)
450  {
451  _encoding.GetBytes(value, 0, value.Length, _largeByteBuffer, 0);
452  OutStream.Write(_largeByteBuffer, 0, byteCount);
453  return;
454  }
455  int num = 0;
456  int num2 = value.Length;
457  while (true)
458  {
459  if (num2 > 0)
460  {
461  int num3 = (num2 > _maxChars) ? _maxChars : num2;
462  if (num < 0 || num3 < 0 || checked(num + num3) > value.Length)
463  {
464  break;
465  }
466  int bytes2;
467  fixed (char* ptr = value)
468  {
469  byte[] largeByteBuffer = _largeByteBuffer;
470  fixed (byte* bytes = largeByteBuffer)
471  {
472  bytes2 = _encoder.GetBytes((char*)checked(unchecked((ulong)ptr) + unchecked((ulong)(UIntPtr)(void*)checked(unchecked((long)num) * 2L))), num3, bytes, _largeByteBuffer.Length, num3 == num2);
473  }
474  }
475  OutStream.Write(_largeByteBuffer, 0, bytes2);
476  num += num3;
477  num2 -= num3;
478  continue;
479  }
480  return;
481  }
482  throw new ArgumentOutOfRangeException("charCount");
483  }
484 
490  [__DynamicallyInvokable]
491  protected void Write7BitEncodedInt(int value)
492  {
493  uint num;
494  for (num = (uint)value; num >= 128; num >>= 7)
495  {
496  Write((byte)(num | 0x80));
497  }
498  Write((byte)num);
499  }
500  }
501 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
A platform-specific type that is used to represent a pointer or a handle.
Definition: UIntPtr.cs:14
virtual void Close()
Closes the current T:System.IO.BinaryWriter and the underlying stream.
void Dispose()
Releases all resources used by the current instance of the T:System.IO.BinaryWriter class.
virtual Stream BaseStream
Gets the underlying stream of the T:System.IO.BinaryWriter.
Definition: BinaryWriter.cs:44
abstract void Write(byte[] buffer, int offset, int count)
When overridden in a derived class, writes a sequence of bytes to the current stream and advances the...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual void Write(long value)
Writes an eight-byte signed integer to the current stream and advances the stream position by eight b...
BinaryWriter()
Initializes a new instance of the T:System.IO.BinaryWriter class that writes to a stream.
Definition: BinaryWriter.cs:55
virtual unsafe void Write(float value)
Writes a four-byte floating-point value to the current stream and advances the stream position by fou...
Converts a set of characters into a sequence of bytes.
Definition: Encoder.cs:11
virtual byte [] GetBytes(char[] chars)
When overridden in a derived class, encodes all the characters in the specified character array into ...
Definition: Encoding.cs:1576
virtual Encoder GetEncoder()
When overridden in a derived class, obtains an encoder that converts a sequence of Unicode characters...
Definition: Encoding.cs:1972
abstract int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush)
When overridden in a derived class, encodes a set of characters from the specified character array an...
virtual void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
virtual void Write(uint value)
Writes a four-byte unsigned integer to the current stream and advances the stream position by four by...
virtual void Write(decimal value)
Writes a decimal value to the current stream and advances the stream position by sixteen bytes.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
virtual unsafe void Write(double value)
Writes an eight-byte floating-point value to the current stream and advances the stream position by e...
virtual unsafe void Write(string value)
Writes a length-prefixed string to this stream in the current encoding of the T:System....
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
abstract bool CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writi...
Definition: Stream.cs:610
void Write7BitEncodedInt(int value)
Writes a 32-bit integer in a compressed format.
virtual void Write(byte[] buffer)
Writes a byte array to the underlying stream.
virtual void WriteByte(byte value)
Writes a byte to the current position in the stream and advances the position within the stream by on...
Definition: Stream.cs:1329
virtual void Write(byte[] buffer, int index, int count)
Writes a region of a byte array to the current stream.
virtual void Write(int value)
Writes a four-byte signed integer to the current stream and advances the stream position by four byte...
virtual unsafe void Write(char ch)
Writes a Unicode character to the current stream and advances the current position of the stream in a...
SeekOrigin
Specifies the position in a stream to use for seeking.
Definition: SeekOrigin.cs:9
virtual long Seek(int offset, SeekOrigin origin)
Sets the position within the current stream.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual void Close()
Closes the current stream and releases any resources (such as sockets and file handles) associated wi...
Definition: Stream.cs:855
static readonly Stream Null
A Stream with no backing store.
Definition: Stream.cs:562
abstract void Flush()
When overridden in a derived class, clears all buffers for this stream and causes any buffered data t...
virtual void Write(ulong value)
Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight...
Represents a UTF-8 encoding of Unicode characters.
Definition: UTF8Encoding.cs:11
virtual void Write(char[] chars, int index, int count)
Writes a section of a character array to the current stream, and advances the current position of the...
virtual void Write(byte value)
Writes an unsigned byte to the current stream and advances the stream position by one byte.
virtual void Write(bool value)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
abstract long Seek(long offset, SeekOrigin origin)
When overridden in a derived class, sets the position within the current stream.
BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
Initializes a new instance of the T:System.IO.BinaryWriter class based on the specified stream and ch...
Definition: BinaryWriter.cs:95
static readonly BinaryWriter Null
Specifies a T:System.IO.BinaryWriter with no backing store.
Definition: BinaryWriter.cs:16
BinaryWriter(Stream output, Encoding encoding)
Initializes a new instance of the T:System.IO.BinaryWriter class based on the specified stream and ch...
Definition: BinaryWriter.cs:81
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.BinaryWriter and optionally releases the man...
virtual void Write(short value)
Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
Specifies that the class can be serialized.
BinaryWriter(Stream output)
Initializes a new instance of the T:System.IO.BinaryWriter class based on the specified stream and us...
Definition: BinaryWriter.cs:69
virtual int GetByteCount(char[] chars)
When overridden in a derived class, calculates the number of bytes produced by encoding all the chara...
Definition: Encoding.cs:1489
Stream OutStream
Holds the underlying stream.
Definition: BinaryWriter.cs:20
virtual void Write(char[] chars)
Writes a character array to the current stream and advances the current position of the stream in acc...
Writes primitive types in binary to a stream and supports writing strings in a specific encoding.
Definition: BinaryWriter.cs:12
virtual void Write(sbyte value)
Writes a signed byte to the current stream and advances the stream position by one byte.
virtual void Write(ushort value)
Writes a two-byte unsigned integer to the current stream and advances the stream position by two byte...
abstract int GetMaxByteCount(int charCount)
When overridden in a derived class, calculates the maximum number of bytes produced by encoding the s...
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16