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 {
76 public virtual void Close() {
81 public void Dispose ()
83 void IDisposable.Dispose()
89 protected virtual void Dispose (bool disposing)
91 if (disposing && OutStream != null)
99 public virtual void Flush() {
103 public virtual long Seek(int offset, SeekOrigin origin) {
105 return OutStream.Seek(offset, origin);
108 public virtual void Write(bool value) {
111 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
113 buffer [0] = (byte) (value ? 1 : 0);
114 OutStream.Write(buffer, 0, 1);
117 public virtual void Write(byte value) {
120 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
122 OutStream.WriteByte(value);
125 public virtual void Write(byte[] buffer) {
128 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
131 throw new ArgumentNullException("buffer");
132 OutStream.Write(buffer, 0, buffer.Length);
135 public virtual void Write(byte[] buffer, int index, int count) {
138 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
141 throw new ArgumentNullException("buffer");
142 OutStream.Write(buffer, index, count);
145 public virtual void Write(char ch) {
148 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
150 char[] dec = new char[1];
152 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
153 OutStream.Write(enc, 0, enc.Length);
156 public virtual void Write(char[] chars) {
159 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
162 throw new ArgumentNullException("chars");
163 byte[] enc = m_encoding.GetBytes(chars, 0, chars.Length);
164 OutStream.Write(enc, 0, enc.Length);
167 public virtual void Write(char[] chars, int index, int count) {
170 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
173 throw new ArgumentNullException("chars");
174 byte[] enc = m_encoding.GetBytes(chars, index, count);
175 OutStream.Write(enc, 0, enc.Length);
178 unsafe public virtual void Write(decimal value) {
181 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
183 byte* value_ptr = (byte *)&value;
186 * decimal in stream is lo32, mi32, hi32, ss32
187 * but its internal structure si ss32, hi32, lo32, mi32
190 if (BitConverter.IsLittleEndian) {
191 for (int i = 0; i < 16; i++) {
193 buffer [i + 12] = value_ptr [i];
195 buffer [i + 4] = value_ptr [i];
197 buffer [i - 8] = value_ptr [i];
199 buffer [i - 8] = value_ptr [i];
202 for (int i = 0; i < 16; i++) {
204 buffer [15 - i] = value_ptr [i];
206 buffer [15 - i] = value_ptr [i];
208 buffer [11 - i] = value_ptr [i];
210 buffer [19 - i] = value_ptr [i];
214 OutStream.Write(buffer, 0, 16);
217 public virtual void Write(double value) {
220 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
222 OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
225 public virtual void Write(short value) {
228 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
230 buffer [0] = (byte) value;
231 buffer [1] = (byte) (value >> 8);
232 OutStream.Write(buffer, 0, 2);
235 public virtual void Write(int value) {
238 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
240 buffer [0] = (byte) value;
241 buffer [1] = (byte) (value >> 8);
242 buffer [2] = (byte) (value >> 16);
243 buffer [3] = (byte) (value >> 24);
244 OutStream.Write(buffer, 0, 4);
247 public virtual void Write(long value) {
250 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
252 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
253 buffer [i] = (byte) (value >> sh);
254 OutStream.Write(buffer, 0, 8);
257 [CLSCompliant(false)]
258 public virtual void Write(sbyte value) {
261 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
263 buffer [0] = (byte) value;
264 OutStream.Write(buffer, 0, 1);
267 public virtual void Write(float value) {
270 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
272 OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
275 byte [] stringBuffer;
276 int maxCharsPerRound;
278 public virtual void Write(string value) {
281 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
283 int len = m_encoding.GetByteCount (value);
284 Write7BitEncodedInt (len);
286 if (stringBuffer == null) {
287 stringBuffer = new byte [512];
288 maxCharsPerRound = 512 / m_encoding.GetMaxByteCount (1);
292 int chrem = value.Length;
294 int cch = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
295 int blen = m_encoding.GetBytes (value, chpos, cch, stringBuffer, 0);
296 OutStream.Write (stringBuffer, 0, blen);
303 [CLSCompliant(false)]
304 public virtual void Write(ushort value) {
307 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
309 buffer [0] = (byte) value;
310 buffer [1] = (byte) (value >> 8);
311 OutStream.Write(buffer, 0, 2);
314 [CLSCompliant(false)]
315 public virtual void Write(uint value) {
318 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
320 buffer [0] = (byte) value;
321 buffer [1] = (byte) (value >> 8);
322 buffer [2] = (byte) (value >> 16);
323 buffer [3] = (byte) (value >> 24);
324 OutStream.Write(buffer, 0, 4);
327 [CLSCompliant(false)]
328 public virtual void Write(ulong value) {
331 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
333 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
334 buffer [i] = (byte) (value >> sh);
335 OutStream.Write(buffer, 0, 8);
338 protected void Write7BitEncodedInt(int value) {
340 int high = (value >> 7) & 0x01ffffff;
341 byte b = (byte)(value & 0x7f);
344 b = (byte)(b | 0x80);