Request Body Considered experimental, will most likely be made internal going forward.
| 513 | * <p>Considered experimental, will most likely be made internal going forward. |
| 514 | */ |
| 515 | @Experimental |
| 516 | public static class Body implements Serializable { |
| 517 | |
| 518 | private transient Charset encoding; |
| 519 | |
| 520 | private byte[] data; |
| 521 | |
| 522 | private Body() { |
| 523 | super(); |
| 524 | } |
| 525 | |
| 526 | private Body(byte[] data) { |
| 527 | this.data = data; |
| 528 | } |
| 529 | |
| 530 | private Body(byte[] data, Charset encoding) { |
| 531 | this.data = data; |
| 532 | this.encoding = encoding; |
| 533 | } |
| 534 | |
| 535 | public Optional<Charset> getEncoding() { |
| 536 | return Optional.ofNullable(this.encoding); |
| 537 | } |
| 538 | |
| 539 | public int length() { |
| 540 | /* calculate the content length based on the data provided */ |
| 541 | return data != null ? data.length : 0; |
| 542 | } |
| 543 | |
| 544 | public byte[] asBytes() { |
| 545 | return data; |
| 546 | } |
| 547 | |
| 548 | public String asString() { |
| 549 | return !isBinary() ? new String(data, encoding) : "Binary data"; |
| 550 | } |
| 551 | |
| 552 | public boolean isBinary() { |
| 553 | return encoding == null || data == null; |
| 554 | } |
| 555 | |
| 556 | public static Body create(String data) { |
| 557 | return new Body(data.getBytes()); |
| 558 | } |
| 559 | |
| 560 | public static Body create(String data, Charset charset) { |
| 561 | return new Body(data.getBytes(charset), charset); |
| 562 | } |
| 563 | |
| 564 | public static Body create(byte[] data) { |
| 565 | return new Body(data); |
| 566 | } |
| 567 | |
| 568 | public static Body create(byte[] data, Charset charset) { |
| 569 | return new Body(data, charset); |
| 570 | } |
| 571 | |
| 572 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected