2 // System.IO.BinaryWriter
5 // Matt Kimball (matt@kimball.net)
10 using System.Globalization;
14 public class BinaryWriter : IDisposable {
16 // Null is a BinaryWriter with no backing store.
17 public static readonly BinaryWriter Null;
19 protected Stream OutStream;
20 private Encoding m_encoding;
21 private byte [] buffer;
23 static BinaryWriter() {
24 Null = new BinaryWriter();
27 protected BinaryWriter() : this (Stream.Null, Encoding.UTF8) {
30 public BinaryWriter(Stream output) : this(output, Encoding.UTF8) {
33 public BinaryWriter(Stream output, Encoding encoding) {
34 if (output == null || encoding == null)
35 throw new ArgumentNullException(Locale.GetText ("Output or Encoding is a null reference."));
37 throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
40 m_encoding = encoding;
41 buffer = new byte [16];
44 public virtual Stream BaseStream {
50 public virtual void Close() {
54 public virtual void Dispose() {
59 public virtual void Flush() {
63 public virtual long Seek(int offset, SeekOrigin origin) {
64 return OutStream.Seek(offset, origin);
67 public virtual void Write(bool value) {
68 buffer [0] = (byte) (value ? 1 : 0);
69 OutStream.Write(buffer, 0, 1);
72 public virtual void Write(byte value) {
73 OutStream.WriteByte(value);
76 public virtual void Write(byte[] value) {
78 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
79 OutStream.Write(value, 0, value.Length);
82 public virtual void Write(byte[] value, int offset, int length) {
84 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
85 OutStream.Write(value, offset, length);
88 public virtual void Write(char value) {
89 char[] dec = new char[1];
91 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
92 OutStream.Write(enc, 0, enc.Length);
95 public virtual void Write(char[] value) {
97 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
98 byte[] enc = m_encoding.GetBytes(value, 0, value.Length);
99 OutStream.Write(enc, 0, enc.Length);
102 public virtual void Write(char[] value, int offset, int length) {
104 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
105 byte[] enc = m_encoding.GetBytes(value, offset, length);
106 OutStream.Write(enc, 0, enc.Length);
109 unsafe public virtual void Write(decimal value) {
110 byte* value_ptr = (byte *)&value;
111 for (int i = 0; i < 16; i++) {
112 buffer [i] = value_ptr [i];
115 OutStream.Write(buffer, 0, 16);
118 public virtual void Write(double value) {
119 OutStream.Write(BitConverter.GetBytes(value), 0, 8);
122 public virtual void Write(short value) {
123 buffer [0] = (byte) value;
124 buffer [1] = (byte) (value >> 8);
125 OutStream.Write(buffer, 0, 2);
128 public virtual void Write(int value) {
129 buffer [0] = (byte) value;
130 buffer [1] = (byte) (value >> 8);
131 buffer [2] = (byte) (value >> 16);
132 buffer [3] = (byte) (value >> 24);
133 OutStream.Write(buffer, 0, 4);
136 public virtual void Write(long value) {
137 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
138 buffer [i] = (byte) (value >> sh);
139 OutStream.Write(buffer, 0, 8);
142 [CLSCompliant(false)]
143 public virtual void Write(sbyte value) {
144 buffer [0] = (byte) value;
145 OutStream.Write(buffer, 0, 1);
148 public virtual void Write(float value) {
149 OutStream.Write(BitConverter.GetBytes(value), 0, 4);
152 public virtual void Write(string value) {
153 Write7BitEncodedInt(value.Length);
154 byte[] enc = m_encoding.GetBytes(value);
155 OutStream.Write(enc, 0, enc.Length);
158 [CLSCompliant(false)]
159 public virtual void Write(ushort value) {
160 buffer [0] = (byte) value;
161 buffer [1] = (byte) (value >> 8);
162 OutStream.Write(buffer, 0, 2);
165 [CLSCompliant(false)]
166 public virtual void Write(uint value) {
167 buffer [0] = (byte) value;
168 buffer [1] = (byte) (value >> 8);
169 buffer [2] = (byte) (value >> 16);
170 buffer [3] = (byte) (value >> 24);
171 OutStream.Write(buffer, 0, 4);
174 [CLSCompliant(false)]
175 public virtual void Write(ulong value) {
176 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
177 buffer [i] = (byte) (value >> sh);
178 OutStream.Write(buffer, 0, 8);
181 protected void Write7BitEncodedInt(int value) {
183 int high = (value >> 7) & 0x01ffffff;
184 byte b = (byte)(value & 0x7f);
187 b = (byte)(b | 0x80);