Import atomix/{storage,utils}
[controller.git] / third-party / atomix / storage / src / main / java / io / atomix / storage / buffer / DirectBytes.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
20 /**
21  * {@link ByteBuffer} based direct bytes.
22  */
23 public class DirectBytes extends ByteBufferBytes {
24
25   /**
26    * Allocates a new direct byte array.
27    *
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}
32    */
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);
36     }
37     return new DirectBytes(ByteBuffer.allocateDirect((int) size));
38   }
39
40   protected DirectBytes(ByteBuffer buffer) {
41     super(buffer);
42   }
43
44   @Override
45   protected ByteBuffer newByteBuffer(int size) {
46     return ByteBuffer.allocateDirect((int) size);
47   }
48
49   @Override
50   public boolean isDirect() {
51     return buffer.isDirect();
52   }
53
54 }