Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/Utils/7BitEncodedInts.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.2 KB
Line 
1// Copyright (c) 2011 Daniel Grunwald
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.IO;
21
22namespace ICSharpCode.NRefactory.Utils
23{
24  /// <summary>
25  /// A binary reader that can read the output of BinaryWriterWith7BitEncodedInts.
26  /// </summary>
27  public sealed class BinaryReaderWith7BitEncodedInts : BinaryReader
28  {
29    public BinaryReaderWith7BitEncodedInts(Stream stream) : base(stream)
30    {
31    }
32   
33    public override short ReadInt16()
34    {
35      return unchecked((short)(ushort)base.Read7BitEncodedInt());
36    }
37   
38    [CLSCompliant(false)]
39    public override ushort ReadUInt16()
40    {
41      return unchecked((ushort)base.Read7BitEncodedInt());
42    }
43   
44    public override int ReadInt32()
45    {
46      return base.Read7BitEncodedInt();
47    }
48   
49    [CLSCompliant(false)]
50    public override uint ReadUInt32()
51    {
52      return unchecked((uint)base.Read7BitEncodedInt());
53    }
54   
55    public override long ReadInt64()
56    {
57      return unchecked((long)this.ReadUInt64());
58    }
59   
60    [CLSCompliant(false)]
61    public override ulong ReadUInt64()
62    {
63      ulong num = 0;
64      int shift = 0;
65      while (shift < 64) {
66        byte b = this.ReadByte();
67        num |= (ulong)(b & 127) << shift;
68        shift += 7;
69        if ((b & 128) == 0) {
70          return num;
71        }
72      }
73      throw new FormatException("Invalid 7-bit int64");
74    }
75  }
76
77  /// <summary>
78  /// A binary writer that encodes all integers as 7-bit-encoded-ints.
79  /// </summary>
80  public sealed class BinaryWriterWith7BitEncodedInts : BinaryWriter
81  {
82    public BinaryWriterWith7BitEncodedInts(Stream stream) : base(stream)
83    {
84    }
85   
86    public override void Write(short value)
87    {
88      base.Write7BitEncodedInt(unchecked((ushort)value));
89    }
90   
91    [CLSCompliant(false)]
92    public override void Write(ushort value)
93    {
94      base.Write7BitEncodedInt(value);
95    }
96   
97    public override void Write(int value)
98    {
99      base.Write7BitEncodedInt(value);
100    }
101   
102    [CLSCompliant(false)]
103    public override void Write(uint value)
104    {
105      base.Write7BitEncodedInt(unchecked((int)value));
106    }
107   
108    public override void Write(long value)
109    {
110      this.Write(unchecked((ulong)value));
111    }
112   
113    [CLSCompliant(false)]
114    public override void Write(ulong value)
115    {
116      while (value >= 128) {
117        this.Write(unchecked((byte)(value | 128u)));
118        value >>= 7;
119      }
120      this.Write(unchecked((byte)value));
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.