Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving chunked data endlessly

As you might know for sending chunked file in HTTP header, there is no content length, so the program must wait for 0 to understand that file ended.

--sample http header  
POST /some/path HTTP/1.1
Host: www.example.com  
Content-Type: text/plain  
Transfer-Encoding: chunked  
25  
This is the data in the first chunk  
8
sequence  
0

for receiving this file the following code can be used.

ResponseHandler<String> reshandler = new ResponseHandler<String>() {
    public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

        HttpEntity entity = response.getEntity();

        InputStream in =  entity.getContent();
        byte[] b = new byte[500];
        StringBuffer out = new StringBuffer();
        int len = in.read(b);
        out.append(new String(b, 0 , len));

        return out.toString();
    }
}; 

but in my case, I work with streaming channel in other word there is no 0 that shows that file ended. anyway if I use this code, it seems that it waits forever to wait for 0 which never happen. my question is there any better approach to receive chunked file from stream channel?

like image 342
arfo Avatar asked Aug 16 '11 10:08

arfo


1 Answers

Ok. I could receive the data by using simple approach(as following and without using Response Handler). anyway I'm still a bit confuse about apache ChunkedInputStream and how it could be usefual while normall inputstream can handle the chunked data.

is =  entity.getContent();
StringBuffer out = new StringBuffer();
byte[] b = new byte[800];
int len = is.read(b);
out.append(new String(b, 0 , len));
result = out.toString();
like image 187
arfo Avatar answered Oct 10 '22 15:10

arfo