1
// Copyright (c) 2011 Daniel Grunwald
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
22
namespace ICSharpCode.NRefactory.Utils
25
/// A binary reader that can read the output of BinaryWriterWith7BitEncodedInts.
27
public sealed class BinaryReaderWith7BitEncodedInts : BinaryReader
29
public BinaryReaderWith7BitEncodedInts(Stream stream) : base(stream)
33
public override short ReadInt16()
35
return unchecked((short)(ushort)base.Read7BitEncodedInt());
39
public override ushort ReadUInt16()
41
return unchecked((ushort)base.Read7BitEncodedInt());
44
public override int ReadInt32()
46
return base.Read7BitEncodedInt();
50
public override uint ReadUInt32()
52
return unchecked((uint)base.Read7BitEncodedInt());
55
public override long ReadInt64()
57
return unchecked((long)this.ReadUInt64());
61
public override ulong ReadUInt64()
66
byte b = this.ReadByte();
67
num |= (ulong)(b & 127) << shift;
73
throw new FormatException("Invalid 7-bit int64");
78
/// A binary writer that encodes all integers as 7-bit-encoded-ints.
80
public sealed class BinaryWriterWith7BitEncodedInts : BinaryWriter
82
public BinaryWriterWith7BitEncodedInts(Stream stream) : base(stream)
86
public override void Write(short value)
88
base.Write7BitEncodedInt(unchecked((ushort)value));
92
public override void Write(ushort value)
94
base.Write7BitEncodedInt(value);
97
public override void Write(int value)
99
base.Write7BitEncodedInt(value);
102
[CLSCompliant(false)]
103
public override void Write(uint value)
105
base.Write7BitEncodedInt(unchecked((int)value));
108
public override void Write(long value)
110
this.Write(unchecked((ulong)value));
113
[CLSCompliant(false)]
114
public override void Write(ulong value)
116
while (value >= 128) {
117
this.Write(unchecked((byte)(value | 128u)));
120
this.Write(unchecked((byte)value));