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 public void Dispose ()
108 protected virtual void Dispose (bool disposing)
110 if (disposing && OutStream != null && !leave_open)
118 public virtual void Flush() {
122 public virtual long Seek(int offset, SeekOrigin origin) {
124 return OutStream.Seek(offset, origin);
127 public virtual void Write(bool value) {
130 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
132 buffer [0] = (byte) (value ? 1 : 0);
133 OutStream.Write(buffer, 0, 1);
136 public virtual void Write(byte value) {
139 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
141 OutStream.WriteByte(value);
144 public virtual void Write(byte[] buffer) {
147 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
150 throw new ArgumentNullException("buffer");
151 OutStream.Write(buffer, 0, buffer.Length);
154 public virtual void Write(byte[] buffer, int index, int count) {
157 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
160 throw new ArgumentNullException("buffer");
161 OutStream.Write(buffer, index, count);
164 public virtual void Write(char ch) {
167 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
169 char[] dec = new char[1];
171 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
172 OutStream.Write(enc, 0, enc.Length);
175 public virtual void Write(char[] chars) {
178 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
181 throw new ArgumentNullException("chars");
182 byte[] enc = m_encoding.GetBytes(chars, 0, chars.Length);
183 OutStream.Write(enc, 0, enc.Length);
186 public virtual void Write(char[] chars, int index, int count) {
189 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
192 throw new ArgumentNullException("chars");
193 byte[] enc = m_encoding.GetBytes(chars, index, count);
194 OutStream.Write(enc, 0, enc.Length);
197 unsafe public virtual void Write(decimal value) {
200 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
202 byte* value_ptr = (byte *)&value;
205 * decimal in stream is lo32, mi32, hi32, ss32
206 * but its internal structure si ss32, hi32, lo32, mi32
209 if (BitConverter.IsLittleEndian) {
210 for (int i = 0; i < 16; i++) {
212 buffer [i + 12] = value_ptr [i];
214 buffer [i + 4] = value_ptr [i];
216 buffer [i - 8] = value_ptr [i];
218 buffer [i - 8] = value_ptr [i];
221 for (int i = 0; i < 16; i++) {
223 buffer [15 - i] = value_ptr [i];
225 buffer [15 - i] = value_ptr [i];
227 buffer [11 - i] = value_ptr [i];
229 buffer [19 - i] = value_ptr [i];
233 OutStream.Write(buffer, 0, 16);
236 public virtual void Write(double value) {
239 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
241 OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
244 public virtual void Write(short value) {
247 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
249 buffer [0] = (byte) value;
250 buffer [1] = (byte) (value >> 8);
251 OutStream.Write(buffer, 0, 2);
254 public virtual void Write(int value) {
257 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
259 buffer [0] = (byte) value;
260 buffer [1] = (byte) (value >> 8);
261 buffer [2] = (byte) (value >> 16);
262 buffer [3] = (byte) (value >> 24);
263 OutStream.Write(buffer, 0, 4);
266 public virtual void Write(long value) {
269 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
271 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
272 buffer [i] = (byte) (value >> sh);
273 OutStream.Write(buffer, 0, 8);
276 [CLSCompliant(false)]
277 public virtual void Write(sbyte value) {
280 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
282 buffer [0] = (byte) value;
283 OutStream.Write(buffer, 0, 1);
286 public virtual void Write(float value) {
289 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
291 OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
294 public virtual void Write(string value) {
297 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
299 int len = m_encoding.GetByteCount (value);
300 Write7BitEncodedInt (len);
302 if (stringBuffer == null) {
303 stringBuffer = new byte [512];
304 maxCharsPerRound = 512 / m_encoding.GetMaxByteCount (1);
308 int chrem = value.Length;
310 int cch = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
311 int blen = m_encoding.GetBytes (value, chpos, cch, stringBuffer, 0);
312 OutStream.Write (stringBuffer, 0, blen);
319 [CLSCompliant(false)]
320 public virtual void Write(ushort value) {
323 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
325 buffer [0] = (byte) value;
326 buffer [1] = (byte) (value >> 8);
327 OutStream.Write(buffer, 0, 2);
330 [CLSCompliant(false)]
331 public virtual void Write(uint value) {
334 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
336 buffer [0] = (byte) value;
337 buffer [1] = (byte) (value >> 8);
338 buffer [2] = (byte) (value >> 16);
339 buffer [3] = (byte) (value >> 24);
340 OutStream.Write(buffer, 0, 4);
343 [CLSCompliant(false)]
344 public virtual void Write(ulong value) {
347 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
349 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
350 buffer [i] = (byte) (value >> sh);
351 OutStream.Write(buffer, 0, 8);
354 protected void Write7BitEncodedInt(int value) {
356 int high = (value >> 7) & 0x01ffffff;
357 byte b = (byte)(value & 0x7f);
360 b = (byte)(b | 0x80);