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.ByteBuffer;
19 import java.nio.ByteOrder;
21 import static com.google.common.base.Preconditions.checkNotNull;
26 public abstract class ByteBufferBytes extends AbstractBytes {
27 protected ByteBuffer buffer;
29 protected ByteBufferBytes(ByteBuffer buffer) {
33 public Bytes reset(ByteBuffer buffer) {
35 this.buffer = checkNotNull(buffer, "buffer cannot be null");
40 * Allocates a new byte buffer.
42 * @param size the buffer size
43 * @return a newly allocated byte buffer
45 protected abstract ByteBuffer newByteBuffer(int size);
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);
55 return reset(newBuffer);
59 public byte[] array() {
60 return buffer.array();
64 * Returns the underlying {@link ByteBuffer}.
66 * @return the underlying byte buffer
68 public ByteBuffer byteBuffer() {
79 return buffer.capacity();
83 public ByteOrder order() {
84 return buffer.order();
88 public Bytes order(ByteOrder order) {
89 return reset(buffer.order(order));
93 * Returns the index for the given offset.
95 private int index(int offset) {
100 public Bytes zero(int offset) {
101 for (int i = index(offset); i < buffer.capacity(); i++) {
102 buffer.put(i, (byte) 0);
108 public Bytes zero(int offset, int length) {
109 for (int i = index(offset); i < offset + length; i++) {
110 buffer.put(i, (byte) 0);
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);
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));
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]);
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));
148 public int readByte(int offset) {
149 return buffer.get(index(offset));
153 public char readChar(int offset) {
154 return buffer.getChar(index(offset));
158 public short readShort(int offset) {
159 return buffer.getShort(index(offset));
163 public int readInt(int offset) {
164 return buffer.getInt(index(offset));
168 public long readLong(int offset) {
169 return buffer.getLong(index(offset));
173 public float readFloat(int offset) {
174 return buffer.getFloat(index(offset));
178 public double readDouble(int offset) {
179 return buffer.getDouble(index(offset));
183 public Bytes writeByte(int offset, int b) {
184 buffer.put(index(offset), (byte) b);
189 public Bytes writeChar(int offset, char c) {
190 buffer.putChar(index(offset), c);
195 public Bytes writeShort(int offset, short s) {
196 buffer.putShort(index(offset), s);
201 public Bytes writeInt(int offset, int i) {
202 buffer.putInt(index(offset), i);
207 public Bytes writeLong(int offset, long l) {
208 buffer.putLong(index(offset), l);
213 public Bytes writeFloat(int offset, float f) {
214 buffer.putFloat(index(offset), f);
219 public Bytes writeDouble(int offset, double d) {
220 buffer.putDouble(index(offset), d);