Hello Safwan,

> 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.

Are you talking about buffering in the client or in the server?
Are you talking about a progress bar at the client or at the server?

> Below is my code snippet:
> 
>                    response.setStatusCode(HttpStatus.SC_OK);

This looks like server side. A user interface on the server side?

> Integer.parseInt(String.valueOf(fSharedFile.length())));

That's an awfully complex way to turn a long into an int.

>                        FileEntity body = new FileEntity(fSharedFile,
> "application/octet-stream");
>                        body.setChunked(true);
>                        BufferedHttpEntity bufBody = new
> BufferedHttpEntity(body);

I don't see the point in buffering a file entity. File entities use
their own buffer in writeTo().

>                        response.setEntity(bufBody);
>                        InputStream in = bufBody.getContent();
> 
>                        OutputStream out = new
> HttpDataOutputStream(new
> SocketHttpDataTransmitter(htRequests.get(this.thisConn.toString()), 8
> * 1024));

You do NOT use a data transmitter directly! At least not if you
want to talk HTTP. You use a HttpServerConnection that knows how
to send the status line and response headers before sending the
response body.
> 
>                        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.

Here is my advice, assuming you want a progress bar on the server side:

1. throw away the code snippet above

2. study class ElementalHttpServer from the examples to understand
   how files are served:
http://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/examples/org/apache/http/examples/ElementalHttpServer.java

3. implement file serving without progress bar, based on what you've
   seen in ElementalHttpServer

4. derive your own FileEntityWithProgressBar from the FileEntity,
   overriding the writeTo() method to update the progress bar

hope that helps,
  Roland

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

Reply via email to