Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream chaining in Java

Tags:

java

io

Is it bad style to keep the references to streams "further down" a filter chain, and use those lower level streams again, or even to swap one type of stream for another? For example:

    OutputStream os = new FileOutputStream("file");
    PrintWriter pw = new PrintWriter(os);
    pw.print("print writer stream");
    pw.flush();
    pw = null;
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeBytes("dos writer stream");
    dos.flush();
    dos = null;
    os.close();

If so, what are the alternatives if I need to use the functionality of both streams, e.g. if I want to write a few lines of text to a stream, followed by binary data, or vice versa?

like image 989
lxgr Avatar asked Nov 02 '11 21:11

lxgr


People also ask

What is chain stream?

stream-chain creates a chain of streams out of regular functions, asynchronous functions, generator functions, and existing streams, while properly handling backpressure. The resulting chain is represented as a Duplex stream, which can be combined with other streams the usual way.

How do I combine two streams in Java?

concat() in Java. Stream. concat() method creates a concatenated stream in which the elements are all the elements of the first stream followed by all the elements of the second stream. The resulting stream is ordered if both of the input streams are ordered, and parallel if either of the input streams is parallel.

How do I concatenate 3 streams?

With three streams we could write Stream. concat(Stream. concat(a, b), c) .

What is method chaining in Java?

Method Chaining is the practice of calling different methods in a single line instead of calling other methods with the same object reference separately. Under this procedure, we have to write the object reference once and then call the methods by separating them with a (dot.).


2 Answers

This can be done in some cases, but it's error-prone. You need to be careful about buffers and stuff like the stream headers of ObjectOutputStream.

if I want to write a few lines of text to a stream, followed by binary data, or vice versa?

For this, all you need to know is that you can convert text to binary data and back but always need to specify an encoding. However, it is also error-prone because people tend to use the API methods that use the platform default encoding, and of course you're basically implementing a parser for a custom binary file format - lots of things can go wrong there.

All in all, if you're creating a file format, especially when mixing text and binary data, it's best to use an existing framework like Google protocol buffers

like image 150
Michael Borgwardt Avatar answered Sep 23 '22 08:09

Michael Borgwardt


If you have to do it, then you have to do it. So if you're dealing with an external dependency that you don't have control over, you just have to do it.

I think the bad style is the fact that you would need to do it. If you had to send binary data across sometimes, and text across at others, it would probably be best to have some kind of message object and send the object itself over the wire with Serialization. The data overhead isn't too much if structured properly.

like image 42
corsiKa Avatar answered Sep 21 '22 08:09

corsiKa