1 /* 2 * Copyright 2021 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.netty5.buffer.api; 17 18 import java.nio.ByteBuffer; 19 20 /** 21 * A view onto the buffer component being processed in a given iteration of 22 * {@link Buffer#forEachReadable(int, ReadableComponentProcessor)}. 23 */ 24 public interface ReadableComponent { 25 26 /** 27 * Check if this component is backed by a cached byte array that can be accessed cheaply. 28 * <p> 29 * <strong>Note</strong> that regardless of what this method returns, the array should not be used to modify the 30 * contents of this buffer component. 31 * 32 * @return {@code true} if {@link #readableArray()} is a cheap operation, otherwise {@code false}. 33 */ 34 boolean hasReadableArray(); 35 36 /** 37 * Get a byte array of the contents of this component. 38 * <p> 39 * <strong>Note</strong> that the array is meant to be read-only. It may either be a direct reference to the 40 * concrete array instance that is backing this component, or it is a fresh copy. Writing to the array may produce 41 * undefined behaviour. 42 * 43 * @return A byte array of the contents of this component. 44 * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. 45 * @see #readableArrayOffset() 46 * @see #readableArrayLength() 47 */ 48 byte[] readableArray(); 49 50 /** 51 * An offset into the {@link #readableArray()} where this component starts. 52 * 53 * @return An offset into {@link #readableArray()}. 54 * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. 55 */ 56 int readableArrayOffset(); 57 58 /** 59 * The number of bytes in the {@link #readableArray()} that belong to this component. 60 * 61 * @return The number of bytes, from the {@link #readableArrayOffset()} into the {@link #readableArray()}, 62 * that belong to this component. 63 * @throws UnsupportedOperationException if {@link #hasReadableArray()} returns {@code false}. 64 */ 65 int readableArrayLength(); 66 67 /** 68 * Give the native memory address backing this buffer, or return 0 if this buffer has no native memory address. 69 * <p> 70 * <strong>Note</strong> that the address should not be used for writing to the buffer memory, and doing so may 71 * produce undefined behaviour. 72 * 73 * @return The native memory address, if any, otherwise 0. 74 */ 75 long readableNativeAddress(); 76 77 /** 78 * Get a {@link ByteBuffer} instance for this memory component. 79 * <p> 80 * <strong>Note</strong> that the {@link ByteBuffer} is read-only, to prevent write accesses to the memory, 81 * when the buffer component is obtained through {@link Buffer#forEachReadable(int, ReadableComponentProcessor)}. 82 * 83 * @return A new {@link ByteBuffer}, with its own position and limit, for this memory component. 84 */ 85 ByteBuffer readableBuffer(); 86 87 /** 88 * Get the number of readable bytes from this component. 89 * 90 * @return The number of bytes that can be read from this readable component. 91 */ 92 int readableBytes(); 93 94 /** 95 * Open a cursor to iterate the readable bytes of this component. 96 * Any offsets internal to the component are not modified by the cursor. 97 * <p> 98 * Care should be taken to ensure that the buffers lifetime extends beyond the cursor and the iteration, and that 99 * the internal offsets of the component (such as {@link Buffer#readerOffset()} and {@link Buffer#writerOffset()}) 100 * are not modified while the iteration takes place. Otherwise, unpredictable behaviour might result. 101 * 102 * @return A {@link ByteCursor} for iterating the readable bytes of this buffer. 103 * @see Buffer#openCursor() 104 */ 105 ByteCursor openCursor(); 106 107 /** 108 * Move the read-offset to indicate that the given number of bytes were read from this component. 109 * 110 * @param byteCount The number of bytes read from this component. 111 * @return itself. 112 * @see Buffer#skipReadableBytes(int) 113 */ 114 ReadableComponent skipReadableBytes(int byteCount); 115 }