- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在搜索 BinaryReader.Skip 函数时遇到了这个 feature request on msdn .他说您可以通过使用它来提供自己的 BinaryReader.Skip() 函数。
只看这段代码,我想知道他为什么选择这种方式来跳过一定数量的字节:
for (int i = 0, i < count; i++) {
reader.ReadByte();
}
这和:
有区别吗?reader.ReadBytes(count);
即使这只是一个小的优化,我也想理解。因为现在我不明白为什么要使用 for 循环。
public void Skip(this BinaryReader reader, int count) {
if (reader.BaseStream.CanSeek) {
reader.BaseStream.Seek(count, SeekOffset.Current);
}
else {
for (int i = 0, i < count; i++) {
reader.ReadByte();
}
}
}
最佳答案
不,没有区别。 编辑:假设流有足够的轮空
ReadByte
方法简单地转发到底层 Stream 的 ReadByte
方法。
ReadBytes
方法调用底层流的 Read
直到它读取所需的字节数。
它是这样定义的:
public virtual byte[] ReadBytes(int count) {
if (count < 0) throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.Ensures(Contract.Result<byte[]>() != null);
Contract.Ensures(Contract.Result<byte[]>().Length <= Contract.OldValue(count));
Contract.EndContractBlock();
if (m_stream==null) __Error.FileNotOpen();
byte[] result = new byte[count];
int numRead = 0;
do {
int n = m_stream.Read(result, numRead, count);
if (n == 0)
break;
numRead += n;
count -= n;
} while (count > 0);
if (numRead != result.Length) {
// Trim array. This should happen on EOF & possibly net streams.
byte[] copy = new byte[numRead];
Buffer.InternalBlockCopy(result, 0, copy, 0, numRead);
result = copy;
}
return result;
}
对于大多数流,ReadBytes
可能会更快。
关于c# - 多个 BinaryReader.Read() 和 BinaryReader.ReadBytes(int i) 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4078936/
我在搜索 BinaryReader.Skip 函数时遇到了这个 feature request on msdn .他说您可以通过使用它来提供自己的 BinaryReader.Skip() 函数。 只看
我有一个基于套接字的应用程序,它使用 BinaryReader 公开接收到的数据客户端的对象。我一直在尝试调试读取器中包含的数据不干净的问题......即我正在读取的缓冲区包含超过新数据大小的旧数据。
Controller : private readonly Dictionary streams; public ActionResult Upload(string qqfile,
我想尝试基本的文件加密和压缩,所以我的第一个问题是 ByteReader 是否是适合该作业的正确阅读器?我的第二个最重要的问题是:使用二进制阅读器时如何循环直到文件末尾?有没有办法像 How read
我需要将 int 数组作为二进制写入输出文件,并在 C++ Linux 的程序中将二进制数据作为 int 读取,类似于 C# 中的 BinaryReader 和 BinaryWriter。我怎样才能做
我正在做家庭作业,但完全卡住了!我想要做的是使用已经定义的输入并使用 saveDataTo() 方法将其保存到文件中,然后使用 readDataFrom() 方法读取输入。 我卡在了第一部分。我不确定
我目前正在编写一个缓存 BaseStream.Position 和 BaseStream.Length 属性的 BinaryReader。这是我目前所拥有的: public class FastBin
我正在使用以下代码序列化数据表中的数据。 var rows = new List>(); 我正在填充 DataTable 中的行并将它们放入字典中。不要问为什么:) using(var fileStr
我正在开发一个解析器来接收 UDP 信息、解析它并存储它。为此,我使用了 BinaryReader,因为它主要是二进制信息。其中一些将是字符串。 MSDN says对于 ReadString() 函数
案例: 再次尝试通过我的 NIC 捕获数据包, 我开发了 2 个扩展用于捕获可变位数 public static string ReadBits ( this BinaryReader Key
当使用 System.IO.BinaryReader 读取二进制文件时,使用任何多字节读取方法时字节顺序似乎不受影响。 BinaryReader br = new BinaryReader(fs, S
我写了下面的方法来使用二进制模式将文件归档到一个文件中: // Compile archive public void CompileArchive(string Fi
我有一个二进制字节数组。在 C# 中,使用 BinaryReader 和 ReadInt32 读取它非常容易(这正是我所需要的)。像这样: reader = new BinaryReader ( ne
我正在继承 BinaryReader 类。 我必须覆盖一些基本方法,例如 ReadUInt16 . 该方法的内部实现是: public virtual ushort ReadUInt16(){
在 C、C++ 中,如果您以二进制方式写入一个整数(例如 fwrite(&someInt,sizeof(int),1,fp))说到文件,您有很多问题需要考虑你想从不同的机器上读取那个整数——例如,字节
BinaryReader 没有 EndOfStream 属性。使用以下代码检查是否已到达流结尾是否安全? reader.BaseStream.Length>reader.BaseStream.Posi
我继承了一些循环通过 BinaryReader 的响应的代码,并且它在一段时间内工作正常(返回 2 个字节),但是客户端需要一段时间才能响应(我假设)并且代码失败进入捕获逻辑。 我找不到任何关于 Re
我想弄清楚我在这里做错了什么。我正在尝试使用 Binary Reader 来轻松地将流中的初始四个字节转换为 Int32 值,该值告诉我其余数据的预期长度。 static void Main(stri
我正在尝试解压缩字节数组并使用二进制读取器将其转换为字符串。执行以下代码时,inStream位置从0变为数组长度,但str始终为空字符串。 BinaryReader br = null; string
我认为 BinaryReader.ReadChars() 方法存在问题。当我在原始套接字 NetworkStream 周围包装 BinaryReader 时,偶尔会出现流损坏,其中正在读取的流不同步。
我是一名优秀的程序员,十分优秀!