Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / buffer / BufferOutput.java
1 /*
2  * Copyright 2015-present Open Networking Foundation
3  *
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
7  *
8  * http://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,
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.
15  */
16 package io.atomix.storage.buffer;
17
18 import java.nio.charset.Charset;
19 import java.util.function.Function;
20
21 /**
22  * Writable buffer.
23  * <p>
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}.
26  *
27  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
28  */
29 public interface BufferOutput<T extends BufferOutput<?>> extends AutoCloseable {
30
31   /**
32    * Writes an array of bytes to the buffer.
33    *
34    * @param bytes The array of bytes to write.
35    * @return The written buffer.
36    */
37   T write(Bytes bytes);
38
39   /**
40    * Writes an array of bytes to the buffer.
41    *
42    * @param bytes The array of bytes to write.
43    * @return The written buffer.
44    */
45   T write(byte[] bytes);
46
47   /**
48    * Writes an array of bytes to the buffer.
49    *
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.
54    */
55   T write(Bytes bytes, int offset, int length);
56
57   /**
58    * Writes an array of bytes to the buffer.
59    *
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.
64    */
65   T write(byte[] bytes, int offset, int length);
66
67   /**
68    * Writes a buffer to the buffer.
69    *
70    * @param buffer The buffer to write.
71    * @return The written buffer.
72    */
73   T write(Buffer buffer);
74
75   /**
76    * Writes an object to the snapshot.
77    *
78    * @param object the object to write
79    * @param encoder the object encoder
80    * @return The snapshot writer.
81    */
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);
86     return (T) this;
87   }
88
89   /**
90    * Writes a byte array.
91    *
92    * @param bytes The byte array to write.
93    * @return The written buffer.
94    */
95   @SuppressWarnings("unchecked")
96   default T writeBytes(byte[] bytes) {
97     write(bytes);
98     return (T) this;
99   }
100
101   /**
102    * Writes a byte to the buffer.
103    *
104    * @param b The byte to write.
105    * @return The written buffer.
106    */
107   T writeByte(int b);
108
109   /**
110    * Writes an unsigned byte to the buffer.
111    *
112    * @param b The byte to write.
113    * @return The written buffer.
114    */
115   T writeUnsignedByte(int b);
116
117   /**
118    * Writes a 16-bit character to the buffer.
119    *
120    * @param c The character to write.
121    * @return The written buffer.
122    */
123   T writeChar(char c);
124
125   /**
126    * Writes a 16-bit signed integer to the buffer.
127    *
128    * @param s The short to write.
129    * @return The written buffer.
130    */
131   T writeShort(short s);
132
133   /**
134    * Writes a 16-bit unsigned integer to the buffer.
135    *
136    * @param s The short to write.
137    * @return The written buffer.
138    */
139   T writeUnsignedShort(int s);
140
141   /**
142    * Writes a 24-bit signed integer to the buffer.
143    *
144    * @param m The integer to write.
145    * @return The written buffer.
146    */
147   T writeMedium(int m);
148
149   /**
150    * Writes a 24-bit unsigned integer to the buffer.
151    *
152    * @param m The integer to write.
153    * @return The written buffer.
154    */
155   T writeUnsignedMedium(int m);
156
157   /**
158    * Writes a 32-bit signed integer to the buffer.
159    *
160    * @param i The integer to write.
161    * @return The written buffer.
162    */
163   T writeInt(int i);
164
165   /**
166    * Writes a 32-bit unsigned integer to the buffer.
167    *
168    * @param i The integer to write.
169    * @return The written buffer.
170    */
171   T writeUnsignedInt(long i);
172
173   /**
174    * Writes a 64-bit signed integer to the buffer.
175    *
176    * @param l The long to write.
177    * @return The written buffer.
178    */
179   T writeLong(long l);
180
181   /**
182    * Writes a single-precision 32-bit floating point number to the buffer.
183    *
184    * @param f The float to write.
185    * @return The written buffer.
186    */
187   T writeFloat(float f);
188
189   /**
190    * Writes a double-precision 64-bit floating point number to the buffer.
191    *
192    * @param d The double to write.
193    * @return The written buffer.
194    */
195   T writeDouble(double d);
196
197   /**
198    * Writes a 1 byte boolean to the buffer.
199    *
200    * @param b The boolean to write.
201    * @return The written buffer.
202    */
203   T writeBoolean(boolean b);
204
205   /**
206    * Writes a string to the buffer.
207    *
208    * @param s The string to write.
209    * @return The written buffer.
210    */
211   T writeString(String s);
212
213   /**
214    * Writes a string to the buffer.
215    *
216    * @param s       The string to write.
217    * @param charset The character set with which to encode the string.
218    * @return The written buffer.
219    */
220   T writeString(String s, Charset charset);
221
222   /**
223    * Writes a UTF-8 string to the buffer.
224    *
225    * @param s The string to write.
226    * @return The written buffer.
227    */
228   T writeUTF8(String s);
229
230   /**
231    * Flushes the buffer to the underlying persistence layer.
232    *
233    * @return The flushed buffer.
234    */
235   T flush();
236
237   @Override
238   void close();
239
240 }