2002-05-05 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / System.IO / BinaryWriter.cs
1 //
2 // System.IO.BinaryWriter
3 //
4 // Author:
5 //   Matt Kimball (matt@kimball.net)
6 //
7
8 using System;
9 using System.Text;
10 using System.Globalization;
11
12 namespace System.IO {
13         [Serializable]
14         public class BinaryWriter : IDisposable {
15
16                 // Null is a BinaryWriter with no backing store.
17                 public static readonly BinaryWriter Null;
18
19                 protected Stream OutStream;
20                 private Encoding m_encoding;
21                 private byte [] buffer;
22
23                 static BinaryWriter() {
24                         Null = new BinaryWriter();
25                 }
26
27                 protected BinaryWriter() : this (Stream.Null, Encoding.UTF8) {
28                 }
29
30                 public BinaryWriter(Stream output) : this(output, Encoding.UTF8) {
31                 }
32
33                 public BinaryWriter(Stream output, Encoding encoding) {
34                         if (output == null || encoding == null) 
35                                 throw new ArgumentNullException(Locale.GetText ("Output or Encoding is a null reference."));
36                         if (!output.CanWrite)
37                                 throw new ArgumentException(Locale.GetText ("Stream does not support writing or already closed."));
38
39                         OutStream = output;
40                         m_encoding = encoding;
41                         buffer = new byte [16];
42                 }
43
44                 public virtual Stream BaseStream {
45                         get {
46                                 return OutStream;
47                         }
48                 }
49
50                 public virtual void Close() {
51                         Dispose();
52                 }
53
54                 public virtual void Dispose() {
55                         OutStream.Close();
56                         OutStream.Dispose();                    
57                 }
58
59                 public virtual void Flush() {
60                         OutStream.Flush();
61                 }
62
63                 public virtual long Seek(int offset, SeekOrigin origin) {
64                         return OutStream.Seek(offset, origin);
65                 }
66
67                 public virtual void Write(bool value) {
68                         buffer [0] = (byte) (value ? 1 : 0);
69                         OutStream.Write(buffer, 0, 1);
70                 }
71
72                 public virtual void Write(byte value) {
73                         OutStream.WriteByte(value);
74                 }
75
76                 public virtual void Write(byte[] value) {
77                         if (value == null)
78                                 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
79                         OutStream.Write(value, 0, value.Length);
80                 }
81
82                 public virtual void Write(byte[] value, int offset, int length) {
83                         if (value == null)
84                                 throw new ArgumentNullException(Locale.GetText ("Byte buffer is a null reference."));
85                         OutStream.Write(value, offset, length);
86                 }
87
88                 public virtual void Write(char value) {
89                         char[] dec = new char[1];
90                         dec[0] = value;
91                         byte[] enc = m_encoding.GetBytes(dec, 0, 1);
92                         OutStream.Write(enc, 0, enc.Length);
93                 }
94                 
95                 public virtual void Write(char[] value) {
96                         if (value == null)
97                                 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
98                         byte[] enc = m_encoding.GetBytes(value, 0, value.Length);
99                         OutStream.Write(enc, 0, enc.Length);
100                 }
101
102                 public virtual void Write(char[] value, int offset, int length) {
103                         if (value == null)
104                                 throw new ArgumentNullException(Locale.GetText ("Chars is a null reference."));
105                         byte[] enc = m_encoding.GetBytes(value, offset, length);
106                         OutStream.Write(enc, 0, enc.Length);
107                 }
108
109                 unsafe public virtual void Write(decimal value) {
110                         byte* value_ptr = (byte *)&value;
111                         for (int i = 0; i < 16; i++) {
112                                 buffer [i] = value_ptr [i];
113                         }
114
115                         OutStream.Write(buffer, 0, 16);
116                 }
117
118                 public virtual void Write(double value) {
119                         OutStream.Write(BitConverter.GetBytes(value), 0, 8);
120                 }
121                 
122                 public virtual void Write(short value) {
123                         buffer [0] = (byte) value;
124                         buffer [1] = (byte) (value >> 8);
125                         OutStream.Write(buffer, 0, 2);
126                 }
127                 
128                 public virtual void Write(int value) {
129                         buffer [0] = (byte) value;
130                         buffer [1] = (byte) (value >> 8);
131                         buffer [2] = (byte) (value >> 16);
132                         buffer [3] = (byte) (value >> 24);
133                         OutStream.Write(buffer, 0, 4);
134                 }
135
136                 public virtual void Write(long value) {
137                         for (int i = 0, sh = 0; i < 8; i++, sh += 8)
138                                 buffer [i] = (byte) (value >> sh);
139                         OutStream.Write(buffer, 0, 8);
140                 }
141
142                 [CLSCompliant(false)]
143                 public virtual void Write(sbyte value) {
144                         buffer [0] = (byte) value;
145                         OutStream.Write(buffer, 0, 1);
146                 }
147
148                 public virtual void Write(float value) {
149                         OutStream.Write(BitConverter.GetBytes(value), 0, 4);
150                 }
151
152                 public virtual void Write(string value) {
153                         Write7BitEncodedInt(value.Length);
154                         byte[] enc = m_encoding.GetBytes(value);
155                         OutStream.Write(enc, 0, enc.Length);
156                 }
157
158                 [CLSCompliant(false)]
159                 public virtual void Write(ushort value) {
160                         buffer [0] = (byte) value;
161                         buffer [1] = (byte) (value >> 8);
162                         OutStream.Write(buffer, 0, 2);
163                 }
164
165                 [CLSCompliant(false)]
166                 public virtual void Write(uint value) {
167                         buffer [0] = (byte) value;
168                         buffer [1] = (byte) (value >> 8);
169                         buffer [2] = (byte) (value >> 16);
170                         buffer [3] = (byte) (value >> 24);
171                         OutStream.Write(buffer, 0, 4);
172                 }
173
174                 [CLSCompliant(false)]
175                 public virtual void Write(ulong value) {
176                         for (int i = 0, sh = 0; i < 8; i++, sh += 8)
177                                 buffer [i] = (byte) (value >> sh);
178                         OutStream.Write(buffer, 0, 8);
179                 }
180
181                 protected void Write7BitEncodedInt(int value) {
182                         do {
183                                 int high = (value >> 7) & 0x01ffffff;
184                                 byte b = (byte)(value & 0x7f);
185
186                                 if (high != 0) {
187                                         b = (byte)(b | 0x80);
188                                 }
189
190                                 Write(b);
191                                 value = high;
192                         } while(value != 0);
193                 }
194         }
195 }