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.ByteOrder;
21 * Common interface for interacting with a memory or disk based array of bytes.
23 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
25 public interface Bytes extends BytesInput<Bytes>, BytesOutput<Bytes>, AutoCloseable {
37 * Returns whether the bytes has an array.
39 * @return Whether the bytes has an underlying array.
41 default boolean hasArray() {
46 * Returns the underlying byte array.
48 * @return the underlying byte array
49 * @throws UnsupportedOperationException if a heap array is not supported
51 default byte[] array() {
52 throw new UnsupportedOperationException();
56 * Returns the count of the bytes.
58 * @return The count of the bytes.
65 * When the bytes are resized, underlying memory addresses in copies of this instance may no longer be valid. Additionally,
66 * if the {@code newSize} is smaller than the current {@code count} then some data may be lost during the resize. Use
69 * @param newSize The count to which to resize this instance.
70 * @return The resized bytes.
72 Bytes resize(int newSize);
75 * Returns the byte order.
77 * For consistency with {@link java.nio.ByteBuffer}, all bytes implementations are initially in {@link ByteOrder#BIG_ENDIAN} order.
79 * @return The byte order.
84 * Sets the byte order, returning a new swapped {@link Bytes} instance.
86 * By default, all bytes are read and written in {@link ByteOrder#BIG_ENDIAN} order. This provides complete
87 * consistency with {@link java.nio.ByteBuffer}. To flip bytes to {@link ByteOrder#LITTLE_ENDIAN} order, this
88 * {@code Bytes} instance is decorated by a {@link SwappedBytes} instance which will reverse
89 * read and written bytes using, e.g. {@link Integer#reverseBytes(int)}.
91 * @param order The byte order.
92 * @return The updated bytes.
93 * @throws NullPointerException If the {@code order} is {@code null}
95 Bytes order(ByteOrder order);
98 * Returns a boolean value indicating whether the bytes are direct.
100 * @return Indicates whether the bytes are direct.
105 * Returns a boolean value indicating whether the bytes are backed by a file.
107 * @return Indicates whether the bytes are backed by a file.