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 / August 2006

Tip: Looking for answers? Try searching our database.

data disappearing from a pipe - what may be the reason?

Thread view: 
Thea - 29 Aug 2006 11:02 GMT
Hello everyone :)
My problem is as in topic.
I need to transfer data from output stream into input stream.
I have created pipe for that (java.nio.Pipe)

now I have my pipe for channelling data.
Problem appears while I try to use it:
I have my own output stream

object of this class is passed to another class, that writes into it,
it is created like this:
sos = new MyServletOutputStream(outputStream);

Now.
I can see the other class writing to this stream using write(byte b[],
int off, int len) method:
11:33:25,919 INFO  [Resize] writing 0; 16384
11:33:25,920 INFO  [Resize] writing 0; 16384
11:33:25,920 INFO  [Resize] writing 0; 15977

However when I try to read from inputStream, I see it as empty
11:33:26,019 INFO  [Resize] on other end of pipe: 0
0 is a result of imputStream.avaliable()

Where does this data go?
How to retrieve it back?
Is there maybe any strange buffering?
Class writing into pipe is run from inside Resize class.
Some code (only relevant parts):

   InputStream inputStream=null;
   OutputStream outputStream=null;
   Pipe pipe=null;
    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws ServletException, IOException {

   final HttpServletResponse resp = (HttpServletResponse) response;
   final HttpServletRequest req = (HttpServletRequest) request;

   pipe = Pipe.open();
   inputStream = Channels.newInputStream(pipe.source());
   outputStream = Channels.newOutputStream(pipe.sink());
   MyServletResponse myresp= new MyServletResponse(resp);
   log.info("My servlet response created! "+myresp);
   log.info("Calling other filters in chain");
   filterChain.doFilter(req,myresp);
    //after this call retrieved data should be in myresp's output stream
and thus in pipe...
   log.info("flushing output from previous filters");
   log.info("on other end of pipe: "+ inputStream.available());
   outputStream.flush();
    //I thought flush might help, but it didn't
   try {
   log.info("Creating ImageMagickProcesssor; avaliable input:
"+inputStream.available());

   ImageMagickProcessor im = new
ImageMagickProcessor(resp.getOutputStream(),inputStream,command);
    //this thing processes data obtained from pipe
   log.info("Calling ImageMagickProcesssor");
   try {
       im.processImageMagick();
   } catch (ImageMagickException e) {
       log.error("Error while processing ImageMagick",e);
   }

       } catch (IOException e) {
           log.error("Exception occured!",e);
       }
}

public class MyServletResponse implements HttpServletResponse {
   final HttpServletResponse wrapped;
    MyServletOutputStream sos = null;

   MyServletResponse(HttpServletResponse realResponse) {
       log.info("Constructing MyServletResponse...");
       this.wrapped = realResponse;
       log.info("Constructing response done");
   }
   public ServletOutputStream getOutputStream() throws IOException {
       synchronized(this) {
           if(sos == null) {
               sos = new MyServletOutputStream(outputStream);
           }
       }
       log.info("returning output stream...");
       return sos;
   }
}

    private class MyServletOutputStream extends ServletOutputStream {
       final OutputStream wrapper;

       public MyServletOutputStream(final OutputStream wrapper) {
           log.info("constructing my output stream...");
           this.wrapper = wrapper;
       }

       public void write(int b) throws IOException {
           wrapper.write(b);
           log.info("writing...");
       }

       public void write(byte b[]) throws IOException {
           wrapper.write(b);
           log.info("Writing...");
       }

       public void write(byte b[], int off, int len) throws
IOException {
           wrapper.write(b, off, len);
           log.info("writing "+off+"; "+len);
       }
   }

Any ideas will be appreciated ^^
Thea - 30 Aug 2006 08:55 GMT
Nevermind, I dropped idea of using pipes - this caused too many
problems.
I used temporary file.
This works just fine. ^^


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.