
io.atomix.catalyst.buffer.DirectBuffer Maven / Gradle / Ivy
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.atomix.catalyst.buffer;
import io.atomix.catalyst.buffer.util.DirectMemoryAllocator;
import io.atomix.catalyst.buffer.util.DirectMemory;
import io.atomix.catalyst.buffer.util.Memory;
import io.atomix.catalyst.util.reference.ReferenceManager;
/**
* Direct {@link java.nio.ByteBuffer} based buffer.
*
* @author Jordan Halterman
*/
public class DirectBuffer extends NativeBuffer {
/**
* Allocates a direct buffer with an initial capacity of {@code 4096} and a maximum capacity of {@link Long#MAX_VALUE}.
*
* When the buffer is constructed, {@link DirectMemoryAllocator} will be used to allocate
* {@code capacity} bytes of off-heap memory. The resulting buffer will be initialized with a capacity of {@code 4096}
* and have a maximum capacity of {@link Long#MAX_VALUE}. The buffer's {@code capacity} will dynamically expand as
* bytes are written to the buffer. The underlying {@link DirectBytes} will be initialized to the next power of {@code 2}.
*
* @return The direct buffer.
*
* @see DirectBuffer#allocate(long)
* @see DirectBuffer#allocate(long, long)
*/
public static DirectBuffer allocate() {
return allocate(DEFAULT_INITIAL_CAPACITY, Long.MAX_VALUE);
}
/**
* Allocates a direct buffer with the given initial capacity.
*
* When the buffer is constructed, {@link DirectMemoryAllocator} will be used to allocate
* {@code capacity} bytes of off-heap memory. The resulting buffer will have an initial capacity of {@code capacity}.
* The underlying {@link DirectBytes} will be initialized to the next power of {@code 2}.
*
* @param initialCapacity The initial capacity of the buffer to allocate (in bytes).
* @return The direct buffer.
* @throws IllegalArgumentException If {@code capacity} is greater than the maximum allowed count for
* a {@link java.nio.ByteBuffer} - {@code Integer.MAX_VALUE - 5}
*
* @see DirectBuffer#allocate()
* @see DirectBuffer#allocate(long, long)
*/
public static DirectBuffer allocate(long initialCapacity) {
return allocate(initialCapacity, Long.MAX_VALUE);
}
/**
* Allocates a new direct buffer.
*
* When the buffer is constructed, {@link DirectMemoryAllocator} will be used to allocate
* {@code capacity} bytes of off-heap memory. The resulting buffer will have an initial capacity of {@code initialCapacity}
* and will be doubled up to {@code maxCapacity} as bytes are written to the buffer. The underlying {@link DirectBytes}
* will be initialized to the next power of {@code 2}.
*
* @param initialCapacity The initial capacity of the buffer to allocate (in bytes).
* @param maxCapacity The maximum capacity of the buffer.
* @return The direct buffer.
* @throws IllegalArgumentException If {@code capacity} or {@code maxCapacity} is greater than the maximum
* allowed count for a {@link java.nio.ByteBuffer} - {@code Integer.MAX_VALUE - 5}
*
* @see DirectBuffer#allocate()
* @see DirectBuffer#allocate(long)
*/
public static DirectBuffer allocate(long initialCapacity, long maxCapacity) {
if (initialCapacity > maxCapacity)
throw new IllegalArgumentException("initial capacity cannot be greater than maximum capacity");
return new DirectBuffer(new DirectBytes(DirectMemory.allocate(Memory.Util.toPow2(initialCapacity))), 0, initialCapacity, maxCapacity);
}
protected DirectBuffer(DirectBytes bytes, long offset, long initialCapacity, long maxCapacity) {
super(bytes, offset, initialCapacity, maxCapacity);
}
protected DirectBuffer(DirectBytes bytes, ReferenceManager referenceManager) {
super(bytes, referenceManager);
}
}