|
15 | 15 | */ |
16 | 16 | package io.netty.buffer.api; |
17 | 17 |
|
| 18 | +import io.netty.buffer.api.ComponentProcessor.ReadableComponentProcessor; |
| 19 | +import io.netty.buffer.api.ComponentProcessor.WritableComponentProcessor; |
| 20 | +import io.netty.buffer.api.ComponentProcessor.ReadableComponent; |
| 21 | +import io.netty.buffer.api.ComponentProcessor.WritableComponent; |
| 22 | + |
18 | 23 | import java.nio.ByteBuffer; |
19 | 24 | import java.nio.ByteOrder; |
20 | 25 |
|
@@ -187,7 +192,7 @@ default int writableBytes() { |
187 | 192 | * Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address. |
188 | 193 | * @return The native memory address, if any, otherwise 0. |
189 | 194 | */ |
190 | | - long getNativeAddress(); |
| 195 | + long nativeAddress(); |
191 | 196 |
|
192 | 197 | /** |
193 | 198 | * Set the read-only state of this buffer. |
@@ -482,4 +487,119 @@ default void ensureWritable(int size) { |
482 | 487 | * or is {@linkplain #readOnly() read-only}. |
483 | 488 | */ |
484 | 489 | void compact(); |
| 490 | + |
| 491 | + /** |
| 492 | + * Get the number of "components" in this buffer. For composite buffers, this is the number of transitive |
| 493 | + * constituent buffers, while non-composite buffers only have one component. |
| 494 | + * |
| 495 | + * @return The number of components in this buffer. |
| 496 | + */ |
| 497 | + int countComponents(); |
| 498 | + |
| 499 | + /** |
| 500 | + * Get the number of "components" in this buffer, that are readable. These are the components that would be |
| 501 | + * processed by {@link #forEachReadable(int, ReadableComponentProcessor)}. For composite buffers, this is the |
| 502 | + * number of transitive constituent buffers that are readable, while non-composite buffers only have at most one |
| 503 | + * readable component. |
| 504 | + * <p> |
| 505 | + * The number of readable components may be less than the {@link #countComponents() component count}, if not all of |
| 506 | + * them have readable data. |
| 507 | + * |
| 508 | + * @return The number of readable components in this buffer. |
| 509 | + */ |
| 510 | + int countReadableComponents(); |
| 511 | + |
| 512 | + /** |
| 513 | + * Get the number of "components" in this buffer, that are writable. These are the components that would be |
| 514 | + * processed by {@link #forEachWritable(int, WritableComponentProcessor)}. For composite buffers, this is the |
| 515 | + * number of transitive constituent buffers that are writable, while non-composite buffers only have at most one |
| 516 | + * writable component. |
| 517 | + * <p> |
| 518 | + * The number of writable components may be less than the {@link #countComponents() component count}, if not all of |
| 519 | + * them have space for writing. |
| 520 | + * |
| 521 | + * @return The number of writable components in this buffer. |
| 522 | + */ |
| 523 | + int countWritableComponents(); |
| 524 | + |
| 525 | + /** |
| 526 | + * Process all readable components of this buffer, and return the number of components processed. |
| 527 | + * <p> |
| 528 | + * The given {@linkplain ReadableComponentProcessor processor} is called for each readable component in this buffer, |
| 529 | + * and passed a component index, for the given component in the iteration, and a {@link ReadableComponent} object |
| 530 | + * for accessing the data within the given component. |
| 531 | + * <p> |
| 532 | + * The component index is specific to the particular invokation of this method. The first call to the consumer will |
| 533 | + * be passed the given initial index, and the next call will be passed the initial index plus one, and so on. |
| 534 | + * <p> |
| 535 | + * The {@linkplain ReadableComponentProcessor component processor} may stop the iteration at any time by returning |
| 536 | + * {@code false}. |
| 537 | + * This will cause the number of components processed to be returned as a negative number (to signal early return), |
| 538 | + * and the number of components processed may then be less than the |
| 539 | + * {@linkplain #countReadableComponents() readable component count}. |
| 540 | + * <p> |
| 541 | + * <strong>Note</strong> that the {@link ReadableComponent} instance passed to the consumer could be reused for |
| 542 | + * multiple calls, so the data must be extracted from the component in the context of the iteration. |
| 543 | + * <p> |
| 544 | + * The {@link ByteBuffer} instances obtained from the component, share life time with that internal component. |
| 545 | + * This means they can be accessed as long as the internal memory store remain unchanged. Methods that may cause |
| 546 | + * such changes, are any method that requires the buffer to be {@linkplain #isOwned() owned}. |
| 547 | + * <p> |
| 548 | + * The best way to ensure this doesn't cause any trouble, is to use the buffers directly as part of the iteration, |
| 549 | + * or immediately after the iteration while we are still in the scope of the method that triggered the iteration. |
| 550 | + * <p> |
| 551 | + * <strong>Note</strong> that the arrays, memory addresses, and byte buffers exposed as components by this method, |
| 552 | + * should not be used for changing the buffer contents. Doing so may cause undefined behaviour. |
| 553 | + * <p> |
| 554 | + * Changes to position and limit of the byte buffers exposed via the processed components, are not reflected back to |
| 555 | + * this buffer instance. |
| 556 | + * |
| 557 | + * @param initialIndex The initial index of the iteration, and the index that will be passed to the first call to |
| 558 | + * the {@linkplain ReadableComponentProcessor#process(int, ReadableComponent) processor}. |
| 559 | + * @param processor The processor that will be used to process the buffer components. |
| 560 | + * @return The number of readable components processed, as a positive number of all readable components were |
| 561 | + * processed, or as a negative number if the iteration was stopped because |
| 562 | + * {@link ReadableComponentProcessor#process(int, ReadableComponent)} returned {@code false}. |
| 563 | + * In any case, the number of components processed may be less than {@link #countComponents()}. |
| 564 | + */ |
| 565 | + int forEachReadable(int initialIndex, ReadableComponentProcessor processor); |
| 566 | + |
| 567 | + /** |
| 568 | + * Process all writable components of this buffer, and return the number of components processed. |
| 569 | + * <p> |
| 570 | + * The given {@linkplain WritableComponentProcessor processor} is called for each writable component in this buffer, |
| 571 | + * and passed a component index, for the given component in the iteration, and a {@link WritableComponent} object |
| 572 | + * for accessing the data within the given component. |
| 573 | + * <p> |
| 574 | + * The component index is specific to the particular invokation of this method. The first call to the consumer will |
| 575 | + * be passed the given initial index, and the next call will be passed the initial index plus one, and so on. |
| 576 | + * <p> |
| 577 | + * The {@link WritableComponentProcessor component processor} may stop the iteration at any time by returning |
| 578 | + * {@code false}. |
| 579 | + * This will cause the number of components processed to be returned as a negative number (to signal early return), |
| 580 | + * and the number of components processed may then be less than the |
| 581 | + * {@linkplain #countReadableComponents() readable component count}. |
| 582 | + * <p> |
| 583 | + * <strong>Note</strong> that the {@link WritableComponent} instance passed to the consumer could be reused for |
| 584 | + * multiple calls, so the data must be extracted from the component in the context of the iteration. |
| 585 | + * <p> |
| 586 | + * The {@link ByteBuffer} instances obtained from the component, share life time with that internal component. |
| 587 | + * This means they can be accessed as long as the internal memory store remain unchanged. Methods that may cause |
| 588 | + * such changes, are any method that requires the buffer to be {@linkplain #isOwned() owned}. |
| 589 | + * <p> |
| 590 | + * The best way to ensure this doesn't cause any trouble, is to use the buffers directly as part of the iteration, |
| 591 | + * or immediately after the iteration while we are still in the scope of the method that triggered the iteration. |
| 592 | + * <p> |
| 593 | + * Changes to position and limit of the byte buffers exposed via the processed components, are not reflected back to |
| 594 | + * this buffer instance. |
| 595 | + * |
| 596 | + * @param initialIndex The initial index of the iteration, and the index that will be passed to the first call to |
| 597 | + * the {@linkplain WritableComponentProcessor#process(int, WritableComponent) processor}. |
| 598 | + * @param processor The processor that will be used to process the buffer components. |
| 599 | + * @return The number of writable components processed, as a positive number of all writable components were |
| 600 | + * processed, or as a negative number if the iteration was stopped because |
| 601 | + * {@link WritableComponentProcessor#process(int, WritableComponent)} returned {@code false}. |
| 602 | + * In any case, the number of components processed may be less than {@link #countComponents()}. |
| 603 | + */ |
| 604 | + int forEachWritable(int initialIndex, WritableComponentProcessor processor); |
485 | 605 | } |
0 commit comments