1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
using System;
using System.IO;
using System.Linq;
namespace SharpCompress.IO
{
internal class MarkingBinaryReader : BinaryReader
{
public MarkingBinaryReader(Stream stream)
: base(stream)
{
}
public long CurrentReadByteCount { get; private set; }
public void Mark()
{
CurrentReadByteCount = 0;
}
public override int Read()
{
throw new NotImplementedException();
}
public override int Read(byte[] buffer, int index, int count)
{
throw new NotImplementedException();
}
public override int Read(char[] buffer, int index, int count)
{
throw new NotImplementedException();
}
public override bool ReadBoolean()
{
return BitConverter.ToBoolean(ReadBytes(1), 0);
}
public override byte ReadByte()
{
return ReadBytes(1).Single();
}
public override byte[] ReadBytes(int count)
{
CurrentReadByteCount += count;
var bytes = base.ReadBytes(count);
if (bytes.Length != count)
{
throw new EndOfStreamException(string.Format("Could not read the requested amount of bytes. End of stream reached. Requested: {0} Read: {1}", count, bytes.Length));
}
return bytes;
}
public override char ReadChar()
{
throw new NotImplementedException();
}
public override char[] ReadChars(int count)
{
throw new NotImplementedException();
}
#if !PORTABLE
public override decimal ReadDecimal()
{
return ByteArrayToDecimal(ReadBytes(16), 0);
}
private decimal ByteArrayToDecimal(byte[] src, int offset)
{
//http://stackoverflow.com/a/16984356/385387
var i1 = BitConverter.ToInt32(src, offset);
var i2 = BitConverter.ToInt32(src, offset + 4);
var i3 = BitConverter.ToInt32(src, offset + 8);
var i4 = BitConverter.ToInt32(src, offset + 12);
return new decimal(new[] { i1, i2, i3, i4 });
}
#endif
public override double ReadDouble()
{
return BitConverter.ToDouble(ReadBytes(8), 0);
}
public override short ReadInt16()
{
return BitConverter.ToInt16(ReadBytes(2), 0);
}
public override int ReadInt32()
{
return BitConverter.ToInt32(ReadBytes(4), 0);
}
public override long ReadInt64()
{
return BitConverter.ToInt64(ReadBytes(8), 0);
}
public override sbyte ReadSByte()
{
return (sbyte)ReadByte();
}
public override float ReadSingle()
{
return BitConverter.ToSingle(ReadBytes(4), 0);
}
public override string ReadString()
{
throw new NotImplementedException();
}
public override ushort ReadUInt16()
{
return BitConverter.ToUInt16(ReadBytes(2), 0);
}
public override uint ReadUInt32()
{
return BitConverter.ToUInt32(ReadBytes(4), 0);
}
public override ulong ReadUInt64()
{
return BitConverter.ToUInt64(ReadBytes(8), 0);
}
}
}
|