Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / buffer / ByteBufferBytes.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.ByteBuffer;
19 import java.nio.ByteOrder;
20
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 /**
24  * Byte buffer bytes.
25  */
26 public abstract class ByteBufferBytes extends AbstractBytes {
27   protected ByteBuffer buffer;
28
29   protected ByteBufferBytes(ByteBuffer buffer) {
30     this.buffer = buffer;
31   }
32
33   public Bytes reset(ByteBuffer buffer) {
34     buffer.clear();
35     this.buffer = checkNotNull(buffer, "buffer cannot be null");
36     return this;
37   }
38
39   /**
40    * Allocates a new byte buffer.
41    *
42    * @param size the buffer size
43    * @return a newly allocated byte buffer
44    */
45   protected abstract ByteBuffer newByteBuffer(int size);
46
47   @Override
48   public Bytes resize(int newSize) {
49     ByteBuffer oldBuffer = buffer;
50     ByteBuffer newBuffer = newByteBuffer(newSize);
51     oldBuffer.position(0).limit(oldBuffer.capacity());
52     newBuffer.position(0).limit(newBuffer.capacity());
53     newBuffer.put(oldBuffer);
54     newBuffer.clear();
55     return reset(newBuffer);
56   }
57
58   @Override
59   public byte[] array() {
60     return buffer.array();
61   }
62
63   /**
64    * Returns the underlying {@link ByteBuffer}.
65    *
66    * @return the underlying byte buffer
67    */
68   public ByteBuffer byteBuffer() {
69     return buffer;
70   }
71
72   @Override
73   public Bytes zero() {
74     return this;
75   }
76
77   @Override
78   public int size() {
79     return buffer.capacity();
80   }
81
82   @Override
83   public ByteOrder order() {
84     return buffer.order();
85   }
86
87   @Override
88   public Bytes order(ByteOrder order) {
89     return reset(buffer.order(order));
90   }
91
92   /**
93    * Returns the index for the given offset.
94    */
95   private int index(int offset) {
96     return (int) offset;
97   }
98
99   @Override
100   public Bytes zero(int offset) {
101     for (int i = index(offset); i < buffer.capacity(); i++) {
102       buffer.put(i, (byte) 0);
103     }
104     return this;
105   }
106
107   @Override
108   public Bytes zero(int offset, int length) {
109     for (int i = index(offset); i < offset + length; i++) {
110       buffer.put(i, (byte) 0);
111     }
112     return this;
113   }
114
115   @Override
116   public Bytes read(int position, byte[] bytes, int offset, int length) {
117     for (int i = 0; i < length; i++) {
118       bytes[index(offset) + i] = (byte) readByte(position + i);
119     }
120     return this;
121   }
122
123   @Override
124   public Bytes read(int position, Bytes bytes, int offset, int length) {
125     for (int i = 0; i < length; i++) {
126       bytes.writeByte(offset + i, readByte(position + i));
127     }
128     return this;
129   }
130
131   @Override
132   public Bytes write(int position, byte[] bytes, int offset, int length) {
133     for (int i = 0; i < length; i++) {
134       buffer.put((int) position + i, (byte) bytes[index(offset) + i]);
135     }
136     return this;
137   }
138
139   @Override
140   public Bytes write(int position, Bytes bytes, int offset, int length) {
141     for (int i = 0; i < length; i++) {
142       buffer.put((int) position + i, (byte) bytes.readByte(offset + i));
143     }
144     return this;
145   }
146
147   @Override
148   public int readByte(int offset) {
149     return buffer.get(index(offset));
150   }
151
152   @Override
153   public char readChar(int offset) {
154     return buffer.getChar(index(offset));
155   }
156
157   @Override
158   public short readShort(int offset) {
159     return buffer.getShort(index(offset));
160   }
161
162   @Override
163   public int readInt(int offset) {
164     return buffer.getInt(index(offset));
165   }
166
167   @Override
168   public long readLong(int offset) {
169     return buffer.getLong(index(offset));
170   }
171
172   @Override
173   public float readFloat(int offset) {
174     return buffer.getFloat(index(offset));
175   }
176
177   @Override
178   public double readDouble(int offset) {
179     return buffer.getDouble(index(offset));
180   }
181
182   @Override
183   public Bytes writeByte(int offset, int b) {
184     buffer.put(index(offset), (byte) b);
185     return this;
186   }
187
188   @Override
189   public Bytes writeChar(int offset, char c) {
190     buffer.putChar(index(offset), c);
191     return this;
192   }
193
194   @Override
195   public Bytes writeShort(int offset, short s) {
196     buffer.putShort(index(offset), s);
197     return this;
198   }
199
200   @Override
201   public Bytes writeInt(int offset, int i) {
202     buffer.putInt(index(offset), i);
203     return this;
204   }
205
206   @Override
207   public Bytes writeLong(int offset, long l) {
208     buffer.putLong(index(offset), l);
209     return this;
210   }
211
212   @Override
213   public Bytes writeFloat(int offset, float f) {
214     buffer.putFloat(index(offset), f);
215     return this;
216   }
217
218   @Override
219   public Bytes writeDouble(int offset, double d) {
220     buffer.putDouble(index(offset), d);
221     return this;
222   }
223
224 }