@Override public ChunkedBodyContents.Chunk next() { if (!hasMoreChunks) { throw new NoSuchElementException(); } try { ChunkedBodyContents.Chunk chunk = readNextChunk(inputStream); hasMoreChunks = chunk.size() > 0; if (!hasMoreChunks) { // throw away the trailer-part readTrailer(inputStream); } return chunk; } catch (IOException e) { throw new RuntimeException(e); } } };
@Override public ChunkedBodyContents.Chunk next() { if (!hasMoreChunks) { throw new NoSuchElementException(); } try { ChunkedBodyContents.Chunk chunk = readNextChunk(inputStream); hasMoreChunks = chunk.size() > 0; if (!hasMoreChunks) { // throw away the trailer-part readTrailer(inputStream); } return chunk; } catch (IOException e) { throw new RuntimeException(e); } } };
/** * Read the next chunk available from the source input stream. * <p> * The last chunk is always the empty chunk. After the empty chunk is returned, the message is completed * and calling this method again after that results in an {@link IllegalStateException}. * <p> * When the last chunk is returned, the trailer-part, if any, is also consumed and can be obtained by * calling the {@link InputStreamChunkDecoder#getTrailer()} method. * * @return the next chunk * @throws IOException if an error occurs while reading the original stream * @throws IllegalStateException if this method is called after the empty chunk is returned */ public ChunkedBodyContents.Chunk readChunk() throws IOException { if (done) { throw new IllegalStateException("HTTP message body is already consumed"); } ChunkedBodyContents.Chunk chunk = parser.readNextChunk(inputStream); if (chunk.size() == 0) { trailer = parser.readTrailer(inputStream); done = true; currentSource = null; } else { currentSource = new ByteArrayInputStream(chunk.getData()); } return chunk; }
/** * Read the next chunk available from the source input stream. * <p> * The last chunk is always the empty chunk. After the empty chunk is returned, the message is completed * and calling this method again after that results in an {@link IllegalStateException}. * <p> * When the last chunk is returned, the trailer-part, if any, is also consumed and can be obtained by * calling the {@link InputStreamChunkDecoder#getTrailer()} method. * * @return the next chunk * @throws IOException if an error occurs while reading the original stream * @throws IllegalStateException if this method is called after the empty chunk is returned */ public ChunkedBodyContents.Chunk readChunk() throws IOException { if (done) { throw new IllegalStateException("HTTP message body is already consumed"); } ChunkedBodyContents.Chunk chunk = parser.readNextChunk(inputStream); if (chunk.size() == 0) { trailer = parser.readTrailer(inputStream); done = true; currentSource = null; } else { currentSource = new ByteArrayInputStream(chunk.getData()); } return chunk; }
/** * Parse the full contents of the chunked message. * * @param inputStream to read message from * @param chunkConsumer consumer of individual chunks * @param trailerConsumer consumer of the trailer part * @throws IOException if an error occurs while reading the stream or calling the callbacks */ public void parseChunkedBody(InputStream inputStream, IOConsumer<ChunkedBodyContents.Chunk> chunkConsumer, IOConsumer<RawHttpHeaders> trailerConsumer) throws IOException { int chunkSize = 1; while (chunkSize > 0) { AtomicBoolean hasExtensions = new AtomicBoolean(false); chunkSize = readChunkSize(inputStream, hasExtensions); if (chunkSize < 0) { throw new IllegalStateException("unexpected EOF, could not read chunked body"); } ChunkedBodyContents.Chunk chunk = readChunk(inputStream, chunkSize, hasExtensions.get()); chunkConsumer.accept(chunk); } RawHttpHeaders trailer = readTrailer(inputStream); trailerConsumer.accept(trailer); }
/** * Parse the full contents of the chunked message. * * @param inputStream to read message from * @param chunkConsumer consumer of individual chunks * @param trailerConsumer consumer of the trailer part * @throws IOException if an error occurs while reading the stream or calling the callbacks */ public void parseChunkedBody(InputStream inputStream, IOConsumer<ChunkedBodyContents.Chunk> chunkConsumer, IOConsumer<RawHttpHeaders> trailerConsumer) throws IOException { int chunkSize = 1; while (chunkSize > 0) { AtomicBoolean hasExtensions = new AtomicBoolean(false); chunkSize = readChunkSize(inputStream, hasExtensions); if (chunkSize < 0) { throw new IllegalStateException("unexpected EOF, could not read chunked body"); } ChunkedBodyContents.Chunk chunk = readChunk(inputStream, chunkSize, hasExtensions.get()); chunkConsumer.accept(chunk); } RawHttpHeaders trailer = readTrailer(inputStream); trailerConsumer.accept(trailer); }