ChunkedInput not working in jersey

前端 未结 1 1025
迷失自我
迷失自我 2021-01-19 06:54

Can anyone help me why the java code is having issue and printing all data in one go instead of prinitng each chunk as javascript code

Java Code :

         


        
相关标签:
1条回答
  • 2021-01-19 07:35

    Form jersey docs:

    Writing chunks with ChunkedOutput is simple, you only call method write() which writes exactly one chunk to the output. With the input reading it is slightly more complicated. The ChunkedInput does not know how to distinguish chunks in the byte stream unless being told by the developer. In order to define custom chunks boundaries, the ChunkedInput offers possibility to register a ChunkParser which reads chunks from the input stream and separates them. Jersey provides several chunk parser implementation and you can implement your own parser to separate your chunks if you need. In our example above the default parser provided by Jersey is used that separates chunks based on presence of a \r\n delimiting character sequence.

    So your server has to separate chunks with \r\n, or you have to register a ChunkParser.

    Assuming you have a constant finalising each chunk you could try:

    Client client = ClientBuilder.newClient();
    //2 is to increase amount of data and 3(seconds) is for time b/w chunked output  ,can be changed
            final Response response = client.target("http://jerseyexample-ravikant.rhcloud.com/rest/jws/streaming/2/3").request()
                    .get();
            final ChunkedInput<String> chunkedInput = response.readEntity(new GenericType<ChunkedInput<String>>() {
            });
            chunkedInput.setParser(ChunkedInput.createParser(BOUNDARY));
            String chunk;
            while ((chunk = chunkedInput.read()) != null) {
                System.err.println("Next chunk received: " );
                System.out.println(chunk);
            }
    

    While BOUNDARY is a finalizing string for each chunk. The in.readLine solution in your edit will break down "chunks" by every newline, even if one chunk consist a \n, it will be interpreted as 2 chunks.

    0 讨论(0)
提交回复
热议问题