我正在使用BinaryReader来解析某些数据。我想将读取的字节复制到辅助流。我怎么能这样做?
如果我从BinaryReader派生(实际上我需要以不同的理由这样做),我需要覆盖哪些方法以获得正确的行为?
答案 0 :(得分:2)
BinaryReader
不是真正的Stream
...它更像是另一个流上的叠加层。你可以:
using (var yourStream = /* ... */)
using (var br = new BinaryReader(yourStream))
{
using (var ms = new MemoryStream())
{
// Important the true, so that the ms remains open!
using (var bw = new BinaryWriter(ms, Encoding.UTF8, true))
{
var readed = br.ReadInt32();
bw.Write(readed);
// All the basic types are supported by various overlaods
}
// Here you have your ms... If you want to reread it:
ms.Position = 0;
using (var br2 = new BinaryReader(ms, Encoding.UTF8, true))
{
// Reread it
}
// Or copy it to another stream, or do whatever you want
}
}
如果您只是想在阅读其他流时复制流,可以使用以下内容:
public class CopyStream : Stream
{
// This is the stream you want to read your data from
public Stream ReadStream { get; set; }
// This is the "logger" stream, where a copy of read data will be put
public Stream LogStream { get; set; }
public override int Read(byte[] buffer, int offset, int count)
{
int res = ReadStream.Read(buffer, offset, count);
if (LogStream != null)
{
if (res > 0)
{
LogStream.Write(buffer, offset, res);
}
}
return res;
}
public override int ReadByte()
{
int res = ReadStream.ReadByte();
if (LogStream != null)
{
if (res >= 0)
{
LogStream.WriteByte((byte)res);
}
}
return res;
}
public override bool CanSeek
{
get { return false; }
}
}
您需要实施/ throw NotImplementedException()
许多其他方法,因为Stream
是abstract class
。右键单击Stream
,Implement Abstract Class
将加快此速度。
基本用途是:
var cs = new CopyStream
{
ReadStream = yourStream,
LogStream = whereyouwanttoWriteStream,
};
using (var br = new BinaryReader(CopyStream, Encoding.UTF8, true))
{
// Here you can read normally from the BinaryReader.
// Everything you read will be copied to LogStream
}
请注意,子类化BinaryReader
非常复杂,因为每个方法都以不同的方式加载数据,因此不存在单个"拦截点"。
答案 1 :(得分:1)
Streams意在封装,而不是覆盖:
public class DuploStream : Stream
{
private readonly Stream _underlyingStream;
private readonly Stream _copyStream;
public DuploStream(Stream underlyingStream, Stream copyStream)
{
_underlyingStream = underlyingStream;
_copyStream = copyStream;
}
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return false; } }
public override void Flush()
{
_underlyingStream.Flush();
}
public override long Length { get { throw new NotSupportedException(); } }
public override long Position { get { throw new NotSupportedException(); } set { throw new NotSupportedException(); } }
public override int Read(byte[] buffer, int offset, int count)
{
var bytesRead = _underlyingStream.Read(buffer, offset, count);
_copyStream.Write(buffer, offset, bytesRead);
return bytesRead;
}
public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
public override void SetLength(long value) { throw new NotSupportedException(); }
public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
}
然后,不是在基础流之上创建BinaryReader
,而是在DuploStream
之上构建它,传递基础流和复制流。