2 // System.IO.BinaryWriter
5 // Matt Kimball (matt@kimball.net)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Globalization;
38 public class BinaryWriter : IDisposable {
40 // Null is a BinaryWriter with no backing store.
41 public static readonly BinaryWriter Null = new BinaryWriter ();
43 protected Stream OutStream;
44 private Encoding m_encoding;
45 private byte [] buffer;
46 private bool disposed = false;
48 protected BinaryWriter() : this (Stream.Null, Encoding.UTF8Unmarked) {
51 public BinaryWriter(Stream output) : this(output, Encoding.UTF8Unmarked) {
54 public BinaryWriter(Stream output, Encoding encoding) {
55 if (output == null || encoding == null)
56 throw new ArgumentNullException(Locale.GetText ("Output or Encoding is a null reference."));
58 throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
61 m_encoding = encoding;
62 buffer = new byte [16];
65 public virtual Stream BaseStream {
71 public virtual void Close() {
75 void IDisposable.Dispose() {
79 protected virtual void Dispose (bool disposing)
81 if (disposing && OutStream != null)
89 public virtual void Flush() {
93 public virtual long Seek(int offset, SeekOrigin origin) {
95 return OutStream.Seek(offset, origin);
98 public virtual void Write(bool value) {
101 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
103 buffer [0] = (byte) (value ? 1 : 0);
104 OutStream.Write(buffer, 0, 1);
107 public virtual void Write(byte value) {
110 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
112 OutStream.WriteByte(value);
115 public virtual void Write(byte[] value) {
118 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
121 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
122 OutStream.Write(value, 0, value.Length);
125 public virtual void Write(byte[] value, int offset, int length) {
128 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
131 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
132 OutStream.Write(value, offset, length);
135 public virtual void Write(char value) {
138 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
140 char[] dec = new char[1];
142 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
143 OutStream.Write(enc, 0, enc.Length);
146 public virtual void Write(char[] value) {
149 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
152 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
153 byte[] enc = m_encoding.GetBytes(value, 0, value.Length);
154 OutStream.Write(enc, 0, enc.Length);
157 public virtual void Write(char[] value, int offset, int length) {
160 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
163 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
164 byte[] enc = m_encoding.GetBytes(value, offset, length);
165 OutStream.Write(enc, 0, enc.Length);
168 unsafe public virtual void Write(decimal value) {
171 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
173 byte* value_ptr = (byte *)&value;
176 * decimal in stream is lo32, mi32, hi32, ss32
177 * but its internal structure si ss32, hi32, lo32, mi32
180 if (BitConverter.IsLittleEndian) {
181 for (int i = 0; i < 16; i++) {
183 buffer [i + 12] = value_ptr [i];
185 buffer [i + 4] = value_ptr [i];
187 buffer [i - 8] = value_ptr [i];
189 buffer [i - 8] = value_ptr [i];
192 for (int i = 0; i < 16; i++) {
194 buffer [15 - i] = value_ptr [i];
196 buffer [15 - i] = value_ptr [i];
198 buffer [11 - i] = value_ptr [i];
200 buffer [19 - i] = value_ptr [i];
204 OutStream.Write(buffer, 0, 16);
207 public virtual void Write(double value) {
210 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
212 OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
215 public virtual void Write(short value) {
218 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
220 buffer [0] = (byte) value;
221 buffer [1] = (byte) (value >> 8);
222 OutStream.Write(buffer, 0, 2);
225 public virtual void Write(int value) {
228 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
230 buffer [0] = (byte) value;
231 buffer [1] = (byte) (value >> 8);
232 buffer [2] = (byte) (value >> 16);
233 buffer [3] = (byte) (value >> 24);
234 OutStream.Write(buffer, 0, 4);
237 public virtual void Write(long value) {
240 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
242 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
243 buffer [i] = (byte) (value >> sh);
244 OutStream.Write(buffer, 0, 8);
247 [CLSCompliant(false)]
248 public virtual void Write(sbyte value) {
251 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
253 buffer [0] = (byte) value;
254 OutStream.Write(buffer, 0, 1);
257 public virtual void Write(float value) {
260 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
262 OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
265 byte [] stringBuffer;
266 int maxCharsPerRound;
268 public virtual void Write(string value) {
271 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
273 int len = m_encoding.GetByteCount (value);
274 Write7BitEncodedInt (len);
276 if (stringBuffer == null) {
277 stringBuffer = new byte [512];
278 maxCharsPerRound = 512 / m_encoding.GetMaxByteCount (1);
282 int chrem = value.Length;
284 int cch = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
285 int blen = m_encoding.GetBytes (value, chpos, cch, stringBuffer, 0);
286 OutStream.Write (stringBuffer, 0, blen);
293 [CLSCompliant(false)]
294 public virtual void Write(ushort value) {
297 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
299 buffer [0] = (byte) value;
300 buffer [1] = (byte) (value >> 8);
301 OutStream.Write(buffer, 0, 2);
304 [CLSCompliant(false)]
305 public virtual void Write(uint value) {
308 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
310 buffer [0] = (byte) value;
311 buffer [1] = (byte) (value >> 8);
312 buffer [2] = (byte) (value >> 16);
313 buffer [3] = (byte) (value >> 24);
314 OutStream.Write(buffer, 0, 4);
317 [CLSCompliant(false)]
318 public virtual void Write(ulong value) {
321 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
323 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
324 buffer [i] = (byte) (value >> sh);
325 OutStream.Write(buffer, 0, 8);
328 protected void Write7BitEncodedInt(int value) {
330 int high = (value >> 7) & 0x01ffffff;
331 byte b = (byte)(value & 0x7f);
334 b = (byte)(b | 0x80);