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;
35 using System.Runtime.InteropServices;
40 public class BinaryWriter : IDisposable {
42 // Null is a BinaryWriter with no backing store.
43 public static readonly BinaryWriter Null = new BinaryWriter ();
45 protected Stream OutStream;
46 private Encoding m_encoding;
47 private byte [] buffer;
48 private bool disposed = false;
50 protected BinaryWriter() : this (Stream.Null, Encoding.UTF8UnmarkedUnsafe) {
53 public BinaryWriter(Stream output) : this(output, Encoding.UTF8UnmarkedUnsafe) {
56 public BinaryWriter(Stream output, Encoding encoding) {
58 throw new ArgumentNullException("output");
60 throw new ArgumentNullException("encoding");
62 throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
65 m_encoding = encoding;
66 buffer = new byte [16];
69 public virtual Stream BaseStream {
75 public virtual void Close() {
80 public void Dispose ()
82 void IDisposable.Dispose()
88 protected virtual void Dispose (bool disposing)
90 if (disposing && OutStream != null)
98 public virtual void Flush() {
102 public virtual long Seek(int offset, SeekOrigin origin) {
104 return OutStream.Seek(offset, origin);
107 public virtual void Write(bool value) {
110 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
112 buffer [0] = (byte) (value ? 1 : 0);
113 OutStream.Write(buffer, 0, 1);
116 public virtual void Write(byte value) {
119 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
121 OutStream.WriteByte(value);
124 public virtual void Write(byte[] buffer) {
127 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
130 throw new ArgumentNullException("buffer");
131 OutStream.Write(buffer, 0, buffer.Length);
134 public virtual void Write(byte[] buffer, int index, int count) {
137 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
140 throw new ArgumentNullException("buffer");
141 OutStream.Write(buffer, index, count);
144 public virtual void Write(char ch) {
147 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
149 char[] dec = new char[1];
151 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
152 OutStream.Write(enc, 0, enc.Length);
155 public virtual void Write(char[] chars) {
158 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
161 throw new ArgumentNullException("chars");
162 byte[] enc = m_encoding.GetBytes(chars, 0, chars.Length);
163 OutStream.Write(enc, 0, enc.Length);
166 public virtual void Write(char[] chars, int index, int count) {
169 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
172 throw new ArgumentNullException("chars");
173 byte[] enc = m_encoding.GetBytes(chars, index, count);
174 OutStream.Write(enc, 0, enc.Length);
177 unsafe public virtual void Write(decimal value) {
180 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
182 byte* value_ptr = (byte *)&value;
185 * decimal in stream is lo32, mi32, hi32, ss32
186 * but its internal structure si ss32, hi32, lo32, mi32
189 if (BitConverter.IsLittleEndian) {
190 for (int i = 0; i < 16; i++) {
192 buffer [i + 12] = value_ptr [i];
194 buffer [i + 4] = value_ptr [i];
196 buffer [i - 8] = value_ptr [i];
198 buffer [i - 8] = value_ptr [i];
201 for (int i = 0; i < 16; i++) {
203 buffer [15 - i] = value_ptr [i];
205 buffer [15 - i] = value_ptr [i];
207 buffer [11 - i] = value_ptr [i];
209 buffer [19 - i] = value_ptr [i];
213 OutStream.Write(buffer, 0, 16);
216 public virtual void Write(double value) {
219 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
221 OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
224 public virtual void Write(short value) {
227 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
229 buffer [0] = (byte) value;
230 buffer [1] = (byte) (value >> 8);
231 OutStream.Write(buffer, 0, 2);
234 public virtual void Write(int value) {
237 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
239 buffer [0] = (byte) value;
240 buffer [1] = (byte) (value >> 8);
241 buffer [2] = (byte) (value >> 16);
242 buffer [3] = (byte) (value >> 24);
243 OutStream.Write(buffer, 0, 4);
246 public virtual void Write(long value) {
249 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
251 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
252 buffer [i] = (byte) (value >> sh);
253 OutStream.Write(buffer, 0, 8);
256 [CLSCompliant(false)]
257 public virtual void Write(sbyte value) {
260 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
262 buffer [0] = (byte) value;
263 OutStream.Write(buffer, 0, 1);
266 public virtual void Write(float value) {
269 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
271 OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
274 byte [] stringBuffer;
275 int maxCharsPerRound;
277 public virtual void Write(string value) {
280 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
282 int len = m_encoding.GetByteCount (value);
283 Write7BitEncodedInt (len);
285 if (stringBuffer == null) {
286 stringBuffer = new byte [512];
287 maxCharsPerRound = 512 / m_encoding.GetMaxByteCount (1);
291 int chrem = value.Length;
293 int cch = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
294 int blen = m_encoding.GetBytes (value, chpos, cch, stringBuffer, 0);
295 OutStream.Write (stringBuffer, 0, blen);
302 [CLSCompliant(false)]
303 public virtual void Write(ushort value) {
306 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
308 buffer [0] = (byte) value;
309 buffer [1] = (byte) (value >> 8);
310 OutStream.Write(buffer, 0, 2);
313 [CLSCompliant(false)]
314 public virtual void Write(uint value) {
317 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
319 buffer [0] = (byte) value;
320 buffer [1] = (byte) (value >> 8);
321 buffer [2] = (byte) (value >> 16);
322 buffer [3] = (byte) (value >> 24);
323 OutStream.Write(buffer, 0, 4);
326 [CLSCompliant(false)]
327 public virtual void Write(ulong value) {
330 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
332 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
333 buffer [i] = (byte) (value >> sh);
334 OutStream.Write(buffer, 0, 8);
337 protected void Write7BitEncodedInt(int value) {
339 int high = (value >> 7) & 0x01ffffff;
340 byte b = (byte)(value & 0x7f);
343 b = (byte)(b | 0x80);