BinaryReader.ReadBytes问题 [英] BinaryReader.ReadBytes issue

查看:102
本文介绍了BinaryReader.ReadBytes问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试将一个巨大的二进制文件的读数优化为一个字节[] ......


我我正在做以下事情..


byte [] ba = new byte [br.BaseStream.Length];


ba = br.ReadBytes ((int)br.BaseStream.Length);


问题是。,BinaryReader.ReadBytes(...)只取一个int wherase

BinaryReader .BaseStream.Length很长。为什么没有一个ReadBytes

需要很长时间?


很可能我不会遇到这个问题但问题就是没有了

less。

解决方案

<<。>>写道:

我正在尝试将一个巨大的二进制文件的读取优化为一个字节[] ......

我正在做以下事情......

byte [] ba = new byte [br.BaseStream.Length];

ba = br.ReadBytes((int)br.BaseStream.Length);


为什么要分配一个数组然后立即把它变成

垃圾?

问题是。,BinaryReader.ReadBytes (...)只取一个int wherase
BinaryReader.BaseStream.Length很长。为什么不存在需要很长时间的ReadBytes?

有可能我不会遇到这个问题,但问题就是没有
少了。



这肯定不理想,但我希望如果你实际上有一个大于2Gb的

文件,你不会想要阅读它全部在

单打电话。


-

Jon Skeet - < sk *** @ pobox。 com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件




" Jon Skeet [C# MVP] QUOT; < SK *** @ pobox.com>在消息中写道

新闻:MP *********************** @ msnews.microsoft.co m ...

<<。>>写道:

我正在尝试将巨大的二进制文件的读取优化为
字节[] ......
我正在做以下事情......

byte [] ba = new byte [br.BaseStream.Length];

ba = br.ReadBytes((int)br.BaseStream.Length);
为什么要分配$ l $ b为什么ReadBytes重新分配或复制到其中?

问题是。,BinaryReader.ReadBytes(...)只取一个int wherase
BinaryReader.BaseStream.Length很长。为什么不存在需要很长时间的ReadBytes


我可能无法解决这个问题,但问题是没有
更少。
它''当然不是很理想,但我希望如果你实际上有一个大于2Gb的
文件,你也不会想要在
单一调用中读取它。




这是我不会接近这个范​​围的极端。


-
Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟
如果回复小组,请不要给我发邮件




我正在尝试将一个巨大的二进制文件的读取优化为一个字节[] ...


将大文件写入byte []并不是任何优化。 />

byte [] ba = new byte [br.BaseStream.Length];


你不需要初始化ba。当你使用

br.ReadBytes(...)

ba = br.ReadBytes((int)br.BaseStream.Length);




单行读取未知流(未知大小)是一种不好的做法。

问题是。, BinaryReader.ReadBytes(...)只取一个int wherase
BinaryReader.BaseStream.Length是一个long。为什么不存在需要很长时间的ReadBytes?




因为大多数情况下不需要用大文件填充内存。

(文件大于2 GB)

考虑您的设计和需求。如果你真的想要
读取大文件而不是你可以轻松拿出内存!


Marcin


Hi,

I am trying to optimize the reading of a huge binary file into a byte[]...

I am doing the following..

byte[] ba = new byte[br.BaseStream.Length];

ba = br.ReadBytes((int)br.BaseStream.Length);

The problem is., BinaryReader.ReadBytes(...) only takes an int wherase
BinaryReader.BaseStream.Length is a long. Why isnt there a ReadBytes that
takes a long?

Chances are I wont reach this problem but the problem will be there none the
less.


解决方案

<<.>> wrote:

I am trying to optimize the reading of a huge binary file into a byte[]...

I am doing the following..

byte[] ba = new byte[br.BaseStream.Length];

ba = br.ReadBytes((int)br.BaseStream.Length);
Why are you allocating an array and then immediately turning it into
garbage?
The problem is., BinaryReader.ReadBytes(...) only takes an int wherase
BinaryReader.BaseStream.Length is a long. Why isnt there a ReadBytes that
takes a long?

Chances are I wont reach this problem but the problem will be there none the
less.



It''s certainly not ideal, but I would expect that if you actually had a
file larger than 2Gb, you wouldn''t want to be reading it all in in a
single call anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...

<<.>> wrote:

I am trying to optimize the reading of a huge binary file into a byte[]...
I am doing the following..

byte[] ba = new byte[br.BaseStream.Length];

ba = br.ReadBytes((int)br.BaseStream.Length);
Why are you allocating an array and then immediately turning it into
garbage?



Why does ReadBytes reallocate it or copy into it?

The problem is., BinaryReader.ReadBytes(...) only takes an int wherase
BinaryReader.BaseStream.Length is a long. Why isnt there a ReadBytes that takes a long?

Chances are I wont reach this problem but the problem will be there none the less.
It''s certainly not ideal, but I would expect that if you actually had a
file larger than 2Gb, you wouldn''t want to be reading it all in in a
single call anyway.



Thats the extreme which I wont be anywhere near that range.


--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Hi,

I am trying to optimize the reading of a huge binary file into a byte[]...
Writing huge files into byte[] is not any optimisation.

byte[] ba = new byte[br.BaseStream.Length];
You don''t need the initialization of "ba" while you are using
br.ReadBytes(...)
ba = br.ReadBytes((int)br.BaseStream.Length);
This is a bad practise to read unknown stream (unknown size) in
single line.
The problem is., BinaryReader.ReadBytes(...) only takes an int wherase
BinaryReader.BaseStream.Length is a long. Why isnt there a ReadBytes that
takes a long?



Because mostly there''s no need to fill memory with huge files.
(files greater than 2 GB)
Think over your design and your needs. If you really want to
read huge files than you can easily get out of memmory!

Marcin


这篇关于BinaryReader.ReadBytes问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆