現在、BaseStream.Position
とBaseStream.Length
のプロパティをキャッシュするBinaryReaderを作成中です。ここで私はこれまで持っているものです:代わりにLength
とPosition
性を付与するBinaryReaderのパフォーマンスを改善する
public class FastBinaryReader
{
BinaryReader reader;
public long Length { get; private set; }
public long Position { get; private set; }
public FastBinaryReader(Stream stream)
{
reader = new BinaryReader(stream);
Length = stream.Length;
Position = 0;
}
public void Seek(long newPosition)
{
reader.BaseStream.Position = newPosition;
Position = newPosition;
}
public byte[] ReadBytes(int count)
{
if (Position + count >= Length)
Position = Length;
else
Position += count;
return reader.ReadBytes(count);
}
public void Close()
{
reader.Close();
}
}
、私は私のように、私はFastBinaryReader.BaseStream.Position
とFastBinaryReader.BaseStream.Length
として私Position
とLength
プロパティを公開することができますBaseStream
プロパティを作成したいです既存のコードは元のBinaryReader
クラスと互換性を維持します。
どうすればいいですか?
最初に「BinaryReader.BaseStream」によってこれらのプロパティにアクセスしないのはなぜですか?結局のところ、ストリームがそれらをサポートしていない場合、キャッシングの量はそれを動作させることはありません。 – Jon
私の独自の位置と長さのプロパティを維持することで、End of Fileをチェックするときの読者のパフォーマンスが大幅に向上します。 1000バイトの読み込みテストでは、速度はほぼ倍増します。 –