Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / buffer / WrappedBytes.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.ByteOrder;
19
20 /**
21  * Wrapped bytes.
22  *
23  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
24  */
25 public class WrappedBytes extends AbstractBytes {
26   protected final Bytes bytes;
27   private final Bytes root;
28
29   public WrappedBytes(Bytes bytes) {
30     if (bytes == null) {
31       throw new NullPointerException("bytes cannot be null");
32     }
33     this.bytes = bytes;
34     this.root = bytes instanceof WrappedBytes ? ((WrappedBytes) bytes).root : bytes;
35   }
36
37   /**
38    * Returns the root bytes.
39    */
40   public Bytes root() {
41     return root;
42   }
43
44   @Override
45   public int size() {
46     return bytes.size();
47   }
48
49   @Override
50   public Bytes resize(int newSize) {
51     return bytes.resize(newSize);
52   }
53
54   @Override
55   public ByteOrder order() {
56     return bytes.order();
57   }
58
59   @Override
60   public Bytes zero() {
61     bytes.zero();
62     return this;
63   }
64
65   @Override
66   public Bytes zero(int offset) {
67     bytes.zero(offset);
68     return this;
69   }
70
71   @Override
72   public Bytes zero(int offset, int length) {
73     bytes.zero(offset, length);
74     return this;
75   }
76
77   @Override
78   public Bytes read(int offset, Bytes dst, int dstOffset, int length) {
79     bytes.read(offset, dst, dstOffset, length);
80     return this;
81   }
82
83   @Override
84   public Bytes read(int offset, byte[] dst, int dstOffset, int length) {
85     bytes.read(offset, dst, dstOffset, length);
86     return this;
87   }
88
89   @Override
90   public int readByte(int offset) {
91     return bytes.readByte(offset);
92   }
93
94   @Override
95   public int readUnsignedByte(int offset) {
96     return bytes.readUnsignedByte(offset);
97   }
98
99   @Override
100   public char readChar(int offset) {
101     return bytes.readChar(offset);
102   }
103
104   @Override
105   public short readShort(int offset) {
106     return bytes.readShort(offset);
107   }
108
109   @Override
110   public int readUnsignedShort(int offset) {
111     return bytes.readUnsignedShort(offset);
112   }
113
114   @Override
115   public int readMedium(int offset) {
116     return bytes.readMedium(offset);
117   }
118
119   @Override
120   public int readUnsignedMedium(int offset) {
121     return bytes.readUnsignedMedium(offset);
122   }
123
124   @Override
125   public int readInt(int offset) {
126     return bytes.readInt(offset);
127   }
128
129   @Override
130   public long readUnsignedInt(int offset) {
131     return bytes.readUnsignedInt(offset);
132   }
133
134   @Override
135   public long readLong(int offset) {
136     return bytes.readLong(offset);
137   }
138
139   @Override
140   public float readFloat(int offset) {
141     return bytes.readFloat(offset);
142   }
143
144   @Override
145   public double readDouble(int offset) {
146     return bytes.readDouble(offset);
147   }
148
149   @Override
150   public boolean readBoolean(int offset) {
151     return bytes.readBoolean(offset);
152   }
153
154   @Override
155   public String readString(int offset) {
156     return bytes.readString(offset);
157   }
158
159   @Override
160   public String readUTF8(int offset) {
161     return bytes.readUTF8(offset);
162   }
163
164   @Override
165   public Bytes write(int offset, Bytes src, int srcOffset, int length) {
166     bytes.write(offset, src, srcOffset, length);
167     return this;
168   }
169
170   @Override
171   public Bytes write(int offset, byte[] src, int srcOffset, int length) {
172     bytes.write(offset, src, srcOffset, length);
173     return this;
174   }
175
176   @Override
177   public Bytes writeByte(int offset, int b) {
178     bytes.writeByte(offset, b);
179     return this;
180   }
181
182   @Override
183   public Bytes writeUnsignedByte(int offset, int b) {
184     bytes.writeUnsignedByte(offset, b);
185     return this;
186   }
187
188   @Override
189   public Bytes writeChar(int offset, char c) {
190     bytes.writeChar(offset, c);
191     return this;
192   }
193
194   @Override
195   public Bytes writeShort(int offset, short s) {
196     bytes.writeShort(offset, s);
197     return this;
198   }
199
200   @Override
201   public Bytes writeUnsignedShort(int offset, int s) {
202     bytes.writeUnsignedShort(offset, s);
203     return this;
204   }
205
206   @Override
207   public Bytes writeMedium(int offset, int m) {
208     bytes.writeMedium(offset, m);
209     return this;
210   }
211
212   @Override
213   public Bytes writeUnsignedMedium(int offset, int m) {
214     bytes.writeUnsignedMedium(offset, m);
215     return this;
216   }
217
218   @Override
219   public Bytes writeInt(int offset, int i) {
220     bytes.writeInt(offset, i);
221     return this;
222   }
223
224   @Override
225   public Bytes writeUnsignedInt(int offset, long i) {
226     bytes.writeUnsignedInt(offset, i);
227     return this;
228   }
229
230   @Override
231   public Bytes writeLong(int offset, long l) {
232     bytes.writeLong(offset, l);
233     return this;
234   }
235
236   @Override
237   public Bytes writeFloat(int offset, float f) {
238     bytes.writeFloat(offset, f);
239     return this;
240   }
241
242   @Override
243   public Bytes writeDouble(int offset, double d) {
244     bytes.writeDouble(offset, d);
245     return this;
246   }
247
248   @Override
249   public Bytes writeBoolean(int offset, boolean b) {
250     bytes.writeBoolean(offset, b);
251     return this;
252   }
253
254   @Override
255   public Bytes writeString(int offset, String s) {
256     bytes.writeString(offset, s);
257     return this;
258   }
259
260   @Override
261   public Bytes writeUTF8(int offset, String s) {
262     bytes.writeUTF8(offset, s);
263     return this;
264   }
265
266   @Override
267   public Bytes flush() {
268     bytes.flush();
269     return this;
270   }
271
272   @Override
273   public void close() {
274     bytes.close();
275   }
276
277 }