- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
目前,C# 中没有NetworkStream.Peek
方法。实现这种功能与 NetworkStream.ReadByte
类似的方法的最佳方法是什么,只是返回的 byte
实际上并未从 Stream
中删除?
最佳答案
我遇到了同样的“查看魔数(Magic Number)然后决定将流发送到哪个流处理器”的要求,不幸的是我无法摆脱这个问题 - 正如对 Aaronaught 的回答的评论中所建议的那样 - 通过传递已经在单独的参数中将字节消耗到流处理方法中,因为这些方法是给定的,他们期望 System.IO.Stream 而不是其他。
我通过创建一个或多或少通用的包装流的 PeekableStream 类解决了这个问题。它适用于 NetworkStreams,但也适用于任何其他 Stream,前提是您Stream.CanRead。
编辑
或者,您可以使用全新的 ReadSeekableStream
做
var readSeekableStream = new ReadSeekableStream(networkStream, /* >= */ count);
...
readSeekableStream.Read(..., count);
readSeekableStream.Seek(-count, SeekOrigin.Current);
无论如何,PeekableStream
来了:
/// <summary>
/// PeekableStream wraps a Stream and can be used to peek ahead in the underlying stream,
/// without consuming the bytes. In other words, doing Peek() will allow you to look ahead in the stream,
/// but it won't affect the result of subsequent Read() calls.
///
/// This is sometimes necessary, e.g. for peeking at the magic number of a stream of bytes and decide which
/// stream processor to hand over the stream.
/// </summary>
public class PeekableStream : Stream
{
private readonly Stream underlyingStream;
private readonly byte[] lookAheadBuffer;
private int lookAheadIndex;
public PeekableStream(Stream underlyingStream, int maxPeekBytes)
{
this.underlyingStream = underlyingStream;
lookAheadBuffer = new byte[maxPeekBytes];
}
protected override void Dispose(bool disposing)
{
if (disposing)
underlyingStream.Dispose();
base.Dispose(disposing);
}
/// <summary>
/// Peeks at a maximum of count bytes, or less if the stream ends before that number of bytes can be read.
///
/// Calls to this method do not influence subsequent calls to Read() and Peek().
///
/// Please note that this method will always peek count bytes unless the end of the stream is reached before that - in contrast to the Read()
/// method, which might read less than count bytes, even though the end of the stream has not been reached.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and
/// (offset + number-of-peeked-bytes - 1) replaced by the bytes peeked from the current source.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data peeked from the current stream.</param>
/// <param name="count">The maximum number of bytes to be peeked from the current stream.</param>
/// <returns>The total number of bytes peeked into the buffer. If it is less than the number of bytes requested then the end of the stream has been reached.</returns>
public virtual int Peek(byte[] buffer, int offset, int count)
{
if (count > lookAheadBuffer.Length)
throw new ArgumentOutOfRangeException("count", "must be smaller than peekable size, which is " + lookAheadBuffer.Length);
while (lookAheadIndex < count)
{
int bytesRead = underlyingStream.Read(lookAheadBuffer, lookAheadIndex, count - lookAheadIndex);
if (bytesRead == 0) // end of stream reached
break;
lookAheadIndex += bytesRead;
}
int peeked = Math.Min(count, lookAheadIndex);
Array.Copy(lookAheadBuffer, 0, buffer, offset, peeked);
return peeked;
}
public override bool CanRead { get { return true; } }
public override long Position
{
get
{
return underlyingStream.Position - lookAheadIndex;
}
set
{
underlyingStream.Position = value;
lookAheadIndex = 0; // this needs to be done AFTER the call to underlyingStream.Position, as that might throw NotSupportedException,
// in which case we don't want to change the lookAhead status
}
}
public override int Read(byte[] buffer, int offset, int count)
{
int bytesTakenFromLookAheadBuffer = 0;
if (count > 0 && lookAheadIndex > 0)
{
bytesTakenFromLookAheadBuffer = Math.Min(count, lookAheadIndex);
Array.Copy(lookAheadBuffer, 0, buffer, offset, bytesTakenFromLookAheadBuffer);
count -= bytesTakenFromLookAheadBuffer;
offset += bytesTakenFromLookAheadBuffer;
lookAheadIndex -= bytesTakenFromLookAheadBuffer;
if (lookAheadIndex > 0) // move remaining bytes in lookAheadBuffer to front
// copying into same array should be fine, according to http://msdn.microsoft.com/en-us/library/z50k9bft(v=VS.90).aspx :
// "If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved
// in a temporary location before destinationArray is overwritten."
Array.Copy(lookAheadBuffer, lookAheadBuffer.Length - bytesTakenFromLookAheadBuffer + 1, lookAheadBuffer, 0, lookAheadIndex);
}
return count > 0
? bytesTakenFromLookAheadBuffer + underlyingStream.Read(buffer, offset, count)
: bytesTakenFromLookAheadBuffer;
}
public override int ReadByte()
{
if (lookAheadIndex > 0)
{
lookAheadIndex--;
byte firstByte = lookAheadBuffer[0];
if (lookAheadIndex > 0) // move remaining bytes in lookAheadBuffer to front
Array.Copy(lookAheadBuffer, 1, lookAheadBuffer, 0, lookAheadIndex);
return firstByte;
}
else
{
return underlyingStream.ReadByte();
}
}
public override long Seek(long offset, SeekOrigin origin)
{
long ret = underlyingStream.Seek(offset, origin);
lookAheadIndex = 0; // this needs to be done AFTER the call to underlyingStream.Seek(), as that might throw NotSupportedException,
// in which case we don't want to change the lookAhead status
return ret;
}
// from here on, only simple delegations to underlyingStream
public override bool CanSeek { get { return underlyingStream.CanSeek; } }
public override bool CanWrite { get { return underlyingStream.CanWrite; } }
public override bool CanTimeout { get { return underlyingStream.CanTimeout; } }
public override int ReadTimeout { get { return underlyingStream.ReadTimeout; } set { underlyingStream.ReadTimeout = value; } }
public override int WriteTimeout { get { return underlyingStream.WriteTimeout; } set { underlyingStream.WriteTimeout = value; } }
public override void Flush() { underlyingStream.Flush(); }
public override long Length { get { return underlyingStream.Length; } }
public override void SetLength(long value) { underlyingStream.SetLength(value); }
public override void Write(byte[] buffer, int offset, int count) { underlyingStream.Write(buffer, offset, count); }
public override void WriteByte(byte value) { underlyingStream.WriteByte(value); }
}
关于C#:实现 NetworkStream.Peek?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2196767/
我希望有人可以解释这种行为,或者它是否可能是 .NET 中的错误。 由于夏令时而及时向后移动意味着 NetworkStream 不注意其属性 ReadTimeout,并且在此代码的情况下会导致循环旋转
我需要从 NetworkStream 中读取,它会随机发送数据,数据包的大小也会不断变化。我正在实现一个多线程应用程序,其中每个线程都有自己的流来读取。如果流中没有数据,应用程序应该一直等待数据到达。
我编写了以下函数来使用 NetworkStream 的异步读取函数(BeginRead 和 EndRead)实现超时功能。它工作正常,直到我注释掉行 Trace.WriteLine("bytesRea
如果 Internet 上的 B 类网络的子网掩码为 255.255.248.0,那么每个子网的最大主机数是多少?(一) 1022(乙) 1023(三) 2046(D) 2047 任何人都可以告诉我如
我读过一个 Tcp Echo Server 的例子,有些事情我不清楚。 TcpClient client = null; NetworkStream netStream = null; try {
This question already has answers here: NetworkStream.ReadAsync with a cancellation token never canc
我正在使用套接字编写一个简单的聊天程序。当我发送长消息时,随后刷新流和短消息,长消息的末尾会附加到短消息中。它看起来像这样: 发送“aaasdsd” 接收“aaasdsd” 发送“bb” 接收“bba
我目前正在从事一个网络项目,我在其中制定了一个二进制协议(protocol)。我的数据包看起来像这样:[1 字节类型][2 字节索引][2 字节长度][长度字节数据] 这是我接收数据包的代码: Net
我的应用程序打开一个 NetworkStream 和一个 StreamWriter 以将 HL7 消息发送到服务。此服务接收 HL7 消息并始终发回确认。我遇到以下问题:在发送 HL7 消息(有效,我
在同一台计算机上,我有 2 个控制台应用程序。 (客户端向服务器发送文件) 在开始之前,我只想提一下我正在使用这两个 async 函数: 从 NetworkStream 读取: public sta
我正在编写一个简单的客户端-服务器应用程序,在查看 MSDN 文档时,我遇到了一些有趣的流类型... http://msdn.microsoft.com/en-us/library/system.io
早上好 我需要一些建议,我正在使用从 TcpClient 流式传输的 networkStream,一切正常,但后来我需要其他地方的一些功能,这需要关闭流。一旦连接关闭,我似乎找不到重新打开连接的方法。
只是尝试使用 Networkstream,这是我写的一个简单代码: 客户端: TcpClient c = new TcpClient(); c.Connect("10.0
我使用 Moq 和 NUnit 作为单元测试框架。 我写了一个方法,将 NetworkStream 对象作为参数: public static void ReadDataIntoBuffer(Netw
好吧,所以我理解 TCP 是基于流而不是基于消息的整个想法 - 我不关心这个。 我想做的是简单地序列化一些东西,然后通过网络将它发送到另一个使用相同的基于消息的协议(protocol)的应用程序。 问
NetworkStream 从套接字缓冲区读取不应该存在的数据时出现问题。顺便说一句,我正在发送非常大的缓冲区。现在我刚刚在本地主机上进行测试。 这是我读取数据的方式,前 4 个字节包含消息的长度,然
我使用 TcpClient 和 NetworkStream 为我们的游戏创建了一个简单的持久套接字连接。正常连接、发送消息和断开连接(退出应用程序/服务器关闭/等)都没有问题。 但是,我遇到了一些问题
有问题的数据是一个 PNG 文件,前缀为 int 大小。 -Sending: ns.Write(BitConverter.GetBytes((int)data.Length),0,4); ns.Wri
NetworkStream.DataAvailable 是否知道发送方的发送缓冲区是否为空?或者它只是简单地指示接收方的读取缓冲区是否有数据?我的假设是后者... 具体来说,对于一些涉及正在进行的对话
我有一个类,它包装了一个 Stream 实例,所以我可以用它来包装 FileStreams 和 NetworkStreams。现在,我想测试流是否仍在传递任何数据,换句话说,我想测试 NetworkS
我是一名优秀的程序员,十分优秀!