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;
21 * {@link ByteBuffer} based direct bytes.
23 public class DirectBytes extends ByteBufferBytes {
26 * Allocates a new direct byte array.
28 * @param size The count of the buffer to allocate (in bytes).
29 * @return The direct buffer.
30 * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed count for
31 * an array on the Java heap - {@code Integer.MAX_VALUE - 5}
33 public static DirectBytes allocate(int size) {
34 if (size > MAX_SIZE) {
35 throw new IllegalArgumentException("size cannot for DirectBytes cannot be greater than " + MAX_SIZE);
37 return new DirectBytes(ByteBuffer.allocateDirect((int) size));
40 protected DirectBytes(ByteBuffer buffer) {
45 protected ByteBuffer newByteBuffer(int size) {
46 return ByteBuffer.allocateDirect((int) size);
50 public boolean isDirect() {
51 return buffer.isDirect();