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

T

Thea

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 ^^
 
T

Thea

Nevermind, I dropped idea of using pipes - this caused too many
problems.
I used temporary file.
This works just fine. ^^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top