I know that java I/O uses decorator pattern. But I feel that I understand its wrong.
Please clarify difference between two code snippets:
snippet 1:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
objectOutputStream.writeObject("this is my string");
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
System.out.println(objectInputStream.readObject());
This application works according my expectations and I see result in console.
snippet 2:
I try to wrap ObjectInputStream and ObjectOutputStream twice:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
ObjectOutputStream objectOutputStreamWrapper = new ObjectOutputStream(objectOutputStream); //double wrapping
objectOutputStreamWrapper.writeObject("this is my string");
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
System.out.println(objectInputStreamWrapper.readObject());
This code just hangs up. I don't understand why. please clarify.
P.S.
It is theoretical question only.
UPDATE
Really hangs up behaviour happens because of I use pipes (According EJP answer).
for example this code works according expectations.
OutputStream outputStream = new FileOutputStream("2.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
ObjectOutputStream objectOutputStreamWrapper = new ObjectOutputStream(objectOutputStream); //double wrapping
objectOutputStreamWrapper.writeObject("this is my string");
objectOutputStream.close();
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("2.txt"));
ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
System.out.println(objectInputStreamWrapper.readObject());
objectInputStream.close();
looks like if I wrap input by 10 decorators I should wrap output by 10 decorators at the same order. Is it true ?
UPDATE + 1
I noticed that problem with flushing only:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
ObjectOutputStream objectOutputStreamWrapper = new ObjectOutputStream(objectOutputStream); //double wrapping
objectOutputStreamWrapper.writeObject("this is my string");
objectOutputStreamWrapper.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
System.out.println(objectInputStreamWrapper.readObject());
You're misusing piped streams. They are intended to be used by a producer thread doing writes and a consumer thread doing reads. See the Javadoc.
The piped streams share a buffer which can fill if the reading thread isn't reading, which stalls your writing thread.
Wrapping streams twice doesn't have anything to do with it, although in this case it is certainly both pointless and problematic.
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
//writing
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
//ObjectOutputStream objectOutputStreamWrapper =new ObjectOutputStream(objectOutputStream);
//ObjectOutputStream objectOutputStreamWrapper1=new ObjectOutputStream(objectOutputStreamWrapper);
//objectOutputStreamWrapper1.writeObject("this is my string");
//objectOutputStreamWrapper1.flush();
objectOutputStream.writeObject("this is my string");
//reading
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
//ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
//ObjectInputStream objectInputStreamWrapper1=new ObjectInputStream(objectInputStreamWrapper);
//System.out.println("going to read from piped source");
//System.out.println(objectInputStreamWrapper1.readObject());
System.out.println(objectInputStream.readObject());
Snippet 1 - OutputStream created with Piped stream, then the input stream directly receive the data from the pipe through buffer without flushing.
Update + 1 - OutputStream created with outputstream wrapper in turn with another wrapper(commented the wrapper statments) and so on works after flush statment. Flush explicitlty flushes the buffered data to the underlying stream, in our case a piped output stream or the output stream .
Nested stream can be used like FilterOutputStream can be used on top of ObjectOutputStream also.
Related
I have a serializable object and I want to write it down to file. Should I use ObjectOutputStream.writeObject() or convert it into a byte[] (using ObjectOutputStream) then use FileOutputStream.write()?
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(file))
objectOutputStream.writeObject(myObject);
or
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out;
out = new ObjectOutputStream(byteOut);
out.writeObject(myObject);
new FileOutputStream(file).write(byteOut.toByteArray());
Buffering can improve performance by coalescing small writes into larger bulk writes. If you want to introduce buffering use a BufferedOutputStream, which does what you're doing with the byte array stream without changing downstream code.
// Unbuffered
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
// Buffered
ObjectOutputStream outputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
This way you can continue to use ObjectOutputStream without the rest of the code having to know whether the stream is buffered or unbuffered.
As with any performance-related change, you should benchmark both approaches to see which performs better. Don't make changes like this without empirical testing. If you don't test, you're just guessing.
I have this client code
dOut = new DataOutputStream(socket.getOutputStream());
oos = new ObjectOutputStream(socket.getOutputStream());
dOut.writeByte(2); <--when readByte on server gives -84
oos.writeObject(rectangle);
if slightly changed
dOut = new DataOutputStream(socket.getOutputStream());
dOut.writeByte(2); <--when readByte on server gives 2
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(rectangle);
why is this happening? case is similar in inputstream as well.
Yes. The data will get hopelessly mixed up.
There's no need for this. ObjectOutputStream already has all the methods of DataOutputStream. You don't need them both.
I'm writing a java server for an assignment, and I have observed some strange behaviour when I write both into a wrapped stream and a wrapper stream, can this cause any problems ? As far as I see, it can, but how ? Pls enlighten me.
as an example:
OutputStream os = new OutputStream(...);
PrintWriter pw = new PrintWriter(os);
And I want to write both in the PrintWriter, and the OutputStream.
To transfer from byte-based stream to character-based stream, you need to use OutputStreamWriter:
An OutputStreamWriter is a bridge from character streams to byte streams.
So that would be:
OutputStream os = ...
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
PrintWriter pw = new PrintWriter(osw);
I think the problem is that you need to specify the encoding, since the constructor PrintWriter(OutputStream out) uses the default encoding which might not be correct for your data input:
OutputStream os = ...
PrintWriter pw = new PrintWriter(os, "UTF-8");
I've the necessity to share a streaming of data between two instances as below:
// get EClasses which should be connected
final uk.man.xman.xcore.Parameter source = getParameter(sourceAnchor);
final uk.man.xman.xcore.Parameter target = getParameter(targetAnchor);
// Set data channels
//Output stream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
source.setOutputStream(dataOutputStream);
//Input stream
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
target.setInputStream(inpuDataStream);
Everything works ok if I write, during those lines of code. Strangely, when I need to use the data channel to write something in another class, like here:
DataOutputStream dataOutputStream = (DataOutputStream) inputParameter.getOutputStream();
System.out.println("WRITE:" + attributes.getValue("value"));
dataOutputStream.writeUTF(attributes.getValue("value"));
dataOutputStream.flush();
I am not able to read, and I really do not know why. Am I missing something?
Thanks for your time
Not sure if that's what you're asking, but you're creating an InputStream that reads from an empty byte array. That doesn't make much sense:
// create an Output stream that will write in memory
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
...
// transform what has been written to the output stream into a byte array.
// Since othing has been written yet, outputStream.toByteArray() returns
// an empty array
DataInputStream inpuDataStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(outputStream.toByteArray())));
what about one BufferedOutputStream wrap another BufferedOutputStream? this question is simple. but confused.
As the following code,
OutputStream file = new FileOutputStream("test.txt");
OutputStream buffer = new BufferedOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(buffer); //wrap buffer twice
OutputStream outputStream = new ObjectOutputStream(bufferedOutputStream); // wrap as ObjectOutputStream
BufferedOutputStream bufferedOutputStream1 = new BufferedOutputStream(outputStream); //wrap back as BufferedOutputStream
ObjectOutput output = new ObjectOutputStream(bufferedOutputStream1);
What about it? Question?
If you're asking whether an inefficiency is introduced, the answer is 'no'. The code is optimised to handle that case, or rather the case where the transfer size >= the buffer size.