Piped<I/O>Stream. Can I pass complex objects?

I investigate java I/O. Now I am reading about pipes.

I wrote simplest code example:

PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();

pipedOutputStream.connect(pipedInputStream);

pipedOutputStream.write(new byte[]{1});
System.out.println(pipedInputStream.read());

I have following question. As I understand - it is very strange to pass bytes in real life.

Is it really to extend this example for pass entire String for example?

Answers


Yes. Decoreate it with ObjectInputStream and ObjectOutputStream.

PipedOutputStream pipedOutputStream = new PipedOutputStream();
PipedInputStream pipedInputStream = new PipedInputStream();

pipedInputStream.connect(pipedOutputStream);

ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);

objectOutputStream.writeObject("Hello world!");
String message = (String)objectInputStream.readObject();

System.out.println(message);

More info on the decoration pattern and specifically for the Java I/O stream decoration, you can find in this StackOverFlow Post

BTW, make sure to Initiate the ObjectOutputStream before the ObjectInputStream and also to connect the pipes using the connect method before the creation of the Object input/output streams. Here is why: http://frequal.com/java/OpenObjectOutputStreamFirst.html


Need Your Help

Special characters in web.config

asp.net .net-3.5 iis-7 web-config

I've create a custom configuration section in my web.config file for my 3.5 web application. The custom configuration contains special characters listed below.

Check for a file version from a Wix Bootstrapper Bundle

wix burn

I've created a bootstrapper application for my WIX installer. One of the installers that I am bundling needs to have an install condition based on the version of a file that is allready installed. I

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.