2 * Copyright 2015-present Open Networking Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.storage.buffer;
18 import java.nio.charset.Charset;
19 import java.util.function.Function;
24 * This interface exposes methods for reading from a byte buffer. Readable buffers maintain a small amount of state
25 * regarding current cursor positions and limits similar to the behavior of {@link java.nio.ByteBuffer}.
27 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
29 public interface BufferInput<T extends BufferInput<?>> extends AutoCloseable {
32 * Returns the buffer's current read/write position.
34 * The position is an internal cursor that tracks where to write/read bytes in the underlying storage implementation.
35 * As bytes are written to or read from the buffer, the position will advance based on the number of bytes read.
37 * @return The buffer's current position.
42 * Returns the number of bytes remaining in the input.
44 * @return The number of bytes remaining in the input.
49 * Returns a boolean value indicating whether the input has bytes remaining.
51 * @return Indicates whether bytes remain to be read from the input.
53 boolean hasRemaining();
56 * Skips the given number of bytes in the input.
58 * @param bytes The number of bytes to attempt to skip.
59 * @return The skipped input.
64 * Reads bytes into the given byte array.
66 * @param bytes The byte array into which to read bytes.
72 * Reads bytes into the given byte array.
74 * @param bytes The byte array into which to read bytes.
80 * Reads bytes into the given byte array starting at the current position.
82 * @param bytes The byte array into which to read bytes.
83 * @param offset The offset at which to write bytes into the given buffer
86 T read(Bytes bytes, int offset, int length);
89 * Reads bytes into the given byte array starting at current position up to the given length.
91 * @param bytes The byte array into which to read bytes.
92 * @param offset The offset at which to write bytes into the given buffer
95 T read(byte[] bytes, int offset, int length);
98 * Reads bytes into the given buffer.
100 * @param buffer The buffer into which to read bytes.
101 * @return The buffer.
103 T read(Buffer buffer);
106 * Reads an object from the buffer.
108 * @param decoder the object decoder
109 * @param <U> the type of the object to read
110 * @return the read object.
112 default <U> U readObject(Function<byte[], U> decoder) {
113 byte[] bytes = readBytes(readInt());
114 return decoder.apply(bytes);
118 * Reads a byte array.
120 * @param length The byte array length
121 * @return The read byte array.
123 default byte[] readBytes(int length) {
124 byte[] bytes = new byte[length];
130 * Reads a byte from the buffer at the current position.
132 * @return The read byte.
137 * Reads an unsigned byte from the buffer at the current position.
139 * @return The read byte.
141 int readUnsignedByte();
144 * Reads a 16-bit character from the buffer at the current position.
146 * @return The read character.
151 * Reads a 16-bit signed integer from the buffer at the current position.
153 * @return The read short.
158 * Reads a 16-bit unsigned integer from the buffer at the current position.
160 * @return The read short.
162 int readUnsignedShort();
165 * Reads a 24-bit signed integer from the buffer at the current position.
167 * @return The read integer.
172 * Reads a 24-bit unsigned integer from the buffer at the current position.
174 * @return The read integer.
176 int readUnsignedMedium();
179 * Reads a 32-bit signed integer from the buffer at the current position.
181 * @return The read integer.
186 * Reads a 32-bit unsigned integer from the buffer at the current position.
188 * @return The read integer.
190 long readUnsignedInt();
193 * Reads a 64-bit signed integer from the buffer at the current position.
195 * @return The read long.
200 * Reads a single-precision 32-bit floating point number from the buffer at the current position.
202 * @return The read float.
207 * Reads a double-precision 64-bit floating point number from the buffer at the current position.
209 * @return The read double.
214 * Reads a 1 byte boolean from the buffer at the current position.
216 * @return The read boolean.
218 boolean readBoolean();
221 * Reads a string from the buffer at the current position.
223 * @return The read string.
228 * Reads a string from the buffer at the current position.
230 * @param charset The character set with which to decode the string.
231 * @return The read string.
233 String readString(Charset charset);
236 * Reads a UTF-8 string from the buffer at the current position.
238 * @return The read string.