java InputStream 和 ByteArrayInputStream 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13656004/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:30:52  来源:igfitidea点击:

What's the difference between InputStream and ByteArrayInputStream?

javainputstreamjnlpbytearrayinputstream

提问by scobur

The following code is extracted from the java web start chapter of the core java volume 1.

以下代码摘自java核心卷1的java web start章节。

     ByteArrayOutputStream out = new ByteArrayOutputStream();
     PrintStream printOut = new PrintStream(out);
     printOut.print(panel.getText());
     //panel.getText() return a String
     InputStream data = new ByteArrayInputStream(out.toByteArray());
     FileSaveService service = (FileSaveService) ServiceManager
           .lookup("javax.jnlp.FileSaveService");
     service.saveFileDialog(".", new String[] { "txt" }, data, "calc.txt");

There are four objects created ,the stream is redirected three times. Is there any other methods to write data to a file by using jnlp api? what's the difference between InputStream and ByteArrayInputStream?

创建了四个对象,流被重定向了三次。有没有其他方法可以使用 jnlp api 将数据写入文件?InputStream 和 ByteArrayInputStream 有什么区别?

回答by Bohemian

ByteArrayInputStreamand ByteArrayOututStreamare in-memory implementations for use when you want to temporarily store the data in memory in a stream-like fashion, then pump it out again somewhere else.

ByteArrayInputStream并且ByteArrayOututStream是内存中的实现,用于当您想以类似流的方式临时将数据存储在内存中,然后在其他地方再次将其抽出时使用。

For example, let's assume you have a method that expects an input stream as a parameter, eg

例如,假设您有一个方法需要输入流作为参数,例如

public Document parseXml(InputStream in) // build an XML document from data read in

but you want to send the contents of say a String to it. Then you'd use a ByteArrayInputStreamand fill it with the contents of your String and pass the ByteArrayInputStreamto the method.

但是您想将 say 字符串的内容发送给它。然后你会使用 aByteArrayInputStream并用你的 String 的内容填充它并将 传递ByteArrayInputStream给方法。


An example of an ByteArrayOutputStreamusage might be if a method writes to an output stream, but you just want to capture the result and get it directly.


一个ByteArrayOutputStream用法示例可能是,如果一个方法写入输出流,但您只想捕获结果并直接获取它。

回答by Yugansh

The InputStream is an abstract class and ByteArrayInputStream is a concrete class of InputStream and offers its own implementation of the abstract idea given by (InputStream),

InputStream 是一个抽象类,而 ByteArrayInputStream 是 InputStream 的一个具体类,并提供了它自己的由 (InputStream) 给出的抽象思想的实现,

In Addition :

此外 :

  • A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream.
  • An internal counter keeps track of the next byte to be supplied by the read method.
  • ByteArrayInputStream 包含一个内部缓冲区,其中包含可以从流中读取的字节。
  • 一个内部计数器跟踪由 read 方法提供的下一个字节。

Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

关闭 ByteArrayInputStream 没有任何效果。可以在关闭流后调用此类中的方法,而不会生成 IOException。

From Java Docs public class ByteArrayInputStream extends InputStream

来自 Java Docs 公共类 ByteArrayInputStream 扩展了 InputStream

回答by AlexWien

InputStream is the common Interface for input streams.
FileInputStream and ByteArrayInputStream both implement that interface.

InputStream 是输入流的通用接口。
FileInputStream 和 ByteArrayInputStream 都实现了该接口。

回答by Amir Qayyum Khan

InputStreamis an abstract class and all classes extend from it are representing an input stream of bytes. Applications that need to define a subclass of InputStreammust always provide a method that returns the next byte of input. Whereas a ByteArrayInputStreamcontains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the readmethod.

InputStream是一个抽象类,从它扩展的所有类都表示字节输入流。需要定义 的子类的应用程序InputStream必须始终提供返回输入的下一个字节的方法。而 aByteArrayInputStream包含一个内部缓冲区,其中包含可以从流中读取的字节。内部计数器跟踪该read方法要提供的下一个字节。

Because of polymorphism concept, you can assign a child to the parent just like

由于多态概念,您可以将子项分配给父项,就像

InputStream data = new ByteArrayInputStream(out.toByteArray());

If we will call data.read()that means we are calling readmethod of ByteArrayInputStream. Because ByteArrayInputStreamis providing implementation of read()where in InputStreammethod read()is abstract.

如果我们调用data.read(),则意味着我们正在调用 的read方法ByteArrayInputStream。因为ByteArrayInputStream提供read()where inInputStream方法的实现read()是抽象的。