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 writing to a byte buffer. Writable 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 BufferOutput<T extends BufferOutput<?>> extends AutoCloseable {
32 * Writes an array of bytes to the buffer.
34 * @param bytes The array of bytes to write.
35 * @return The written buffer.
40 * Writes an array of bytes to the buffer.
42 * @param bytes The array of bytes to write.
43 * @return The written buffer.
45 T write(byte[] bytes);
48 * Writes an array of bytes to the buffer.
50 * @param bytes The array of bytes to write.
51 * @param offset The offset at which to start writing the bytes.
52 * @param length The number of bytes from the provided byte array to write to the buffer.
53 * @return The written buffer.
55 T write(Bytes bytes, int offset, int length);
58 * Writes an array of bytes to the buffer.
60 * @param bytes The array of bytes to write.
61 * @param offset The offset at which to start writing the bytes.
62 * @param length The number of bytes from the provided byte array to write to the buffer.
63 * @return The written buffer.
65 T write(byte[] bytes, int offset, int length);
68 * Writes a buffer to the buffer.
70 * @param buffer The buffer to write.
71 * @return The written buffer.
73 T write(Buffer buffer);
76 * Writes an object to the snapshot.
78 * @param object the object to write
79 * @param encoder the object encoder
80 * @return The snapshot writer.
82 @SuppressWarnings("unchecked")
83 default <U> T writeObject(U object, Function<U, byte[]> encoder) {
84 byte[] bytes = encoder.apply(object);
85 writeInt(bytes.length).write(bytes);
90 * Writes a byte array.
92 * @param bytes The byte array to write.
93 * @return The written buffer.
95 @SuppressWarnings("unchecked")
96 default T writeBytes(byte[] bytes) {
102 * Writes a byte to the buffer.
104 * @param b The byte to write.
105 * @return The written buffer.
110 * Writes an unsigned byte to the buffer.
112 * @param b The byte to write.
113 * @return The written buffer.
115 T writeUnsignedByte(int b);
118 * Writes a 16-bit character to the buffer.
120 * @param c The character to write.
121 * @return The written buffer.
126 * Writes a 16-bit signed integer to the buffer.
128 * @param s The short to write.
129 * @return The written buffer.
131 T writeShort(short s);
134 * Writes a 16-bit unsigned integer to the buffer.
136 * @param s The short to write.
137 * @return The written buffer.
139 T writeUnsignedShort(int s);
142 * Writes a 24-bit signed integer to the buffer.
144 * @param m The integer to write.
145 * @return The written buffer.
147 T writeMedium(int m);
150 * Writes a 24-bit unsigned integer to the buffer.
152 * @param m The integer to write.
153 * @return The written buffer.
155 T writeUnsignedMedium(int m);
158 * Writes a 32-bit signed integer to the buffer.
160 * @param i The integer to write.
161 * @return The written buffer.
166 * Writes a 32-bit unsigned integer to the buffer.
168 * @param i The integer to write.
169 * @return The written buffer.
171 T writeUnsignedInt(long i);
174 * Writes a 64-bit signed integer to the buffer.
176 * @param l The long to write.
177 * @return The written buffer.
182 * Writes a single-precision 32-bit floating point number to the buffer.
184 * @param f The float to write.
185 * @return The written buffer.
187 T writeFloat(float f);
190 * Writes a double-precision 64-bit floating point number to the buffer.
192 * @param d The double to write.
193 * @return The written buffer.
195 T writeDouble(double d);
198 * Writes a 1 byte boolean to the buffer.
200 * @param b The boolean to write.
201 * @return The written buffer.
203 T writeBoolean(boolean b);
206 * Writes a string to the buffer.
208 * @param s The string to write.
209 * @return The written buffer.
211 T writeString(String s);
214 * Writes a string to the buffer.
216 * @param s The string to write.
217 * @param charset The character set with which to encode the string.
218 * @return The written buffer.
220 T writeString(String s, Charset charset);
223 * Writes a UTF-8 string to the buffer.
225 * @param s The string to write.
226 * @return The written buffer.
228 T writeUTF8(String s);
231 * Flushes the buffer to the underlying persistence layer.
233 * @return The flushed buffer.