Question

Is there a helper method in HTTP Client that allows you to consume chunk encoded data whenever one chunk arrived on the wire? Perhaps I overlooked this but I couldn't find anything related with this topic. I did see this Chunked http decoding in java? but that ChunkedInputStream is no longer available in HTTP Client 4.x

Here's what coming in over the wire.

HttpResponseHandlerImpl.setContentType(): application/json
HttpResponseHandlerImpl.setContentLength(): -1
58495 [main] DEBUG org.apache.http.wire  - << "24[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "{"firstName":"David","lastName":"0"}"
58495 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "24[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "{"firstName":"David","lastName":"1"}"
58495 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "24[\r][\n]"
58495 [main] DEBUG org.apache.http.wire  - << "{"firstName":"David","lastName":"2"}"
58501 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"
58501 [main] DEBUG org.apache.http.wire  - << "0[\r][\n]"
58501 [main] DEBUG org.apache.http.wire  - << "[\r][\n]"

What I want to do is to process a chunk at a time. Is there any helper method that allows you to do this? or I just need to get the InputStream and do this manually? Manually as in get the chunk size then read the buffer based on the chunk size.

Was it helpful?

Solution

Not the idealistic solution since I was hoping to utilize the HttpComponents helper if any to parse chunk encoded data but I got this working using the Jackson's JsonParser http://jackson.codehaus.org/, and I know that the response returning back from my web service would always be JSON objects.

Here's the code

    try {

        MappingJsonFactory jsonFactory = new MappingJsonFactory();

        JsonParser jsonParser = jsonFactory.createParser(inputStream);

        JsonToken token = null;

        token = jsonParser.nextToken();

        StringBuffer buffer = new StringBuffer();

        do {

            if (token == JsonToken.START_OBJECT) {
                buffer.append("{");
            } else if (token == JsonToken.END_OBJECT) {
                buffer.append("}");
                System.out.println("Received chunk: " + buffer.toString());
                buffer.setLength(0);
            } else if (token == JsonToken.FIELD_NAME) {
                jsonParser.nextToken();
                buffer.append("\"" + jsonParser.getCurrentName() + "\":");
                buffer.append("\"" + jsonParser.getText() + "\":");
            } else if (token == JsonToken.VALUE_STRING) {
                buffer.append("\"" + jsonParser.getCurrentName() + "\":");
                buffer.append("\"" + jsonParser.getText() + "\":");
            }

            token = jsonParser.nextToken();

        } while (token != null);

    } catch (Throwable th) {
        th.printStackTrace();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top