Java BufferInputStream 与 ByteArrayInputStream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18291074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BufferInputStream vs ByteArrayInputStream
提问by IUnknown
Here are three ways to read an entire file into memory before processing it:
以下是在处理之前将整个文件读入内存的三种方法:
Approach A:
方法一:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
Approach B:
方法B:
ByteArrayInputStream bi =
new ByteArrayInputStream(
org.apache.commons.io.FileUtils.readFileToByteArray(file))
Approach C:
方法 C:
File file = new File(yourFileName);
RandomAccessFile ra = new RandomAccessFile(yourFileName, "rw"):
byte[] b = new byte[(int)file.length()];
try {
ra.read(b);
} catch (Exception e) {
e.printStackTrace();
}
Why would I prefer one approach over another?
Are there any specific use-cases that call for one approach over another?
Why not use a fixed-length byte[]
instead?
为什么我更喜欢一种方法而不是另一种方法?
是否有任何特定用例需要一种方法而不是另一种方法?
为什么不使用固定长度byte[]
呢?
采纳答案by Durandal
Unless you need anything special in terms of capabilites (e.g. random access), an InputStream wrapped into a BufferedInputStream is the general purpose choice for reading sequentially from any kind of data source that provides a streaming capability.
除非您需要任何特殊的功能(例如随机访问),否则包装到 BufferedInputStream 中的 InputStream 是从提供流功能的任何类型的数据源中顺序读取的通用选择。
This will provide reasonable performance (through buffering), the code is generic in that it can process any stream, and also very important - the size of processable streams isn't limited by the available heap memory by this idiom.
这将提供合理的性能(通过缓冲),代码是通用的,因为它可以处理任何流,而且非常重要 - 可处理流的大小不受此习惯用法可用堆内存的限制。
So unless you have a very compelling reason to code against a special case API, use a standard InputStream and wrap it as needed.
因此,除非您有非常令人信服的理由针对特殊情况 API 进行编码,否则请使用标准 InputStream 并根据需要对其进行包装。
EDIT: In response to things asked in the Comment by IUnknown
编辑:回应 IUnknown 在评论中提出的问题
1 What is the approach in the case of a random access - I thought BufferedInputStream is the preferred solution even in that case?
1在随机访问的情况下有什么方法 - 我认为 BufferedInputStream 即使在这种情况下也是首选的解决方案?
There is no generic interface for random access. You thought wrong. You could at least be so courteous to study the basics of basics: http://docs.oracle.com/javase/tutorial/essential/io/
没有用于随机访问的通用接口。你想错了。您至少可以有礼貌地学习基础知识:http: //docs.oracle.com/javase/tutorial/essential/io/
2 the size of processable streams isn't limited by the available heap memory - the buffer must be having a limit.Are you saying that the internal array automatically re-sizes if its filled up while reading?
2可处理流的大小不受可用堆内存的限制 - 缓冲区必须有限制。您是说内部数组在读取时如果填满会自动重新调整大小吗?
Again, thats covered in the basics (see above). With ByteArrayInputStream you need a byte[] to hold the entire stream. How's that notlimited by memory? (even worse, its also hard capped by max array size).
同样,这在基础知识中涵盖(见上文)。使用 ByteArrayInputStream 您需要一个 byte[] 来保存整个流。怎么就这么不受内存限制?(更糟糕的是,它也受到最大数组大小的限制)。
3 Is there any difference in the buffer behavior between a Buffered stream and a ByteArray stream - I thought they are backed up by similar buffer behaviour
3 Buffered 流和 ByteArray 流之间的缓冲区行为是否有任何区别 - 我认为它们由类似的缓冲区行为支持
I don't know what to say. You thought wrong. Set aside the basic facts they both extend InputStream and somehow use a byte array internally (and technically either could be implemented without using any array, its just the most natural way to do it). They have nothingin common. A BufferedStream hold a small dynamic portion of anotherstream. ByteArrayInputStream was covered in 2.
我不知道该说些什么。你想错了。撇开基本事实,它们都扩展了 InputStream 并以某种方式在内部使用了字节数组(从技术上讲,两者都可以在不使用任何数组的情况下实现,这只是最自然的方法)。他们没有任何共同点。BufferedStream 包含另一个流的一小部分动态部分。ByteArrayInputStream 已在 2 中介绍。
Advice: People here will gladly advise you on which tool to use for which job. But don't expect to be spoon-fed. Show someeffort, stackoverflow isn't a tutoring site. Stop "thinking" and start "learning" - the tutorials are out there and have been since the dawn of the language.
建议:这里的人会很乐意为您建议使用哪种工具进行哪种工作。但不要指望被勺子喂养。表现出一些努力,stackoverflow 不是一个辅导网站。停止“思考”并开始“学习” - 教程就在那里,并且从语言诞生之日起就已经存在。