Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 202dd54

Browse files
authored
Merge pull request #24 from netty/buffer-iterate
Add support for iterating underlying buffer components
2 parents 8cdcfd5 + 1ff8b4b commit 202dd54

File tree

6 files changed

+1554
-117
lines changed

6 files changed

+1554
-117
lines changed

src/main/java/io/netty/buffer/api/Buf.java

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
*/
1616
package io.netty.buffer.api;
1717

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+
1823
import java.nio.ByteBuffer;
1924
import java.nio.ByteOrder;
2025

@@ -187,7 +192,7 @@ default int writableBytes() {
187192
* Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address.
188193
* @return The native memory address, if any, otherwise 0.
189194
*/
190-
long getNativeAddress();
195+
long nativeAddress();
191196

192197
/**
193198
* Set the read-only state of this buffer.
@@ -482,4 +487,119 @@ default void ensureWritable(int size) {
482487
* or is {@linkplain #readOnly() read-only}.
483488
*/
484489
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);
485605
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright 2020 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.buffer.api;
17+
18+
import java.nio.ByteBuffer;
19+
20+
/**
21+
* This interface contain a collection of APIs used in the {@link Buf#forEachReadable(int, ReadableComponentProcessor)}
22+
* and {@link Buf#forEachWritable(int, WritableComponentProcessor)} methods.
23+
*/
24+
public interface ComponentProcessor {
25+
/**
26+
* A processor of {@linkplain ReadableComponent readable components}.
27+
*/
28+
@FunctionalInterface
29+
interface ReadableComponentProcessor extends ComponentProcessor {
30+
/**
31+
* Process the given component at the given index in the
32+
* {@link Buf#forEachReadable(int, ReadableComponentProcessor) iteration}.
33+
* <p>
34+
* The component object itself is only valid during this call, but the {@link ByteBuffer byte buffers}, arrays,
35+
* and native address pointers obtained from it, will be valid until any
36+
* {@link Buf#isOwned() ownership} requiring operation is performed on the buffer.
37+
*
38+
* @param index The current index of the given buffer component, based on the initial index passed to the
39+
* {@link Buf#forEachReadable(int, ReadableComponentProcessor)} method.
40+
* @param component The current buffer component being processed.
41+
* @return {@code true} if the iteration should continue and more components should be processed, otherwise
42+
* {@code false} to stop the iteration early.
43+
*/
44+
boolean process(int index, ReadableComponent component);
45+
}
46+
47+
/**
48+
* A processor of {@linkplain WritableComponent writable components}.
49+
*/
50+
@FunctionalInterface
51+
interface WritableComponentProcessor extends ComponentProcessor {
52+
/**
53+
* Process the given component at the given index in the
54+
* {@link Buf#forEachWritable(int, WritableComponentProcessor)} iteration}.
55+
* <p>
56+
* The component object itself is only valid during this call, but the {@link ByteBuffer byte buffers}, arrays,
57+
* and native address pointers obtained from it, will be valid until any
58+
* {@link Buf#isOwned() ownership} requiring operation is performed on the buffer.
59+
*
60+
* @param index The current index of the given buffer component, based on the initial index passed to the
61+
* {@link Buf#forEachWritable(int, WritableComponentProcessor)} method.
62+
* @param component The current buffer component being processed.
63+
* @return {@code true} if the iteration should continue and more components should be processed, otherwise
64+
* {@code false} to stop the iteration early.
65+
*/
66+
boolean process(int index, WritableComponent component);
67+
}
68+
69+
/**
70+
* A view onto the buffer component being processed in a given iteration of
71+
* {@link Buf#forEachReadable(int, ReadableComponentProcessor)}.
72+
*/
73+
interface ReadableComponent {
74+
75+
/**
76+
* Check if this component is backed by a cached byte array than can be accessed cheaply.
77+
* <p>
78+
* <strong>Note</strong> that regardless of what this method returns, the array should not be used to modify the
79+
* contents of this buffer component.
80+
*
81+
* @return {@code true} if {@link #readableArray()} is a cheap operation, otherwise {@code false}.
82+
*/
83+
boolean hasReadableArray();
84+
85+
/**
86+
* Get a byte array of the contents of this component.
87+
* <p>
88+
* <strong>Note</strong> that the array is meant to be read-only. It may either be a direct reference to the
89+
* concrete array instance that is backing this component, or it is a fresh copy.
90+
* Writing to the array may produce undefined behaviour.
91+
*
92+
* @return A byte array of the contents of this component.
93+
* @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}.
94+
*/
95+
byte[] readableArray();
96+
97+
/**
98+
* An offset into the {@link #readableArray()} where this component starts.
99+
*
100+
* @return An offset into {@link #readableArray()}.
101+
* @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}.
102+
*/
103+
int readableArrayOffset();
104+
105+
/**
106+
* Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address.
107+
* <p>
108+
* <strong>Note</strong> that the address should not be used for writing to the buffer memory, and doing so may
109+
* produce undefined behaviour.
110+
*
111+
* @return The native memory address, if any, otherwise 0.
112+
*/
113+
long readableNativeAddress();
114+
115+
/**
116+
* Get a {@link ByteBuffer} instance for this memory component.
117+
* <p>
118+
* <strong>Note</strong> that the {@link ByteBuffer} is read-only, to prevent write accesses to the memory,
119+
* when the buffer component is obtained through {@link Buf#forEachReadable(int, ReadableComponentProcessor)}.
120+
*
121+
* @return A new {@link ByteBuffer}, with its own position and limit, for this memory component.
122+
*/
123+
ByteBuffer readableBuffer();
124+
}
125+
126+
/**
127+
* A view onto the buffer component being processed in a given iteration of
128+
* {@link Buf#forEachWritable(int, WritableComponentProcessor)}.
129+
*/
130+
interface WritableComponent {
131+
132+
/**
133+
* Check if this component is backed by a cached byte array than can be accessed cheaply.
134+
*
135+
* @return {@code true} if {@link #writableArray()} is a cheap operation, otherwise {@code false}.
136+
*/
137+
boolean hasWritableArray();
138+
139+
/**
140+
* Get a byte array of the contents of this component.
141+
*
142+
* @return A byte array of the contents of this component.
143+
* @throws UnsupportedOperationException if {@link #hasWritableArray()} returns {@code false}.
144+
*/
145+
byte[] writableArray();
146+
147+
/**
148+
* An offset into the {@link #writableArray()} where this component starts.
149+
*
150+
* @return An offset into {@link #writableArray()}.
151+
* @throws UnsupportedOperationException if {@link #hasWritableArray()} returns {@code false}.
152+
*/
153+
int writableArrayOffset();
154+
155+
/**
156+
* Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address.
157+
*
158+
* @return The native memory address, if any, otherwise 0.
159+
*/
160+
long writableNativeAddress();
161+
162+
/**
163+
* Get a {@link ByteBuffer} instance for this memory component, which can be used for modifying the buffer
164+
* contents.
165+
*
166+
* @return A new {@link ByteBuffer}, with its own position and limit, for this memory component.
167+
*/
168+
ByteBuffer writableBuffer();
169+
}
170+
}

0 commit comments

Comments
 (0)