From 5434fa88b43ca8454c0dc1b143f210f3a9444d05 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 12 Feb 2021 18:28:05 +0100 Subject: [PATCH] Explode ComponentProcessor into its contained interfaces --- src/main/java/io/netty/buffer/api/Buffer.java | 5 - .../netty/buffer/api/ComponentProcessor.java | 173 ------------------ .../io/netty/buffer/api/CompositeBuffer.java | 3 - .../netty/buffer/api/ReadableComponent.java | 76 ++++++++ .../api/ReadableComponentProcessor.java | 40 ++++ .../netty/buffer/api/WritableComponent.java | 63 +++++++ .../api/WritableComponentProcessor.java | 40 ++++ .../netty/buffer/api/memseg/MemSegBuffer.java | 8 +- 8 files changed, 223 insertions(+), 185 deletions(-) delete mode 100644 src/main/java/io/netty/buffer/api/ComponentProcessor.java create mode 100644 src/main/java/io/netty/buffer/api/ReadableComponent.java create mode 100644 src/main/java/io/netty/buffer/api/ReadableComponentProcessor.java create mode 100644 src/main/java/io/netty/buffer/api/WritableComponent.java create mode 100644 src/main/java/io/netty/buffer/api/WritableComponentProcessor.java diff --git a/src/main/java/io/netty/buffer/api/Buffer.java b/src/main/java/io/netty/buffer/api/Buffer.java index 4c9cb4b..4edbd30 100644 --- a/src/main/java/io/netty/buffer/api/Buffer.java +++ b/src/main/java/io/netty/buffer/api/Buffer.java @@ -15,11 +15,6 @@ */ package io.netty.buffer.api; -import io.netty.buffer.api.ComponentProcessor.ReadableComponentProcessor; -import io.netty.buffer.api.ComponentProcessor.WritableComponentProcessor; -import io.netty.buffer.api.ComponentProcessor.ReadableComponent; -import io.netty.buffer.api.ComponentProcessor.WritableComponent; - import java.nio.ByteBuffer; import java.nio.ByteOrder; diff --git a/src/main/java/io/netty/buffer/api/ComponentProcessor.java b/src/main/java/io/netty/buffer/api/ComponentProcessor.java deleted file mode 100644 index 1d4a832..0000000 --- a/src/main/java/io/netty/buffer/api/ComponentProcessor.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2020 The Netty Project - * - * The Netty Project licenses this file to you under the Apache License, - * version 2.0 (the "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at: - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - */ -package io.netty.buffer.api; - -import java.nio.ByteBuffer; - -/** - * This interface contain a collection of APIs used in the - * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)} and - * {@link Buffer#forEachWritable(int, WritableComponentProcessor)} methods. - */ -public interface ComponentProcessor { - /** - * A processor of {@linkplain ReadableComponent readable components}. - */ - @FunctionalInterface - interface ReadableComponentProcessor extends ComponentProcessor { - /** - * Process the given component at the given index in the - * {@link Buffer#forEachReadable(int, ReadableComponentProcessor) iteration}. - *

- * The component object itself is only valid during this call, but the {@link ByteBuffer byte buffers}, arrays, - * and native address pointers obtained from it, will be valid until any - * {@link Buffer#isOwned() ownership} requiring operation is performed on the buffer. - * - * @param index The current index of the given buffer component, based on the initial index passed to the - * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)} method. - * @param component The current buffer component being processed. - * @return {@code true} if the iteration should continue and more components should be processed, otherwise - * {@code false} to stop the iteration early. - */ - boolean process(int index, ReadableComponent component) throws E; - } - - /** - * A processor of {@linkplain WritableComponent writable components}. - */ - @FunctionalInterface - interface WritableComponentProcessor extends ComponentProcessor { - /** - * Process the given component at the given index in the - * {@link Buffer#forEachWritable(int, WritableComponentProcessor)} iteration}. - *

- * The component object itself is only valid during this call, but the {@link ByteBuffer byte buffers}, arrays, - * and native address pointers obtained from it, will be valid until any - * {@link Buffer#isOwned() ownership} requiring operation is performed on the buffer. - * - * @param index The current index of the given buffer component, based on the initial index passed to the - * {@link Buffer#forEachWritable(int, WritableComponentProcessor)} method. - * @param component The current buffer component being processed. - * @return {@code true} if the iteration should continue and more components should be processed, otherwise - * {@code false} to stop the iteration early. - */ - boolean process(int index, WritableComponent component) throws E; - } - - /** - * A view onto the buffer component being processed in a given iteration of - * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)}. - */ - interface ReadableComponent { - - /** - * Check if this component is backed by a cached byte array than can be accessed cheaply. - *

- * Note that regardless of what this method returns, the array should not be used to modify the - * contents of this buffer component. - * - * @return {@code true} if {@link #readableArray()} is a cheap operation, otherwise {@code false}. - */ - boolean hasReadableArray(); - - /** - * Get a byte array of the contents of this component. - *

- * Note that the array is meant to be read-only. It may either be a direct reference to the - * concrete array instance that is backing this component, or it is a fresh copy. - * Writing to the array may produce undefined behaviour. - * - * @return A byte array of the contents of this component. - * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. - */ - byte[] readableArray(); - - /** - * An offset into the {@link #readableArray()} where this component starts. - * - * @return An offset into {@link #readableArray()}. - * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. - */ - int readableArrayOffset(); - - /** - * Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address. - *

- * Note that the address should not be used for writing to the buffer memory, and doing so may - * produce undefined behaviour. - * - * @return The native memory address, if any, otherwise 0. - */ - long readableNativeAddress(); - - /** - * Get a {@link ByteBuffer} instance for this memory component. - *

- * Note that the {@link ByteBuffer} is read-only, to prevent write accesses to the memory, - * when the buffer component is obtained through - * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)}. - * - * @return A new {@link ByteBuffer}, with its own position and limit, for this memory component. - */ - ByteBuffer readableBuffer(); - // todo for Unsafe-based impl, DBB.attachment needs to keep underlying memory alive - } - - /** - * A view onto the buffer component being processed in a given iteration of - * {@link Buffer#forEachWritable(int, WritableComponentProcessor)}. - */ - interface WritableComponent { - - /** - * Check if this component is backed by a cached byte array than can be accessed cheaply. - * - * @return {@code true} if {@link #writableArray()} is a cheap operation, otherwise {@code false}. - */ - boolean hasWritableArray(); - - /** - * Get a byte array of the contents of this component. - * - * @return A byte array of the contents of this component. - * @throws UnsupportedOperationException if {@link #hasWritableArray()} returns {@code false}. - */ - byte[] writableArray(); - - /** - * An offset into the {@link #writableArray()} where this component starts. - * - * @return An offset into {@link #writableArray()}. - * @throws UnsupportedOperationException if {@link #hasWritableArray()} returns {@code false}. - */ - int writableArrayOffset(); - - /** - * Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address. - * - * @return The native memory address, if any, otherwise 0. - */ - long writableNativeAddress(); - - /** - * Get a {@link ByteBuffer} instance for this memory component, which can be used for modifying the buffer - * contents. - * - * @return A new {@link ByteBuffer}, with its own position and limit, for this memory component. - */ - ByteBuffer writableBuffer(); - } -} diff --git a/src/main/java/io/netty/buffer/api/CompositeBuffer.java b/src/main/java/io/netty/buffer/api/CompositeBuffer.java index 572abb3..5823f05 100644 --- a/src/main/java/io/netty/buffer/api/CompositeBuffer.java +++ b/src/main/java/io/netty/buffer/api/CompositeBuffer.java @@ -15,9 +15,6 @@ */ package io.netty.buffer.api; -import io.netty.buffer.api.ComponentProcessor.ReadableComponentProcessor; -import io.netty.buffer.api.ComponentProcessor.WritableComponentProcessor; - import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; diff --git a/src/main/java/io/netty/buffer/api/ReadableComponent.java b/src/main/java/io/netty/buffer/api/ReadableComponent.java new file mode 100644 index 0000000..3310582 --- /dev/null +++ b/src/main/java/io/netty/buffer/api/ReadableComponent.java @@ -0,0 +1,76 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api; + +import java.nio.ByteBuffer; + +/** + * A view onto the buffer component being processed in a given iteration of + * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)}. + */ +public interface ReadableComponent { + + /** + * Check if this component is backed by a cached byte array than can be accessed cheaply. + *

+ * Note that regardless of what this method returns, the array should not be used to modify the + * contents of this buffer component. + * + * @return {@code true} if {@link #readableArray()} is a cheap operation, otherwise {@code false}. + */ + boolean hasReadableArray(); + + /** + * Get a byte array of the contents of this component. + *

+ * Note that the array is meant to be read-only. It may either be a direct reference to the + * concrete array instance that is backing this component, or it is a fresh copy. Writing to the array may produce + * undefined behaviour. + * + * @return A byte array of the contents of this component. + * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. + */ + byte[] readableArray(); + + /** + * An offset into the {@link #readableArray()} where this component starts. + * + * @return An offset into {@link #readableArray()}. + * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. + */ + int readableArrayOffset(); + + /** + * Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address. + *

+ * Note that the address should not be used for writing to the buffer memory, and doing so may + * produce undefined behaviour. + * + * @return The native memory address, if any, otherwise 0. + */ + long readableNativeAddress(); + + /** + * Get a {@link ByteBuffer} instance for this memory component. + *

+ * Note that the {@link ByteBuffer} is read-only, to prevent write accesses to the memory, + * when the buffer component is obtained through {@link Buffer#forEachReadable(int, ReadableComponentProcessor)}. + * + * @return A new {@link ByteBuffer}, with its own position and limit, for this memory component. + */ + ByteBuffer readableBuffer(); + // todo for Unsafe-based impl, DBB.attachment needs to keep underlying memory alive +} diff --git a/src/main/java/io/netty/buffer/api/ReadableComponentProcessor.java b/src/main/java/io/netty/buffer/api/ReadableComponentProcessor.java new file mode 100644 index 0000000..b6e21f4 --- /dev/null +++ b/src/main/java/io/netty/buffer/api/ReadableComponentProcessor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api; + +import java.nio.ByteBuffer; + +/** + * A processor of {@linkplain ReadableComponent readable components}. + */ +@FunctionalInterface +public interface ReadableComponentProcessor { + /** + * Process the given component at the given index in the + * {@link Buffer#forEachReadable(int, ReadableComponentProcessor) iteration}. + *

+ * The component object itself is only valid during this call, but the {@link ByteBuffer byte buffers}, arrays, and + * native address pointers obtained from it, will be valid until any {@link Buffer#isOwned() ownership} requiring + * operation is performed on the buffer. + * + * @param index The current index of the given buffer component, based on the initial index passed to the + * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)} method. + * @param component The current buffer component being processed. + * @return {@code true} if the iteration should continue and more components should be processed, otherwise + * {@code false} to stop the iteration early. + */ + boolean process(int index, ReadableComponent component) throws E; +} diff --git a/src/main/java/io/netty/buffer/api/WritableComponent.java b/src/main/java/io/netty/buffer/api/WritableComponent.java new file mode 100644 index 0000000..5d7485a --- /dev/null +++ b/src/main/java/io/netty/buffer/api/WritableComponent.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api; + +import java.nio.ByteBuffer; + +/** + * A view onto the buffer component being processed in a given iteration of + * {@link Buffer#forEachWritable(int, WritableComponentProcessor)}. + */ +public interface WritableComponent { + + /** + * Check if this component is backed by a cached byte array than can be accessed cheaply. + * + * @return {@code true} if {@link #writableArray()} is a cheap operation, otherwise {@code false}. + */ + boolean hasWritableArray(); + + /** + * Get a byte array of the contents of this component. + * + * @return A byte array of the contents of this component. + * @throws UnsupportedOperationException if {@link #hasWritableArray()} returns {@code false}. + */ + byte[] writableArray(); + + /** + * An offset into the {@link #writableArray()} where this component starts. + * + * @return An offset into {@link #writableArray()}. + * @throws UnsupportedOperationException if {@link #hasWritableArray()} returns {@code false}. + */ + int writableArrayOffset(); + + /** + * Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address. + * + * @return The native memory address, if any, otherwise 0. + */ + long writableNativeAddress(); + + /** + * Get a {@link ByteBuffer} instance for this memory component, which can be used for modifying the buffer + * contents. + * + * @return A new {@link ByteBuffer}, with its own position and limit, for this memory component. + */ + ByteBuffer writableBuffer(); +} diff --git a/src/main/java/io/netty/buffer/api/WritableComponentProcessor.java b/src/main/java/io/netty/buffer/api/WritableComponentProcessor.java new file mode 100644 index 0000000..d4000d3 --- /dev/null +++ b/src/main/java/io/netty/buffer/api/WritableComponentProcessor.java @@ -0,0 +1,40 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api; + +import java.nio.ByteBuffer; + +/** + * A processor of {@linkplain WritableComponent writable components}. + */ +@FunctionalInterface +public interface WritableComponentProcessor { + /** + * Process the given component at the given index in the + * {@link Buffer#forEachWritable(int, WritableComponentProcessor)} iteration}. + *

+ * The component object itself is only valid during this call, but the {@link ByteBuffer byte buffers}, arrays, and + * native address pointers obtained from it, will be valid until any {@link Buffer#isOwned() ownership} requiring + * operation is performed on the buffer. + * + * @param index The current index of the given buffer component, based on the initial index passed to the + * {@link Buffer#forEachWritable(int, WritableComponentProcessor)} method. + * @param component The current buffer component being processed. + * @return {@code true} if the iteration should continue and more components should be processed, otherwise + * {@code false} to stop the iteration early. + */ + boolean process(int index, WritableComponent component) throws E; +} diff --git a/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java b/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java index 91cb069..c46ba2b 100644 --- a/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java +++ b/src/main/java/io/netty/buffer/api/memseg/MemSegBuffer.java @@ -19,10 +19,10 @@ import io.netty.buffer.api.BufferAllocator; import io.netty.buffer.api.AllocatorControl; import io.netty.buffer.api.Buffer; import io.netty.buffer.api.ByteCursor; -import io.netty.buffer.api.ComponentProcessor.ReadableComponent; -import io.netty.buffer.api.ComponentProcessor.ReadableComponentProcessor; -import io.netty.buffer.api.ComponentProcessor.WritableComponent; -import io.netty.buffer.api.ComponentProcessor.WritableComponentProcessor; +import io.netty.buffer.api.ReadableComponent; +import io.netty.buffer.api.ReadableComponentProcessor; +import io.netty.buffer.api.WritableComponent; +import io.netty.buffer.api.WritableComponentProcessor; import io.netty.buffer.api.Drop; import io.netty.buffer.api.Owned; import io.netty.buffer.api.RcSupport;