Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

constructing a graph (linking streams)

Thread view: 
Martijn - 26 Apr 2007 17:43 GMT
Hi,

I want to create a graph where output from one component is input for the
next.  The components are invoked serially, so one component needs to be
done before the next one kicks in:

         +--------+       +--------+       +--------+
   ====> | COMP 1 | ====> | COMP 2 | ====> | COMP 3 | ====>
         +--------+       +--------+       +--------+

Lets consider all streams to be byte streams to keep it simple (or char
streams if that would help).  What I have done now is create an array, e.g.
byte[] buf and simply use a ByteArrayOutputStream and copy the output into
it, and create a new ByteArrayInputStream (I might be able to reset it, but
I figured the overhead would be minimal, as it was using the same buffer
anyway, so I chose to play it safe).

In code (forgive me for any mistakes):

   byte[]                   buf;
   ByteArrayInputStream     istream = new ByteArrayInputStream(buf);
   ByteArrayOutputStream    ostream = new ByteArrayOutputStream();

   // init the buffer (from file, which happens to be a component with no
input)

   Comp1(ostream, istream);

   buf = ostream.toByteArray(); // this invalidates the original buffer,
right?
   ostream.reset();

   Comp2(ostream, istream);

   // etc...

But this invalidates my buffer each time and requires an extra copy phase I
would like to get rid of.  Any more appropriate solution available for this?
Which classes should I have a look at?  Any pointers are greatly
appreciated!

Thanks for the help,

Signature

Martijn
http://www.sereneconcepts.nl

Joshua Cranmer - 28 Apr 2007 00:43 GMT
> Hi,
>
[quoted text clipped - 19 lines]
>
> Thanks for the help,

What I tend to prefer for chained parsers is to define new InputStreams
that take the previous one as a source, like so:

Step1Transformer s1t = new Step1Transformer(input);
Step2Transformer s2t = new Step2Transformer(s1t);
byte[] data = s2t.getDataHowever(); // Internally invokes s1t's parser.
Martijn - 29 Apr 2007 07:59 GMT
>> Hi,
>>
[quoted text clipped - 27 lines]
> byte[] data = s2t.getDataHowever(); // Internally invokes s1t's
> parser.

Hi Joshua,

that sounds like a really good idea.  It is similar to two approaches I had
taken, but much simpeler!

1) Each component had a reader as an argument and returned a
   reader which could be fed to the next component (this is
   what I have finally chosen, but I might replace it with
   your approach at a later time)

2) Create a callback-wrapper class that has an abstract
   method in the form of comp1(reader, writer), then chain
   the graph in a graph object using pipes.  This was only
   tested, but not fully implemented.

Thanks for the tip!

Signature

Martijn
http://www.sereneconcepts.nl



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.