private long ReadInt64Impl(Stream stream, out int bytesRead, VarIntType varIntType) { long currentValue = 0; var byteIndex = 0; while (true) { var currentByte = stream.ReadByte(); if (currentByte == -1) { throw new EndOfStreamException(); } (var byteValue, var readMore) = this.DecodeSingleByte((byte)currentByte, byteIndex++, varIntType); currentValue |= byteValue; if (!readMore) { break; } } bytesRead = byteIndex; return(currentValue); }
private long ToInt64Impl(byte[] value, out int bytesRead, VarIntType varIntType) { long current = 0x00; var byteIndex = 0; while (true) { (var byteValue, var readMore) = this.DecodeSingleByte(value[byteIndex], byteIndex++, varIntType); current |= byteValue; if (!readMore) { break; } } bytesRead = byteIndex; return(current); }
private int GetMaxLength(VarIntType type) => type switch {
private (long byteValue, bool readMore) DecodeSingleByte(byte currentByte, int byteIndex, VarIntType varIntType) { if (byteIndex >= this.GetMaxLength(varIntType)) { throw new FormatException($"{varIntType} cannot span over more than ten bytes."); } return( byteValue : (currentByte & 0x7f) * (long)Math.Pow(0x80, byteIndex), readMore : (currentByte & 0x80) != 0 ); }