620 * implementation, as it has effectively no data to publish. |
620 * implementation, as it has effectively no data to publish. |
621 */ |
621 */ |
622 public interface BodyPublisher extends Flow.Publisher<ByteBuffer> { |
622 public interface BodyPublisher extends Flow.Publisher<ByteBuffer> { |
623 |
623 |
624 /** |
624 /** |
|
625 * Returns a request body publisher whose body is retrieved from the |
|
626 * given {@code Flow.Publisher}. The returned request body publisher |
|
627 * has an unknown content length. |
|
628 * |
|
629 * @apiNote This method can be used as an adapter between {@code |
|
630 * BodyPublisher} and {@code Flow.Publisher}, where the amount of |
|
631 * request body that the publisher will publish is unknown. |
|
632 * |
|
633 * @param publisher the publisher responsible for publishing the body |
|
634 * @return a BodyPublisher |
|
635 */ |
|
636 static BodyPublisher fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher) { |
|
637 return new RequestPublishers.PublisherAdapter(publisher, -1L); |
|
638 } |
|
639 |
|
640 /** |
|
641 * Returns a request body publisher whose body is retrieved from the |
|
642 * given {@code Flow.Publisher}. The returned request body publisher |
|
643 * has the given content length. |
|
644 * |
|
645 * <p> The given {@code contentLength} is a positive number, that |
|
646 * represents the exact amount of bytes the {@code publisher} must |
|
647 * publish. |
|
648 * |
|
649 * @apiNote This method can be used as an adapter between {@code |
|
650 * BodyPublisher} and {@code Flow.Publisher}, where the amount of |
|
651 * request body that the publisher will publish is known. |
|
652 * |
|
653 * @param publisher the publisher responsible for publishing the body |
|
654 * @param contentLength a positive number representing the exact |
|
655 * amount of bytes the publisher will publish |
|
656 * @throws IllegalArgumentException if the content length is |
|
657 * non-positive |
|
658 * @return a BodyPublisher |
|
659 */ |
|
660 static BodyPublisher fromPublisher(Flow.Publisher<? extends ByteBuffer> publisher, |
|
661 long contentLength) { |
|
662 if (contentLength < 1) |
|
663 throw new IllegalArgumentException("non-positive contentLength: " + contentLength); |
|
664 return new RequestPublishers.PublisherAdapter(publisher, contentLength); |
|
665 } |
|
666 |
|
667 /** |
625 * Returns a request body publisher whose body is the given {@code |
668 * Returns a request body publisher whose body is the given {@code |
626 * String}, converted using the {@link StandardCharsets#UTF_8 UTF_8} |
669 * String}, converted using the {@link StandardCharsets#UTF_8 UTF_8} |
627 * character set. |
670 * character set. |
628 * |
671 * |
629 * @param body the String containing the body |
672 * @param body the String containing the body |