10

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

     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?

scobur
  • 275
  • 2
  • 3
  • 9
  • 3
    Computers are very fast. Nothing is ever "horrible" unless you need to do it a very large number of times, or its very performance-critical. – JimN Dec 01 '12 at 03:22
  • You should change the title: it is data not date. Then it would be horrible to write a 8 byte date timestamp to a file. – AlexWien Dec 01 '12 at 03:28
  • I mean it's too complex for a programmer ,not the computer.And I want to know is it necessary to code in this way. – scobur Dec 01 '12 at 03:32
  • There are two questions there. The second is trivial and is answered by the Javadoc. Is the first question your real question? If so you should amend your title. – user207421 Dec 02 '12 at 02:36

4 Answers4

8

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.

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.


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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

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),

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.

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

From Java Docs public class ByteArrayInputStream extends InputStream

Yugansh
  • 365
  • 3
  • 9
0

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

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

InputStream is an abstract class and all classes extend from it are representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input. Whereas 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.

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. Because ByteArrayInputStream is providing implementation of read() where in InputStream method read() is abstract.

Oli
  • 9,766
  • 5
  • 25
  • 46
Amir Qayyum Khan
  • 473
  • 2
  • 15