1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
9 namespace Mono.Profiler.Log {
11 sealed class LogReader : IDisposable {
13 static readonly Encoding _encoding = Encoding.UTF8;
15 readonly BinaryReader _reader;
17 byte[] _stringBuffer = new byte [1024];
19 public LogReader (Stream stream, bool leaveOpen)
21 _reader = new BinaryReader (stream, _encoding, leaveOpen);
24 public void Dispose ()
29 public byte[] ReadBytes (int count)
31 var bytes = new byte [count];
33 // BinaryReader.ReadBytes doesn't necessarily read the specified
34 // amount of bytes, so just do it this way.
35 for (var i = 0; i < bytes.Length; i++)
36 bytes [i] = ReadByte ();
41 public byte ReadByte ()
43 return _reader.ReadByte ();
46 public ushort ReadUInt16 ()
48 return _reader.ReadUInt16 ();
51 public int ReadInt32 ()
53 return _reader.ReadInt32 ();
56 public long ReadInt64 ()
58 return _reader.ReadInt64 ();
61 public ulong ReadUInt64 ()
63 return _reader.ReadUInt64 ();
66 public double ReadDouble ()
68 return _reader.ReadDouble ();
71 public string ReadHeaderString ()
73 return _encoding.GetString (ReadBytes (ReadInt32 ()));
76 public string ReadCString ()
82 while ((val = ReadByte ()) != 0) {
83 if (pos == _stringBuffer.Length)
84 Array.Resize (ref _stringBuffer, System.Math.Max (_stringBuffer.Length * 2, pos + 1));
86 _stringBuffer [pos++] = val;
89 return _encoding.GetString (_stringBuffer, 0, pos);
92 public long ReadSLeb128 ()
100 result |= (long) (b & 0x7f) << shift;
103 if ((b & 0x80) != 0x80) {
104 if (shift < sizeof (long) * 8 && (b & 0x40) == 0x40)
105 result |= -(1L << shift);
114 public ulong ReadULeb128 ()
122 result |= (ulong) (b & 0x7f) << shift;
124 if ((b & 0x80) != 0x80)