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;
36 using System.Runtime.InteropServices;
44 public class BinaryWriter : IDisposable {
46 // Null is a BinaryWriter with no backing store.
47 public static readonly BinaryWriter Null = new BinaryWriter ();
49 protected Stream OutStream;
50 private Encoding m_encoding;
51 private byte [] buffer;
52 private bool disposed = false;
54 protected BinaryWriter() : this (Stream.Null, Encoding.UTF8UnmarkedUnsafe) {
57 public BinaryWriter(Stream output) : this(output, Encoding.UTF8UnmarkedUnsafe) {
60 public BinaryWriter(Stream output, Encoding encoding) {
61 if (output == null || encoding == null)
62 throw new ArgumentNullException(Locale.GetText ("Output or Encoding is a null reference."));
64 throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
67 m_encoding = encoding;
68 buffer = new byte [16];
71 public virtual Stream BaseStream {
77 public virtual void Close() {
81 void IDisposable.Dispose() {
85 protected virtual void Dispose (bool disposing)
87 if (disposing && OutStream != null)
95 public virtual void Flush() {
99 public virtual long Seek(int offset, SeekOrigin origin) {
101 return OutStream.Seek(offset, origin);
104 public virtual void Write(bool value) {
107 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
109 buffer [0] = (byte) (value ? 1 : 0);
110 OutStream.Write(buffer, 0, 1);
113 public virtual void Write(byte value) {
116 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
118 OutStream.WriteByte(value);
121 public virtual void Write(byte[] value) {
124 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
127 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
128 OutStream.Write(value, 0, value.Length);
131 public virtual void Write(byte[] value, int offset, int length) {
134 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
137 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
138 OutStream.Write(value, offset, length);
141 public virtual void Write(char value) {
144 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
146 char[] dec = new char[1];
148 byte[] enc = m_encoding.GetBytes(dec, 0, 1);
149 OutStream.Write(enc, 0, enc.Length);
152 public virtual void Write(char[] value) {
155 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
158 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
159 byte[] enc = m_encoding.GetBytes(value, 0, value.Length);
160 OutStream.Write(enc, 0, enc.Length);
163 public virtual void Write(char[] value, int offset, int length) {
166 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
169 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
170 byte[] enc = m_encoding.GetBytes(value, offset, length);
171 OutStream.Write(enc, 0, enc.Length);
174 unsafe public virtual void Write(decimal value) {
177 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
179 byte* value_ptr = (byte *)&value;
182 * decimal in stream is lo32, mi32, hi32, ss32
183 * but its internal structure si ss32, hi32, lo32, mi32
186 if (BitConverter.IsLittleEndian) {
187 for (int i = 0; i < 16; i++) {
189 buffer [i + 12] = value_ptr [i];
191 buffer [i + 4] = value_ptr [i];
193 buffer [i - 8] = value_ptr [i];
195 buffer [i - 8] = value_ptr [i];
198 for (int i = 0; i < 16; i++) {
200 buffer [15 - i] = value_ptr [i];
202 buffer [15 - i] = value_ptr [i];
204 buffer [11 - i] = value_ptr [i];
206 buffer [19 - i] = value_ptr [i];
210 OutStream.Write(buffer, 0, 16);
213 public virtual void Write(double value) {
216 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
218 OutStream.Write(BitConverterLE.GetBytes(value), 0, 8);
221 public virtual void Write(short value) {
224 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
226 buffer [0] = (byte) value;
227 buffer [1] = (byte) (value >> 8);
228 OutStream.Write(buffer, 0, 2);
231 public virtual void Write(int value) {
234 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
236 buffer [0] = (byte) value;
237 buffer [1] = (byte) (value >> 8);
238 buffer [2] = (byte) (value >> 16);
239 buffer [3] = (byte) (value >> 24);
240 OutStream.Write(buffer, 0, 4);
243 public virtual void Write(long value) {
246 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
248 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
249 buffer [i] = (byte) (value >> sh);
250 OutStream.Write(buffer, 0, 8);
253 [CLSCompliant(false)]
254 public virtual void Write(sbyte value) {
257 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
259 buffer [0] = (byte) value;
260 OutStream.Write(buffer, 0, 1);
263 public virtual void Write(float value) {
266 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
268 OutStream.Write(BitConverterLE.GetBytes(value), 0, 4);
271 byte [] stringBuffer;
272 int maxCharsPerRound;
274 public virtual void Write(string value) {
277 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
279 int len = m_encoding.GetByteCount (value);
280 Write7BitEncodedInt (len);
282 if (stringBuffer == null) {
283 stringBuffer = new byte [512];
284 maxCharsPerRound = 512 / m_encoding.GetMaxByteCount (1);
288 int chrem = value.Length;
290 int cch = (chrem > maxCharsPerRound) ? maxCharsPerRound : chrem;
291 int blen = m_encoding.GetBytes (value, chpos, cch, stringBuffer, 0);
292 OutStream.Write (stringBuffer, 0, blen);
299 [CLSCompliant(false)]
300 public virtual void Write(ushort value) {
303 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
305 buffer [0] = (byte) value;
306 buffer [1] = (byte) (value >> 8);
307 OutStream.Write(buffer, 0, 2);
310 [CLSCompliant(false)]
311 public virtual void Write(uint value) {
314 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
316 buffer [0] = (byte) value;
317 buffer [1] = (byte) (value >> 8);
318 buffer [2] = (byte) (value >> 16);
319 buffer [3] = (byte) (value >> 24);
320 OutStream.Write(buffer, 0, 4);
323 [CLSCompliant(false)]
324 public virtual void Write(ulong value) {
327 throw new ObjectDisposedException ("BinaryWriter", "Cannot write to a closed BinaryWriter");
329 for (int i = 0, sh = 0; i < 8; i++, sh += 8)
330 buffer [i] = (byte) (value >> sh);
331 OutStream.Write(buffer, 0, 8);
334 protected void Write7BitEncodedInt(int value) {
336 int high = (value >> 7) & 0x01ffffff;
337 byte b = (byte)(value & 0x7f);
340 b = (byte)(b | 0x80);