简体   繁体   English

InputStream和ByteArrayInputStream有什么区别?

[英]What's the difference between InputStream and ByteArrayInputStream?

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? 还有其他使用jnlp api将数据写入文件的方法吗? what's the difference between InputStream and ByteArrayInputStream? InputStream和ByteArrayInputStream有什么区别?

ByteArrayInputStream and ByteArrayOututStream are 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. ByteArrayInputStreamByteArrayOututStream是内存中的实现,当您想以类似流的方式将数据临时存储在内存中,然后再次将其泵出其他位置时,可以使用它们。

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 ByteArrayInputStream and fill it with the contents of your String and pass the ByteArrayInputStream to the method. 然后,您将使用ByteArrayInputStream并用String的内容填充它,并将ByteArrayInputStream传递给该方法。


An example of an ByteArrayOutputStream usage might be if a method writes to an output stream, but you just want to capture the result and get it directly. ByteArrayOutputStream用法的一个示例可能是某个方法写入输出流,但是您只想捕获结果并直接获取它。

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是InputStream的抽象类,而ByteArrayInputStream是InputStream的具体类,并提供了自己对(InputStream)给出的抽象思想的实现,

In Addition : 此外 :

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

Closing a ByteArrayInputStream has no effect. 关闭ByteArrayInputStream无效。 The methods in this class can be called after the stream has been closed without generating an IOException. 可以在关闭流之后调用此类中的方法,而不会产生IOException。

From Java Docs public class ByteArrayInputStream extends InputStream 从Java Docs公共类ByteArrayInputStream扩展InputStream

InputStream is the common Interface for input streams. InputStream是输入流的通用接口。
FileInputStream and ByteArrayInputStream both implement that interface. FileInputStream和ByteArrayInputStream都实现该接口。

InputStream is an abstract class and all classes extend from it are representing an input stream of bytes. InputStream是一个抽象类,从其扩展的所有类都表示一个字节输入流。 Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input. 需要定义InputStream子类的应用程序必须始终提供一种返回下一个输入字节的方法。 Whereas a ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. ByteArrayInputStream包含一个内部缓冲区,该缓冲区包含可以从流中读取的字节。 An internal counter keeps track of the next byte to be supplied by the read method. 内部计数器跟踪由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 read method of ByteArrayInputStream . 如果我们调用data.read() ,则意味着我们正在调用ByteArrayInputStream read方法。 Because ByteArrayInputStream is providing implementation of read() where in InputStream method read() is abstract. 因为ByteArrayInputStream提供了read() ,所以在InputStream方法中read()是抽象的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM