corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5  
6 namespace CSJ2K.Util
7 {
8 internal class EndianBinaryWriter : BinaryWriter
9 {
10 private bool _bigEndian = false;
11  
12 //
13 // Summary:
14 // Initializes a new instance of the System.IO.BinaryWriter class based on the
15 // supplied stream and using UTF-8 as the encoding for strings.
16 //
17 // Parameters:
18 // output:
19 // The output stream.
20 //
21 // Exceptions:
22 // System.ArgumentException:
23 // The stream does not support writing, or the stream is already closed.
24 //
25 // System.ArgumentNullException:
26 // output is null.
27 public EndianBinaryWriter(Stream input) : base(input)
28 {
29 }
30 //
31 // Summary:
32 // Initializes a new instance of the System.IO.BinaryWriter class based on the
33 // supplied stream and a specific character encoding.
34 //
35 // Parameters:
36 // encoding:
37 // The character encoding.
38 //
39 // output:
40 // The supplied stream.
41 //
42 // Exceptions:
43 // System.ArgumentNullException:
44 // output or encoding is null.
45 //
46 // System.ArgumentException:
47 // The stream does not support writing, or the stream is already closed.
48 public EndianBinaryWriter(Stream input, Encoding encoding) : base(input, encoding)
49 {
50  
51 }
52 public EndianBinaryWriter(Stream input, Encoding encoding, bool bigEndian)
53 : base(input, encoding)
54 {
55 _bigEndian = bigEndian;
56 }
57  
58 public EndianBinaryWriter(Stream input, bool bigEndian) : base(input, bigEndian ? Encoding.BigEndianUnicode : Encoding.ASCII)
59 {
60 _bigEndian = bigEndian;
61 }
62  
63 // Summary:
64 // Gets the underlying stream of the System.IO.BinaryWriter.
65 //
66 // Returns:
67 // The underlying stream associated with the BinaryWriter.
68 //public virtual Stream BaseStream { get; }
69  
70 // Summary:
71 // Closes the current System.IO.BinaryWriter and the underlying stream.
72 //public virtual void Close();
73 //
74 // Summary:
75 // Releases the unmanaged resources used by the System.IO.BinaryWriter and optionally
76 // releases the managed resources.
77 //
78 // Parameters:
79 // disposing:
80 // true to release both managed and unmanaged resources; false to release only
81 // unmanaged resources.
82 //protected virtual void Dispose(bool disposing);
83 //
84 // Summary:
85 // Clears all buffers for the current writer and causes any buffered data to
86 // be written to the underlying device.
87 //public virtual void Flush();
88 //
89 // Summary:
90 // Sets the position within the current stream.
91 //
92 // Parameters:
93 // offset:
94 // A byte offset relative to origin.
95 //
96 // origin:
97 // A field of System.IO.SeekOrigin indicating the reference point from which
98 // the new position is to be obtained.
99 //
100 // Returns:
101 // The position with the current stream.
102 //
103 // Exceptions:
104 // System.ArgumentException:
105 // The System.IO.SeekOrigin value is invalid.
106 //
107 // System.IO.IOException:
108 // The file pointer was moved to an invalid location.
109 //public virtual long Seek(int offset, SeekOrigin origin);
110 //
111 // Summary:
112 // Writes a one-byte Boolean value to the current stream, with 0 representing
113 // false and 1 representing true.
114 //
115 // Parameters:
116 // value:
117 // The Boolean value to write (0 or 1).
118 //
119 // Exceptions:
120 // System.ObjectDisposedException:
121 // The stream is closed.
122 //
123 // System.IO.IOException:
124 // An I/O error occurs.
125 //public virtual void Write(bool value);
126 //
127 // Summary:
128 // Writes an unsigned byte to the current stream and advances the stream position
129 // by one byte.
130 //
131 // Parameters:
132 // value:
133 // The unsigned byte to write.
134 //
135 // Exceptions:
136 // System.ObjectDisposedException:
137 // The stream is closed.
138 //
139 // System.IO.IOException:
140 // An I/O error occurs.
141 //public virtual void Write(byte value);
142 //
143 // Summary:
144 // Writes a byte array to the underlying stream.
145 //
146 // Parameters:
147 // buffer:
148 // A byte array containing the data to write.
149 //
150 // Exceptions:
151 // System.ArgumentNullException:
152 // buffer is null.
153 //
154 // System.ObjectDisposedException:
155 // The stream is closed.
156 //
157 // System.IO.IOException:
158 // An I/O error occurs.
159 //public virtual void Write(byte[] buffer);
160 //
161 // Summary:
162 // Writes a Unicode character to the current stream and advances the current
163 // position of the stream in accordance with the Encoding used and the specific
164 // characters being written to the stream.
165 //
166 // Parameters:
167 // ch:
168 // The non-surrogate, Unicode character to write.
169 //
170 // Exceptions:
171 // System.ObjectDisposedException:
172 // The stream is closed.
173 //
174 // System.ArgumentException:
175 // ch is a single surrogate character.
176 //
177 // System.IO.IOException:
178 // An I/O error occurs.
179 //public virtual void Write(char ch);
180 //
181 // Summary:
182 // Writes a character array to the current stream and advances the current position
183 // of the stream in accordance with the Encoding used and the specific characters
184 // being written to the stream.
185 //
186 // Parameters:
187 // chars:
188 // A character array containing the data to write.
189 //
190 // Exceptions:
191 // System.ObjectDisposedException:
192 // The stream is closed.
193 //
194 // System.IO.IOException:
195 // An I/O error occurs.
196 //
197 // System.ArgumentNullException:
198 // chars is null.
199 //public virtual void Write(char[] chars);
200 //
201 // Summary:
202 // Writes a decimal value to the current stream and advances the stream position
203 // by sixteen bytes.
204 //
205 // Parameters:
206 // value:
207 // The decimal value to write.
208 //
209 // Exceptions:
210 // System.ObjectDisposedException:
211 // The stream is closed.
212 //
213 // System.IO.IOException:
214 // An I/O error occurs.
215 public override void Write(decimal value)
216 {
217 if (_bigEndian)
218 {
219 throw new System.NotImplementedException();
220 /*
221 int[] decimalints=decimal.GetBits(value);
222  
223 // TODO: Is the whole thing reversed or just the individual ints?
224 // Maybe we should just call ReadInt32 4 times?
225 //byte[] buf = this.ReadBytes(16);
226 //Array.Reverse(buf);
227 //int[] decimalints = new int[4];
228 //decimalints[0] = BitConverter.ToInt32(buf, 0);
229 //decimalints[1] = BitConverter.ToInt32(buf, 4);
230 //decimalints[2] = BitConverter.ToInt32(buf, 8);
231 //decimalints[3] = BitConverter.ToInt32(buf, 12);
232 //return new decimal(decimalints);
233 */
234 }
235 else base.Write(value);
236 }
237 //
238 // Summary:
239 // Writes an eight-byte floating-point value to the current stream and advances
240 // the stream position by eight bytes.
241 //
242 // Parameters:
243 // value:
244 // The eight-byte floating-point value to write.
245 //
246 // Exceptions:
247 // System.ObjectDisposedException:
248 // The stream is closed.
249 //
250 // System.IO.IOException:
251 // An I/O error occurs.
252 public override void Write(double value)
253 {
254 if (_bigEndian)
255 {
256 byte[] buf = BitConverter.GetBytes(value);
257 Array.Reverse(buf);
258 base.Write(buf);
259 }
260 else base.Write(value);
261 }
262 //
263 // Summary:
264 // Writes a four-byte floating-point value to the current stream and advances
265 // the stream position by four bytes.
266 //
267 // Parameters:
268 // value:
269 // The four-byte floating-point value to write.
270 //
271 // Exceptions:
272 // System.ObjectDisposedException:
273 // The stream is closed.
274 //
275 // System.IO.IOException:
276 // An I/O error occurs.
277 public override void Write(float value)
278 {
279 if (_bigEndian)
280 {
281 byte[] buf = BitConverter.GetBytes(value);
282 Array.Reverse(buf);
283 base.Write(buf);
284 }
285 else base.Write(value);
286 }
287 //
288 // Summary:
289 // Writes a four-byte signed integer to the current stream and advances the
290 // stream position by four bytes.
291 //
292 // Parameters:
293 // value:
294 // The four-byte signed integer to write.
295 //
296 // Exceptions:
297 // System.ObjectDisposedException:
298 // The stream is closed.
299 //
300 // System.IO.IOException:
301 // An I/O error occurs.
302 public override void Write(int value)
303 {
304 if (_bigEndian)
305 {
306 byte[] buf = BitConverter.GetBytes(value);
307 Array.Reverse(buf);
308 base.Write(buf);
309 }
310 else base.Write(value);
311 }
312 //
313 // Summary:
314 // Writes an eight-byte signed integer to the current stream and advances the
315 // stream position by eight bytes.
316 //
317 // Parameters:
318 // value:
319 // The eight-byte signed integer to write.
320 //
321 // Exceptions:
322 // System.ObjectDisposedException:
323 // The stream is closed.
324 //
325 // System.IO.IOException:
326 // An I/O error occurs.
327 public override void Write(long value)
328 {
329 if (_bigEndian)
330 {
331 byte[] buf = BitConverter.GetBytes(value);
332 Array.Reverse(buf);
333 base.Write(buf);
334 }
335 else base.Write(value);
336 }
337 //
338 // Summary:
339 // Writes a signed byte to the current stream and advances the stream position
340 // by one byte.
341 //
342 // Parameters:
343 // value:
344 // The signed byte to write.
345 //
346 // Exceptions:
347 // System.ObjectDisposedException:
348 // The stream is closed.
349 //
350 // System.IO.IOException:
351 // An I/O error occurs.
352 //[CLSCompliant(false)]
353 //public virtual void Write(sbyte value);
354 //
355 // Summary:
356 // Writes a two-byte signed integer to the current stream and advances the stream
357 // position by two bytes.
358 //
359 // Parameters:
360 // value:
361 // The two-byte signed integer to write.
362 //
363 // Exceptions:
364 // System.ObjectDisposedException:
365 // The stream is closed.
366 //
367 // System.IO.IOException:
368 // An I/O error occurs.
369 public override void Write(short value)
370 {
371 if (_bigEndian)
372 {
373 byte[] buf = BitConverter.GetBytes(value);
374 Array.Reverse(buf);
375 base.Write(buf);
376 }
377 else base.Write(value);
378 }
379 //
380 // Summary:
381 // Writes a length-prefixed string to this stream in the current encoding of
382 // the System.IO.BinaryWriter, and advances the current position of the stream
383 // in accordance with the encoding used and the specific characters being written
384 // to the stream.
385 //
386 // Parameters:
387 // value:
388 // The value to write.
389 //
390 // Exceptions:
391 // System.ObjectDisposedException:
392 // The stream is closed.
393 //
394 // System.IO.IOException:
395 // An I/O error occurs.
396 //
397 // System.ArgumentNullException:
398 // value is null.
399 //public virtual void Write(string value);
400 //
401 // Summary:
402 // Writes a four-byte unsigned integer to the current stream and advances the
403 // stream position by four bytes.
404 //
405 // Parameters:
406 // value:
407 // The four-byte unsigned integer to write.
408 //
409 // Exceptions:
410 // System.ObjectDisposedException:
411 // The stream is closed.
412 //
413 // System.IO.IOException:
414 // An I/O error occurs.
415 public override void Write(uint value)
416 {
417 if (_bigEndian)
418 {
419 byte[] buf = BitConverter.GetBytes(value);
420 Array.Reverse(buf);
421 base.Write(buf);
422 }
423 else base.Write(value);
424 }
425 //
426 // Summary:
427 // Writes an eight-byte unsigned integer to the current stream and advances
428 // the stream position by eight bytes.
429 //
430 // Parameters:
431 // value:
432 // The eight-byte unsigned integer to write.
433 //
434 // Exceptions:
435 // System.ObjectDisposedException:
436 // The stream is closed.
437 //
438 // System.IO.IOException:
439 // An I/O error occurs.
440 public override void Write(ulong value)
441 {
442 if (_bigEndian)
443 {
444 byte[] buf = BitConverter.GetBytes(value);
445 Array.Reverse(buf);
446 base.Write(buf);
447 }
448 else base.Write(value);
449 }
450 //
451 // Summary:
452 // Writes a two-byte unsigned integer to the current stream and advances the
453 // stream position by two bytes.
454 //
455 // Parameters:
456 // value:
457 // The two-byte unsigned integer to write.
458 //
459 // Exceptions:
460 // System.ObjectDisposedException:
461 // The stream is closed.
462 //
463 // System.IO.IOException:
464 // An I/O error occurs.
465 public override void Write(ushort value)
466 {
467 if (_bigEndian)
468 {
469 byte[] buf = BitConverter.GetBytes(value);
470 Array.Reverse(buf);
471 base.Write(buf);
472 }
473 else base.Write(value);
474 }
475 //
476 // Summary:
477 // Writes a region of a byte array to the current stream.
478 //
479 // Parameters:
480 // count:
481 // The number of bytes to write.
482 //
483 // buffer:
484 // A byte array containing the data to write.
485 //
486 // index:
487 // The starting point in buffer at which to begin writing.
488 //
489 // Exceptions:
490 // System.ArgumentNullException:
491 // buffer is null.
492 //
493 // System.IO.IOException:
494 // An I/O error occurs.
495 //
496 // System.ObjectDisposedException:
497 // The stream is closed.
498 //
499 // System.ArgumentOutOfRangeException:
500 // index or count is negative.
501 //
502 // System.ArgumentException:
503 // The buffer length minus index is less than count.
504 //public virtual void Write(byte[] buffer, int index, int count);
505 //
506 // Summary:
507 // Writes a section of a character array to the current stream, and advances
508 // the current position of the stream in accordance with the Encoding used and
509 // perhaps the specific characters being written to the stream.
510 //
511 // Parameters:
512 // chars:
513 // A character array containing the data to write.
514 //
515 // count:
516 // The number of characters to write.
517 //
518 // index:
519 // The starting point in buffer from which to begin writing.
520 //
521 // Exceptions:
522 // System.IO.IOException:
523 // An I/O error occurs.
524 //
525 // System.ObjectDisposedException:
526 // The stream is closed.
527 //
528 // System.ArgumentOutOfRangeException:
529 // index or count is negative.
530 //
531 // System.ArgumentException:
532 // The buffer length minus index is less than count.
533 //
534 // System.ArgumentNullException:
535 // chars is null.
536 //public virtual void Write(char[] chars, int index, int count);
537 //
538 // Summary:
539 // Writes a 32-bit integer in a compressed format.
540 //
541 // Parameters:
542 // value:
543 // The 32-bit integer to be written.
544 //
545 // Exceptions:
546 // System.ObjectDisposedException:
547 // The stream is closed.
548 //
549 // System.IO.EndOfStreamException:
550 // The end of the stream is reached.
551 //
552 // System.IO.IOException:
553 // The stream is closed.
554 //protected void Write7BitEncodedInt(int value)
555  
556 }
557 }