Receiving chunked data endlessly
Asked Answered
F

1

6

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?

Finedraw answered 16/8, 2011 at 10:24 Comment(0)
F
1

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();
Finedraw answered 22/8, 2011 at 1:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.