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.
6 using System.Collections.Generic;
10 namespace Mono.Profiler.Log {
12 public sealed class LogReader : IDisposable {
14 public static Encoding Encoding { get; } = Encoding.UTF8;
16 public LogStream BaseStream => (LogStream) _reader.BaseStream;
18 public long Position { get; private set; }
20 readonly BinaryReader _reader;
22 byte[] _stringBuffer = new byte [1024];
24 public LogReader (LogStream stream, bool leaveOpen)
26 _reader = new BinaryReader (stream, Encoding, leaveOpen);
29 public void Dispose ()
34 internal byte ReadByte ()
36 var b = _reader.ReadByte ();
38 Position += sizeof (byte);
43 internal ushort ReadUInt16 ()
45 var i = _reader.ReadUInt16 ();
47 Position += sizeof (ushort);
52 internal int ReadInt32 ()
54 var i = _reader.ReadInt32 ();
56 Position += sizeof (int);
61 internal long ReadInt64 ()
63 var i = _reader.ReadInt64 ();
65 Position += sizeof (long);
70 internal ulong ReadUInt64 ()
72 var i = _reader.ReadUInt64 ();
74 Position += sizeof (ulong);
79 internal double ReadDouble ()
81 var d = _reader.ReadDouble ();
83 Position += sizeof (double);
88 internal string ReadHeaderString ()
90 var bytes = new byte [ReadInt32 ()];
92 // ReadBytes doesn't necessarily read the specified amount of
93 // bytes, so just do it this way.
94 for (var i = 0; i < bytes.Length; i++)
95 bytes [i] = ReadByte ();
97 return Encoding.GetString (bytes);
100 internal string ReadCString ()
106 while ((val = ReadByte ()) != 0) {
107 if (pos == _stringBuffer.Length)
108 Array.Resize (ref _stringBuffer, System.Math.Max (_stringBuffer.Length * 2, pos + 1));
110 _stringBuffer [pos++] = val;
113 return Encoding.GetString (_stringBuffer, 0, pos);
116 internal long ReadSLeb128 ()
124 result |= (long) (b & 0x7f) << shift;
127 if ((b & 0x80) != 0x80) {
128 if (shift < sizeof (long) * 8 && (b & 0x40) == 0x40)
129 result |= -(1L << shift);
138 internal ulong ReadULeb128 ()
146 result |= (ulong) (b & 0x7f) << shift;
148 if ((b & 0x80) != 0x80)