Bug 15574. XML deserialization recursion: add array type to known_types?
[mono.git] / mcs / class / corlib / System.IO / BinaryReader.cs
1 //
2 // System.IO.BinaryReader
3 //
4 // Author:
5 //   Matt Kimball (matt@kimball.net)
6 //   Dick Porter (dick@ximian.com)
7 //   Marek Safar (marek.safar@gmail.com)
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Copyright 2011 Xamarin Inc.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Text;
36 using System.Globalization;
37 using Mono.Security;
38 using System.Runtime.InteropServices;
39
40 namespace System.IO {
41         [ComVisible (true)]
42         public class BinaryReader : IDisposable {
43                 Stream m_stream;
44                 Encoding m_encoding;
45
46                 byte[] m_buffer;
47
48                 Decoder decoder;
49                 char[] charBuffer;
50                 byte[] charByteBuffer;
51                 
52                 //
53                 // 128 chars should cover most strings in one grab.
54                 //
55                 const int MaxBufferSize = 128;
56
57                 
58                 private bool m_disposed;
59                 
60                 public BinaryReader(Stream input) 
61                         : this(input, Encoding.UTF8UnmarkedUnsafe)
62                 {
63                 }
64                 
65 #if NET_4_5
66                 readonly bool leave_open;
67                 
68                 public BinaryReader(Stream input, Encoding encoding)
69                         : this (input, encoding, false)
70                 {
71                 }
72                 
73                 public BinaryReader(Stream input, Encoding encoding, bool leaveOpen)
74 #else
75                 const bool leave_open = false;
76                 
77                 public BinaryReader(Stream input, Encoding encoding)
78 #endif
79                 {
80                         if (input == null || encoding == null) 
81                                 throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
82                         if (!input.CanRead)
83                                 throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));
84
85                         m_stream = input;
86                         m_encoding = encoding;
87 #if NET_4_5
88                         leave_open = leaveOpen;
89 #endif
90                         decoder = encoding.GetDecoder ();
91                         
92                         // internal buffer size is documented to be between 16 and the value
93                         // returned by GetMaxByteCount for the specified encoding
94                         m_buffer = new byte [Math.Max (16, encoding.GetMaxByteCount (1))];
95                 }
96
97                 public virtual Stream BaseStream {
98                         get {
99                                 return m_stream;
100                         }
101                 }
102
103                 public virtual void Close() {
104                         Dispose (true);
105                         m_disposed = true;
106                 }
107                 
108                 protected virtual void Dispose (bool disposing)
109                 {
110                         if (disposing && m_stream != null && !leave_open)
111                                 m_stream.Close ();
112
113                         m_disposed = true;
114                         m_buffer = null;
115                         m_encoding = null;
116                         m_stream = null;
117                         charBuffer = null;
118                 }
119
120 #if NET_4_0
121                 public void Dispose ()
122 #else
123                 void IDisposable.Dispose() 
124 #endif
125                 {
126                         Dispose (true);
127                 }
128
129                 protected virtual void FillBuffer (int numBytes)
130                 {
131                         if (numBytes > m_buffer.Length)
132                                 throw new ArgumentOutOfRangeException ("numBytes");
133                         if (m_disposed)
134                                 throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
135                         if (m_stream==null)
136                                 throw new IOException("Stream is invalid");
137                         
138                         /* Cope with partial reads */
139                         int pos=0;
140
141                         while(pos<numBytes) {
142                                 int n=m_stream.Read(m_buffer, pos, numBytes-pos);
143                                 if(n==0) {
144                                         throw new EndOfStreamException();
145                                 }
146
147                                 pos+=n;
148                         }
149                 }
150
151                 public virtual int PeekChar() {
152                         if(m_stream==null) {
153                                 
154                                 if (m_disposed)
155                                         throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
156
157                                 throw new IOException("Stream is invalid");
158                         }
159
160                         if ( !m_stream.CanSeek )
161                         {
162                                 return -1;
163                         }
164
165                         char[] result = new char[1];
166                         int bcount;
167
168                         int ccount = ReadCharBytes (result, 0, 1, out bcount);
169
170                         // Reposition the stream
171                         m_stream.Position -= bcount;
172
173                         // If we read 0 characters then return -1
174                         if (ccount == 0) 
175                         {
176                                 return -1;
177                         }
178                         
179                         // Return the single character we read
180                         return result[0];
181                 }
182
183                 public virtual int Read() {
184                         if (charBuffer == null)
185                                 charBuffer = new char [MaxBufferSize];
186
187                         int count = Read (charBuffer, 0, 1);
188                         if(count == 0) {
189                                 /* No chars available */
190                                 return -1;
191                         }
192                         
193                         return charBuffer [0];
194                 }
195
196                 public virtual int Read(byte[] buffer, int index, int count) {
197                         if(m_stream==null) {
198
199                                 if (m_disposed)
200                                         throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
201
202                                 throw new IOException("Stream is invalid");
203                         }
204                         
205                         if (buffer == null) {
206                                 throw new ArgumentNullException("buffer is null");
207                         }
208                         if (index < 0) {
209                                 throw new ArgumentOutOfRangeException("index is less than 0");
210                         }
211                         if (count < 0) {
212                                 throw new ArgumentOutOfRangeException("count is less than 0");
213                         }
214                         if (buffer.Length - index < count) {
215                                 throw new ArgumentException("buffer is too small");
216                         }
217
218                         int bytes_read=m_stream.Read(buffer, index, count);
219
220                         return(bytes_read);
221                 }
222
223                 public virtual int Read(char[] buffer, int index, int count) {
224
225                         if(m_stream==null) {
226
227                                 if (m_disposed)
228                                         throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
229
230                                 throw new IOException("Stream is invalid");
231                         }
232                         
233                         if (buffer == null) {
234                                 throw new ArgumentNullException("buffer is null");
235                         }
236                         if (index < 0) {
237                                 throw new ArgumentOutOfRangeException("index is less than 0");
238                         }
239                         if (count < 0) {
240                                 throw new ArgumentOutOfRangeException("count is less than 0");
241                         }
242                         if (buffer.Length - index < count) {
243                                 throw new ArgumentException("buffer is too small");
244                         }
245
246                         int bytes_read;
247                         return ReadCharBytes (buffer, index, count, out bytes_read);
248                 }
249
250                 private int ReadCharBytes (char[] buffer, int index, int count, out int bytes_read) 
251                 {
252                         int chars_read = 0;
253                         bytes_read = 0;
254                         
255                         while (chars_read < count) {
256                                 int pos = 0;
257                                 while (true) {
258                                         CheckBuffer (pos + 1);
259
260                                         int read_byte = m_stream.ReadByte ();
261
262                                         if (read_byte == -1)
263                                                 /* EOF */
264                                                 return chars_read;
265
266                                         m_buffer [pos ++] = (byte)read_byte;
267                                         bytes_read ++;
268
269                                         int n = m_encoding.GetChars (m_buffer, 0, pos, buffer, index + chars_read);
270                                         if (n > 0)
271                                                 break;
272                                 }
273                                 chars_read ++;
274                         }
275
276                         return chars_read;
277                 }
278
279                 protected int Read7BitEncodedInt() {
280                         int ret = 0;
281                         int shift = 0;
282                         int len;
283                         byte b;
284
285                         for (len = 0; len < 5; ++len) {
286                                 b = ReadByte();
287                                 
288                                 ret = ret | ((b & 0x7f) << shift);
289                                 shift += 7;
290                                 if ((b & 0x80) == 0)
291                                         break;
292                         }
293
294                         if (len < 5)
295                                 return ret;
296                         else
297                                 throw new FormatException ("Too many bytes in what should have been a 7 bit encoded Int32.");
298                 }
299
300                 public virtual bool ReadBoolean() {
301                         // Return value:
302                         //  true if the byte is non-zero; otherwise false.
303                         return ReadByte() != 0;
304                 }
305
306                 public virtual byte ReadByte() {
307                         if (m_stream == null) {
308                                 if (m_disposed)
309                                         throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
310
311                                 throw new IOException ("Stream is invalid");
312                         }
313                         
314                         int val = m_stream.ReadByte ();
315                         if (val != -1)
316                                 return (byte) val;
317                         
318                         throw new EndOfStreamException ();
319                 }
320
321                 public virtual byte[] ReadBytes(int count) {
322                         if(m_stream==null) {
323
324                                 if (m_disposed)
325                                         throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
326
327                                 throw new IOException("Stream is invalid");
328                         }
329                         
330                         if (count < 0) {
331                                 throw new ArgumentOutOfRangeException("count is less than 0");
332                         }
333
334                         /* Can't use FillBuffer() here, because it's OK to
335                          * return fewer bytes than were requested
336                          */
337
338                         byte[] buf = new byte[count];
339                         int pos=0;
340                         
341                         while(pos < count) 
342                         {
343                                 int n=m_stream.Read(buf, pos, count-pos);
344                                 if(n==0) {
345                                         /* EOF */
346                                         break;
347                                 }
348
349                                 pos+=n;
350                         }
351                                 
352                         if (pos!=count) {
353                                 byte[] new_buffer=new byte[pos];
354                                 Buffer.BlockCopyInternal (buf, 0, new_buffer, 0, pos);
355                                 return(new_buffer);
356                         }
357                         
358                         return(buf);
359                 }
360
361                 public virtual char ReadChar() {
362                         int ch=Read();
363
364                         if(ch==-1) {
365                                 throw new EndOfStreamException();
366                         }
367
368                         return((char)ch);
369                 }
370
371                 public virtual char[] ReadChars (int count)
372                 {
373                         if (count < 0) {
374                                 throw new ArgumentOutOfRangeException("count is less than 0");
375                         }
376
377                         if (m_stream == null) {
378                                 if (m_disposed)
379                                         throw new ObjectDisposedException ("BinaryReader", "Cannot read from a closed BinaryReader.");
380
381                                 throw new IOException("Stream is invalid");
382                         }
383
384                         if (count == 0)
385                                 return EmptyArray<char>.Value;
386                                         
387                         char[] full = new char[count];
388                         int bytes_read;
389                         int chars = ReadCharBytes (full, 0, count, out bytes_read);                     
390                         
391                         if (chars == 0)
392                                 throw new EndOfStreamException();
393
394                         if (chars != count) {
395                                 var new_buffer = new char[chars];
396                                 Buffer.BlockCopyInternal (full, 0, new_buffer, 0, 2 * chars);
397                                 return new_buffer;
398                         }
399
400                         return full;
401                 }
402
403                 unsafe public virtual decimal ReadDecimal() {
404                         FillBuffer(16);
405
406                         decimal ret;
407                         byte* ret_ptr = (byte *)&ret;
408                         
409                         /*
410                          * internal representation of decimal is 
411                          * ss32, hi32, lo32, mi32, 
412                          * but in stream it is 
413                          * lo32, mi32, hi32, ss32
414                          * So we have to rerange this int32 values
415                          */                       
416                   
417                         if (BitConverter.IsLittleEndian) {
418                                 for (int i = 0; i < 16; i++) {
419                                         if (i < 4) {
420                                                 // lo 8 - 12                      
421                                                 ret_ptr [i + 8] = m_buffer [i];
422                                         } else if (i < 8) {
423                                                 // mid 12 - 16
424                                                 ret_ptr [i + 8] = m_buffer [i];
425                                         } else if (i < 12) {
426                                                 // hi 4 - 8
427                                                 ret_ptr [i - 4] = m_buffer [i];
428                                         } else if (i < 16) {
429                                                 // ss 0 - 4
430                                                 ret_ptr [i - 12] = m_buffer [i];
431                                         }                               
432                                 }
433                         } else {
434                                 for (int i = 0; i < 16; i++) {
435                                         if (i < 4) {
436                                                 // lo 8 - 12                      
437                                                 ret_ptr [11 - i] = m_buffer [i];
438                                         } else if (i < 8) {
439                                                 // mid 12 - 16
440                                                 ret_ptr [19 - i] = m_buffer [i];
441                                         } else if (i < 12) {
442                                                 // hi 4 - 8
443                                                 ret_ptr [15 - i] = m_buffer [i];
444                                         } else if (i < 16) {
445                                                 // ss 0 - 4
446                                                 ret_ptr [15 - i] = m_buffer [i];
447                                         }                               
448                                 }
449                         }
450
451                         return ret;
452                 }
453
454                 public virtual double ReadDouble() {
455                         FillBuffer(8);
456
457                         return(BitConverterLE.ToDouble(m_buffer, 0));
458                 }
459
460                 public virtual short ReadInt16() {
461                         FillBuffer(2);
462
463                         return((short) (m_buffer[0] | (m_buffer[1] << 8)));
464                 }
465
466                 public virtual int ReadInt32() {
467                         FillBuffer(4);
468
469                         return(m_buffer[0] | (m_buffer[1] << 8) |
470                                (m_buffer[2] << 16) | (m_buffer[3] << 24));
471                 }
472
473                 public virtual long ReadInt64() {
474                         FillBuffer(8);
475
476                         uint ret_low  = (uint) (m_buffer[0]            |
477                                                (m_buffer[1] << 8)  |
478                                                (m_buffer[2] << 16) |
479                                                (m_buffer[3] << 24)
480                                                );
481                         uint ret_high = (uint) (m_buffer[4]        |
482                                                (m_buffer[5] << 8)  |
483                                                (m_buffer[6] << 16) |
484                                                (m_buffer[7] << 24)
485                                                );
486                         return (long) ((((ulong) ret_high) << 32) | ret_low);
487                 }
488
489                 [CLSCompliant(false)]
490                 public virtual sbyte ReadSByte() {
491                         return (sbyte) ReadByte ();
492                 }
493
494                 public virtual string ReadString() {
495                         /* Inspection of BinaryWriter-written files
496                          * shows that the length is given in bytes,
497                          * not chars
498                          */
499                         int len = Read7BitEncodedInt();
500                         
501                         if (len < 0)
502                                 throw new IOException ("Invalid binary file (string len < 0)");
503
504                         if (len == 0)
505                                 return String.Empty;
506                         
507                         if (charByteBuffer == null) {
508                                 charBuffer = new char [m_encoding.GetMaxByteCount (MaxBufferSize)];
509                                 charByteBuffer = new byte [MaxBufferSize];
510                         }
511
512                         //
513                         // We read the string here in small chunks. Also, we
514                         // Attempt to optimize the common case of short strings.
515                         //
516                         StringBuilder sb = null;
517                         do {
518                                 int readLen = Math.Min (MaxBufferSize, len);
519                                 
520                                 int n = m_stream.Read (charByteBuffer, 0, readLen);
521                                 if (n == 0)
522                                         throw new EndOfStreamException();
523                                 
524                                 int cch = decoder.GetChars (charByteBuffer, 0, n, charBuffer, 0);
525
526                                 if (sb == null && readLen == len) // ok, we got out the easy way, dont bother with the sb
527                                         return new String (charBuffer, 0, cch);
528
529                                 if (sb == null)
530                                         // Len is a fairly good estimate of the number of chars in a string
531                                         // Most of the time 1 byte == 1 char
532                                         sb = new StringBuilder (len);
533                                 
534                                 sb.Append (charBuffer, 0, cch);
535                                 len -= readLen;
536                         } while (len > 0);
537
538                         return sb.ToString();
539                 }
540
541                 public virtual float ReadSingle() {
542                         FillBuffer(4);
543
544                         return(BitConverterLE.ToSingle(m_buffer, 0));
545                 }
546
547                 [CLSCompliant(false)]
548                 public virtual ushort ReadUInt16() {
549                         FillBuffer(2);
550
551                         return((ushort) (m_buffer[0] | (m_buffer[1] << 8)));
552                 }
553
554                 [CLSCompliant(false)]
555                 public virtual uint ReadUInt32() {
556                         FillBuffer(4);
557                                 
558
559                         return((uint) (m_buffer[0] |
560                                        (m_buffer[1] << 8) |
561                                        (m_buffer[2] << 16) |
562                                        (m_buffer[3] << 24)));
563                 }
564
565                 [CLSCompliant(false)]
566                 public virtual ulong ReadUInt64() {
567                         FillBuffer(8);
568
569                         uint ret_low  = (uint) (m_buffer[0]            |
570                                                (m_buffer[1] << 8)  |
571                                                (m_buffer[2] << 16) |
572                                                (m_buffer[3] << 24)
573                                                );
574                         uint ret_high = (uint) (m_buffer[4]        |
575                                                (m_buffer[5] << 8)  |
576                                                (m_buffer[6] << 16) |
577                                                (m_buffer[7] << 24)
578                                                );
579                         return (((ulong) ret_high) << 32) | ret_low;
580                 }
581
582                 /* Ensures that m_buffer is at least length bytes
583                  * long, growing it if necessary
584                  */
585                 private void CheckBuffer(int length)
586                 {
587                         if(m_buffer.Length <= length) {
588                                 byte[] new_buffer=new byte[length];
589                                 Buffer.BlockCopyInternal (m_buffer, 0, new_buffer, 0, m_buffer.Length);
590                                 m_buffer=new_buffer;
591                         }
592                 }
593         }
594 }