Greetings,

I have a question on how to buffer a HttpResponse and perform some
operations in each iteration. In short, I would like to buffer a
FileEntity response and update a JProgressBar with the transfer
progress of the FileEntity. Below is my code snippet:

                   response.setStatusCode(HttpStatus.SC_OK);
                   File fSharedFile = new File(getSharedFiles().get(path));
                   String sFileName = fSharedFile.getName();

                   if (fSharedFile.exists()) {
                       //display progress bar and set the size
                       showUlProgressBar(fSharedFile.getName());
                       setUlProgressBarSize(sFileName,
Integer.parseInt(String.valueOf(fSharedFile.length())));

                       FileEntity body = new FileEntity(fSharedFile,
"application/octet-stream");
                       body.setChunked(true);
                       BufferedHttpEntity bufBody = new
BufferedHttpEntity(body);
                       response.setEntity(bufBody);
                       InputStream in = bufBody.getContent();

                       OutputStream out = new
HttpDataOutputStream(new
SocketHttpDataTransmitter(htRequests.get(this.thisConn.toString()), 8
* 1024));

                       System.out.println("Serving file " +
fSharedFile.getPath());

                       // Transfer bytes from in to out
                       byte[] buf = new byte[8 * 1024];
                       int len;
                       int iTransferredSize = 0;

                       while ((len = in.read(buf)) > 0) {
                           out.write(buf, 0, len);
                           iTransferredSize += len;
                           //update file transfer progress
                           updateUlProgressBar(sFileName, iTransferredSize);
                       }

                       System.out.println("Done serving file " +
fSharedFile.getPath());

                       //Close streams
                       in.close();
                       out.close();

                       //hide progress bar
                       hideUlProgressBar(fSharedFile.getName());
                   }


The response will be sent to the client but it will be malformed. For
some reason the client receives the HTTP stream but is unable to act
on it.

I would be really glad if someone could point me in the right direction.

Thanks in advance.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to