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;
23 * This interface exposes methods for writing bytes to specific positions in a byte array.
25 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
27 public interface BytesOutput<T extends BytesOutput<T>> {
30 * Zeros out all bytes in the array.
32 * @return The written bytes.
37 * Zeros out all bytes starting at the given offset in the array.
39 * @param offset The offset at which to start zeroing out bytes.
40 * @return The written bytes.
45 * Zeros out bytes starting at the given offset up to the given length.
47 * @param offset The offset at which to start zeroing out bytes.
48 * @param length THe total number of bytes to zero out.
49 * @return The written bytes.
51 T zero(int offset, int length);
54 * Writes an array of bytes to the buffer.
56 * @param offset The offset at which to start writing the bytes.
57 * @param src The array of bytes to write.
58 * @param srcOffset The offset at which to start reading bytes from the given source.
59 * @param length The number of bytes from the provided byte array to write to the buffer.
60 * @return The written buffer.
62 T write(int offset, Bytes src, int srcOffset, int length);
65 * Writes an array of bytes to the buffer.
67 * @param offset The offset at which to start writing the bytes.
68 * @param src The array of bytes to write.
69 * @param srcOffset The offset at which to start reading bytes from the given source.
70 * @param length The number of bytes from the provided byte array to write to the buffer.
71 * @return The written buffer.
73 T write(int offset, byte[] src, int srcOffset, int length);
76 * Writes a byte to the buffer at the given offset.
78 * @param offset The offset at which to write the byte.
79 * @param b The byte to write.
80 * @return The written buffer.
82 T writeByte(int offset, int b);
85 * Writes an unsigned byte to the buffer at the given position.
87 * @param offset The offset at which to write the byte.
88 * @param b The byte to write.
89 * @return The written buffer.
91 T writeUnsignedByte(int offset, int b);
94 * Writes a 16-bit character to the buffer at the given offset.
96 * @param offset The offset at which to write the character.
97 * @param c The character to write.
98 * @return The written buffer.
100 T writeChar(int offset, char c);
103 * Writes a 16-bit signed integer to the buffer at the given offset.
105 * @param offset The offset at which to write the short.
106 * @param s The short to write.
107 * @return The written buffer.
109 T writeShort(int offset, short s);
112 * Writes a 16-bit unsigned integer to the buffer at the given offset.
114 * @param offset The offset at which to write the short.
115 * @param s The short to write.
116 * @return The written buffer.
118 T writeUnsignedShort(int offset, int s);
121 * Writes a 24-bit signed integer to the buffer at the given offset.
123 * @param offset The offset at which to write the short.
124 * @param m The short to write.
125 * @return The written buffer.
127 T writeMedium(int offset, int m);
130 * Writes a 24-bit unsigned integer to the buffer at the given offset.
132 * @param offset The offset at which to write the short.
133 * @param m The short to write.
134 * @return The written buffer.
136 T writeUnsignedMedium(int offset, int m);
139 * Writes a 32-bit signed integer to the buffer at the given offset.
141 * @param offset The offset at which to write the integer.
142 * @param i The integer to write.
143 * @return The written buffer.
145 T writeInt(int offset, int i);
148 * Writes a 32-bit unsigned integer to the buffer at the given offset.
150 * @param offset The offset at which to write the integer.
151 * @param i The integer to write.
152 * @return The written buffer.
154 T writeUnsignedInt(int offset, long i);
157 * Writes a 64-bit signed integer to the buffer at the given offset.
159 * @param offset The offset at which to write the long.
160 * @param l The long to write.
161 * @return The written buffer.
163 T writeLong(int offset, long l);
166 * Writes a single-precision 32-bit floating point number to the buffer at the given offset.
168 * @param offset The offset at which to write the float.
169 * @param f The float to write.
170 * @return The written buffer.
172 T writeFloat(int offset, float f);
175 * Writes a double-precision 64-bit floating point number to the buffer at the given offset.
177 * @param offset The offset at which to write the double.
178 * @param d The double to write.
179 * @return The written buffer.
181 T writeDouble(int offset, double d);
184 * Writes a 1 byte boolean to the buffer at the given offset.
186 * @param offset The offset at which to write the boolean.
187 * @param b The boolean to write.
188 * @return The written buffer.
190 T writeBoolean(int offset, boolean b);
193 * Writes a string to the buffer at the given offset.
195 * @param offset The offset at which to write the string.
196 * @param s The string to write.
197 * @return The written buffer.
199 T writeString(int offset, String s);
202 * Writes a string to the buffer at the given offset.
204 * @param offset The offset at which to write the string.
205 * @param s The string to write.
206 * @param charset The character set with which to encode the string.
207 * @return The written buffer.
209 T writeString(int offset, String s, Charset charset);
212 * Writes a UTF-8 string to the buffer at the given offset.
214 * @param offset The offset at which to write the string.
215 * @param s The string to write.
216 * @return The written buffer.
218 T writeUTF8(int offset, String s);
221 * Flushes the bytes to the underlying persistence layer.
223 * @return The flushed buffer.