2 // System.IO.BinaryWriter
5 // Matt Kimball (matt@kimball.net)
6 // Marek Safar (marek.safar@gmail.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Copyright 2011 Xamarin Inc.
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
37 using System.Runtime.InteropServices;
42 public class BinaryWriter : IDisposable {
44 // Null is a BinaryWriter with no backing store.
45 public static readonly BinaryWriter Null = new BinaryWriter ();
47 protected Stream OutStream;
48 private Encoding m_encoding;
49 private byte [] buffer;
54 protected BinaryWriter() : this (Stream.Null, Encoding.UTF8UnmarkedUnsafe)
58 public BinaryWriter(Stream output) : this(output, Encoding.UTF8UnmarkedUnsafe)
63 readonly bool leave_open;
65 public BinaryWriter(Stream output, Encoding encoding)
66 : this (output, encoding, false)
70 public BinaryWriter(Stream output, Encoding encoding, bool leaveOpen)
72 const bool leave_open = false;
74 public BinaryWriter(Stream output, Encoding encoding)
78 throw new ArgumentNullException("output");
80 throw new ArgumentNullException("encoding");
82 throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
85 leave_open = leaveOpen;
88 m_encoding = encoding;
89 buffer = new byte [16];
92 public virtual Stream BaseStream {
99 public virtual void Close() {
103 #if NET_4_0 || NET_2_1
104 public void Dispose ()
106 void IDisposable.Dispose()
112 protected virtual void Dispose (bool disposing)
114 if (disposing && OutStream != null && !leave_open)
122 public virtual void Flush() {
126 public virtual long Seek(int offset, SeekOrigin origin) {
128 return OutStream.Seek(offset, origin);
131 public virtual void Write(bool value) {
134 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
136 buffer [0] = (byte) (value ? 1 : 0);
137 OutStream.Write(buffer, 0, 1);
140 public virtual void Write(byte value) {
143 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
145 OutStream.WriteByte(value);
148 public virtual void Write(byte[] buffer) {
151 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
154 throw new ArgumentNullException("buffer");
155 OutStream.Write(buffer, 0, buffer.Length);
158 public virtual void Write(byte[] buffer, int index, int count) {
161 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
164 throw new ArgumentNullException("buffer");
165 OutStream.Write(buffer, index, count);
168 public virtual void Write(char ch) {
171 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
173 char[] dec = new char[1];
175 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
176 OutStream.Write(enc, 0, enc.Length);
179 public virtual void Write(char[] chars) {
182 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
185 throw new ArgumentNullException("chars");
186 byte[] enc = m_encoding.GetBytes(chars, 0, chars.Length);
187 OutStream.Write(enc, 0, enc.Length);
190 public virtual void Write(char[] chars, int index, int count) {
193 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
196 throw new ArgumentNullException("chars");
197 byte[] enc = m_encoding.GetBytes(chars, index, count);
198 OutStream.Write(enc, 0, enc.Length);
201 unsafe public virtual void Write(decimal value) {
204 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
206 byte* value_ptr = (byte *)&value;
209 * decimal in stream is lo32, mi32, hi32, ss32
210 * but its internal structure si ss32, hi32, lo32, mi32
213 if (BitConverter.IsLittleEndian) {
214 for (int i = 0; i < 16; i++) {
216 buffer [i + 12] = value_ptr [i];
218 buffer [i + 4] = value_ptr [i];
220 buffer [i - 8] = value_ptr [i];
222 buffer [i - 8] = value_ptr [i];
225 for (int i = 0; i < 16; i++) {
227 buffer [15 - i] = value_ptr [i];
229 buffer [15 - i] = value_ptr [i];
231 buffer [11 - i] = value_ptr [i];
233 buffer [19 - i] = value_ptr [i];
237 OutStream.Write(buffer, 0, 16);
240 public virtual void Write(double value) {
243 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
245 OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
248 public virtual void Write(short value) {
251 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
253 buffer [0] = (byte) value;
254 buffer [1] = (byte) (value >> 8);
255 OutStream.Write(buffer, 0, 2);
258 public virtual void Write(int value) {
261 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
263 buffer [0] = (byte) value;
264 buffer [1] = (byte) (value >> 8);
265 buffer [2] = (byte) (value >> 16);
266 buffer [3] = (byte) (value >> 24);
267 OutStream.Write(buffer, 0, 4);
270 public virtual void Write(long value) {
273 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
275 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
276 buffer [i] = (byte) (value >> sh);
277 OutStream.Write(buffer, 0, 8);
280 [CLSCompliant(false)]
281 public virtual void Write(sbyte value) {
284 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
286 buffer [0] = (byte) value;
287 OutStream.Write(buffer, 0, 1);
290 public virtual void Write(float value) {
293 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
295 OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
298 public virtual void Write(string value) {
301 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
303 int len = m_encoding.GetByteCount (value);
304 Write7BitEncodedInt (len);
306 if (stringBuffer == null) {
307 stringBuffer = new byte [512];
308 maxCharsPerRound = 512 / m_encoding.GetMaxByteCount (1);
312 int chrem = value.Length;
314 int cch = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
315 int blen = m_encoding.GetBytes (value, chpos, cch, stringBuffer, 0);
316 OutStream.Write (stringBuffer, 0, blen);
323 [CLSCompliant(false)]
324 public virtual void Write(ushort value) {
327 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
329 buffer [0] = (byte) value;
330 buffer [1] = (byte) (value >> 8);
331 OutStream.Write(buffer, 0, 2);
334 [CLSCompliant(false)]
335 public virtual void Write(uint value) {
338 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
340 buffer [0] = (byte) value;
341 buffer [1] = (byte) (value >> 8);
342 buffer [2] = (byte) (value >> 16);
343 buffer [3] = (byte) (value >> 24);
344 OutStream.Write(buffer, 0, 4);
347 [CLSCompliant(false)]
348 public virtual void Write(ulong value) {
351 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
353 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
354 buffer [i] = (byte) (value >> sh);
355 OutStream.Write(buffer, 0, 8);
358 protected void Write7BitEncodedInt(int value) {
360 int high = (value >> 7) & 0x01ffffff;
361 byte b = (byte)(value & 0x7f);
364 b = (byte)(b | 0x80);