1 /*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
290 assert clen == 0;
291 debug.log(Level.DEBUG, "No more chunks: %d", clen);
292 // the DONE state is not really needed but it helps with
293 // assertions...
294 state = ChunkState.DONE;
295 return null;
296 }
297
298 int clen = chunklen;
299 assert clen > 0;
300 assert st == ChunkState.READING_DATA;
301
302 ByteBuffer returnBuffer = READMORE; // May be a hunk or a chunk
303 if (unfulfilled > 0) {
304 int bytesread = chunk.remaining();
305 debug.log(Level.DEBUG, "Reading chunk: available %d, needed %d",
306 bytesread, unfulfilled);
307
308 int bytes2return = Math.min(bytesread, unfulfilled);
309 debug.log(Level.DEBUG, "Returning chunk bytes: %d", bytes2return);
310 returnBuffer = Utils.slice(chunk, bytes2return);
311 unfulfilled = bytesremaining -= bytes2return;
312 if (unfulfilled == 0) bytesToConsume = 2;
313 }
314
315 assert unfulfilled >= 0;
316
317 if (unfulfilled == 0) {
318 debug.log(Level.DEBUG,
319 "No more bytes to read - %d yet to consume.",
320 unfulfilled);
321 // check whether the trailing CRLF is consumed, try to
322 // consume it if not. If tryConsumeBytes needs more bytes
323 // then we will come back here later - skipping the block
324 // that reads data because remaining==0, and finding
325 // that the two bytes are now consumed.
326 if (tryConsumeBytes(chunk) == 0) {
327 // we're done for this chunk! reset all states and
328 // prepare to read the next chunk.
329 chunklen = -1;
330 partialChunklen = 0;
422 + closedExceptionally);
423 return;
424 }
425 boolean completed = false;
426 try {
427 int unfulfilled = remaining;
428 debug.log(Level.DEBUG, "Parser got %d bytes (%d remaining / %d)",
429 b.remaining(), unfulfilled, contentLength);
430 assert unfulfilled != 0 || contentLength == 0 || b.remaining() == 0;
431
432 if (unfulfilled == 0 && contentLength > 0) return;
433
434 if (b.hasRemaining() && unfulfilled > 0) {
435 // only reduce demand if we actually push something.
436 // we would not have come here if there was no
437 // demand.
438 boolean hasDemand = sub.demand().tryDecrement();
439 assert hasDemand;
440 int amount = Math.min(b.remaining(), unfulfilled);
441 unfulfilled = remaining -= amount;
442 ByteBuffer buffer = Utils.slice(b, amount);
443 pusher.onNext(List.of(buffer));
444 }
445 if (unfulfilled == 0) {
446 // We're done! All data has been received.
447 assert closedExceptionally == null;
448 onFinished.run();
449 pusher.onComplete();
450 completed = true;
451 onComplete.accept(closedExceptionally); // should be null
452 } else {
453 assert b.remaining() == 0;
454 }
455 } catch (Throwable t) {
456 debug.log(Level.DEBUG, "Unexpected exception", t);
457 closedExceptionally = t;
458 if (!completed) {
459 onComplete.accept(t);
460 }
461 }
462 }
|
1 /*
2 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
290 assert clen == 0;
291 debug.log(Level.DEBUG, "No more chunks: %d", clen);
292 // the DONE state is not really needed but it helps with
293 // assertions...
294 state = ChunkState.DONE;
295 return null;
296 }
297
298 int clen = chunklen;
299 assert clen > 0;
300 assert st == ChunkState.READING_DATA;
301
302 ByteBuffer returnBuffer = READMORE; // May be a hunk or a chunk
303 if (unfulfilled > 0) {
304 int bytesread = chunk.remaining();
305 debug.log(Level.DEBUG, "Reading chunk: available %d, needed %d",
306 bytesread, unfulfilled);
307
308 int bytes2return = Math.min(bytesread, unfulfilled);
309 debug.log(Level.DEBUG, "Returning chunk bytes: %d", bytes2return);
310 returnBuffer = Utils.sliceWithLimitedCapacity(chunk, bytes2return);
311 unfulfilled = bytesremaining -= bytes2return;
312 if (unfulfilled == 0) bytesToConsume = 2;
313 }
314
315 assert unfulfilled >= 0;
316
317 if (unfulfilled == 0) {
318 debug.log(Level.DEBUG,
319 "No more bytes to read - %d yet to consume.",
320 unfulfilled);
321 // check whether the trailing CRLF is consumed, try to
322 // consume it if not. If tryConsumeBytes needs more bytes
323 // then we will come back here later - skipping the block
324 // that reads data because remaining==0, and finding
325 // that the two bytes are now consumed.
326 if (tryConsumeBytes(chunk) == 0) {
327 // we're done for this chunk! reset all states and
328 // prepare to read the next chunk.
329 chunklen = -1;
330 partialChunklen = 0;
422 + closedExceptionally);
423 return;
424 }
425 boolean completed = false;
426 try {
427 int unfulfilled = remaining;
428 debug.log(Level.DEBUG, "Parser got %d bytes (%d remaining / %d)",
429 b.remaining(), unfulfilled, contentLength);
430 assert unfulfilled != 0 || contentLength == 0 || b.remaining() == 0;
431
432 if (unfulfilled == 0 && contentLength > 0) return;
433
434 if (b.hasRemaining() && unfulfilled > 0) {
435 // only reduce demand if we actually push something.
436 // we would not have come here if there was no
437 // demand.
438 boolean hasDemand = sub.demand().tryDecrement();
439 assert hasDemand;
440 int amount = Math.min(b.remaining(), unfulfilled);
441 unfulfilled = remaining -= amount;
442 ByteBuffer buffer = Utils.sliceWithLimitedCapacity(b, amount);
443 pusher.onNext(List.of(buffer));
444 }
445 if (unfulfilled == 0) {
446 // We're done! All data has been received.
447 assert closedExceptionally == null;
448 onFinished.run();
449 pusher.onComplete();
450 completed = true;
451 onComplete.accept(closedExceptionally); // should be null
452 } else {
453 assert b.remaining() == 0;
454 }
455 } catch (Throwable t) {
456 debug.log(Level.DEBUG, "Unexpected exception", t);
457 closedExceptionally = t;
458 if (!completed) {
459 onComplete.accept(t);
460 }
461 }
462 }
|