Is there a way to read a stream backwards? The stream is seekable.
Read a stream in reverse [duplicate]
You can reverse a stream by doing the following:
var reversedStream = new ReverseStream(stream);
To reverse a file:
using (var inputFile = File.OpenRead("file.dat"))
using (var inputFileReversed = new ReverseStream(inputFile))
using (var outputFile = File.Open("file.dat.reversed", FileMode.Create, FileAccess.Write))
{
inputFileReversed.CopyTo(outputFile);
}
Which uses the ReverseStream
class:
public class ReverseStream : Stream
{
readonly Stream stream;
public ReverseStream(Stream stream)
{
if (!stream.CanSeek) throw new Exception("Stream cannot seek");
stream.Seek(stream.Position, SeekOrigin.End);
this.stream = stream;
}
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => stream.Length;
public override long Position
{
get
{
var position = stream.Length - stream.Position;
return position;
}
set
{
stream.Position = stream.Length - value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
if (stream.Position == 0) return 0;
var startReadFrom = stream.Position - count;
if (startReadFrom < 0)
{
count += (int)startReadFrom;
startReadFrom = 0;
}
stream.Seek(startReadFrom, SeekOrigin.Begin);
var bytesRead = stream.Read(buffer, offset, count);
stream.Seek(startReadFrom, SeekOrigin.Begin);
Array.Reverse(buffer, offset, bytesRead);
return bytesRead;
}
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
stream.Seek(offset, SeekOrigin.End);
break;
case SeekOrigin.End:
stream.Seek(offset, SeekOrigin.Begin);
break;
case SeekOrigin.Current:
stream.Seek(-offset, SeekOrigin.Current);
break;
}
return Position;
}
public override void SetLength(long value)
{
}
public override void Write(byte[] buffer, int offset, int count)
{
}
public override void Flush()
{
}
}
In some edge cases streams could be sought and not have a fixed (ahead of time) Length. This class doesn't handle them (in fact it couldn't be handled). E.g.: some endless streams. –
Ellen
© 2022 - 2024 — McMap. All rights reserved.