From: Robert Varga Date: Thu, 2 Mar 2023 10:00:10 +0000 (+0100) Subject: Remove atomix.storage.buffer X-Git-Tag: v7.0.5~81 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=cda35098ebb86cf3f3c9ca617cb0831947626984 Remove atomix.storage.buffer This package is not used anywhere, remove it. This also removes the need for atomix.utils.concurrent, which we remove as well. JIRA: CONTROLLER-2071 Change-Id: I0664f6b12e47b04c2c0531c095b3d6e857081f61 Signed-off-by: Robert Varga --- diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/AbstractBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/AbstractBuffer.java deleted file mode 100644 index 5a86e406a9..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/AbstractBuffer.java +++ /dev/null @@ -1,946 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferenceManager; -import io.atomix.utils.memory.Memory; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteOrder; -import java.nio.InvalidMarkException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.concurrent.atomic.AtomicInteger; - -import static io.atomix.storage.buffer.Bytes.BOOLEAN; -import static io.atomix.storage.buffer.Bytes.BYTE; -import static io.atomix.storage.buffer.Bytes.SHORT; - -/** - * Abstract buffer implementation. - * - * @author Jordan Halterman - */ -public abstract class AbstractBuffer implements Buffer { - static final int DEFAULT_INITIAL_CAPACITY = 4096; - static final int MAX_SIZE = Integer.MAX_VALUE - 5; - - protected final Bytes bytes; - private int offset; - private int initialCapacity; - private int capacity; - private int maxCapacity; - private int position; - private int limit = -1; - private int mark = -1; - private final AtomicInteger references = new AtomicInteger(); - protected final ReferenceManager referenceManager; - private SwappedBuffer swap; - - protected AbstractBuffer(Bytes bytes, ReferenceManager referenceManager) { - this(bytes, 0, 0, 0, referenceManager); - } - - protected AbstractBuffer(Bytes bytes, int offset, int initialCapacity, int maxCapacity, ReferenceManager referenceManager) { - if (bytes == null) { - throw new NullPointerException("bytes cannot be null"); - } - if (offset < 0) { - throw new IndexOutOfBoundsException("offset out of bounds of the underlying byte array"); - } - this.bytes = bytes; - this.offset = offset; - this.capacity = 0; - this.initialCapacity = initialCapacity; - this.maxCapacity = maxCapacity; - capacity(initialCapacity); - this.referenceManager = referenceManager; - references.set(1); - } - - /** - * Resets the buffer's internal offset and capacity. - */ - protected AbstractBuffer reset(int offset, int capacity, int maxCapacity) { - this.offset = offset; - this.capacity = 0; - this.initialCapacity = capacity; - this.maxCapacity = maxCapacity; - capacity(initialCapacity); - references.set(0); - rewind(); - return this; - } - - @Override - public Buffer acquire() { - references.incrementAndGet(); - return this; - } - - @Override - public boolean release() { - if (references.decrementAndGet() == 0) { - if (referenceManager != null) { - referenceManager.release(this); - } else { - bytes.close(); - } - return true; - } - return false; - } - - @Override - public int references() { - return references.get(); - } - - @Override - public Bytes bytes() { - return bytes; - } - - @Override - public ByteOrder order() { - return bytes.order(); - } - - @Override - public Buffer order(ByteOrder order) { - if (order == null) { - throw new NullPointerException("order cannot be null"); - } - if (order == order()) { - return this; - } - if (swap != null) { - return swap; - } - swap = new SwappedBuffer(this, offset, capacity, maxCapacity, referenceManager); - return swap; - } - - @Override - public boolean isDirect() { - return bytes.isDirect(); - } - - @Override - public boolean isFile() { - return bytes.isFile(); - } - - @Override - public boolean isReadOnly() { - return false; - } - - @Override - public Buffer asReadOnlyBuffer() { - return new ReadOnlyBuffer(this, referenceManager) - .reset(offset, capacity, maxCapacity) - .position(position) - .limit(limit); - } - - @Override - public Buffer compact() { - compact(offset(position), offset, (limit != -1 ? limit : capacity) - offset(position)); - return clear(); - } - - /** - * Compacts the given bytes. - */ - protected abstract void compact(int from, int to, int length); - - @Override - public Buffer slice() { - int maxCapacity = this.maxCapacity - position; - int capacity = Math.min(Math.min(initialCapacity, maxCapacity), bytes.size() - offset(position)); - if (limit != -1) { - capacity = maxCapacity = limit - position; - } - return new SlicedBuffer(this, bytes, offset(position), capacity, maxCapacity); - } - - @Override - public Buffer slice(int length) { - checkSlice(position, length); - return new SlicedBuffer(this, bytes, offset(position), length, length); - } - - @Override - public Buffer slice(int offset, int length) { - checkSlice(offset, length); - return new SlicedBuffer(this, bytes, offset(offset), length, length); - } - - @Override - public int offset() { - return offset; - } - - @Override - public int capacity() { - return capacity; - } - - /** - * Updates the buffer capacity. - */ - public Buffer capacity(int capacity) { - if (capacity > maxCapacity) { - throw new IllegalArgumentException("capacity cannot be greater than maximum capacity"); - } else if (capacity < this.capacity) { - throw new IllegalArgumentException("capacity cannot be decreased"); - } else if (capacity != this.capacity) { - // It's possible that the bytes could already meet the requirements of the capacity. - if (offset(capacity) > bytes.size()) { - bytes.resize((int) Math.min(Memory.Util.toPow2(offset(capacity)), Integer.MAX_VALUE)); - } - this.capacity = capacity; - } - return this; - } - - @Override - public int maxCapacity() { - return maxCapacity; - } - - @Override - public int position() { - return position; - } - - @Override - public Buffer position(int position) { - if (limit != -1 && position > limit) { - throw new IllegalArgumentException("position cannot be greater than limit"); - } else if (limit == -1 && position > maxCapacity) { - throw new IllegalArgumentException("position cannot be greater than capacity"); - } - if (position > capacity) { - capacity((int) Math.min(maxCapacity, Memory.Util.toPow2(position))); - } - this.position = position; - return this; - } - - /** - * Returns the real offset of the given relative offset. - */ - private int offset(int offset) { - return this.offset + offset; - } - - @Override - public int limit() { - return limit; - } - - @Override - public Buffer limit(int limit) { - if (limit > maxCapacity) { - throw new IllegalArgumentException("limit cannot be greater than buffer capacity"); - } - if (limit < -1) { - throw new IllegalArgumentException("limit cannot be negative"); - } - if (limit != -1 && offset(limit) > bytes.size()) { - bytes.resize(offset(limit)); - } - this.limit = limit; - return this; - } - - @Override - public int remaining() { - return (limit == -1 ? maxCapacity : limit) - position; - } - - @Override - public boolean hasRemaining() { - return remaining() > 0; - } - - @Override - public Buffer flip() { - limit = position; - position = 0; - mark = -1; - return this; - } - - @Override - public Buffer mark() { - this.mark = position; - return this; - } - - @Override - public Buffer rewind() { - position = 0; - mark = -1; - return this; - } - - @Override - public Buffer reset() { - if (mark == -1) { - throw new InvalidMarkException(); - } - position = mark; - return this; - } - - @Override - public Buffer skip(int length) { - if (length > remaining()) { - throw new IndexOutOfBoundsException("length cannot be greater than remaining bytes in the buffer"); - } - position += length; - return this; - } - - @Override - public Buffer clear() { - position = 0; - limit = -1; - mark = -1; - return this; - } - - /** - * Checks that the offset is within the bounds of the buffer. - */ - protected void checkOffset(int offset) { - if (offset(offset) < this.offset) { - throw new IndexOutOfBoundsException(); - } else if (limit == -1) { - if (offset > maxCapacity) { - throw new IndexOutOfBoundsException(); - } - } else { - if (offset > limit) { - throw new IndexOutOfBoundsException(); - } - } - } - - /** - * Checks bounds for a slice. - */ - protected int checkSlice(int offset, int length) { - checkOffset(offset); - if (limit == -1) { - if (offset + length > capacity) { - if (capacity < maxCapacity) { - capacity(calculateCapacity(offset + length)); - } else { - throw new BufferUnderflowException(); - } - } - } else { - if (offset + length > limit) { - throw new BufferUnderflowException(); - } - } - return offset(offset); - } - - /** - * Checks bounds for a read for the given length. - */ - protected int checkRead(int length) { - checkRead(position, length); - int previousPosition = this.position; - this.position = previousPosition + length; - return offset(previousPosition); - } - - /** - * Checks bounds for a read. - */ - protected int checkRead(int offset, int length) { - checkOffset(offset); - if (limit == -1) { - if (offset + length > capacity) { - if (capacity < maxCapacity) { - if (this.offset + offset + length <= bytes.size()) { - capacity = bytes.size() - this.offset; - } else { - capacity(calculateCapacity(offset + length)); - } - } else { - throw new BufferUnderflowException(); - } - } - } else { - if (offset + length > limit) { - throw new BufferUnderflowException(); - } - } - return offset(offset); - } - - /** - * Checks bounds for a write of the given length. - */ - protected int checkWrite(int length) { - checkWrite(position, length); - int previousPosition = this.position; - this.position = previousPosition + length; - return offset(previousPosition); - } - - /** - * Checks bounds for a write. - */ - protected int checkWrite(int offset, int length) { - checkOffset(offset); - if (limit == -1) { - if (offset + length > capacity) { - if (capacity < maxCapacity) { - capacity(calculateCapacity(offset + length)); - } else { - throw new BufferOverflowException(); - } - } - } else { - if (offset + length > limit) { - throw new BufferOverflowException(); - } - } - return offset(offset); - } - - /** - * Calculates the next capacity that meets the given minimum capacity. - */ - private int calculateCapacity(int minimumCapacity) { - int newCapacity = Math.min(Math.max(capacity, 2), minimumCapacity); - while (newCapacity < Math.min(minimumCapacity, maxCapacity)) { - newCapacity <<= 1; - } - return Math.min(newCapacity, maxCapacity); - } - - @Override - public Buffer zero() { - bytes.zero(offset); - return this; - } - - @Override - public Buffer zero(int offset) { - checkOffset(offset); - bytes.zero(offset(offset)); - return this; - } - - @Override - public Buffer zero(int offset, int length) { - checkOffset(offset); - bytes.zero(offset(offset), length); - return this; - } - - @Override - public Buffer read(Buffer buffer) { - int length = Math.min(buffer.remaining(), remaining()); - read(buffer.bytes(), buffer.offset() + buffer.position(), length); - buffer.position(buffer.position() + length); - return this; - } - - @Override - public Buffer read(Bytes bytes) { - this.bytes.read(checkRead(bytes.size()), bytes, 0, bytes.size()); - return this; - } - - @Override - public Buffer read(Bytes bytes, int offset, int length) { - this.bytes.read(checkRead(length), bytes, offset, length); - return this; - } - - @Override - public Buffer read(int srcOffset, Bytes bytes, int dstOffset, int length) { - this.bytes.read(checkRead(srcOffset, length), bytes, dstOffset, length); - return this; - } - - @Override - public Buffer read(byte[] bytes) { - this.bytes.read(checkRead(bytes.length), bytes, 0, bytes.length); - return this; - } - - @Override - public Buffer read(byte[] bytes, int offset, int length) { - this.bytes.read(checkRead(length), bytes, offset, length); - return this; - } - - @Override - public Buffer read(int srcOffset, byte[] bytes, int dstOffset, int length) { - this.bytes.read(checkRead(srcOffset, length), bytes, dstOffset, length); - return this; - } - - @Override - public int readByte() { - return bytes.readByte(checkRead(BYTE)); - } - - @Override - public int readByte(int offset) { - return bytes.readByte(checkRead(offset, BYTE)); - } - - @Override - public int readUnsignedByte() { - return bytes.readUnsignedByte(checkRead(BYTE)); - } - - @Override - public int readUnsignedByte(int offset) { - return bytes.readUnsignedByte(checkRead(offset, BYTE)); - } - - @Override - public char readChar() { - return bytes.readChar(checkRead(Bytes.CHARACTER)); - } - - @Override - public char readChar(int offset) { - return bytes.readChar(checkRead(offset, Bytes.CHARACTER)); - } - - @Override - public short readShort() { - return bytes.readShort(checkRead(SHORT)); - } - - @Override - public short readShort(int offset) { - return bytes.readShort(checkRead(offset, SHORT)); - } - - @Override - public int readUnsignedShort() { - return bytes.readUnsignedShort(checkRead(SHORT)); - } - - @Override - public int readUnsignedShort(int offset) { - return bytes.readUnsignedShort(checkRead(offset, SHORT)); - } - - @Override - public int readMedium() { - return bytes.readMedium(checkRead(3)); - } - - @Override - public int readMedium(int offset) { - return bytes.readMedium(checkRead(offset, 3)); - } - - @Override - public int readUnsignedMedium() { - return bytes.readUnsignedMedium(checkRead(3)); - } - - @Override - public int readUnsignedMedium(int offset) { - return bytes.readUnsignedMedium(checkRead(offset, 3)); - } - - @Override - public int readInt() { - return bytes.readInt(checkRead(Bytes.INTEGER)); - } - - @Override - public int readInt(int offset) { - return bytes.readInt(checkRead(offset, Bytes.INTEGER)); - } - - @Override - public long readUnsignedInt() { - return bytes.readUnsignedInt(checkRead(Bytes.INTEGER)); - } - - @Override - public long readUnsignedInt(int offset) { - return bytes.readUnsignedInt(checkRead(offset, Bytes.INTEGER)); - } - - @Override - public long readLong() { - return bytes.readLong(checkRead(Bytes.LONG)); - } - - @Override - public long readLong(int offset) { - return bytes.readLong(checkRead(offset, Bytes.LONG)); - } - - @Override - public float readFloat() { - return bytes.readFloat(checkRead(Bytes.FLOAT)); - } - - @Override - public float readFloat(int offset) { - return bytes.readFloat(checkRead(offset, Bytes.FLOAT)); - } - - @Override - public double readDouble() { - return bytes.readDouble(checkRead(Bytes.DOUBLE)); - } - - @Override - public double readDouble(int offset) { - return bytes.readDouble(checkRead(offset, Bytes.DOUBLE)); - } - - @Override - public boolean readBoolean() { - return bytes.readBoolean(checkRead(BYTE)); - } - - @Override - public boolean readBoolean(int offset) { - return bytes.readBoolean(checkRead(offset, BYTE)); - } - - @Override - public String readString(Charset charset) { - if (readBoolean(position)) { - byte[] bytes = new byte[readUnsignedShort(position + BOOLEAN)]; - read(position + BOOLEAN + SHORT, bytes, 0, bytes.length); - this.position += BOOLEAN + SHORT + bytes.length; - return new String(bytes, charset); - } else { - this.position += BOOLEAN; - } - return null; - } - - @Override - public String readString(int offset, Charset charset) { - if (readBoolean(offset)) { - byte[] bytes = new byte[readUnsignedShort(offset + BOOLEAN)]; - read(offset + BOOLEAN + SHORT, bytes, 0, bytes.length); - return new String(bytes, charset); - } - return null; - } - - @Override - public String readString() { - return readString(Charset.defaultCharset()); - } - - @Override - public String readString(int offset) { - return readString(offset, Charset.defaultCharset()); - } - - @Override - public String readUTF8() { - return readString(StandardCharsets.UTF_8); - } - - @Override - public String readUTF8(int offset) { - return readString(offset, StandardCharsets.UTF_8); - } - - @Override - public Buffer write(Buffer buffer) { - int length = Math.min(buffer.remaining(), remaining()); - write(buffer.bytes(), buffer.offset() + buffer.position(), length); - buffer.position(buffer.position() + length); - return this; - } - - @Override - public Buffer write(Bytes bytes) { - this.bytes.write(checkWrite(bytes.size()), bytes, 0, bytes.size()); - return this; - } - - @Override - public Buffer write(Bytes bytes, int offset, int length) { - this.bytes.write(checkWrite(length), bytes, offset, length); - return this; - } - - @Override - public Buffer write(int offset, Bytes bytes, int srcOffset, int length) { - this.bytes.write(checkWrite(offset, length), bytes, srcOffset, length); - return this; - } - - @Override - public Buffer write(byte[] bytes) { - this.bytes.write(checkWrite(bytes.length), bytes, 0, bytes.length); - return this; - } - - @Override - public Buffer write(byte[] bytes, int offset, int length) { - this.bytes.write(checkWrite(length), bytes, offset, length); - return this; - } - - @Override - public Buffer write(int offset, byte[] bytes, int srcOffset, int length) { - this.bytes.write(checkWrite(offset, length), bytes, srcOffset, length); - return this; - } - - @Override - public Buffer writeByte(int b) { - bytes.writeByte(checkWrite(BYTE), b); - return this; - } - - @Override - public Buffer writeByte(int offset, int b) { - bytes.writeByte(checkWrite(offset, BYTE), b); - return this; - } - - @Override - public Buffer writeUnsignedByte(int b) { - bytes.writeUnsignedByte(checkWrite(BYTE), b); - return this; - } - - @Override - public Buffer writeUnsignedByte(int offset, int b) { - bytes.writeUnsignedByte(checkWrite(offset, BYTE), b); - return this; - } - - @Override - public Buffer writeChar(char c) { - bytes.writeChar(checkWrite(Bytes.CHARACTER), c); - return this; - } - - @Override - public Buffer writeChar(int offset, char c) { - bytes.writeChar(checkWrite(offset, Bytes.CHARACTER), c); - return this; - } - - @Override - public Buffer writeShort(short s) { - bytes.writeShort(checkWrite(SHORT), s); - return this; - } - - @Override - public Buffer writeShort(int offset, short s) { - bytes.writeShort(checkWrite(offset, SHORT), s); - return this; - } - - @Override - public Buffer writeUnsignedShort(int s) { - bytes.writeUnsignedShort(checkWrite(SHORT), s); - return this; - } - - @Override - public Buffer writeUnsignedShort(int offset, int s) { - bytes.writeUnsignedShort(checkWrite(offset, SHORT), s); - return this; - } - - @Override - public Buffer writeMedium(int m) { - bytes.writeMedium(checkWrite(3), m); - return this; - } - - @Override - public Buffer writeMedium(int offset, int m) { - bytes.writeMedium(checkWrite(offset, 3), m); - return this; - } - - @Override - public Buffer writeUnsignedMedium(int m) { - bytes.writeUnsignedMedium(checkWrite(3), m); - return this; - } - - @Override - public Buffer writeUnsignedMedium(int offset, int m) { - bytes.writeUnsignedMedium(checkWrite(offset, 3), m); - return this; - } - - @Override - public Buffer writeInt(int i) { - bytes.writeInt(checkWrite(Bytes.INTEGER), i); - return this; - } - - @Override - public Buffer writeInt(int offset, int i) { - bytes.writeInt(checkWrite(offset, Bytes.INTEGER), i); - return this; - } - - @Override - public Buffer writeUnsignedInt(long i) { - bytes.writeUnsignedInt(checkWrite(Bytes.INTEGER), i); - return this; - } - - @Override - public Buffer writeUnsignedInt(int offset, long i) { - bytes.writeUnsignedInt(checkWrite(offset, Bytes.INTEGER), i); - return this; - } - - @Override - public Buffer writeLong(long l) { - bytes.writeLong(checkWrite(Bytes.LONG), l); - return this; - } - - @Override - public Buffer writeLong(int offset, long l) { - bytes.writeLong(checkWrite(offset, Bytes.LONG), l); - return this; - } - - @Override - public Buffer writeFloat(float f) { - bytes.writeFloat(checkWrite(Bytes.FLOAT), f); - return this; - } - - @Override - public Buffer writeFloat(int offset, float f) { - bytes.writeFloat(checkWrite(offset, Bytes.FLOAT), f); - return this; - } - - @Override - public Buffer writeDouble(double d) { - bytes.writeDouble(checkWrite(Bytes.DOUBLE), d); - return this; - } - - @Override - public Buffer writeDouble(int offset, double d) { - bytes.writeDouble(checkWrite(offset, Bytes.DOUBLE), d); - return this; - } - - @Override - public Buffer writeBoolean(boolean b) { - bytes.writeBoolean(checkWrite(BYTE), b); - return this; - } - - @Override - public Buffer writeBoolean(int offset, boolean b) { - bytes.writeBoolean(checkWrite(offset, BYTE), b); - return this; - } - - @Override - public Buffer writeString(String s, Charset charset) { - if (s == null) { - return writeBoolean(checkWrite(BOOLEAN), Boolean.FALSE); - } else { - byte[] bytes = s.getBytes(charset); - checkWrite(position, BOOLEAN + SHORT + bytes.length); - writeBoolean(Boolean.TRUE) - .writeUnsignedShort(bytes.length) - .write(bytes, 0, bytes.length); - return this; - } - } - - @Override - public Buffer writeString(int offset, String s, Charset charset) { - if (s == null) { - return writeBoolean(checkWrite(offset, BOOLEAN), Boolean.FALSE); - } else { - byte[] bytes = s.getBytes(charset); - checkWrite(offset, BOOLEAN + SHORT + bytes.length); - writeBoolean(offset, Boolean.TRUE) - .writeUnsignedShort(offset + BOOLEAN, bytes.length) - .write(offset + BOOLEAN + SHORT, bytes, 0, bytes.length); - return this; - } - } - - @Override - public Buffer writeString(String s) { - return writeString(s, Charset.defaultCharset()); - } - - @Override - public Buffer writeString(int offset, String s) { - return writeString(offset, s, Charset.defaultCharset()); - } - - @Override - public Buffer writeUTF8(String s) { - return writeString(s, StandardCharsets.UTF_8); - } - - @Override - public Buffer writeUTF8(int offset, String s) { - return writeString(offset, s, StandardCharsets.UTF_8); - } - - @Override - public Buffer flush() { - bytes.flush(); - return this; - } - - @Override - public void close() { - references.set(0); - if (referenceManager != null) { - referenceManager.release(this); - } else { - bytes.close(); - } - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/AbstractBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/AbstractBytes.java deleted file mode 100644 index c7edbb6d2b..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/AbstractBytes.java +++ /dev/null @@ -1,229 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteOrder; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; - -/** - * Abstract bytes implementation. - *

- * This class provides common state and bounds checking functionality for all {@link Bytes} implementations. - * - * @author Jordan Halterman - */ -public abstract class AbstractBytes implements Bytes { - static final int MAX_SIZE = Integer.MAX_VALUE - 5; - - private boolean open = true; - private SwappedBytes swap; - - /** - * Checks whether the block is open. - */ - protected void checkOpen() { - if (!open) { - throw new IllegalStateException("bytes not open"); - } - } - - /** - * Checks that the offset is within the bounds of the buffer. - */ - protected void checkOffset(int offset) { - checkOpen(); - if (offset < 0 || offset > size()) { - throw new IndexOutOfBoundsException(); - } - } - - /** - * Checks bounds for a read. - */ - protected int checkRead(int offset, int length) { - checkOffset(offset); - int position = offset + length; - if (position > size()) { - throw new BufferUnderflowException(); - } - return position; - } - - /** - * Checks bounds for a write. - */ - protected int checkWrite(int offset, int length) { - checkOffset(offset); - int position = offset + length; - if (position > size()) { - throw new BufferOverflowException(); - } - return position; - } - - @Override - public boolean isDirect() { - return false; - } - - @Override - public boolean isFile() { - return false; - } - - @Override - public ByteOrder order() { - return ByteOrder.BIG_ENDIAN; - } - - @Override - public Bytes order(ByteOrder order) { - if (order == null) { - throw new NullPointerException("order cannot be null"); - } - if (order == order()) { - return this; - } - if (swap != null) { - return swap; - } - swap = new SwappedBytes(this); - return swap; - } - - @Override - public boolean readBoolean(int offset) { - return readByte(offset) == 1; - } - - @Override - public int readUnsignedByte(int offset) { - return readByte(offset) & 0xFF; - } - - @Override - public int readUnsignedShort(int offset) { - return readShort(offset) & 0xFFFF; - } - - @Override - public int readMedium(int offset) { - return (readByte(offset)) << 16 - | (readByte(offset + 1) & 0xff) << 8 - | (readByte(offset + 2) & 0xff); - } - - @Override - public int readUnsignedMedium(int offset) { - return (readByte(offset) & 0xff) << 16 - | (readByte(offset + 1) & 0xff) << 8 - | (readByte(offset + 2) & 0xff); - } - - @Override - public long readUnsignedInt(int offset) { - return readInt(offset) & 0xFFFFFFFFL; - } - - @Override - public String readString(int offset) { - return readString(offset, Charset.defaultCharset()); - } - - @Override - public String readString(int offset, Charset charset) { - if (readBoolean(offset)) { - byte[] bytes = new byte[readUnsignedShort(offset + BYTE)]; - read(offset + BYTE + SHORT, bytes, 0, bytes.length); - return new String(bytes, charset); - } - return null; - } - - @Override - public String readUTF8(int offset) { - return readString(offset, StandardCharsets.UTF_8); - } - - @Override - public Bytes writeBoolean(int offset, boolean b) { - return writeByte(offset, b ? 1 : 0); - } - - @Override - public Bytes writeUnsignedByte(int offset, int b) { - return writeByte(offset, (byte) b); - } - - @Override - public Bytes writeUnsignedShort(int offset, int s) { - return writeShort(offset, (short) s); - } - - @Override - public Bytes writeMedium(int offset, int m) { - writeByte(offset, (byte) (m >>> 16)); - writeByte(offset + 1, (byte) (m >>> 8)); - writeByte(offset + 2, (byte) m); - return this; - } - - @Override - public Bytes writeUnsignedMedium(int offset, int m) { - return writeMedium(offset, m); - } - - @Override - public Bytes writeUnsignedInt(int offset, long i) { - return writeInt(offset, (int) i); - } - - @Override - public Bytes writeString(int offset, String s) { - return writeString(offset, s, Charset.defaultCharset()); - } - - @Override - public Bytes writeString(int offset, String s, Charset charset) { - if (s == null) { - return writeBoolean(offset, Boolean.FALSE); - } else { - writeBoolean(offset, Boolean.TRUE); - byte[] bytes = s.getBytes(charset); - return writeUnsignedShort(offset + BYTE, bytes.length) - .write(offset + BYTE + SHORT, bytes, 0, bytes.length); - } - } - - @Override - public Bytes writeUTF8(int offset, String s) { - return writeString(offset, s, StandardCharsets.UTF_8); - } - - @Override - public Bytes flush() { - return this; - } - - @Override - public void close() { - open = false; - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/Buffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/Buffer.java deleted file mode 100644 index fe29597d89..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/Buffer.java +++ /dev/null @@ -1,1387 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferenceCounted; - -import java.nio.ByteOrder; - -/** - * Navigable byte buffer for input/output operations. - *

- * The byte buffer provides a fluent interface for reading bytes from and writing bytes to some underlying storage - * implementation. The {@code Buffer} type is agnostic about the specific underlying storage implementation, but different - * buffer implementations may be designed for specific storage layers. - *

- * Aside from the underlying storage implementation, this buffer works very similarly to Java's {@link java.nio.ByteBuffer}. - * It intentionally exposes methods that can be easily understood by any developer with experience with {@code ByteBuffer}. - *

- * In order to support reading and writing from buffers, {@code Buffer} implementations maintain a series of pointers to - * aid in navigating the buffer. - *

- * Most notable of these pointers is the {@code position}. When values are written to or read from the buffer, the - * buffer increments its internal {@code position} according to the number of bytes read. This allows users to iterate - * through the bytes in the buffer without maintaining external pointers. - *

- *

- *   {@code
- *      try (Buffer buffer = DirectBuffer.allocate(1024)) {
- *        buffer.writeInt(1);
- *        buffer.flip();
- *        assert buffer.readInt() == 1;
- *      }
- *   }
- * 
- *

- * Buffers implement {@link ReferenceCounted} in order to keep track of the number of currently - * held references to a given buffer. When a buffer is constructed, the buffer contains only {@code 1} reference. This - * reference can be released via the {@link Buffer#close()} method which can be called automatically - * by using a try-with-resources statement demonstrated above. Additional references to the buffer should be acquired via - * {@link ReferenceCounted#acquire()} and released via - * {@link ReferenceCounted#release}. Once all references to a buffer have been released - including - * the initial reference - the memory will be freed (for in-memory buffers) and writes will be automatically flushed to - * disk (for persistent buffers). - * - * @author Jordan Halterman - */ -public interface Buffer extends BytesInput, BufferInput, BytesOutput, BufferOutput, ReferenceCounted { - - /** - * Returns whether the buffer has an array. - * - * @return Whether the buffer has an underlying array. - */ - default boolean hasArray() { - return false; - } - - /** - * Returns the underlying byte array. - * - * @return the underlying byte array - * @throws UnsupportedOperationException if a heap array is not supported - */ - default byte[] array() { - throw new UnsupportedOperationException(); - } - - /** - * Returns the byte order. - *

- * For consistency with {@link java.nio.ByteBuffer}, all buffer implementations are initially in {@link ByteOrder#BIG_ENDIAN} order. - * - * @return The byte order. - */ - ByteOrder order(); - - /** - * Sets the byte order, returning a new swapped {@link Buffer} instance. - *

- * By default, all buffers are read and written in {@link ByteOrder#BIG_ENDIAN} order. This provides complete - * consistency with {@link java.nio.ByteBuffer}. To flip buffers to {@link ByteOrder#LITTLE_ENDIAN} order, this - * buffer's {@code Bytes} instance is decorated by a {@link SwappedBytes} instance which will reverse - * read and written bytes using, e.g. {@link Integer#reverseBytes(int)}. - * - * @param order The byte order. - * @return The updated buffer. - */ - Buffer order(ByteOrder order); - - /** - * Returns a boolean value indicating whether the buffer is a direct buffer. - * - * @return Indicates whether the buffer is a direct buffer. - */ - boolean isDirect(); - - /** - * Returns a boolean value indicating whether the buffer is a read-only buffer. - * - * @return Indicates whether the buffer is a read-only buffer. - */ - boolean isReadOnly(); - - /** - * Returns a boolean value indicating whether the buffer is backed by a file. - * - * @return Indicates whether the buffer is backed by a file. - */ - boolean isFile(); - - /** - * Returns a read-only view of the buffer. - *

- * The returned buffer will share the underlying {@link Bytes} with which buffer, but the buffer's {@code limit}, - * {@code capacity}, and {@code position} will be independent of this buffer. - * - * @return A read-only buffer. - */ - Buffer asReadOnlyBuffer(); - - /** - * Returns the buffer's starting offset within the underlying {@link Bytes}. - *

- * The offset is used to calculate the absolute position of the buffer's relative {@link Buffer#position() position} - * within the underlying {@link Bytes}. - * - * @return The buffer's offset. - */ - int offset(); - - /** - * Returns the buffer's capacity. - *

- * The capacity represents the total amount of storage space allocated to the buffer by the underlying storage - * implementation. As bytes are written to the buffer, the buffer's capacity may grow up to {@link Buffer#maxCapacity()}. - * - * @return The buffer's capacity. - */ - int capacity(); - - /** - * Sets the buffer's capacity. - *

- * The given capacity must be greater than the current {@link Buffer#capacity() capacity} and less than or equal to - * {@link Buffer#maxCapacity()}. When the capacity is changed, the underlying {@link Bytes} will be resized via - * {@link Bytes#resize(int)}. - * - * @param capacity The capacity to which to resize the buffer. - * @return The resized buffer. - * @throws IllegalArgumentException If the given {@code capacity} is less than the current {@code capacity} - * or greater than {@link Buffer#maxCapacity()} - */ - Buffer capacity(int capacity); - - /** - * Returns the maximum allowed capacity for the buffer. - *

- * The maximum capacity is the limit up to which this buffer's {@link Buffer#capacity() capacity} can be expanded. - * While the capacity grows, the maximum capacity is fixed from the moment the buffer is created and cannot change. - * - * @return The buffer's maximum capacity. - */ - int maxCapacity(); - - /** - * Sets the buffer's current read/write position. - *

- * The position is an internal cursor that tracks where to write/read bytes in the underlying storage implementation. - * - * @param position The position to set. - * @return This buffer. - * @throws IllegalArgumentException If the given position is less than {@code 0} or more than {@link Buffer#limit()} - */ - Buffer position(int position); - - /** - * Returns the buffer's read/write limit. - *

- * The limit dictates the highest position to which bytes can be read from or written to the buffer. If the limit is - * not explicitly set then it will always equal the buffer's {@link Buffer#maxCapacity() maxCapacity}. Note that the - * limit may be set by related methods such as {@link Buffer#flip()} - * - * @return The buffer's limit. - */ - int limit(); - - /** - * Sets the buffer's read/write limit. - *

- * The limit dictates the highest position to which bytes can be read from or written to the buffer. The limit must - * be within the bounds of the buffer, i.e. greater than {@code 0} and less than or equal to {@link Buffer#capacity()} - * - * @param limit The limit to set. - * @return This buffer. - * @throws IllegalArgumentException If the given limit is less than {@code 0} or more than {@link Buffer#capacity()} - */ - Buffer limit(int limit); - - /** - * Returns the number of bytes remaining in the buffer until the {@link Buffer#limit()} is reached. - *

- * The bytes remaining is calculated by {@code buffer.limit() - buffer.position()}. If no limit is set on the buffer - * then the {@link Buffer#maxCapacity() maxCapacity} will be used. - * - * @return The number of bytes remaining in the buffer. - */ - @Override - int remaining(); - - /** - * Returns a boolean indicating whether the buffer has bytes remaining. - *

- * If {@link Buffer#remaining()} is greater than {@code 0} then this method will return {@code true}, otherwise - * {@code false} - * - * @return Indicates whether bytes are remaining in the buffer. {@code true} if {@link Buffer#remaining()} is - * greater than {@code 0}, {@code false} otherwise. - */ - @Override - boolean hasRemaining(); - - /** - * Flips the buffer. - *

- * The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. - *

-   *   {@code
-   *   assert buffer.writeLong(1234).flip().readLong() == 1234;
-   *   }
-   * 
- * - * @return This buffer. - */ - Buffer flip(); - - /** - * Sets a mark at the current position. - *

- * The mark is a simple internal reference to the buffer's current position. Marks can be used to reset the buffer - * to a specific position after some operation. - *

- *

-   *   {@code
-   *   buffer.mark();
-   *   buffer.writeInt(1).writeBoolean(true);
-   *   buffer.reset();
-   *   assert buffer.readInt() == 1;
-   *   }
-   * 
- * - * @return This buffer. - */ - Buffer mark(); - - /** - * Resets the buffer's position to the previously-marked position. - *

- * Invoking this method neither changes nor discards the mark's value. - * - * @return This buffer. - * @throws java.nio.InvalidMarkException If no mark is set. - */ - Buffer reset(); - - /** - * Rewinds the buffer. The position is set to zero and the mark is discarded. - * - * @return This buffer. - */ - Buffer rewind(); - - /** - * Advances the buffer's {@code position} {@code length} bytes. - * - * @param length The number of bytes to advance this buffer's {@code position}. - * @return This buffer. - * @throws IndexOutOfBoundsException If {@code length} is greater than {@link Buffer#remaining()} - */ - @Override - Buffer skip(int length); - - /** - * Clears the buffer. - *

- * The position is set to zero, the limit is set to the capacity, and the mark is discarded. - * - * @return This buffer. - */ - Buffer clear(); - - /** - * Compacts the buffer, moving bytes from the current position to the end of the buffer to the head of the buffer. - * - * @return This buffer. - */ - Buffer compact(); - - /** - * Returns a duplicate of the buffer. - * - * @return A duplicate buffer. - */ - Buffer duplicate(); - - /** - * Returns the bytes underlying the buffer. - *

- * The buffer is a wrapper around {@link Bytes} that handles writing sequences of bytes by tracking positions and - * limits. This method returns the {@link Bytes} that this buffer wraps. - * - * @return The underlying bytes. - */ - Bytes bytes(); - - /** - * Returns a view of this buffer starting at the current position. - *

- * The returned buffer will contain the same underlying {@link Bytes} instance as this buffer, but its pointers will - * be offset by the current {@code position} of this buffer at the time the slice is created. Calls to - * {@link Buffer#rewind()} and similar methods on the resulting {@link Buffer} will result in - * the buffer's {@code position} being reset to the {@code position} of this buffer at the time the slice was created. - *

- * The returned buffer is reference counted separately from this buffer. Therefore, closing the returned buffer will - * release the buffer back to the internal buffer poll and will not result in this buffer being closed. Users should - * always call {@link Buffer#close()} once finished using the buffer slice. - * - * @return A slice of this buffer. - * @see Buffer#slice(int) - * @see Buffer#slice(int, int) - */ - Buffer slice(); - - /** - * Returns a view of this buffer of the given length starting at the current position. - *

- * The returned buffer will contain the same underlying {@link Bytes} instance as this buffer, but its pointers will - * be offset by the current {@code position} of this buffer at the time the slice is created. Calls to - * {@link Buffer#rewind()} and similar methods on the resulting {@link Buffer} will result in - * the buffer's {@code position} being reset to the {@code position} of this buffer at the time the slice was created. - *

- * The returned buffer is reference counted separately from this buffer. Therefore, closing the returned buffer will - * release the buffer back to the internal buffer poll and will not result in this buffer being closed. Users should - * always call {@link Buffer#close()} once finished using the buffer slice. - * - * @param length The length of the slice. - * @return A slice of this buffer. - * @see Buffer#slice() - * @see Buffer#slice(int, int) - */ - Buffer slice(int length); - - /** - * Returns a view of this buffer starting at the given offset with the given length. - *

- * The returned buffer will contain the same underlying {@link Bytes} instance as this buffer, but its pointers will - * be offset by the given {@code offset} and its length limited by the given {@code length}. Calls to - * {@link Buffer#rewind()} and similar methods on the resulting {@link Buffer} will result in - * the buffer's {@code position} being reset to the given {@code offset}. - *

- * The returned buffer is reference counted separately from this buffer. Therefore, closing the returned buffer will - * release the buffer back to the internal buffer poll and will not result in this buffer being closed. Users should - * always call {@link Buffer#close()} once finished using the buffer slice. - * - * @param offset The offset at which to begin the slice. - * @param length The number of bytes in the slice. - * @return The buffer slice. - * @throws IndexOutOfBoundsException If the given offset is not contained within the bounds of this buffer - * @throws java.nio.BufferUnderflowException If the length of the remaining bytes in the buffer is less than {@code length} - * @see Buffer#slice() - * @see Buffer#slice(int) - */ - Buffer slice(int offset, int length); - - /** - * Reads bytes into the given buffer. - *

- * Bytes will be read starting at the current buffer position until either {@link Buffer#limit()} has been reached. - * If {@link Buffer#remaining()} is less than the {@link Buffer#remaining()} of the given buffer, a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @param buffer The buffer into which to read bytes. - * @return The buffer. - * @throws java.nio.BufferUnderflowException If the given {@link Buffer#remaining()} is greater than this buffer's - * {@link Buffer#remaining()} - */ - @Override - Buffer read(Buffer buffer); - - /** - * Reads bytes into the given byte array. - *

- * Bytes will be read starting at the current buffer position until either the byte array {@code length} or the - * {@link Buffer#limit()} has been reached. If {@link Buffer#remaining()} - * is less than the {@code length} of the given byte array, a {@link java.nio.BufferUnderflowException} will be - * thrown. - * - * @param bytes The byte array into which to read bytes. - * @return The buffer. - * @throws java.nio.BufferUnderflowException If the given byte array's {@code length} is greater than - * {@link Buffer#remaining()} - * @see Buffer#read(Bytes, int, int) - * @see Buffer#read(int, Bytes, int, int) - */ - @Override - Buffer read(Bytes bytes); - - /** - * Reads bytes into the given byte array. - *

- * Bytes will be read starting at the current buffer position until either the byte array {@code length} or the - * {@link Buffer#limit()} has been reached. If {@link Buffer#remaining()} - * is less than the {@code length} of the given byte array, a {@link java.nio.BufferUnderflowException} will be - * thrown. - * - * @param bytes The byte array into which to read bytes. - * @return The buffer. - * @throws java.nio.BufferUnderflowException If the given byte array's {@code length} is greater than - * {@link Buffer#remaining()} - * @see Buffer#read(byte[], int, int) - * @see Buffer#read(int, byte[], int, int) - */ - @Override - Buffer read(byte[] bytes); - - /** - * Reads bytes into the given byte array starting at the current position. - *

- * Bytes will be read from the current position up to the given length. If the provided {@code length} is - * greater than {@link Buffer#remaining()} then a {@link java.nio.BufferUnderflowException} will - * be thrown. If the {@code offset} is out of bounds of the buffer then an {@link IndexOutOfBoundsException} - * will be thrown. - * - * @param bytes The byte array into which to read bytes. - * @param dstOffset The offset at which to write bytes into the given buffer - * @return The buffer. - * @throws java.nio.BufferUnderflowException If {@code length} is greater than {@link Buffer#remaining()} - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#read(Bytes) - * @see Buffer#read(int, Bytes, int, int) - */ - @Override - Buffer read(Bytes bytes, int dstOffset, int length); - - /** - * Reads bytes into the given byte array starting at the given offset up to the given length. - *

- * Bytes will be read from the given starting offset up to the given length. If the provided {@code length} is - * greater than {@link Buffer#limit() - srcOffset} then a {@link java.nio.BufferUnderflowException} will - * be thrown. If the {@code srcOffset} is out of bounds of the buffer then an {@link IndexOutOfBoundsException} - * will be thrown. - * - * @param srcOffset The offset from which to start reading bytes. - * @param bytes The byte array into which to read bytes. - * @param dstOffset The offset at which to write bytes into the given buffer - * @return The buffer. - * @throws java.nio.BufferUnderflowException If {@code length} is greater than {@link Buffer#remaining()} - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#read(Bytes) - * @see Buffer#read(Bytes, int, int) - */ - @Override - Buffer read(int srcOffset, Bytes bytes, int dstOffset, int length); - - /** - * Reads bytes into the given byte array starting at current position up to the given length. - *

- * Bytes will be read from the current position up to the given length. If the provided {@code length} is - * greater than {@link Buffer#remaining()} then a {@link java.nio.BufferUnderflowException} will - * be thrown. If the {@code offset} is out of bounds of the buffer then an {@link IndexOutOfBoundsException} - * will be thrown. - * - * @param bytes The byte array into which to read bytes. - * @param offset The offset at which to write bytes into the given buffer - * @return The buffer. - * @throws java.nio.BufferUnderflowException If {@code length} is greater than {@link Buffer#remaining()} - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#read(byte[]) - * @see Buffer#read(int, byte[], int, int) - */ - @Override - Buffer read(byte[] bytes, int offset, int length); - - /** - * Reads bytes into the given byte array starting at the given offset up to the given length. - *

- * Bytes will be read from the given starting offset up to the given length. If the provided {@code length} is - * greater than {@link Buffer#remaining()} then a {@link java.nio.BufferUnderflowException} will - * be thrown. If the {@code offset} is out of bounds of the buffer then an {@link IndexOutOfBoundsException} - * will be thrown. - * - * @param srcOffset The offset from which to start reading bytes. - * @param bytes The byte array into which to read bytes. - * @param dstOffset The offset at which to write bytes into the given buffer - * @return The buffer. - * @throws java.nio.BufferUnderflowException If {@code length} is greater than {@link Buffer#remaining()} - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#read(byte[]) - * @see Buffer#read(byte[], int, int) - */ - @Override - Buffer read(int srcOffset, byte[] bytes, int dstOffset, int length); - - /** - * Reads a byte from the buffer at the current position. - *

- * When the byte is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#BYTE}. If - * there are no bytes remaining in the buffer then a {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read byte. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#BYTE} - * @see Buffer#readByte(int) - */ - @Override - int readByte(); - - /** - * Reads a byte from the buffer at the given offset. - *

- * The byte will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the byte. - * @return The read byte. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readByte() - */ - @Override - int readByte(int offset); - - /** - * Reads an unsigned byte from the buffer at the current position. - *

- * When the byte is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#BYTE}. If - * there are no bytes remaining in the buffer then a {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read byte. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#BYTE} - * @see Buffer#readUnsignedByte(int) - */ - @Override - int readUnsignedByte(); - - /** - * Reads an unsigned byte from the buffer at the given offset. - *

- * The byte will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the byte. - * @return The read byte. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readUnsignedByte() - */ - @Override - int readUnsignedByte(int offset); - - /** - * Reads a 16-bit character from the buffer at the current position. - *

- * When the character is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#CHARACTER}. - * If there are less than {@link Bytes#CHARACTER} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read character. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#CHARACTER} - * @see Buffer#readChar(int) - */ - @Override - char readChar(); - - /** - * Reads a 16-bit character from the buffer at the given offset. - *

- * The character will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the character. - * @return The read character. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readChar() - */ - @Override - char readChar(int offset); - - /** - * Reads a 16-bit signed integer from the buffer at the current position. - *

- * When the short is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#SHORT}. - * If there are less than {@link Bytes#SHORT} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read short. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#SHORT} - * @see Buffer#readShort(int) - */ - @Override - short readShort(); - - /** - * Reads a 16-bit signed integer from the buffer at the given offset. - *

- * The short will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the short. - * @return The read short. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readShort() - */ - @Override - short readShort(int offset); - - /** - * Reads a 16-bit unsigned integer from the buffer at the current position. - *

- * When the short is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#SHORT}. - * If there are less than {@link Bytes#SHORT} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read short. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#SHORT} - * @see Buffer#readUnsignedShort(int) - */ - @Override - int readUnsignedShort(); - - /** - * Reads a 16-bit unsigned integer from the buffer at the given offset. - *

- * The short will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the short. - * @return The read short. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readUnsignedShort() - */ - @Override - int readUnsignedShort(int offset); - - /** - * Reads a 32-bit signed integer from the buffer at the current position. - *

- * When the integer is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#INTEGER}. - * If there are less than {@link Bytes#INTEGER} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read integer. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#INTEGER} - * @see Buffer#readInt(int) - */ - @Override - int readInt(); - - /** - * Reads a 32-bit signed integer from the buffer at the given offset. - *

- * The integer will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the integer. - * @return The read integer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readInt() - */ - @Override - int readInt(int offset); - - /** - * Reads a 32-bit unsigned integer from the buffer at the current position. - *

- * When the integer is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#INTEGER}. - * If there are less than {@link Bytes#INTEGER} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read integer. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#INTEGER} - * @see Buffer#readUnsignedInt(int) - */ - @Override - long readUnsignedInt(); - - /** - * Reads a 32-bit unsigned integer from the buffer at the given offset. - *

- * The integer will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the integer. - * @return The read integer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readUnsignedInt() - */ - @Override - long readUnsignedInt(int offset); - - /** - * Reads a 64-bit signed integer from the buffer at the current position. - *

- * When the long is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#LONG}. - * If there are less than {@link Bytes#LONG} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read long. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#LONG} - * @see Buffer#readLong(int) - */ - @Override - long readLong(); - - /** - * Reads a 64-bit signed integer from the buffer at the given offset. - *

- * The long will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the long. - * @return The read long. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readLong() - */ - @Override - long readLong(int offset); - - /** - * Reads a single-precision 32-bit floating point number from the buffer at the current position. - *

- * When the float is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#FLOAT}. - * If there are less than {@link Bytes#FLOAT} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read float. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#FLOAT} - * @see Buffer#readFloat(int) - */ - @Override - float readFloat(); - - /** - * Reads a single-precision 32-bit floating point number from the buffer at the given offset. - *

- * The float will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the float. - * @return The read float. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readFloat() - */ - @Override - float readFloat(int offset); - - /** - * Reads a double-precision 64-bit floating point number from the buffer at the current position. - *

- * When the double is read from the buffer, the buffer's {@code position} will be advanced by {@link Bytes#DOUBLE}. - * If there are less than {@link Bytes#DOUBLE} bytes remaining in the buffer then a - * {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read double. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@link Bytes#DOUBLE} - * @see Buffer#readDouble(int) - */ - @Override - double readDouble(); - - /** - * Reads a double-precision 64-bit floating point number from the buffer at the given offset. - *

- * The double will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the double. - * @return The read double. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readDouble() - */ - @Override - double readDouble(int offset); - - /** - * Reads a 1 byte boolean from the buffer at the current position. - *

- * When the boolean is read from the buffer, the buffer's {@code position} will be advanced by {@code 1}. - * If there are no bytes remaining in the buffer then a {@link java.nio.BufferUnderflowException} will be thrown. - * - * @return The read boolean. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@code 1} - * @see Buffer#readBoolean(int) - */ - @Override - boolean readBoolean(); - - /** - * Reads a 1 byte boolean from the buffer at the given offset. - *

- * The boolean will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the boolean. - * @return The read boolean. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readBoolean() - */ - @Override - boolean readBoolean(int offset); - - /** - * Reads a UTF-8 string from the buffer at the current position. - *

- * When the string is read from the buffer, the buffer's {@code position} will be advanced by 2 bytes plus the byte - * length of the string. If there are no bytes remaining in the buffer then a {@link java.nio.BufferUnderflowException} - * will be thrown. - * - * @return The read string. - * @throws java.nio.BufferUnderflowException If {@link Buffer#remaining()} is less than {@code 1} - * @see Buffer#readUTF8(int) - */ - @Override - String readUTF8(); - - /** - * Reads a UTF-8 string from the buffer at the given offset. - *

- * The string will be read from the given offset. If the given index is out of the bounds of the buffer then a - * {@link IndexOutOfBoundsException} will be thrown. - * - * @param offset The offset at which to read the boolean. - * @return The read string. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#readUTF8() - */ - @Override - String readUTF8(int offset); - - /** - * Writes a buffer to the buffer. - *

- * When the buffer is written to the buffer, the buffer's {@code position} will be advanced by the number of bytes - * in the provided buffer. If the provided {@link Buffer#remaining()} exceeds {@link Buffer#remaining()} then an - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param buffer The buffer to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If the given buffer's {@link Buffer#remaining()} bytes exceeds this buffer's - * remaining bytes. - */ - @Override - Buffer write(Buffer buffer); - - /** - * Writes an array of bytes to the buffer. - *

- * When the bytes are written to the buffer, the buffer's {@code position} will be advanced by the number of bytes - * in the provided byte array. If the number of bytes exceeds {@link Buffer#limit()} then an - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param bytes The array of bytes to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If the number of bytes exceeds the buffer's remaining bytes. - * @see Buffer#write(Bytes, int, int) - * @see Buffer#write(int, Bytes, int, int) - */ - @Override - Buffer write(Bytes bytes); - - /** - * Writes an array of bytes to the buffer. - *

- * When the bytes are written to the buffer, the buffer's {@code position} will be advanced by the number of bytes - * in the provided byte array. If the number of bytes exceeds {@link Buffer#limit()} then an - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param bytes The array of bytes to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If the number of bytes exceeds the buffer's remaining bytes. - * @see Buffer#write(byte[], int, int) - * @see Buffer#write(int, byte[], int, int) - */ - @Override - Buffer write(byte[] bytes); - - /** - * Writes an array of bytes to the buffer. - *

- * The bytes will be written starting at the current position up to the given length. If the length of the byte array - * is larger than the provided {@code length} then only {@code length} bytes will be read from the array. If the - * provided {@code length} is greater than the remaining bytes in this buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param bytes The array of bytes to write. - * @param offset The offset at which to start writing the bytes. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are not enough bytes remaining in the buffer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. - * @see Buffer#write(Bytes) - * @see Buffer#write(int, Bytes, int, int) - */ - @Override - Buffer write(Bytes bytes, int offset, int length); - - /** - * Writes an array of bytes to the buffer. - *

- * The bytes will be written starting at the given offset up to the given length. If the remaining bytes in the byte array - * is larger than the provided {@code length} then only {@code length} bytes will be read from the array. If the - * provided {@code length} is greater than {@link Buffer#limit()} minus {@code offset} then a - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to start writing the bytes. - * @param src The array of bytes to write. - * @param srcOffset The offset at which to begin reading bytes from the source. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are not enough bytes remaining in the buffer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. - * @see Buffer#write(Bytes) - * @see Buffer#write(Bytes, int, int) - */ - @Override - Buffer write(int offset, Bytes src, int srcOffset, int length); - - /** - * Writes an array of bytes to the buffer. - *

- * The bytes will be written starting at the current position up to the given length. If the length of the byte array - * is larger than the provided {@code length} then only {@code length} bytes will be read from the array. If the - * provided {@code length} is greater than the remaining bytes in this buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param bytes The array of bytes to write. - * @param offset The offset at which to start writing the bytes. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are not enough bytes remaining in the buffer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. - * @see Buffer#write(byte[]) - * @see Buffer#write(int, byte[], int, int) - */ - @Override - Buffer write(byte[] bytes, int offset, int length); - - /** - * Writes an array of bytes to the buffer. - *

- * The bytes will be written starting at the given offset up to the given length. If the remaining bytes in the byte array - * is larger than the provided {@code length} then only {@code length} bytes will be read from the array. If the - * provided {@code length} is greater than {@link Buffer#limit()} minus {@code offset} then a - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to start writing the bytes. - * @param src The array of bytes to write. - * @param srcOffset The offset at which to begin reading bytes from the source. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are not enough bytes remaining in the buffer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. - * @see Buffer#write(byte[]) - * @see Buffer#write(byte[], int, int) - */ - @Override - Buffer write(int offset, byte[] src, int srcOffset, int length); - - /** - * Writes a byte to the buffer at the current position. - *

- * When the byte is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#BYTE}. If - * there are no bytes remaining in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param b The byte to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are no bytes remaining in the buffer. - * @see Buffer#writeByte(int, int) - */ - @Override - Buffer writeByte(int b); - - /** - * Writes a byte to the buffer at the given offset. - *

- * The byte will be written at the given offset. If there are no bytes remaining in the buffer then a - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the byte. - * @param b The byte to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are not enough bytes remaining in the buffer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeByte(int) - */ - @Override - Buffer writeByte(int offset, int b); - - /** - * Writes an unsigned byte to the buffer at the current position. - *

- * When the byte is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#BYTE}. If - * there are no bytes remaining in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param b The byte to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are no bytes remaining in the buffer. - * @see Buffer#writeUnsignedByte(int, int) - */ - @Override - Buffer writeUnsignedByte(int b); - - /** - * Writes an unsigned byte to the buffer at the given offset. - *

- * The byte will be written at the given offset. If there are no bytes remaining in the buffer then a - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the byte. - * @param b The byte to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If there are not enough bytes remaining in the buffer. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeUnsignedByte(int) - */ - @Override - Buffer writeUnsignedByte(int offset, int b); - - /** - * Writes a 16-bit character to the buffer at the current position. - *

- * When the character is written to the buffer, the buffer's {@code position} will be advanced by - * {@link Bytes#CHARACTER}. If less than {@code 2} bytes are remaining in the buffer then a - * {@link java.nio.BufferOverflowException} will be thrown. - * - * @param c The character to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#CHARACTER}. - * @see Buffer#writeChar(int, char) - */ - @Override - Buffer writeChar(char c); - - /** - * Writes a 16-bit character to the buffer at the given offset. - *

- * The character will be written at the given offset. If there are less than {@link Bytes#CHARACTER} bytes remaining - * in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the character. - * @param c The character to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#CHARACTER}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeChar(char) - */ - @Override - Buffer writeChar(int offset, char c); - - /** - * Writes a 16-bit signed integer to the buffer at the current position. - *

- * When the short is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#SHORT}. If - * less than {@link Bytes#SHORT} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param s The short to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#SHORT}. - * @see Buffer#writeShort(int, short) - */ - @Override - Buffer writeShort(short s); - - /** - * Writes a 16-bit signed integer to the buffer at the given offset. - *

- * The short will be written at the given offset. If there are less than {@link Bytes#SHORT} bytes remaining in the buffer - * then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the short. - * @param s The short to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#SHORT}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeShort(short) - */ - @Override - Buffer writeShort(int offset, short s); - - /** - * Writes a 16-bit signed integer to the buffer at the current position. - *

- * When the short is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#SHORT}. If - * less than {@link Bytes#SHORT} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param s The short to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#SHORT}. - * @see Buffer#writeUnsignedShort(int, int) - */ - @Override - Buffer writeUnsignedShort(int s); - - /** - * Writes a 16-bit signed integer to the buffer at the given offset. - *

- * The short will be written at the given offset. If there are less than {@link Bytes#SHORT} bytes remaining in the buffer - * then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the short. - * @param s The short to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#SHORT}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeUnsignedShort(int) - */ - @Override - Buffer writeUnsignedShort(int offset, int s); - - /** - * Writes a 32-bit signed integer to the buffer at the current position. - *

- * When the integer is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#INTEGER}. - * If less than {@link Bytes#INTEGER} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param i The integer to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#INTEGER}. - * @see Buffer#writeInt(int, int) - */ - @Override - Buffer writeInt(int i); - - /** - * Writes a 32-bit signed integer to the buffer at the given offset. - *

- * The integer will be written at the given offset. If there are less than {@link Bytes#INTEGER} bytes remaining - * in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the integer. - * @param i The integer to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#INTEGER}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeInt(int) - */ - @Override - Buffer writeInt(int offset, int i); - - /** - * Writes a 32-bit signed integer to the buffer at the current position. - *

- * When the integer is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#INTEGER}. - * If less than {@link Bytes#INTEGER} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param i The integer to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#INTEGER}. - * @see Buffer#writeUnsignedInt(int, long) - */ - @Override - Buffer writeUnsignedInt(long i); - - /** - * Writes a 32-bit signed integer to the buffer at the given offset. - *

- * The integer will be written at the given offset. If there are less than {@link Bytes#INTEGER} bytes remaining - * in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the integer. - * @param i The integer to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#INTEGER}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeUnsignedInt(long) - */ - @Override - Buffer writeUnsignedInt(int offset, long i); - - /** - * Writes a 64-bit signed integer to the buffer at the current position. - *

- * When the long is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#LONG}. - * If less than {@link Bytes#LONG} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param l The long to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#LONG}. - * @see Buffer#writeLong(int, long) - */ - @Override - Buffer writeLong(long l); - - /** - * Writes a 64-bit signed integer to the buffer at the given offset. - *

- * The long will be written at the given offset. If there are less than {@link Bytes#LONG} bytes remaining - * in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the long. - * @param l The long to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#LONG}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeLong(long) - */ - @Override - Buffer writeLong(int offset, long l); - - /** - * Writes a single-precision 32-bit floating point number to the buffer at the current position. - *

- * When the float is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#FLOAT}. - * If less than {@link Bytes#FLOAT} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param f The float to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#FLOAT}. - * @see Buffer#writeFloat(int, float) - */ - @Override - Buffer writeFloat(float f); - - /** - * Writes a single-precision 32-bit floating point number to the buffer at the given offset. - *

- * The float will be written at the given offset. If there are less than {@link Bytes#FLOAT} bytes remaining - * in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the float. - * @param f The float to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#FLOAT}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeFloat(float) - */ - @Override - Buffer writeFloat(int offset, float f); - - /** - * Writes a double-precision 64-bit floating point number to the buffer at the current position. - *

- * When the double is written to the buffer, the buffer's {@code position} will be advanced by {@link Bytes#DOUBLE}. - * If less than {@link Bytes#DOUBLE} bytes are remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param d The double to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#DOUBLE}. - * @see Buffer#writeDouble(int, double) - */ - @Override - Buffer writeDouble(double d); - - /** - * Writes a double-precision 64-bit floating point number to the buffer at the given offset. - *

- * The double will be written at the given offset. If there are less than {@link Bytes#DOUBLE} bytes remaining - * in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the double. - * @param d The double to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@link Bytes#DOUBLE}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeDouble(double) - */ - @Override - Buffer writeDouble(int offset, double d); - - /** - * Writes a 1 byte boolean to the buffer at the current position. - *

- * When the boolean is written to the buffer, the buffer's {@code position} will be advanced by {@code 1}. - * If there are no bytes remaining in the buffer then a {@link java.nio.BufferOverflowException} - * will be thrown. - * - * @param b The boolean to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If the number of bytes exceeds the buffer's remaining bytes. - * @see Buffer#writeBoolean(int, boolean) - */ - @Override - Buffer writeBoolean(boolean b); - - /** - * Writes a 1 byte boolean to the buffer at the given offset. - *

- * The boolean will be written as a single byte at the given offset. If there are no bytes remaining in the buffer - * then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the boolean. - * @param b The boolean to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@code 1}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeBoolean(boolean) - */ - @Override - Buffer writeBoolean(int offset, boolean b); - - /** - * Writes a UTF-8 string to the buffer at the current position. - *

- * The string will be written with a two-byte unsigned byte length followed by the UTF-8 bytes. If there are not enough - * bytes remaining in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param s The string to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If the number of bytes exceeds the buffer's remaining bytes. - * @see Buffer#writeUTF8(int, String) - */ - @Override - Buffer writeUTF8(String s); - - /** - * Writes a UTF-8 string to the buffer at the given offset. - *

- * The string will be written with a two-byte unsigned byte length followed by the UTF-8 bytes. If there are not enough - * bytes remaining in the buffer then a {@link java.nio.BufferOverflowException} will be thrown. - * - * @param offset The offset at which to write the string. - * @param s The string to write. - * @return The written buffer. - * @throws java.nio.BufferOverflowException If {@link Buffer#remaining()} is less than {@code 1}. - * @throws IndexOutOfBoundsException If the given offset is out of the bounds of the buffer. Note that - * bounds are determined by the buffer's {@link Buffer#limit()} rather than capacity. - * @see Buffer#writeUTF8(String) - */ - @Override - Buffer writeUTF8(int offset, String s); - - /** - * Closes the buffer. - *

- * This method effectively acts as an alias to {@link Buffer#release()} and allows buffers to - * be used as resources in try-with-resources statements. When the buffer is closed the internal reference counter - * defined by {@link ReferenceCounted} will be decremented. However, if references to the - * buffer still exist then those references will be allowed to continue to operate on the buffer until all references - * have been released. - */ - @Override - void close(); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferAllocator.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferAllocator.java deleted file mode 100644 index c9d3c54654..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferAllocator.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -/** - * Buffer allocator. - * - * @author Jordan Halterman - */ -public interface BufferAllocator { - - /** - * Allocates a dynamic capacity buffer. - * - * @return The allocated buffer. - */ - Buffer allocate(); - - /** - * Allocates a dynamic capacity buffer with the given initial capacity. - * - * @param initialCapacity The initial buffer capacity. - * @return The allocated buffer. - */ - Buffer allocate(int initialCapacity); - - /** - * Allocates a new buffer. - * - * @param initialCapacity The initial buffer capacity. - * @param maxCapacity The maximum buffer capacity. - * @return The allocated buffer. - */ - Buffer allocate(int initialCapacity, int maxCapacity); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferInput.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferInput.java deleted file mode 100644 index d33c360ee7..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferInput.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.charset.Charset; -import java.util.function.Function; - -/** - * Readable buffer. - *

- * This interface exposes methods for reading from a byte buffer. Readable buffers maintain a small amount of state - * regarding current cursor positions and limits similar to the behavior of {@link java.nio.ByteBuffer}. - * - * @author Jordan Halterman - */ -public interface BufferInput> extends AutoCloseable { - - /** - * Returns the buffer's current read/write position. - *

- * The position is an internal cursor that tracks where to write/read bytes in the underlying storage implementation. - * As bytes are written to or read from the buffer, the position will advance based on the number of bytes read. - * - * @return The buffer's current position. - */ - int position(); - - /** - * Returns the number of bytes remaining in the input. - * - * @return The number of bytes remaining in the input. - */ - int remaining(); - - /** - * Returns a boolean value indicating whether the input has bytes remaining. - * - * @return Indicates whether bytes remain to be read from the input. - */ - boolean hasRemaining(); - - /** - * Skips the given number of bytes in the input. - * - * @param bytes The number of bytes to attempt to skip. - * @return The skipped input. - */ - T skip(int bytes); - - /** - * Reads bytes into the given byte array. - * - * @param bytes The byte array into which to read bytes. - * @return The buffer. - */ - T read(Bytes bytes); - - /** - * Reads bytes into the given byte array. - * - * @param bytes The byte array into which to read bytes. - * @return The buffer. - */ - T read(byte[] bytes); - - /** - * Reads bytes into the given byte array starting at the current position. - * - * @param bytes The byte array into which to read bytes. - * @param offset The offset at which to write bytes into the given buffer - * @return The buffer. - */ - T read(Bytes bytes, int offset, int length); - - /** - * Reads bytes into the given byte array starting at current position up to the given length. - * - * @param bytes The byte array into which to read bytes. - * @param offset The offset at which to write bytes into the given buffer - * @return The buffer. - */ - T read(byte[] bytes, int offset, int length); - - /** - * Reads bytes into the given buffer. - * - * @param buffer The buffer into which to read bytes. - * @return The buffer. - */ - T read(Buffer buffer); - - /** - * Reads an object from the buffer. - * - * @param decoder the object decoder - * @param the type of the object to read - * @return the read object. - */ - default U readObject(Function decoder) { - byte[] bytes = readBytes(readInt()); - return decoder.apply(bytes); - } - - /** - * Reads a byte array. - * - * @param length The byte array length - * @return The read byte array. - */ - default byte[] readBytes(int length) { - byte[] bytes = new byte[length]; - read(bytes); - return bytes; - } - - /** - * Reads a byte from the buffer at the current position. - * - * @return The read byte. - */ - int readByte(); - - /** - * Reads an unsigned byte from the buffer at the current position. - * - * @return The read byte. - */ - int readUnsignedByte(); - - /** - * Reads a 16-bit character from the buffer at the current position. - * - * @return The read character. - */ - char readChar(); - - /** - * Reads a 16-bit signed integer from the buffer at the current position. - * - * @return The read short. - */ - short readShort(); - - /** - * Reads a 16-bit unsigned integer from the buffer at the current position. - * - * @return The read short. - */ - int readUnsignedShort(); - - /** - * Reads a 24-bit signed integer from the buffer at the current position. - * - * @return The read integer. - */ - int readMedium(); - - /** - * Reads a 24-bit unsigned integer from the buffer at the current position. - * - * @return The read integer. - */ - int readUnsignedMedium(); - - /** - * Reads a 32-bit signed integer from the buffer at the current position. - * - * @return The read integer. - */ - int readInt(); - - /** - * Reads a 32-bit unsigned integer from the buffer at the current position. - * - * @return The read integer. - */ - long readUnsignedInt(); - - /** - * Reads a 64-bit signed integer from the buffer at the current position. - * - * @return The read long. - */ - long readLong(); - - /** - * Reads a single-precision 32-bit floating point number from the buffer at the current position. - * - * @return The read float. - */ - float readFloat(); - - /** - * Reads a double-precision 64-bit floating point number from the buffer at the current position. - * - * @return The read double. - */ - double readDouble(); - - /** - * Reads a 1 byte boolean from the buffer at the current position. - * - * @return The read boolean. - */ - boolean readBoolean(); - - /** - * Reads a string from the buffer at the current position. - * - * @return The read string. - */ - String readString(); - - /** - * Reads a string from the buffer at the current position. - * - * @param charset The character set with which to decode the string. - * @return The read string. - */ - String readString(Charset charset); - - /** - * Reads a UTF-8 string from the buffer at the current position. - * - * @return The read string. - */ - String readUTF8(); - - @Override - void close(); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferOutput.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferOutput.java deleted file mode 100644 index 722afb73f9..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferOutput.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.charset.Charset; -import java.util.function.Function; - -/** - * Writable buffer. - *

- * This interface exposes methods for writing to a byte buffer. Writable buffers maintain a small amount of state - * regarding current cursor positions and limits similar to the behavior of {@link java.nio.ByteBuffer}. - * - * @author Jordan Halterman - */ -public interface BufferOutput> extends AutoCloseable { - - /** - * Writes an array of bytes to the buffer. - * - * @param bytes The array of bytes to write. - * @return The written buffer. - */ - T write(Bytes bytes); - - /** - * Writes an array of bytes to the buffer. - * - * @param bytes The array of bytes to write. - * @return The written buffer. - */ - T write(byte[] bytes); - - /** - * Writes an array of bytes to the buffer. - * - * @param bytes The array of bytes to write. - * @param offset The offset at which to start writing the bytes. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - */ - T write(Bytes bytes, int offset, int length); - - /** - * Writes an array of bytes to the buffer. - * - * @param bytes The array of bytes to write. - * @param offset The offset at which to start writing the bytes. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - */ - T write(byte[] bytes, int offset, int length); - - /** - * Writes a buffer to the buffer. - * - * @param buffer The buffer to write. - * @return The written buffer. - */ - T write(Buffer buffer); - - /** - * Writes an object to the snapshot. - * - * @param object the object to write - * @param encoder the object encoder - * @return The snapshot writer. - */ - @SuppressWarnings("unchecked") - default T writeObject(U object, Function encoder) { - byte[] bytes = encoder.apply(object); - writeInt(bytes.length).write(bytes); - return (T) this; - } - - /** - * Writes a byte array. - * - * @param bytes The byte array to write. - * @return The written buffer. - */ - @SuppressWarnings("unchecked") - default T writeBytes(byte[] bytes) { - write(bytes); - return (T) this; - } - - /** - * Writes a byte to the buffer. - * - * @param b The byte to write. - * @return The written buffer. - */ - T writeByte(int b); - - /** - * Writes an unsigned byte to the buffer. - * - * @param b The byte to write. - * @return The written buffer. - */ - T writeUnsignedByte(int b); - - /** - * Writes a 16-bit character to the buffer. - * - * @param c The character to write. - * @return The written buffer. - */ - T writeChar(char c); - - /** - * Writes a 16-bit signed integer to the buffer. - * - * @param s The short to write. - * @return The written buffer. - */ - T writeShort(short s); - - /** - * Writes a 16-bit unsigned integer to the buffer. - * - * @param s The short to write. - * @return The written buffer. - */ - T writeUnsignedShort(int s); - - /** - * Writes a 24-bit signed integer to the buffer. - * - * @param m The integer to write. - * @return The written buffer. - */ - T writeMedium(int m); - - /** - * Writes a 24-bit unsigned integer to the buffer. - * - * @param m The integer to write. - * @return The written buffer. - */ - T writeUnsignedMedium(int m); - - /** - * Writes a 32-bit signed integer to the buffer. - * - * @param i The integer to write. - * @return The written buffer. - */ - T writeInt(int i); - - /** - * Writes a 32-bit unsigned integer to the buffer. - * - * @param i The integer to write. - * @return The written buffer. - */ - T writeUnsignedInt(long i); - - /** - * Writes a 64-bit signed integer to the buffer. - * - * @param l The long to write. - * @return The written buffer. - */ - T writeLong(long l); - - /** - * Writes a single-precision 32-bit floating point number to the buffer. - * - * @param f The float to write. - * @return The written buffer. - */ - T writeFloat(float f); - - /** - * Writes a double-precision 64-bit floating point number to the buffer. - * - * @param d The double to write. - * @return The written buffer. - */ - T writeDouble(double d); - - /** - * Writes a 1 byte boolean to the buffer. - * - * @param b The boolean to write. - * @return The written buffer. - */ - T writeBoolean(boolean b); - - /** - * Writes a string to the buffer. - * - * @param s The string to write. - * @return The written buffer. - */ - T writeString(String s); - - /** - * Writes a string to the buffer. - * - * @param s The string to write. - * @param charset The character set with which to encode the string. - * @return The written buffer. - */ - T writeString(String s, Charset charset); - - /** - * Writes a UTF-8 string to the buffer. - * - * @param s The string to write. - * @return The written buffer. - */ - T writeUTF8(String s); - - /** - * Flushes the buffer to the underlying persistence layer. - * - * @return The flushed buffer. - */ - T flush(); - - @Override - void close(); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferPool.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferPool.java deleted file mode 100644 index d5bc81856f..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BufferPool.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferenceFactory; -import io.atomix.utils.concurrent.ReferencePool; - -/** - * Buffer pool. - * - * @author Jordan Halterman - */ -public class BufferPool extends ReferencePool { - - public BufferPool(ReferenceFactory factory) { - super(factory); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ByteBufferBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ByteBufferBuffer.java deleted file mode 100644 index aa3e28e930..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ByteBufferBuffer.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferenceManager; - -/** - * {@link java.nio.ByteBuffer} based buffer. - */ -public abstract class ByteBufferBuffer extends AbstractBuffer { - protected final ByteBufferBytes bytes; - - public ByteBufferBuffer(ByteBufferBytes bytes, ReferenceManager referenceManager) { - super(bytes, referenceManager); - this.bytes = bytes; - } - - public ByteBufferBuffer(ByteBufferBytes bytes, int offset, int initialCapacity, int maxCapacity, ReferenceManager referenceManager) { - super(bytes, offset, initialCapacity, maxCapacity, referenceManager); - this.bytes = bytes; - } - - @Override - public byte[] array() { - return bytes.array(); - } - - @Override - protected void compact(int from, int to, int length) { - byte[] bytes = new byte[1024]; - int position = from; - while (position < from + length) { - int size = Math.min((from + length) - position, 1024); - this.bytes.read(position, bytes, 0, size); - this.bytes.write(0, bytes, 0, size); - position += size; - } - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ByteBufferBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ByteBufferBytes.java deleted file mode 100644 index a3ed8b7c34..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ByteBufferBytes.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Byte buffer bytes. - */ -public abstract class ByteBufferBytes extends AbstractBytes { - protected ByteBuffer buffer; - - protected ByteBufferBytes(ByteBuffer buffer) { - this.buffer = buffer; - } - - public Bytes reset(ByteBuffer buffer) { - buffer.clear(); - this.buffer = checkNotNull(buffer, "buffer cannot be null"); - return this; - } - - /** - * Allocates a new byte buffer. - * - * @param size the buffer size - * @return a newly allocated byte buffer - */ - protected abstract ByteBuffer newByteBuffer(int size); - - @Override - public Bytes resize(int newSize) { - ByteBuffer oldBuffer = buffer; - ByteBuffer newBuffer = newByteBuffer(newSize); - oldBuffer.position(0).limit(oldBuffer.capacity()); - newBuffer.position(0).limit(newBuffer.capacity()); - newBuffer.put(oldBuffer); - newBuffer.clear(); - return reset(newBuffer); - } - - @Override - public byte[] array() { - return buffer.array(); - } - - /** - * Returns the underlying {@link ByteBuffer}. - * - * @return the underlying byte buffer - */ - public ByteBuffer byteBuffer() { - return buffer; - } - - @Override - public Bytes zero() { - return this; - } - - @Override - public int size() { - return buffer.capacity(); - } - - @Override - public ByteOrder order() { - return buffer.order(); - } - - @Override - public Bytes order(ByteOrder order) { - return reset(buffer.order(order)); - } - - /** - * Returns the index for the given offset. - */ - private int index(int offset) { - return (int) offset; - } - - @Override - public Bytes zero(int offset) { - for (int i = index(offset); i < buffer.capacity(); i++) { - buffer.put(i, (byte) 0); - } - return this; - } - - @Override - public Bytes zero(int offset, int length) { - for (int i = index(offset); i < offset + length; i++) { - buffer.put(i, (byte) 0); - } - return this; - } - - @Override - public Bytes read(int position, byte[] bytes, int offset, int length) { - for (int i = 0; i < length; i++) { - bytes[index(offset) + i] = (byte) readByte(position + i); - } - return this; - } - - @Override - public Bytes read(int position, Bytes bytes, int offset, int length) { - for (int i = 0; i < length; i++) { - bytes.writeByte(offset + i, readByte(position + i)); - } - return this; - } - - @Override - public Bytes write(int position, byte[] bytes, int offset, int length) { - for (int i = 0; i < length; i++) { - buffer.put((int) position + i, (byte) bytes[index(offset) + i]); - } - return this; - } - - @Override - public Bytes write(int position, Bytes bytes, int offset, int length) { - for (int i = 0; i < length; i++) { - buffer.put((int) position + i, (byte) bytes.readByte(offset + i)); - } - return this; - } - - @Override - public int readByte(int offset) { - return buffer.get(index(offset)); - } - - @Override - public char readChar(int offset) { - return buffer.getChar(index(offset)); - } - - @Override - public short readShort(int offset) { - return buffer.getShort(index(offset)); - } - - @Override - public int readInt(int offset) { - return buffer.getInt(index(offset)); - } - - @Override - public long readLong(int offset) { - return buffer.getLong(index(offset)); - } - - @Override - public float readFloat(int offset) { - return buffer.getFloat(index(offset)); - } - - @Override - public double readDouble(int offset) { - return buffer.getDouble(index(offset)); - } - - @Override - public Bytes writeByte(int offset, int b) { - buffer.put(index(offset), (byte) b); - return this; - } - - @Override - public Bytes writeChar(int offset, char c) { - buffer.putChar(index(offset), c); - return this; - } - - @Override - public Bytes writeShort(int offset, short s) { - buffer.putShort(index(offset), s); - return this; - } - - @Override - public Bytes writeInt(int offset, int i) { - buffer.putInt(index(offset), i); - return this; - } - - @Override - public Bytes writeLong(int offset, long l) { - buffer.putLong(index(offset), l); - return this; - } - - @Override - public Bytes writeFloat(int offset, float f) { - buffer.putFloat(index(offset), f); - return this; - } - - @Override - public Bytes writeDouble(int offset, double d) { - buffer.putDouble(index(offset), d); - return this; - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/Bytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/Bytes.java deleted file mode 100644 index 7a28b998e5..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/Bytes.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.ByteOrder; - -/** - * Common interface for interacting with a memory or disk based array of bytes. - * - * @author Jordan Halterman - */ -public interface Bytes extends BytesInput, BytesOutput, AutoCloseable { - int BYTE = 1; - int BOOLEAN = 1; - int CHARACTER = 2; - int SHORT = 2; - int MEDIUM = 3; - int INTEGER = 4; - int LONG = 8; - int FLOAT = 4; - int DOUBLE = 8; - - /** - * Returns whether the bytes has an array. - * - * @return Whether the bytes has an underlying array. - */ - default boolean hasArray() { - return false; - } - - /** - * Returns the underlying byte array. - * - * @return the underlying byte array - * @throws UnsupportedOperationException if a heap array is not supported - */ - default byte[] array() { - throw new UnsupportedOperationException(); - } - - /** - * Returns the count of the bytes. - * - * @return The count of the bytes. - */ - int size(); - - /** - * Resizes the bytes. - *

- * When the bytes are resized, underlying memory addresses in copies of this instance may no longer be valid. Additionally, - * if the {@code newSize} is smaller than the current {@code count} then some data may be lost during the resize. Use - * with caution. - * - * @param newSize The count to which to resize this instance. - * @return The resized bytes. - */ - Bytes resize(int newSize); - - /** - * Returns the byte order. - *

- * For consistency with {@link java.nio.ByteBuffer}, all bytes implementations are initially in {@link ByteOrder#BIG_ENDIAN} order. - * - * @return The byte order. - */ - ByteOrder order(); - - /** - * Sets the byte order, returning a new swapped {@link Bytes} instance. - *

- * By default, all bytes are read and written in {@link ByteOrder#BIG_ENDIAN} order. This provides complete - * consistency with {@link java.nio.ByteBuffer}. To flip bytes to {@link ByteOrder#LITTLE_ENDIAN} order, this - * {@code Bytes} instance is decorated by a {@link SwappedBytes} instance which will reverse - * read and written bytes using, e.g. {@link Integer#reverseBytes(int)}. - * - * @param order The byte order. - * @return The updated bytes. - * @throws NullPointerException If the {@code order} is {@code null} - */ - Bytes order(ByteOrder order); - - /** - * Returns a boolean value indicating whether the bytes are direct. - * - * @return Indicates whether the bytes are direct. - */ - boolean isDirect(); - - /** - * Returns a boolean value indicating whether the bytes are backed by a file. - * - * @return Indicates whether the bytes are backed by a file. - */ - boolean isFile(); - - @Override - void close(); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BytesInput.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BytesInput.java deleted file mode 100644 index f097b135c2..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BytesInput.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.charset.Charset; - -/** - * Readable bytes. - *

- * This interface exposes methods for reading bytes from specific positions in a byte array. - * - * @author Jordan Halterman - */ -public interface BytesInput> { - - /** - * Reads bytes into the given byte array starting at the given offset up to the given length. - * - * @param offset The offset from which to start reading bytes. - * @param dst The byte array into which to read bytes. - * @param dstOffset The offset at which to write bytes into the given buffer. - * @param length The total number of bytes to read. - * @return The buffer. - */ - T read(int offset, Bytes dst, int dstOffset, int length); - - /** - * Reads bytes into the given byte array starting at the given offset up to the given length. - * - * @param offset The offset from which to start reading bytes. - * @param dst The byte array into which to read bytes. - * @param dstOffset The offset at which to write bytes into the given buffer - * @param length The total number of bytes to read. - * @return The buffer. - */ - T read(int offset, byte[] dst, int dstOffset, int length); - - /** - * Reads a byte from the buffer at the given offset. - * - * @param offset The offset at which to read the byte. - * @return The read byte. - */ - int readByte(int offset); - - /** - * Reads an unsigned byte from the buffer at the given offset. - * - * @param offset The offset at which to read the byte. - * @return The read unsigned byte. - */ - int readUnsignedByte(int offset); - - /** - * Reads a 16-bit character from the buffer at the given offset. - * - * @param offset The offset at which to read the character. - * @return The read character. - */ - char readChar(int offset); - - /** - * Reads a 16-bit signed integer from the buffer at the given offset. - * - * @param offset The offset at which to read the short. - * @return The read short. - */ - short readShort(int offset); - - /** - * Reads a 16-bit unsigned integer from the buffer at the given offset. - * - * @param offset The offset at which to read the short. - * @return The read short. - */ - int readUnsignedShort(int offset); - - /** - * Reads a 24-bit signed integer from the buffer at the given offset. - * - * @param offset The offset at which to read the integer. - * @return The read medium. - */ - int readMedium(int offset); - - /** - * Reads a 24-bin unsigned integer from the buffer at the given offset. - * - * @param offset The offset at which to read the integer. - * @return The read medium. - */ - int readUnsignedMedium(int offset); - - /** - * Reads a 32-bit signed integer from the buffer at the given offset. - * - * @param offset The offset at which to read the integer. - * @return The read integer. - */ - int readInt(int offset); - - /** - * Reads a 32-bit unsigned integer from the buffer at the given offset. - * - * @param offset The offset at which to read the integer. - * @return The read integer. - */ - long readUnsignedInt(int offset); - - /** - * Reads a 64-bit signed integer from the buffer at the given offset. - * - * @param offset The offset at which to read the long. - * @return The read long. - */ - long readLong(int offset); - - /** - * Reads a single-precision 32-bit floating point number from the buffer at the given offset. - * - * @param offset The offset at which to read the float. - * @return The read float. - */ - float readFloat(int offset); - - /** - * Reads a double-precision 64-bit floating point number from the buffer at the given offset. - * - * @param offset The offset at which to read the double. - * @return The read double. - */ - double readDouble(int offset); - - /** - * Reads a 1 byte boolean from the buffer at the given offset. - * - * @param offset The offset at which to read the boolean. - * @return The read boolean. - */ - boolean readBoolean(int offset); - - /** - * Reads a string from the buffer at the given offset. - * - * @param offset The offset at which to read the string. - * @return The read string. - */ - String readString(int offset); - - /** - * Reads a string from the buffer at the given offset. - * - * @param offset The offset at which to read the string. - * @param charset The character set with which to decode the string. - * @return The read string. - */ - String readString(int offset, Charset charset); - - /** - * Reads a UTF-8 string from the buffer at the given offset. - * - * @param offset The offset at which to read the string. - * @return The read string. - */ - String readUTF8(int offset); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BytesOutput.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BytesOutput.java deleted file mode 100644 index 30673b5503..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/BytesOutput.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.charset.Charset; - -/** - * Writable bytes. - *

- * This interface exposes methods for writing bytes to specific positions in a byte array. - * - * @author Jordan Halterman - */ -public interface BytesOutput> { - - /** - * Zeros out all bytes in the array. - * - * @return The written bytes. - */ - T zero(); - - /** - * Zeros out all bytes starting at the given offset in the array. - * - * @param offset The offset at which to start zeroing out bytes. - * @return The written bytes. - */ - T zero(int offset); - - /** - * Zeros out bytes starting at the given offset up to the given length. - * - * @param offset The offset at which to start zeroing out bytes. - * @param length THe total number of bytes to zero out. - * @return The written bytes. - */ - T zero(int offset, int length); - - /** - * Writes an array of bytes to the buffer. - * - * @param offset The offset at which to start writing the bytes. - * @param src The array of bytes to write. - * @param srcOffset The offset at which to start reading bytes from the given source. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - */ - T write(int offset, Bytes src, int srcOffset, int length); - - /** - * Writes an array of bytes to the buffer. - * - * @param offset The offset at which to start writing the bytes. - * @param src The array of bytes to write. - * @param srcOffset The offset at which to start reading bytes from the given source. - * @param length The number of bytes from the provided byte array to write to the buffer. - * @return The written buffer. - */ - T write(int offset, byte[] src, int srcOffset, int length); - - /** - * Writes a byte to the buffer at the given offset. - * - * @param offset The offset at which to write the byte. - * @param b The byte to write. - * @return The written buffer. - */ - T writeByte(int offset, int b); - - /** - * Writes an unsigned byte to the buffer at the given position. - * - * @param offset The offset at which to write the byte. - * @param b The byte to write. - * @return The written buffer. - */ - T writeUnsignedByte(int offset, int b); - - /** - * Writes a 16-bit character to the buffer at the given offset. - * - * @param offset The offset at which to write the character. - * @param c The character to write. - * @return The written buffer. - */ - T writeChar(int offset, char c); - - /** - * Writes a 16-bit signed integer to the buffer at the given offset. - * - * @param offset The offset at which to write the short. - * @param s The short to write. - * @return The written buffer. - */ - T writeShort(int offset, short s); - - /** - * Writes a 16-bit unsigned integer to the buffer at the given offset. - * - * @param offset The offset at which to write the short. - * @param s The short to write. - * @return The written buffer. - */ - T writeUnsignedShort(int offset, int s); - - /** - * Writes a 24-bit signed integer to the buffer at the given offset. - * - * @param offset The offset at which to write the short. - * @param m The short to write. - * @return The written buffer. - */ - T writeMedium(int offset, int m); - - /** - * Writes a 24-bit unsigned integer to the buffer at the given offset. - * - * @param offset The offset at which to write the short. - * @param m The short to write. - * @return The written buffer. - */ - T writeUnsignedMedium(int offset, int m); - - /** - * Writes a 32-bit signed integer to the buffer at the given offset. - * - * @param offset The offset at which to write the integer. - * @param i The integer to write. - * @return The written buffer. - */ - T writeInt(int offset, int i); - - /** - * Writes a 32-bit unsigned integer to the buffer at the given offset. - * - * @param offset The offset at which to write the integer. - * @param i The integer to write. - * @return The written buffer. - */ - T writeUnsignedInt(int offset, long i); - - /** - * Writes a 64-bit signed integer to the buffer at the given offset. - * - * @param offset The offset at which to write the long. - * @param l The long to write. - * @return The written buffer. - */ - T writeLong(int offset, long l); - - /** - * Writes a single-precision 32-bit floating point number to the buffer at the given offset. - * - * @param offset The offset at which to write the float. - * @param f The float to write. - * @return The written buffer. - */ - T writeFloat(int offset, float f); - - /** - * Writes a double-precision 64-bit floating point number to the buffer at the given offset. - * - * @param offset The offset at which to write the double. - * @param d The double to write. - * @return The written buffer. - */ - T writeDouble(int offset, double d); - - /** - * Writes a 1 byte boolean to the buffer at the given offset. - * - * @param offset The offset at which to write the boolean. - * @param b The boolean to write. - * @return The written buffer. - */ - T writeBoolean(int offset, boolean b); - - /** - * Writes a string to the buffer at the given offset. - * - * @param offset The offset at which to write the string. - * @param s The string to write. - * @return The written buffer. - */ - T writeString(int offset, String s); - - /** - * Writes a string to the buffer at the given offset. - * - * @param offset The offset at which to write the string. - * @param s The string to write. - * @param charset The character set with which to encode the string. - * @return The written buffer. - */ - T writeString(int offset, String s, Charset charset); - - /** - * Writes a UTF-8 string to the buffer at the given offset. - * - * @param offset The offset at which to write the string. - * @param s The string to write. - * @return The written buffer. - */ - T writeUTF8(int offset, String s); - - /** - * Flushes the bytes to the underlying persistence layer. - * - * @return The flushed buffer. - */ - T flush(); - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/DirectBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/DirectBuffer.java deleted file mode 100644 index 32cbd17d72..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/DirectBuffer.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.memory.Memory; - -import static com.google.common.base.Preconditions.checkArgument; - -/** - * Direct {@link java.nio.ByteBuffer} based buffer. - * - * @author Jordan Halterman - */ -public class DirectBuffer extends ByteBufferBuffer { - - /** - * Allocates a direct buffer with an initial capacity of {@code 4096} and a maximum capacity of {@link Long#MAX_VALUE}. - * - * @return The direct buffer. - * @see DirectBuffer#allocate(int) - * @see DirectBuffer#allocate(int, int) - */ - public static DirectBuffer allocate() { - return allocate(DEFAULT_INITIAL_CAPACITY, MAX_SIZE); - } - - /** - * Allocates a direct buffer with the given initial capacity. - * - * @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(int, int) - */ - public static DirectBuffer allocate(int initialCapacity) { - return allocate(initialCapacity, MAX_SIZE); - } - - /** - * Allocates a new direct buffer. - * - * @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(int) - */ - public static DirectBuffer allocate(int initialCapacity, int maxCapacity) { - checkArgument(initialCapacity <= maxCapacity, "initial capacity cannot be greater than maximum capacity"); - return new DirectBuffer(DirectBytes.allocate((int) Math.min(Memory.Util.toPow2(initialCapacity), MAX_SIZE)), 0, initialCapacity, maxCapacity); - } - - protected DirectBuffer(DirectBytes bytes, int offset, int initialCapacity, int maxCapacity) { - super(bytes, offset, initialCapacity, maxCapacity, null); - } - - @Override - public DirectBuffer duplicate() { - return new DirectBuffer((DirectBytes) bytes, offset(), capacity(), maxCapacity()); - } -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/DirectBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/DirectBytes.java deleted file mode 100644 index 2256f24196..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/DirectBytes.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.ByteBuffer; - -/** - * {@link ByteBuffer} based direct bytes. - */ -public class DirectBytes extends ByteBufferBytes { - - /** - * Allocates a new direct byte array. - * - * @param size The count of the buffer to allocate (in bytes). - * @return The direct buffer. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed count for - * an array on the Java heap - {@code Integer.MAX_VALUE - 5} - */ - public static DirectBytes allocate(int size) { - if (size > MAX_SIZE) { - throw new IllegalArgumentException("size cannot for DirectBytes cannot be greater than " + MAX_SIZE); - } - return new DirectBytes(ByteBuffer.allocateDirect((int) size)); - } - - protected DirectBytes(ByteBuffer buffer) { - super(buffer); - } - - @Override - protected ByteBuffer newByteBuffer(int size) { - return ByteBuffer.allocateDirect((int) size); - } - - @Override - public boolean isDirect() { - return buffer.isDirect(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/FileBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/FileBuffer.java deleted file mode 100644 index c375311783..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/FileBuffer.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.memory.Memory; - -import java.io.File; -import java.nio.channels.FileChannel; - -import static com.google.common.base.Preconditions.checkArgument; - -/** - * File buffer. - *

- * File buffers wrap a simple {@link java.io.RandomAccessFile} instance to provide random access to a file on local disk. All - * operations are delegated directly to the {@link java.io.RandomAccessFile} interface, and limitations are dependent on the - * semantics of the underlying file. - * - * @author Jordan Halterman - */ -public class FileBuffer extends AbstractBuffer { - - /** - * Allocates a file buffer of unlimited capacity. - *

- * The buffer will initially be allocated with {@code 4096} bytes. As bytes are written to the resulting buffer and - * the original capacity is reached, the buffer's capacity will double. - * - * @param file The file to allocate. - * @return The allocated buffer. - * @see FileBuffer#allocate(File, int) - * @see FileBuffer#allocate(File, int, int) - * @see FileBuffer#allocate(File, String, int, int) - */ - public static FileBuffer allocate(File file) { - return allocate(file, FileBytes.DEFAULT_MODE, DEFAULT_INITIAL_CAPACITY, Integer.MAX_VALUE); - } - - /** - * Allocates a file buffer with the given initial capacity. - *

- * If the underlying file is empty, the file count will expand dynamically as bytes are written to the file. - * The underlying {@link FileBytes} will be initialized to the nearest power of {@code 2}. - * - * @param file The file to allocate. - * @param initialCapacity The initial capacity of the bytes to allocate. - * @return The allocated buffer. - * @see FileBuffer#allocate(File) - * @see FileBuffer#allocate(File, int, int) - * @see FileBuffer#allocate(File, String, int, int) - */ - public static FileBuffer allocate(File file, int initialCapacity) { - return allocate(file, FileBytes.DEFAULT_MODE, initialCapacity, Integer.MAX_VALUE); - } - - /** - * Allocates a file buffer. - *

- * The underlying {@link java.io.RandomAccessFile} will be created in {@code rw} mode by default. - * The resulting buffer will be initialized with a capacity of {@code initialCapacity}. The underlying {@link FileBytes} - * will be initialized to the nearest power of {@code 2}. As bytes are written to the file the buffer's capacity will - * double up to {@code maxCapacity}. - * - * @param file The file to allocate. - * @param initialCapacity The initial capacity of the buffer. - * @param maxCapacity The maximum allowed capacity of the buffer. - * @return The allocated buffer. - * @see FileBuffer#allocate(File) - * @see FileBuffer#allocate(File, int) - * @see FileBuffer#allocate(File, String, int, int) - */ - public static FileBuffer allocate(File file, int initialCapacity, int maxCapacity) { - return allocate(file, FileBytes.DEFAULT_MODE, initialCapacity, maxCapacity); - } - - /** - * Allocates a file buffer. - *

- * The resulting buffer will be initialized with a capacity of {@code initialCapacity}. The underlying {@link FileBytes} - * will be initialized to the nearest power of {@code 2}. As bytes are written to the file the buffer's capacity will - * double up to {@code maxCapacity}. - * - * @param file The file to allocate. - * @param mode The mode in which to open the underlying {@link java.io.RandomAccessFile}. - * @param initialCapacity The initial capacity of the buffer. - * @param maxCapacity The maximum allowed capacity of the buffer. - * @return The allocated buffer. - * @see FileBuffer#allocate(File) - * @see FileBuffer#allocate(File, int) - * @see FileBuffer#allocate(File, int, int) - */ - public static FileBuffer allocate(File file, String mode, int initialCapacity, int maxCapacity) { - checkArgument(initialCapacity <= maxCapacity, "initial capacity cannot be greater than maximum capacity"); - return new FileBuffer(new FileBytes(file, mode, (int) Math.min(Memory.Util.toPow2(initialCapacity), maxCapacity)), 0, initialCapacity, maxCapacity); - } - - private final FileBytes bytes; - - private FileBuffer(FileBytes bytes, int offset, int initialCapacity, int maxCapacity) { - super(bytes, offset, initialCapacity, maxCapacity, null); - this.bytes = bytes; - } - - /** - * Returns the underlying file object. - * - * @return The underlying file. - */ - public File file() { - return ((FileBytes) bytes).file(); - } - - /** - * Maps a portion of the underlying file into memory in {@link FileChannel.MapMode#READ_WRITE} mode - * starting at the current position up to the given {@code count}. - * - * @param size The count of the bytes to map into memory. - * @return The mapped buffer. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed - * {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE} - */ - public MappedBuffer map(int size) { - return map(position(), size, FileChannel.MapMode.READ_WRITE); - } - - /** - * Maps a portion of the underlying file into memory starting at the current position up to the given {@code count}. - * - * @param size The count of the bytes to map into memory. - * @param mode The mode in which to map the bytes into memory. - * @return The mapped buffer. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed - * {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE} - */ - public MappedBuffer map(int size, FileChannel.MapMode mode) { - return map(position(), size, mode); - } - - /** - * Maps a portion of the underlying file into memory in {@link FileChannel.MapMode#READ_WRITE} mode - * starting at the given {@code offset} up to the given {@code count}. - * - * @param offset The offset from which to map bytes into memory. - * @param size The count of the bytes to map into memory. - * @return The mapped buffer. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed - * {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE} - */ - public MappedBuffer map(int offset, int size) { - return map(offset, size, FileChannel.MapMode.READ_WRITE); - } - - /** - * Maps a portion of the underlying file into memory starting at the given {@code offset} up to the given {@code count}. - * - * @param offset The offset from which to map bytes into memory. - * @param size The count of the bytes to map into memory. - * @param mode The mode in which to map the bytes into memory. - * @return The mapped buffer. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed - * {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE} - */ - public MappedBuffer map(int offset, int size, FileChannel.MapMode mode) { - return new MappedBuffer(((FileBytes) bytes).map(offset, size, mode), 0, size, size); - } - - @Override - protected void compact(int from, int to, int length) { - byte[] bytes = new byte[1024]; - int position = from; - while (position < from + length) { - int size = Math.min((from + length) - position, 1024); - this.bytes.read(position, bytes, 0, size); - this.bytes.write(0, bytes, 0, size); - position += size; - } - } - - @Override - public FileBuffer duplicate() { - return new FileBuffer(new FileBytes(bytes.file(), bytes.mode(), bytes.size()), offset(), capacity(), maxCapacity()); - } - - /** - * Duplicates the buffer using the given mode. - * - * @return The mode with which to open the duplicate buffer. - */ - public FileBuffer duplicate(String mode) { - return new FileBuffer(new FileBytes(bytes.file(), mode, bytes.size()), offset(), capacity(), maxCapacity()); - } - - /** - * Deletes the underlying file. - */ - public void delete() { - bytes.delete(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/FileBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/FileBytes.java deleted file mode 100644 index 8784d2407d..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/FileBytes.java +++ /dev/null @@ -1,531 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.memory.Memory; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteOrder; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.file.Files; - -/** - * File bytes. - *

- * File bytes wrap a simple {@link RandomAccessFile} instance to provide random access to a randomAccessFile on local disk. All - * operations are delegated directly to the {@link RandomAccessFile} interface, and limitations are dependent on the - * semantics of the underlying randomAccessFile. - *

- * Bytes are always stored in the underlying randomAccessFile in {@link ByteOrder#BIG_ENDIAN} order. - * To flip the byte order to read or write to/from a randomAccessFile in {@link ByteOrder#LITTLE_ENDIAN} order use - * {@link Bytes#order(ByteOrder)}. - * - * @author Jordan Halterman - */ -public class FileBytes extends AbstractBytes { - static final String DEFAULT_MODE = "rw"; - - /** - * Allocates a randomAccessFile buffer of unlimited count. - *

- * The buffer will be allocated with {@link Long#MAX_VALUE} bytes. As bytes are written to the buffer, the underlying - * {@link RandomAccessFile} will expand. - * - * @param file The randomAccessFile to allocate. - * @return The allocated buffer. - */ - public static FileBytes allocate(File file) { - return allocate(file, DEFAULT_MODE, Integer.MAX_VALUE); - } - - /** - * Allocates a randomAccessFile buffer. - *

- * If the underlying randomAccessFile is empty, the randomAccessFile count will expand dynamically as bytes are written to the randomAccessFile. - * - * @param file The randomAccessFile to allocate. - * @param size The count of the bytes to allocate. - * @return The allocated buffer. - */ - public static FileBytes allocate(File file, int size) { - return allocate(file, DEFAULT_MODE, size); - } - - /** - * Allocates a randomAccessFile buffer. - *

- * If the underlying randomAccessFile is empty, the randomAccessFile count will expand dynamically as bytes are written to the randomAccessFile. - * - * @param file The randomAccessFile to allocate. - * @param mode The mode in which to open the underlying {@link RandomAccessFile}. - * @param size The count of the bytes to allocate. - * @return The allocated buffer. - */ - public static FileBytes allocate(File file, String mode, int size) { - return new FileBytes(file, mode, (int) Math.min(Memory.Util.toPow2(size), Integer.MAX_VALUE)); - } - - private static final int PAGE_SIZE = 1024 * 4; - private static final byte[] BLANK_PAGE = new byte[PAGE_SIZE]; - - private final File file; - private final String mode; - private final RandomAccessFile randomAccessFile; - private int size; - - FileBytes(File file, String mode, int size) { - if (file == null) { - throw new NullPointerException("file cannot be null"); - } - if (mode == null) { - mode = DEFAULT_MODE; - } - if (size < 0) { - throw new IllegalArgumentException("size must be positive"); - } - - this.file = file; - this.mode = mode; - this.size = size; - try { - this.randomAccessFile = new RandomAccessFile(file, mode); - if (size > randomAccessFile.length()) { - randomAccessFile.setLength(size); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - /** - * Returns the underlying file object. - * - * @return The underlying file. - */ - public File file() { - return file; - } - - /** - * Returns the file mode. - * - * @return The file mode. - */ - public String mode() { - return mode; - } - - @Override - public int size() { - return size; - } - - @Override - public Bytes resize(int newSize) { - if (newSize < size) { - throw new IllegalArgumentException("cannot decrease file bytes size; use zero() to decrease file size"); - } - int oldSize = this.size; - this.size = newSize; - try { - long length = randomAccessFile.length(); - if (newSize > length) { - randomAccessFile.setLength(newSize); - zero(oldSize); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public boolean isFile() { - return true; - } - - /** - * Maps a portion of the randomAccessFile into memory in {@link FileChannel.MapMode#READ_WRITE} mode and returns - * a {@link MappedBytes} instance. - * - * @param offset The offset from which to map the randomAccessFile into memory. - * @param size The count of the bytes to map into memory. - * @return The mapped bytes. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed - * {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE} - */ - public MappedBytes map(int offset, int size) { - return map(offset, size, parseMode(mode)); - } - - /** - * Maps a portion of the randomAccessFile into memory and returns a {@link MappedBytes} instance. - * - * @param offset The offset from which to map the randomAccessFile into memory. - * @param size The count of the bytes to map into memory. - * @param mode The mode in which to map the randomAccessFile into memory. - * @return The mapped bytes. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed - * {@link java.nio.MappedByteBuffer} count: {@link Integer#MAX_VALUE} - */ - public MappedBytes map(int offset, int size, FileChannel.MapMode mode) { - MappedByteBuffer mappedByteBuffer = mapFile(randomAccessFile, offset, size, mode); - return new MappedBytes(file, randomAccessFile, mappedByteBuffer, mode); - } - - private static MappedByteBuffer mapFile(RandomAccessFile randomAccessFile, int offset, int size, FileChannel.MapMode mode) { - try { - return randomAccessFile.getChannel().map(mode, offset, size); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static FileChannel.MapMode parseMode(String mode) { - switch (mode) { - case "r": - return FileChannel.MapMode.READ_ONLY; - case "rw": - default: - return FileChannel.MapMode.READ_WRITE; - } - } - - @Override - public ByteOrder order() { - return ByteOrder.BIG_ENDIAN; - } - - /** - * Seeks to the given offset. - */ - private void seekToOffset(int offset) throws IOException { - if (randomAccessFile.getFilePointer() != offset) { - randomAccessFile.seek(offset); - } - } - - @Override - public Bytes zero() { - try { - randomAccessFile.setLength(0); - randomAccessFile.setLength(size); - for (int i = 0; i < size; i += PAGE_SIZE) { - randomAccessFile.write(BLANK_PAGE, 0, Math.min(size - i, PAGE_SIZE)); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes zero(int offset) { - try { - int length = Math.max(offset, size); - randomAccessFile.setLength(offset); - randomAccessFile.setLength(length); - seekToOffset(offset); - for (int i = offset; i < length; i += PAGE_SIZE) { - randomAccessFile.write(BLANK_PAGE, 0, Math.min(length - i, PAGE_SIZE)); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes zero(int offset, int length) { - for (int i = offset; i < offset + length; i++) { - writeByte(i, (byte) 0); - } - return this; - } - - @Override - public Bytes read(int position, Bytes bytes, int offset, int length) { - checkRead(position, length); - if (bytes instanceof WrappedBytes) { - bytes = ((WrappedBytes) bytes).root(); - } - if (bytes.hasArray()) { - try { - seekToOffset(position); - randomAccessFile.readFully(bytes.array(), (int) offset, (int) length); - } catch (IOException e) { - throw new RuntimeException(e); - } - } else { - try { - seekToOffset(position); - byte[] readBytes = new byte[(int) length]; - randomAccessFile.readFully(readBytes); - bytes.write(offset, readBytes, 0, length); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - return this; - } - - @Override - public Bytes read(int position, byte[] bytes, int offset, int length) { - checkRead(position, length); - try { - seekToOffset(position); - randomAccessFile.readFully(bytes, (int) offset, (int) length); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public int readByte(int offset) { - checkRead(offset, BYTE); - try { - seekToOffset(offset); - return randomAccessFile.readByte(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public char readChar(int offset) { - checkRead(offset, CHARACTER); - try { - seekToOffset(offset); - return randomAccessFile.readChar(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public short readShort(int offset) { - checkRead(offset, SHORT); - try { - seekToOffset(offset); - return randomAccessFile.readShort(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public int readInt(int offset) { - checkRead(offset, INTEGER); - try { - seekToOffset(offset); - return randomAccessFile.readInt(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public long readLong(int offset) { - checkRead(offset, LONG); - try { - seekToOffset(offset); - return randomAccessFile.readLong(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public float readFloat(int offset) { - checkRead(offset, FLOAT); - try { - seekToOffset(offset); - return randomAccessFile.readFloat(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public double readDouble(int offset) { - checkRead(offset, DOUBLE); - try { - seekToOffset(offset); - return randomAccessFile.readDouble(); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public Bytes write(int position, Bytes bytes, int offset, int length) { - checkWrite(position, length); - if (bytes instanceof WrappedBytes) { - bytes = ((WrappedBytes) bytes).root(); - } - if (bytes.hasArray()) { - try { - seekToOffset(position); - randomAccessFile.write(bytes.array(), (int) offset, (int) length); - } catch (IOException e) { - throw new RuntimeException(e); - } - } else { - try { - seekToOffset(position); - byte[] writeBytes = new byte[(int) length]; - bytes.read(offset, writeBytes, 0, length); - randomAccessFile.write(writeBytes); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - return this; - } - - @Override - public Bytes write(int position, byte[] bytes, int offset, int length) { - checkWrite(position, length); - try { - seekToOffset(position); - randomAccessFile.write(bytes, (int) offset, (int) length); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeByte(int offset, int b) { - checkWrite(offset, BYTE); - try { - seekToOffset(offset); - randomAccessFile.writeByte(b); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeChar(int offset, char c) { - checkWrite(offset, CHARACTER); - try { - seekToOffset(offset); - randomAccessFile.writeChar(c); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeShort(int offset, short s) { - checkWrite(offset, SHORT); - try { - seekToOffset(offset); - randomAccessFile.writeShort(s); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeInt(int offset, int i) { - checkWrite(offset, INTEGER); - try { - seekToOffset(offset); - randomAccessFile.writeInt(i); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeLong(int offset, long l) { - checkWrite(offset, LONG); - try { - seekToOffset(offset); - randomAccessFile.writeLong(l); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeFloat(int offset, float f) { - checkWrite(offset, FLOAT); - try { - seekToOffset(offset); - randomAccessFile.writeFloat(f); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes writeDouble(int offset, double d) { - checkWrite(offset, DOUBLE); - try { - seekToOffset(offset); - randomAccessFile.writeDouble(d); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public Bytes flush() { - try { - randomAccessFile.getFD().sync(); - } catch (IOException e) { - throw new RuntimeException(e); - } - return this; - } - - @Override - public void close() { - try { - randomAccessFile.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - super.close(); - } - - /** - * Deletes the underlying file. - */ - public void delete() { - try { - close(); - Files.delete(file.toPath()); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/HeapBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/HeapBuffer.java deleted file mode 100644 index 509f7925f1..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/HeapBuffer.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.memory.Memory; - -import static com.google.common.base.Preconditions.checkArgument; - -/** - * Direct {@link java.nio.ByteBuffer} based buffer. - * - * @author Jordan Halterman - */ -public class HeapBuffer extends ByteBufferBuffer { - - /** - * Allocates a direct buffer with an initial capacity of {@code 4096} and a maximum capacity of {@link Long#MAX_VALUE}. - * - * @return The direct buffer. - * @see HeapBuffer#allocate(int) - * @see HeapBuffer#allocate(int, int) - */ - public static HeapBuffer allocate() { - return allocate(DEFAULT_INITIAL_CAPACITY, MAX_SIZE); - } - - /** - * Allocates a direct buffer with the given initial capacity. - * - * @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 HeapBuffer#allocate() - * @see HeapBuffer#allocate(int, int) - */ - public static HeapBuffer allocate(int initialCapacity) { - return allocate(initialCapacity, MAX_SIZE); - } - - /** - * Allocates a new direct buffer. - * - * @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 HeapBuffer#allocate() - * @see HeapBuffer#allocate(int) - */ - public static HeapBuffer allocate(int initialCapacity, int maxCapacity) { - checkArgument(initialCapacity <= maxCapacity, "initial capacity cannot be greater than maximum capacity"); - return new HeapBuffer(HeapBytes.allocate((int) Math.min(Memory.Util.toPow2(initialCapacity), MAX_SIZE)), 0, initialCapacity, maxCapacity); - } - - /** - * Wraps the given bytes in a heap buffer. - *

- * The buffer will be created with an initial capacity and maximum capacity equal to the byte array count. - * - * @param bytes The bytes to wrap. - * @return The wrapped bytes. - */ - public static HeapBuffer wrap(byte[] bytes) { - return new HeapBuffer(HeapBytes.wrap(bytes), 0, bytes.length, bytes.length); - } - - private final HeapBytes bytes; - - protected HeapBuffer(HeapBytes bytes, int offset, int initialCapacity, int maxCapacity) { - super(bytes, offset, initialCapacity, maxCapacity, null); - this.bytes = bytes; - } - - @Override - public boolean hasArray() { - return true; - } - - @Override - public HeapBuffer duplicate() { - return new HeapBuffer(bytes, offset(), capacity(), maxCapacity()); - } -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/HeapBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/HeapBytes.java deleted file mode 100644 index 7821c2e2b7..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/HeapBytes.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.ByteBuffer; - -/** - * {@link ByteBuffer} based heap bytes. - */ -public class HeapBytes extends ByteBufferBytes { - public static final byte[] EMPTY = new byte[0]; - - /** - * Allocates a new heap byte array. - * - * @param size The count of the buffer to allocate (in bytes). - * @return The heap buffer. - * @throws IllegalArgumentException If {@code count} is greater than the maximum allowed count for - * an array on the Java heap - {@code Integer.MAX_VALUE - 5} - */ - public static HeapBytes allocate(int size) { - if (size > MAX_SIZE) { - throw new IllegalArgumentException("size cannot for HeapBytes cannot be greater than " + MAX_SIZE); - } - return new HeapBytes(ByteBuffer.allocate((int) size)); - } - - /** - * Wraps the given bytes in a {@link HeapBytes} object. - *

- * The returned {@link Bytes} object will be backed by a {@link ByteBuffer} instance that - * wraps the given byte array. The {@link Bytes#size()} will be equivalent to the provided - * by array {@code length}. - * - * @param bytes The bytes to wrap. - */ - public static HeapBytes wrap(byte[] bytes) { - return new HeapBytes(ByteBuffer.wrap(bytes)); - } - - protected HeapBytes(ByteBuffer buffer) { - super(buffer); - } - - @Override - protected ByteBuffer newByteBuffer(int size) { - return ByteBuffer.allocate((int) size); - } - - @Override - public boolean hasArray() { - return true; - } -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/MappedBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/MappedBuffer.java deleted file mode 100644 index 87731b4ece..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/MappedBuffer.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.io.File; -import java.nio.channels.FileChannel; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * Direct {@link java.nio.ByteBuffer} based buffer. - * - * @author Jordan Halterman - */ -public class MappedBuffer extends ByteBufferBuffer { - - /** - * Allocates a dynamic capacity mapped buffer in {@link FileChannel.MapMode#READ_WRITE} mode with an initial capacity - * of {@code 16MiB} and a maximum capacity of {@link Integer#MAX_VALUE}. - *

- * The resulting buffer will have a maximum capacity of {@link Integer#MAX_VALUE}. As bytes are written to the buffer - * its capacity will double in count each time the current capacity is reached. Memory will be mapped by opening and - * expanding the given {@link File} to the desired {@code capacity} and mapping the file contents into memory via - * {@link FileChannel#map(FileChannel.MapMode, long, long)}. - * - * @param file The file to map into memory. - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @see #allocate(File, FileChannel.MapMode) - * @see #allocate(File, int) - * @see #allocate(File, FileChannel.MapMode, int) - * @see #allocate(File, int, int) - * @see #allocate(File, FileChannel.MapMode, int, int) - */ - public static MappedBuffer allocate(File file) { - return allocate(file, FileChannel.MapMode.READ_WRITE, DEFAULT_INITIAL_CAPACITY, Integer.MAX_VALUE); - } - - /** - * Allocates a dynamic capacity mapped buffer in {@link FileChannel.MapMode#READ_WRITE} mode with an initial capacity - * of {@code 16MiB} and a maximum capacity of {@link Integer#MAX_VALUE}. - *

- * The resulting buffer will be initialized to a capacity of {@code 4096} and have a maximum capacity of - * {@link Integer#MAX_VALUE}. As bytes are written to the buffer its capacity will double in count each time the current - * capacity is reached. Memory will be mapped by opening and expanding the given {@link File} to the desired - * {@code capacity} and mapping the file contents into memory via - * {@link FileChannel#map(FileChannel.MapMode, long, long)}. - * - * @param file The file to map into memory. - * @param mode The mode with which to map the file. - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @see #allocate(File) - * @see #allocate(File, int) - * @see #allocate(File, FileChannel.MapMode, int) - * @see #allocate(File, int, int) - * @see #allocate(File, FileChannel.MapMode, int, int) - */ - public static MappedBuffer allocate(File file, FileChannel.MapMode mode) { - return allocate(file, mode, DEFAULT_INITIAL_CAPACITY, Integer.MAX_VALUE); - } - - /** - * Allocates a fixed capacity mapped buffer in {@link FileChannel.MapMode#READ_WRITE} mode. - *

- * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code capacity} and mapping the - * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}. - *

- * The resulting buffer will have a capacity of {@code capacity}. The underlying {@link MappedBytes} will be - * initialized to the next power of {@code 2}. - * - * @param file The file to map into memory. - * @param capacity The fixed capacity of the buffer to allocate (in bytes). - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @throws IllegalArgumentException If the {@code capacity} is greater than {@link Integer#MAX_VALUE}. - * @see #allocate(File) - * @see #allocate(File, FileChannel.MapMode) - * @see #allocate(File, FileChannel.MapMode, int) - * @see #allocate(File, int, int) - * @see #allocate(File, FileChannel.MapMode, int, int) - */ - public static MappedBuffer allocate(File file, int capacity) { - return allocate(file, FileChannel.MapMode.READ_WRITE, capacity, capacity); - } - - /** - * Allocates a fixed capacity mapped buffer in the given {@link FileChannel.MapMode}. - *

- * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code capacity} and mapping the - * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}. - *

- * The resulting buffer will have a capacity of {@code capacity}. The underlying {@link MappedBytes} will be - * initialized to the next power of {@code 2}. - * - * @param file The file to map into memory. - * @param mode The mode with which to map the file. - * @param capacity The fixed capacity of the buffer to allocate (in bytes). - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @throws IllegalArgumentException If the {@code capacity} is greater than {@link Integer#MAX_VALUE}. - * @see #allocate(File) - * @see #allocate(File, FileChannel.MapMode) - * @see #allocate(File, int) - * @see #allocate(File, int, int) - * @see #allocate(File, FileChannel.MapMode, int, int) - */ - public static MappedBuffer allocate(File file, FileChannel.MapMode mode, int capacity) { - return allocate(file, mode, capacity, capacity); - } - - /** - * Allocates a mapped buffer. - *

- * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code count} and mapping the - * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}. - *

- * The resulting buffer will have a capacity of {@code initialCapacity}. The underlying {@link MappedBytes} will be - * initialized to the next power of {@code 2}. As bytes are written to the buffer, the buffer's capacity will double - * as int as {@code maxCapacity > capacity}. - * - * @param file The file to map into memory. If the file doesn't exist it will be automatically created. - * @param initialCapacity The initial capacity of the buffer. - * @param maxCapacity The maximum capacity of the buffer. - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @throws IllegalArgumentException If the {@code capacity} or {@code maxCapacity} is greater than - * {@link Integer#MAX_VALUE}. - * @see #allocate(File) - * @see #allocate(File, FileChannel.MapMode) - * @see #allocate(File, int) - * @see #allocate(File, FileChannel.MapMode, int) - * @see #allocate(File, FileChannel.MapMode, int, int) - */ - public static MappedBuffer allocate(File file, int initialCapacity, int maxCapacity) { - return allocate(file, FileChannel.MapMode.READ_WRITE, initialCapacity, maxCapacity); - } - - /** - * Allocates a mapped buffer. - *

- * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code count} and mapping the - * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}. - *

- * The resulting buffer will have a capacity of {@code initialCapacity}. The underlying {@link MappedBytes} will be - * initialized to the next power of {@code 2}. As bytes are written to the buffer, the buffer's capacity will double - * as int as {@code maxCapacity > capacity}. - * - * @param file The file to map into memory. If the file doesn't exist it will be automatically created. - * @param mode The mode with which to map the file. - * @param initialCapacity The initial capacity of the buffer. - * @param maxCapacity The maximum capacity of the buffer. - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @throws IllegalArgumentException If the {@code capacity} or {@code maxCapacity} is greater than - * {@link Integer#MAX_VALUE}. - * @see #allocate(File) - * @see #allocate(File, FileChannel.MapMode) - * @see #allocate(File, int) - * @see #allocate(File, FileChannel.MapMode, int) - * @see #allocate(File, int, int) - */ - public static MappedBuffer allocate(File file, FileChannel.MapMode mode, int initialCapacity, int maxCapacity) { - checkNotNull(file, "file cannot be null"); - checkNotNull(mode, "mode cannot be null"); - checkArgument(initialCapacity <= maxCapacity, "initial capacity cannot be greater than maximum capacity"); - return new MappedBuffer(MappedBytes.allocate(file, mode, initialCapacity), 0, initialCapacity, maxCapacity); - } - - protected MappedBuffer(MappedBytes bytes, int offset, int initialCapacity, int maxCapacity) { - super(bytes, offset, initialCapacity, maxCapacity, null); - } - - @Override - public MappedBuffer duplicate() { - return new MappedBuffer((MappedBytes) bytes, offset(), capacity(), maxCapacity()); - } - - /** - * Deletes the underlying file. - */ - public void delete() { - ((MappedBytes) bytes).delete(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/MappedBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/MappedBytes.java deleted file mode 100644 index 9f4edbf6e4..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/MappedBytes.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.memory.BufferCleaner; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.ByteBuffer; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.file.Files; - -/** - * {@link ByteBuffer} based mapped bytes. - */ -public class MappedBytes extends ByteBufferBytes { - - private static final Logger LOGGER = LoggerFactory.getLogger(MappedBytes.class); - - /** - * Allocates a mapped buffer in {@link FileChannel.MapMode#READ_WRITE} mode. - *

- * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code count} and mapping the - * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}. - * - * @param file The file to map into memory. If the file doesn't exist it will be automatically created. - * @param size The count of the buffer to allocate (in bytes). - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @throws IllegalArgumentException If {@code count} is greater than {@link MappedBytes#MAX_SIZE} - * @see #allocate(File, FileChannel.MapMode, int) - */ - public static MappedBytes allocate(File file, int size) { - return allocate(file, FileChannel.MapMode.READ_WRITE, size); - } - - /** - * Allocates a mapped buffer. - *

- * Memory will be mapped by opening and expanding the given {@link File} to the desired {@code count} and mapping the - * file contents into memory via {@link FileChannel#map(FileChannel.MapMode, long, long)}. - * - * @param file The file to map into memory. If the file doesn't exist it will be automatically created. - * @param mode The mode with which to map the file. - * @param size The count of the buffer to allocate (in bytes). - * @return The mapped buffer. - * @throws NullPointerException If {@code file} is {@code null} - * @throws IllegalArgumentException If {@code count} is greater than {@link Integer#MAX_VALUE} - * @see #allocate(File, int) - */ - public static MappedBytes allocate(File file, FileChannel.MapMode mode, int size) { - return FileBytes.allocate(file, size).map(0, size, mode); - } - - private final File file; - private final RandomAccessFile randomAccessFile; - private final FileChannel.MapMode mode; - - protected MappedBytes(File file, RandomAccessFile randomAccessFile, MappedByteBuffer buffer, FileChannel.MapMode mode) { - super(buffer); - this.file = file; - this.randomAccessFile = randomAccessFile; - this.mode = mode; - } - - @Override - protected ByteBuffer newByteBuffer(int size) { - try { - return randomAccessFile.getChannel().map(mode, 0, size); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - @Override - public boolean isDirect() { - return true; - } - - @Override - public Bytes flush() { - ((MappedByteBuffer) buffer).force(); - return this; - } - - @Override - public void close() { - try { - BufferCleaner.freeBuffer(buffer); - } catch (Exception e) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Failed to unmap direct buffer", e); - } - } - try { - randomAccessFile.close(); - } catch (IOException e) { - throw new RuntimeException(e); - } - super.close(); - } - - /** - * Deletes the underlying file. - */ - public void delete() { - try { - close(); - Files.delete(file.toPath()); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static String parseMode(FileChannel.MapMode mode) { - if (mode == FileChannel.MapMode.READ_ONLY) { - return "r"; - } else if (mode == FileChannel.MapMode.READ_WRITE) { - return "rw"; - } - throw new IllegalArgumentException("unsupported map mode"); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/PooledAllocator.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/PooledAllocator.java deleted file mode 100644 index 8dbe978f18..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/PooledAllocator.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferencePool; - -/** - * Pooled buffer allocator. - * - * @author Jordan Halterman - */ -public abstract class PooledAllocator implements BufferAllocator { - private final ReferencePool pool; - - protected PooledAllocator(ReferencePool pool) { - this.pool = pool; - } - - /** - * Returns the maximum buffer capacity. - * - * @return The maximum buffer capacity. - */ - protected abstract int maxCapacity(); - - @Override - public Buffer allocate() { - return allocate(4096, maxCapacity()); - } - - @Override - public Buffer allocate(int capacity) { - return allocate(capacity, maxCapacity()); - } - - @Override - public Buffer allocate(int initialCapacity, int maxCapacity) { - return pool.acquire().reset(0, initialCapacity, maxCapacity).clear(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ReadOnlyBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ReadOnlyBuffer.java deleted file mode 100644 index 5378b6873c..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/ReadOnlyBuffer.java +++ /dev/null @@ -1,271 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferenceManager; - -import java.nio.ReadOnlyBufferException; - -/** - * Read-only buffer. - * - * @author Jordan Halterman - */ -public class ReadOnlyBuffer extends AbstractBuffer { - private final Buffer root; - - public ReadOnlyBuffer(Buffer buffer, ReferenceManager referenceManager) { - super(buffer.bytes(), referenceManager); - this.root = buffer; - } - - @Override - public boolean isDirect() { - return root.isDirect(); - } - - @Override - public boolean isFile() { - return root.isFile(); - } - - @Override - public boolean isReadOnly() { - return true; - } - - @Override - public Buffer compact() { - throw new ReadOnlyBufferException(); - } - - @Override - protected void compact(int from, int to, int length) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer duplicate() { - return new ReadOnlyBuffer(root, referenceManager); - } - - @Override - public Buffer acquire() { - root.acquire(); - return this; - } - - @Override - public boolean release() { - return root.release(); - } - - @Override - public Buffer zero(int offset, int length) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer zero(int offset) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer zero() { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeBoolean(int offset, boolean b) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(Buffer buffer) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(Bytes bytes) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(Bytes bytes, int offset, int length) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(int offset, Bytes bytes, int srcOffset, int length) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(byte[] bytes) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(byte[] bytes, int offset, int length) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer write(int offset, byte[] bytes, int srcOffset, int length) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeByte(int b) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeByte(int offset, int b) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedByte(int b) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedByte(int offset, int b) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeChar(char c) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeChar(int offset, char c) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeShort(short s) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeShort(int offset, short s) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedShort(int s) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedShort(int offset, int s) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeMedium(int m) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeMedium(int offset, int m) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedMedium(int m) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedMedium(int offset, int m) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeInt(int i) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeInt(int offset, int i) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedInt(long i) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUnsignedInt(int offset, long i) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeLong(long l) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeLong(int offset, long l) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeFloat(float f) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeFloat(int offset, float f) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeDouble(double d) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeDouble(int offset, double d) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeBoolean(boolean b) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer writeUTF8(String s) { - throw new ReadOnlyBufferException(); - } - - @Override - public Buffer flush() { - throw new ReadOnlyBufferException(); - } - - @Override - public void close() { - root.release(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SlicedBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SlicedBuffer.java deleted file mode 100644 index fe34f2562c..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SlicedBuffer.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -/** - * Sliced buffer. - *

- * The sliced buffer provides a view of a subset of an underlying buffer. This buffer operates directly on the {@link Bytes} - * underlying the child {@link Buffer} instance. - * - * @author Jordan Halterman - */ -public class SlicedBuffer extends AbstractBuffer { - private final Buffer root; - - public SlicedBuffer(Buffer root, Bytes bytes, int offset, int initialCapacity, int maxCapacity) { - super(bytes, offset, initialCapacity, maxCapacity, null); - this.root = root; - root.acquire(); - } - - /** - * Returns the root buffer. - * - * @return The root buffer. - */ - public Buffer root() { - return root; - } - - @Override - public boolean isDirect() { - return root.isDirect(); - } - - @Override - protected void compact(int from, int to, int length) { - if (root instanceof AbstractBuffer) { - ((AbstractBuffer) root).compact(from, to, length); - } - } - - @Override - public boolean isFile() { - return root.isFile(); - } - - @Override - public boolean isReadOnly() { - return root.isReadOnly(); - } - - @Override - public Buffer compact() { - return null; - } - - @Override - public Buffer duplicate() { - return new SlicedBuffer(root, bytes, offset(), capacity(), maxCapacity()); - } - - @Override - public Buffer acquire() { - root.acquire(); - return this; - } - - @Override - public boolean release() { - return root.release(); - } - - @Override - public void close() { - root.release(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SwappedBuffer.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SwappedBuffer.java deleted file mode 100644 index 3b30a61210..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SwappedBuffer.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import io.atomix.utils.concurrent.ReferenceManager; - -import java.nio.ByteOrder; - -/** - * Byte order swapped buffer. - * - * @author Jordan Halterman - */ -public class SwappedBuffer extends AbstractBuffer { - private final Buffer root; - - SwappedBuffer(Buffer root, Bytes bytes, ReferenceManager referenceManager) { - super(bytes, referenceManager); - this.root = root; - } - - public SwappedBuffer(Buffer buffer, int offset, int initialCapacity, int maxCapacity, ReferenceManager referenceManager) { - super(buffer.bytes().order(buffer.order() == ByteOrder.BIG_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN), offset, initialCapacity, maxCapacity, referenceManager); - this.root = buffer instanceof SwappedBuffer ? ((SwappedBuffer) buffer).root : buffer; - root.acquire(); - } - - /** - * Returns the root buffer. - * - * @return The root buffer. - */ - public Buffer root() { - return root; - } - - @Override - public boolean isDirect() { - return root.isDirect(); - } - - @Override - public boolean isFile() { - return root.isFile(); - } - - @Override - public boolean isReadOnly() { - return root.isReadOnly(); - } - - @Override - protected void compact(int from, int to, int length) { - if (root instanceof AbstractBuffer) { - ((AbstractBuffer) root).compact(from, to, length); - } - } - - @Override - public Buffer duplicate() { - return new SwappedBuffer(root, offset(), capacity(), maxCapacity(), referenceManager); - } - - @Override - public Buffer acquire() { - root.acquire(); - return this; - } - - @Override - public boolean release() { - return root.release(); - } - - @Override - public void close() { - root.release(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SwappedBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SwappedBytes.java deleted file mode 100644 index 2bcda70f9a..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/SwappedBytes.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.ByteOrder; - -/** - * Bytes in swapped order. - * - * @author Jordan Halterman - */ -public class SwappedBytes extends WrappedBytes { - - public SwappedBytes(Bytes bytes) { - super(bytes); - } - - @Override - public ByteOrder order() { - return bytes.order() == ByteOrder.BIG_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN; - } - - @Override - public char readChar(int offset) { - return Character.reverseBytes(bytes.readChar(offset)); - } - - @Override - public short readShort(int offset) { - return Short.reverseBytes(bytes.readShort(offset)); - } - - @Override - public int readUnsignedShort(int offset) { - return Short.reverseBytes(bytes.readShort(offset)) & 0xFFFF; - } - - @Override - public int readMedium(int offset) { - return Integer.reverseBytes(bytes.readMedium(offset)); - } - - @Override - public int readUnsignedMedium(int offset) { - return Integer.reverseBytes(bytes.readUnsignedMedium(offset)); - } - - @Override - public int readInt(int offset) { - return Integer.reverseBytes(bytes.readInt(offset)); - } - - @Override - public long readUnsignedInt(int offset) { - return Integer.reverseBytes(bytes.readInt(offset)) & 0xFFFFFFFFL; - } - - @Override - public long readLong(int offset) { - return Long.reverseBytes(bytes.readLong(offset)); - } - - @Override - public float readFloat(int offset) { - return Float.intBitsToFloat(readInt(offset)); - } - - @Override - public double readDouble(int offset) { - return Double.longBitsToDouble(readLong(offset)); - } - - @Override - public Bytes writeChar(int offset, char c) { - bytes.writeChar(offset, Character.reverseBytes(c)); - return this; - } - - @Override - public Bytes writeShort(int offset, short s) { - bytes.writeShort(offset, Short.reverseBytes(s)); - return this; - } - - @Override - public Bytes writeUnsignedShort(int offset, int s) { - bytes.writeUnsignedShort(offset, Short.reverseBytes((short) s)); - return this; - } - - @Override - public Bytes writeMedium(int offset, int m) { - bytes.writeMedium(offset, Integer.reverseBytes(m)); - return this; - } - - @Override - public Bytes writeUnsignedMedium(int offset, int m) { - bytes.writeUnsignedMedium(offset, Integer.reverseBytes(m)); - return this; - } - - @Override - public Bytes writeInt(int offset, int i) { - bytes.writeInt(offset, Integer.reverseBytes(i)); - return this; - } - - @Override - public Bytes writeUnsignedInt(int offset, long i) { - bytes.writeUnsignedInt(offset, Integer.reverseBytes((int) i)); - return this; - } - - @Override - public Bytes writeLong(int offset, long l) { - bytes.writeLong(offset, Long.reverseBytes(l)); - return this; - } - - @Override - public Bytes writeFloat(int offset, float f) { - return writeInt(offset, Float.floatToRawIntBits(f)); - } - - @Override - public Bytes writeDouble(int offset, double d) { - return writeLong(offset, Double.doubleToRawLongBits(d)); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledAllocator.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledAllocator.java deleted file mode 100644 index 6a2ccab56c..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledAllocator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -/** - * Unpooled buffer allocator. - * - * @author Jordan Halterman - */ -public abstract class UnpooledAllocator implements BufferAllocator { - - /** - * Returns the maximum buffer capacity. - * - * @return The maximum buffer capacity. - */ - protected abstract int maxCapacity(); - - @Override - public Buffer allocate() { - return allocate(4096, maxCapacity()); - } - - @Override - public Buffer allocate(int capacity) { - return allocate(capacity, capacity); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledDirectAllocator.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledDirectAllocator.java deleted file mode 100644 index ada468f10e..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledDirectAllocator.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -/** - * Unpooled direct allocator. - * - * @author Jordan Halterman - */ -public class UnpooledDirectAllocator extends UnpooledAllocator { - - @Override - public Buffer allocate(int initialCapacity, int maxCapacity) { - return DirectBuffer.allocate(initialCapacity, maxCapacity); - } - - @Override - protected int maxCapacity() { - return Integer.MAX_VALUE; - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledHeapAllocator.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledHeapAllocator.java deleted file mode 100644 index fe90415ddc..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/UnpooledHeapAllocator.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -/** - * Unpooled heap allocator. - * - * @author Jordan Halterman - */ -public class UnpooledHeapAllocator extends UnpooledAllocator { - - @Override - protected int maxCapacity() { - return HeapBuffer.MAX_SIZE; - } - - @Override - public Buffer allocate(int initialCapacity, int maxCapacity) { - return HeapBuffer.allocate(initialCapacity, maxCapacity); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/WrappedBytes.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/WrappedBytes.java deleted file mode 100644 index 474f0d3e6b..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/WrappedBytes.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.nio.ByteOrder; - -/** - * Wrapped bytes. - * - * @author Jordan Halterman - */ -public class WrappedBytes extends AbstractBytes { - protected final Bytes bytes; - private final Bytes root; - - public WrappedBytes(Bytes bytes) { - if (bytes == null) { - throw new NullPointerException("bytes cannot be null"); - } - this.bytes = bytes; - this.root = bytes instanceof WrappedBytes ? ((WrappedBytes) bytes).root : bytes; - } - - /** - * Returns the root bytes. - */ - public Bytes root() { - return root; - } - - @Override - public int size() { - return bytes.size(); - } - - @Override - public Bytes resize(int newSize) { - return bytes.resize(newSize); - } - - @Override - public ByteOrder order() { - return bytes.order(); - } - - @Override - public Bytes zero() { - bytes.zero(); - return this; - } - - @Override - public Bytes zero(int offset) { - bytes.zero(offset); - return this; - } - - @Override - public Bytes zero(int offset, int length) { - bytes.zero(offset, length); - return this; - } - - @Override - public Bytes read(int offset, Bytes dst, int dstOffset, int length) { - bytes.read(offset, dst, dstOffset, length); - return this; - } - - @Override - public Bytes read(int offset, byte[] dst, int dstOffset, int length) { - bytes.read(offset, dst, dstOffset, length); - return this; - } - - @Override - public int readByte(int offset) { - return bytes.readByte(offset); - } - - @Override - public int readUnsignedByte(int offset) { - return bytes.readUnsignedByte(offset); - } - - @Override - public char readChar(int offset) { - return bytes.readChar(offset); - } - - @Override - public short readShort(int offset) { - return bytes.readShort(offset); - } - - @Override - public int readUnsignedShort(int offset) { - return bytes.readUnsignedShort(offset); - } - - @Override - public int readMedium(int offset) { - return bytes.readMedium(offset); - } - - @Override - public int readUnsignedMedium(int offset) { - return bytes.readUnsignedMedium(offset); - } - - @Override - public int readInt(int offset) { - return bytes.readInt(offset); - } - - @Override - public long readUnsignedInt(int offset) { - return bytes.readUnsignedInt(offset); - } - - @Override - public long readLong(int offset) { - return bytes.readLong(offset); - } - - @Override - public float readFloat(int offset) { - return bytes.readFloat(offset); - } - - @Override - public double readDouble(int offset) { - return bytes.readDouble(offset); - } - - @Override - public boolean readBoolean(int offset) { - return bytes.readBoolean(offset); - } - - @Override - public String readString(int offset) { - return bytes.readString(offset); - } - - @Override - public String readUTF8(int offset) { - return bytes.readUTF8(offset); - } - - @Override - public Bytes write(int offset, Bytes src, int srcOffset, int length) { - bytes.write(offset, src, srcOffset, length); - return this; - } - - @Override - public Bytes write(int offset, byte[] src, int srcOffset, int length) { - bytes.write(offset, src, srcOffset, length); - return this; - } - - @Override - public Bytes writeByte(int offset, int b) { - bytes.writeByte(offset, b); - return this; - } - - @Override - public Bytes writeUnsignedByte(int offset, int b) { - bytes.writeUnsignedByte(offset, b); - return this; - } - - @Override - public Bytes writeChar(int offset, char c) { - bytes.writeChar(offset, c); - return this; - } - - @Override - public Bytes writeShort(int offset, short s) { - bytes.writeShort(offset, s); - return this; - } - - @Override - public Bytes writeUnsignedShort(int offset, int s) { - bytes.writeUnsignedShort(offset, s); - return this; - } - - @Override - public Bytes writeMedium(int offset, int m) { - bytes.writeMedium(offset, m); - return this; - } - - @Override - public Bytes writeUnsignedMedium(int offset, int m) { - bytes.writeUnsignedMedium(offset, m); - return this; - } - - @Override - public Bytes writeInt(int offset, int i) { - bytes.writeInt(offset, i); - return this; - } - - @Override - public Bytes writeUnsignedInt(int offset, long i) { - bytes.writeUnsignedInt(offset, i); - return this; - } - - @Override - public Bytes writeLong(int offset, long l) { - bytes.writeLong(offset, l); - return this; - } - - @Override - public Bytes writeFloat(int offset, float f) { - bytes.writeFloat(offset, f); - return this; - } - - @Override - public Bytes writeDouble(int offset, double d) { - bytes.writeDouble(offset, d); - return this; - } - - @Override - public Bytes writeBoolean(int offset, boolean b) { - bytes.writeBoolean(offset, b); - return this; - } - - @Override - public Bytes writeString(int offset, String s) { - bytes.writeString(offset, s); - return this; - } - - @Override - public Bytes writeUTF8(int offset, String s) { - bytes.writeUTF8(offset, s); - return this; - } - - @Override - public Bytes flush() { - bytes.flush(); - return this; - } - - @Override - public void close() { - bytes.close(); - } - -} diff --git a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/package-info.java b/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/package-info.java deleted file mode 100644 index df126075c4..0000000000 --- a/third-party/atomix/storage/src/main/java/io/atomix/storage/buffer/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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. - */ - -/** - * Provides a low-level {@link io.atomix.storage.buffer.Buffer} abstraction backed by on- or off-heap memory, memory mapped - * files, or {@link java.io.RandomAccessFile}. - */ -package io.atomix.storage.buffer; diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/BufferTest.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/BufferTest.java deleted file mode 100644 index cfa025b9a9..0000000000 --- a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/BufferTest.java +++ /dev/null @@ -1,674 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import org.junit.Test; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteOrder; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Base buffer test. - * - * @author Jordan Halterman - */ -public abstract class BufferTest { - - /** - * Creates a new test buffer. - */ - protected abstract Buffer createBuffer(int capacity); - - /** - * Creates a new test buffer. - */ - protected abstract Buffer createBuffer(int capacity, int maxCapacity); - - @Test - public void testPosition() { - Buffer buffer = createBuffer(8); - assertEquals(0, buffer.position()); - buffer.writeInt(10); - assertEquals(4, buffer.position()); - buffer.position(0); - assertEquals(0, buffer.position()); - assertEquals(10, buffer.readInt()); - } - - @Test - public void testFlip() { - Buffer buffer = createBuffer(8); - buffer.writeInt(10); - assertEquals(4, buffer.position()); - assertEquals(8, buffer.capacity()); - assertEquals(-1, buffer.limit()); - assertEquals(8, buffer.capacity()); - buffer.flip(); - assertEquals(4, buffer.limit()); - assertEquals(0, buffer.position()); - } - - @Test - public void testLimit() { - Buffer buffer = createBuffer(8); - assertEquals(0, buffer.position()); - assertEquals(-1, buffer.limit()); - assertEquals(8, buffer.capacity()); - buffer.limit(4); - assertEquals(4, buffer.limit()); - assertTrue(buffer.hasRemaining()); - buffer.writeInt(10); - assertEquals(0, buffer.remaining()); - assertFalse(buffer.hasRemaining()); - } - - @Test - public void testClear() { - Buffer buffer = createBuffer(8); - buffer.limit(6); - assertEquals(6, buffer.limit()); - buffer.writeInt(10); - assertEquals(4, buffer.position()); - buffer.clear(); - assertEquals(-1, buffer.limit()); - assertEquals(8, buffer.capacity()); - assertEquals(0, buffer.position()); - } - - @Test - public void testMarkReset() { - assertTrue(createBuffer(12).writeInt(10).mark().writeBoolean(true).reset().readBoolean()); - } - - @Test(expected = BufferUnderflowException.class) - public void testReadIntThrowsBufferUnderflowWithNoRemainingBytesRelative() { - createBuffer(4, 4) - .writeInt(10) - .readInt(); - } - - @Test(expected = BufferUnderflowException.class) - public void testReadIntThrowsBufferUnderflowWithNoRemainingBytesAbsolute() { - createBuffer(4, 4).readInt(2); - } - - @Test(expected = BufferOverflowException.class) - public void testWriteIntThrowsBufferOverflowWithNoRemainingBytesRelative() { - createBuffer(4, 4).writeInt(10).writeInt(20); - } - - @Test(expected = BufferOverflowException.class) - public void testReadIntThrowsBufferOverflowWithNoRemainingBytesAbsolute() { - createBuffer(4, 4).writeInt(4, 10); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testReadIntThrowsIndexOutOfBounds() { - createBuffer(4, 4).readInt(10); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testWriteIntThrowsIndexOutOfBounds() { - createBuffer(4, 4).writeInt(10, 10); - } - - @Test - public void testWriteReadByteRelative() { - assertEquals(10, createBuffer(16).writeByte(10).flip().readByte()); - } - - @Test - public void testWriteReadByteAbsolute() { - assertEquals(10, createBuffer(16).writeByte(4, 10).readByte(4)); - } - - @Test - public void testWriteReadUnsignedByteRelative() { - assertEquals(10, createBuffer(16).writeUnsignedByte(10).flip().readUnsignedByte()); - } - - @Test - public void testWriteReadUnsignedByteAbsolute() { - assertEquals(10, createBuffer(16).writeUnsignedByte(4, 10).readUnsignedByte(4)); - } - - @Test - public void testWriteReadShortRelative() { - assertEquals(10, createBuffer(16).writeShort((short) 10).flip().readShort()); - } - - @Test - public void testWriteReadShortAbsolute() { - assertEquals(10, createBuffer(16).writeShort(4, (short) 10).readShort(4)); - } - - @Test - public void testWriteReadUnsignedShortRelative() { - assertEquals(10, createBuffer(16).writeUnsignedShort((short) 10).flip().readUnsignedShort()); - } - - @Test - public void testWriteReadUnsignedShortAbsolute() { - assertEquals(10, createBuffer(16).writeUnsignedShort(4, (short) 10).readUnsignedShort(4)); - } - - @Test - public void testWriteReadIntRelative() { - assertEquals(10, createBuffer(16).writeInt(10).flip().readInt()); - } - - @Test - public void testWriteReadUnsignedIntAbsolute() { - assertEquals(10, createBuffer(16).writeUnsignedInt(4, 10).readUnsignedInt(4)); - } - - @Test - public void testWriteReadUnsignedIntRelative() { - assertEquals(10, createBuffer(16).writeUnsignedInt(10).flip().readUnsignedInt()); - } - - @Test - public void testWriteReadIntAbsolute() { - assertEquals(10, createBuffer(16).writeInt(4, 10).readInt(4)); - } - - @Test - public void testWriteReadLongRelative() { - assertEquals(12345, createBuffer(16).writeLong(12345).flip().readLong()); - } - - @Test - public void testWriteReadLongAbsolute() { - assertEquals(12345, createBuffer(16).writeLong(4, 12345).readLong(4)); - } - - @Test - public void testWriteReadFloatRelative() { - assertEquals(10.6f, createBuffer(16).writeFloat(10.6f).flip().readFloat(), .001); - } - - @Test - public void testWriteReadFloatAbsolute() { - assertEquals(10.6f, createBuffer(16).writeFloat(4, 10.6f).readFloat(4), .001); - } - - @Test - public void testWriteReadDoubleRelative() { - assertEquals(10.6, createBuffer(16).writeDouble(10.6).flip().readDouble(), .001); - } - - @Test - public void testWriteReadDoubleAbsolute() { - assertEquals(10.6, createBuffer(16).writeDouble(4, 10.6).readDouble(4), .001); - } - - @Test - public void testWriteReadBooleanRelative() { - assertTrue(createBuffer(16).writeBoolean(true).flip().readBoolean()); - } - - @Test - public void testWriteReadBooleanAbsolute() { - assertTrue(createBuffer(16).writeBoolean(4, true).readBoolean(4)); - } - - @Test - public void testWriteReadStringRelative() { - Buffer buffer = createBuffer(38) - .writeString("Hello world!") - .writeString("Hello world again!") - .flip(); - assertEquals("Hello world!", buffer.readString()); - assertEquals("Hello world again!", buffer.readString()); - } - - @Test - public void testWriteReadStringAbsolute() { - Buffer buffer = createBuffer(46) - .writeString(4, "Hello world!") - .writeString(20, "Hello world again!"); - assertEquals("Hello world!", buffer.readString(4)); - assertEquals("Hello world again!", buffer.readString(20)); - } - - @Test - public void testWriteReadUTF8Relative() { - Buffer buffer = createBuffer(38) - .writeUTF8("Hello world!") - .writeUTF8("Hello world again!") - .flip(); - assertEquals("Hello world!", buffer.readUTF8()); - assertEquals("Hello world again!", buffer.readUTF8()); - } - - @Test - public void testWriteReadUTF8Absolute() { - Buffer buffer = createBuffer(46) - .writeUTF8(4, "Hello world!") - .writeUTF8(20, "Hello world again!"); - assertEquals("Hello world!", buffer.readUTF8(4)); - assertEquals("Hello world again!", buffer.readUTF8(20)); - } - - @Test - public void testReadWriter() { - Buffer writeBuffer = createBuffer(8).writeLong(10).flip(); - Buffer readBuffer = createBuffer(8); - writeBuffer.read(readBuffer); - assertEquals(10, readBuffer.flip().readLong()); - } - - @Test - public void testWriteReadSwappedIntRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeInt(10).flip().readInt()); - } - - @Test - public void testWriteReadSwappedIntAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeInt(4, 10).readInt(4)); - } - - @Test - public void testAbsoluteSlice() { - Buffer buffer = createBuffer(1024); - buffer.writeLong(10).writeLong(11).rewind(); - Buffer slice = buffer.slice(8, 1016); - assertEquals(0, slice.position()); - assertEquals(11, slice.readLong()); - } - - @Test - public void testRelativeSliceWithoutLength() { - Buffer buffer = createBuffer(1024, 1024); - buffer.writeLong(10).writeLong(11).writeLong(12).rewind(); - assertEquals(10, buffer.readLong()); - Buffer slice = buffer.slice(); - assertEquals(0, slice.position()); - assertEquals(-1, slice.limit()); - assertEquals(1016, slice.capacity()); - assertEquals(1016, slice.maxCapacity()); - assertEquals(11, slice.readLong()); - assertEquals(11, slice.readLong(0)); - slice.close(); - Buffer slice2 = buffer.skip(8).slice(); - assertEquals(0, slice2.position()); - assertEquals(-1, slice2.limit()); - assertEquals(1008, slice2.capacity()); - assertEquals(1008, slice2.maxCapacity()); - assertEquals(12, slice2.readLong()); - assertEquals(12, slice2.readLong(0)); - } - - @Test - public void testRelativeSliceWithLength() { - Buffer buffer = createBuffer(1024); - buffer.writeLong(10).writeLong(11).writeLong(12).rewind(); - assertEquals(10, buffer.readLong()); - Buffer slice = buffer.slice(8); - assertEquals(0, slice.position()); - assertEquals(11, slice.readLong()); - assertEquals(11, slice.readLong(0)); - slice.close(); - Buffer slice2 = buffer.skip(8).slice(8); - assertEquals(0, slice2.position()); - assertEquals(12, slice2.readLong()); - assertEquals(12, slice2.readLong(0)); - slice2.close(); - } - - @Test - public void testSliceOfSlice() { - Buffer buffer = createBuffer(1024); - buffer.writeLong(10).writeLong(11).writeLong(12).rewind(); - assertEquals(10, buffer.readLong()); - Buffer slice = buffer.slice(); - assertEquals(11, slice.readLong()); - Buffer sliceOfSlice = slice.slice(); - assertEquals(12, sliceOfSlice.readLong()); - assertEquals(8, sliceOfSlice.position()); - } - - @Test - public void testSliceWithLimit() { - Buffer buffer = createBuffer(1024).limit(16); - buffer.writeLong(10); - Buffer slice = buffer.slice(); - assertEquals(0, slice.position()); - assertEquals(8, slice.capacity()); - assertEquals(8, slice.maxCapacity()); - assertEquals(8, slice.remaining()); - } - - @Test - public void testSliceWithLittleRemaining() { - Buffer buffer = createBuffer(1024, 2048); - buffer.position(1020); - Buffer slice = buffer.slice(8); - assertEquals(0, slice.position()); - assertEquals(-1, slice.limit()); - } - - @Test - public void testCompact() { - Buffer buffer = createBuffer(1024); - buffer.position(100).writeLong(1234).position(100).compact(); - assertEquals(0, buffer.position()); - assertEquals(1234, buffer.readLong()); - } - - @Test - public void testSwappedPosition() { - Buffer buffer = createBuffer(8).order(ByteOrder.LITTLE_ENDIAN); - assertEquals(0, buffer.position()); - buffer.writeInt(10); - assertEquals(4, buffer.position()); - buffer.position(0); - assertEquals(0, buffer.position()); - assertEquals(10, buffer.readInt()); - } - - @Test - public void testSwappedFlip() { - Buffer buffer = createBuffer(8).order(ByteOrder.LITTLE_ENDIAN); - buffer.writeInt(10); - assertEquals(4, buffer.position()); - assertEquals(8, buffer.capacity()); - assertEquals(-1, buffer.limit()); - assertEquals(8, buffer.capacity()); - buffer.flip(); - assertEquals(4, buffer.limit()); - assertEquals(0, buffer.position()); - } - - @Test - public void testSwappedLimit() { - Buffer buffer = createBuffer(8).order(ByteOrder.LITTLE_ENDIAN); - assertEquals(0, buffer.position()); - assertEquals(-1, buffer.limit()); - assertEquals(8, buffer.capacity()); - buffer.limit(4); - assertEquals(4, buffer.limit()); - assertTrue(buffer.hasRemaining()); - buffer.writeInt(10); - assertEquals(0, buffer.remaining()); - assertFalse(buffer.hasRemaining()); - } - - @Test - public void testSwappedClear() { - Buffer buffer = createBuffer(8).order(ByteOrder.LITTLE_ENDIAN); - buffer.limit(6); - assertEquals(6, buffer.limit()); - buffer.writeInt(10); - assertEquals(4, buffer.position()); - buffer.clear(); - assertEquals(-1, buffer.limit()); - assertEquals(8, buffer.capacity()); - assertEquals(0, buffer.position()); - } - - @Test - public void testSwappedMarkReset() { - assertTrue(createBuffer(12).order(ByteOrder.LITTLE_ENDIAN).writeInt(10).mark().writeBoolean(true).reset().readBoolean()); - } - - @Test(expected = BufferUnderflowException.class) - public void testSwappedReadIntThrowsBufferUnderflowWithNoRemainingBytesRelative() { - createBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN) - .writeInt(10) - .readInt(); - } - - @Test(expected = BufferUnderflowException.class) - public void testSwappedReadIntThrowsBufferUnderflowWithNoRemainingBytesAbsolute() { - createBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN).readInt(2); - } - - @Test(expected = BufferOverflowException.class) - public void testSwappedWriteIntThrowsBufferOverflowWithNoRemainingBytesRelative() { - createBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN).writeInt(10).writeInt(20); - } - - @Test(expected = BufferOverflowException.class) - public void testSwappedReadIntThrowsBufferOverflowWithNoRemainingBytesAbsolute() { - createBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN).writeInt(4, 10); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testSwappedReadIntThrowsIndexOutOfBounds() { - createBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN).readInt(10); - } - - @Test(expected = IndexOutOfBoundsException.class) - public void testSwappedWriteIntThrowsIndexOutOfBounds() { - createBuffer(4, 4).order(ByteOrder.LITTLE_ENDIAN).writeInt(10, 10); - } - - @Test - public void testSwappedWriteReadByteRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeByte(10).flip().readByte()); - } - - @Test - public void testSwappedWriteReadByteAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeByte(4, 10).readByte(4)); - } - - @Test - public void testSwappedWriteReadUnsignedByteRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeUnsignedByte(10).flip().readUnsignedByte()); - } - - @Test - public void testSwappedWriteReadUnsignedByteAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeUnsignedByte(4, 10).readUnsignedByte(4)); - } - - @Test - public void testSwappedWriteReadShortRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeShort((short) 10).flip().readShort()); - } - - @Test - public void testSwappedWriteReadShortAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeShort(4, (short) 10).readShort(4)); - } - - @Test - public void testSwappedWriteReadUnsignedShortRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeUnsignedShort((short) 10).flip().readUnsignedShort()); - } - - @Test - public void testSwappedWriteReadUnsignedShortAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeUnsignedShort(4, (short) 10).readUnsignedShort(4)); - } - - @Test - public void testSwappedWriteReadIntRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeInt(10).flip().readInt()); - } - - @Test - public void testSwappedWriteReadUnsignedIntAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeUnsignedInt(4, 10).readUnsignedInt(4)); - } - - @Test - public void testSwappedWriteReadUnsignedIntRelative() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeUnsignedInt(10).flip().readUnsignedInt()); - } - - @Test - public void testSwappedWriteReadIntAbsolute() { - assertEquals(10, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeInt(4, 10).readInt(4)); - } - - @Test - public void testSwappedWriteReadLongRelative() { - assertEquals(12345, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeLong(12345).flip().readLong()); - } - - @Test - public void testSwappedWriteReadLongAbsolute() { - assertEquals(12345, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeLong(4, 12345).readLong(4)); - } - - @Test - public void testSwappedWriteReadFloatRelative() { - assertEquals(10.6f, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeFloat(10.6f).flip().readFloat(), .001); - } - - @Test - public void testSwappedWriteReadFloatAbsolute() { - assertEquals(10.6f, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeFloat(4, 10.6f).readFloat(4), .001); - } - - @Test - public void testSwappedWriteReadDoubleRelative() { - assertEquals(10.6, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeDouble(10.6).flip().readDouble(), .001); - } - - @Test - public void testSwappedWriteReadDoubleAbsolute() { - assertEquals(10.6, createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeDouble(4, 10.6).readDouble(4), .001); - } - - @Test - public void testSwappedWriteReadBooleanRelative() { - assertTrue(createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeBoolean(true).flip().readBoolean()); - } - - @Test - public void testSwappedWriteReadBooleanAbsolute() { - assertTrue(createBuffer(16).order(ByteOrder.LITTLE_ENDIAN).writeBoolean(4, true).readBoolean(4)); - } - - @Test - public void testSwappedReadWriter() { - Buffer writeBuffer = createBuffer(8).order(ByteOrder.LITTLE_ENDIAN).writeLong(10).flip(); - Buffer readBuffer = createBuffer(8).order(ByteOrder.LITTLE_ENDIAN); - writeBuffer.read(readBuffer); - assertEquals(10, readBuffer.flip().readLong()); - } - - @Test - public void testSwappedAbsoluteSlice() { - Buffer buffer = createBuffer(1024).order(ByteOrder.LITTLE_ENDIAN); - buffer.writeLong(10).writeLong(11).rewind(); - Buffer slice = buffer.slice(8, 1016); - assertEquals(0, slice.position()); - assertEquals(11, slice.readLong()); - } - - @Test - public void testSwappedRelativeSliceWithoutLength() { - Buffer buffer = createBuffer(1024, 1024).order(ByteOrder.LITTLE_ENDIAN); - buffer.writeLong(10).writeLong(11).writeLong(12).rewind(); - assertEquals(10, buffer.readLong()); - Buffer slice = buffer.slice(); - assertEquals(0, slice.position()); - assertEquals(-1, slice.limit()); - assertEquals(1016, slice.capacity()); - assertEquals(1016, slice.maxCapacity()); - assertEquals(11, slice.readLong()); - assertEquals(11, slice.readLong(0)); - slice.close(); - Buffer slice2 = buffer.skip(8).slice(); - assertEquals(0, slice2.position()); - assertEquals(-1, slice2.limit()); - assertEquals(1008, slice2.capacity()); - assertEquals(1008, slice2.maxCapacity()); - assertEquals(12, slice2.readLong()); - assertEquals(12, slice2.readLong(0)); - } - - @Test - public void testSwappedRelativeSliceWithLength() { - Buffer buffer = createBuffer(1024).order(ByteOrder.LITTLE_ENDIAN); - buffer.writeLong(10).writeLong(11).writeLong(12).rewind(); - assertEquals(10, buffer.readLong()); - Buffer slice = buffer.slice(8); - assertEquals(0, slice.position()); - assertEquals(11, slice.readLong()); - assertEquals(11, slice.readLong(0)); - slice.close(); - Buffer slice2 = buffer.skip(8).slice(8); - assertEquals(0, slice2.position()); - assertEquals(12, slice2.readLong()); - assertEquals(12, slice2.readLong(0)); - slice2.close(); - } - - @Test - public void testSwappedSliceOfSlice() { - Buffer buffer = createBuffer(1024).order(ByteOrder.LITTLE_ENDIAN); - buffer.writeLong(10).writeLong(11).writeLong(12).rewind(); - assertEquals(10, buffer.readLong()); - Buffer slice = buffer.slice(); - assertEquals(11, slice.readLong()); - Buffer sliceOfSlice = slice.slice(); - assertEquals(12, sliceOfSlice.readLong()); - assertEquals(8, sliceOfSlice.position()); - } - - @Test - public void testSwappedSliceWithLimit() { - Buffer buffer = createBuffer(1024).order(ByteOrder.LITTLE_ENDIAN).limit(16); - buffer.writeLong(10); - Buffer slice = buffer.slice(); - assertEquals(0, slice.position()); - assertEquals(8, slice.capacity()); - assertEquals(8, slice.maxCapacity()); - assertEquals(8, slice.remaining()); - } - - @Test - public void testSwappedSliceWithLittleRemaining() { - Buffer buffer = createBuffer(1024, 2048).order(ByteOrder.LITTLE_ENDIAN); - buffer.position(1020); - Buffer slice = buffer.slice(8); - assertEquals(0, slice.position()); - assertEquals(-1, slice.limit()); - } - - @Test - public void testSwappedCompact() { - Buffer buffer = createBuffer(1024).order(ByteOrder.LITTLE_ENDIAN); - buffer.position(100).writeLong(1234).position(100).compact(); - assertEquals(0, buffer.position()); - assertEquals(1234, buffer.readLong()); - } - - @Test - public void testCapacity0Read() { - Buffer buffer = createBuffer(0, 1024); - assertEquals(0, buffer.readLong()); - } - - @Test - public void testCapacity0Write() { - Buffer buffer = createBuffer(0, 1024); - buffer.writeLong(10); - assertEquals(10, buffer.readLong(0)); - } - -} diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/DirectBufferTest.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/DirectBufferTest.java deleted file mode 100644 index 5602b5ad6e..0000000000 --- a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/DirectBufferTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Direct buffer test. - * - * @author Jordan Halterman - */ -public class DirectBufferTest extends BufferTest { - - @Override - protected Buffer createBuffer(int capacity) { - return DirectBuffer.allocate(capacity); - } - - @Override - protected Buffer createBuffer(int capacity, int maxCapacity) { - return DirectBuffer.allocate(capacity, maxCapacity); - } - - @Test - public void testByteBufferToDirectBuffer() { - ByteBuffer byteBuffer = ByteBuffer.allocate(8); - byteBuffer.putLong(10); - byteBuffer.flip(); - - DirectBuffer directBuffer = DirectBuffer.allocate(8); - directBuffer.write(byteBuffer.array()); - directBuffer.flip(); - assertEquals(directBuffer.readLong(), byteBuffer.getLong()); - assertTrue(directBuffer.isDirect()); - directBuffer.release(); - } - -} diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/FileBufferTest.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/FileBufferTest.java deleted file mode 100644 index 9a8262f718..0000000000 --- a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/FileBufferTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import org.junit.AfterClass; -import org.junit.Test; - -import java.io.File; -import java.nio.file.Files; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * File buffer test. - * - * @author Jordan Halterman - */ -public class FileBufferTest extends BufferTest { - @AfterClass - public static void afterTest() { - FileTesting.cleanFiles(); - } - - @Override - protected Buffer createBuffer(int capacity) { - return FileBuffer.allocate(FileTesting.createFile(), capacity); - } - - @Override - protected Buffer createBuffer(int capacity, int maxCapacity) { - return FileBuffer.allocate(FileTesting.createFile(), capacity, maxCapacity); - } - - @Test - public void testFileToHeapBuffer() { - File file = FileTesting.createFile(); - try (FileBuffer buffer = FileBuffer.allocate(file, 16)) { - buffer.writeLong(10).writeLong(11).flip(); - byte[] bytes = new byte[16]; - buffer.read(bytes).rewind(); - HeapBuffer heapBuffer = HeapBuffer.wrap(bytes); - assertEquals(buffer.readLong(), heapBuffer.readLong()); - assertEquals(buffer.readLong(), heapBuffer.readLong()); - } - } - - /** - * Rests reopening a file that has been closed. - */ - @Test - public void testPersist() { - File file = FileTesting.createFile(); - try (FileBuffer buffer = FileBuffer.allocate(file, 16)) { - buffer.writeLong(10).writeLong(11).flip(); - assertEquals(10, buffer.readLong()); - assertEquals(11, buffer.readLong()); - } - try (FileBuffer buffer = FileBuffer.allocate(file, 16)) { - assertEquals(10, buffer.readLong()); - assertEquals(11, buffer.readLong()); - } - } - - /** - * Tests deleting a file. - */ - @Test - public void testDelete() { - File file = FileTesting.createFile(); - FileBuffer buffer = FileBuffer.allocate(file, 16); - buffer.writeLong(10).writeLong(11).flip(); - assertEquals(10, buffer.readLong()); - assertEquals(11, buffer.readLong()); - assertTrue(Files.exists(file.toPath())); - buffer.delete(); - assertFalse(Files.exists(file.toPath())); - } - -} diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/FileTesting.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/FileTesting.java deleted file mode 100644 index fc980885fb..0000000000 --- a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/FileTesting.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import java.io.File; -import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.UUID; - -public abstract class FileTesting { - public static File createFile() { - File file = new File("target/test-files/" + UUID.randomUUID().toString()); - file.getParentFile().mkdirs(); - return file; - } - - public static void cleanFiles() { - Path directory = Paths.get("target/test-files/"); - try { - Files.walkFileTree(directory, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (Exception ignore) { - } - } -} diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/HeapBufferTest.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/HeapBufferTest.java deleted file mode 100644 index 98730289a8..0000000000 --- a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/HeapBufferTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; - -/** - * Heap buffer test. - * - * @author Jordan Halterman - */ -public class HeapBufferTest extends BufferTest { - - @Override - protected Buffer createBuffer(int capacity) { - return HeapBuffer.allocate(capacity); - } - - @Override - protected Buffer createBuffer(int capacity, int maxCapacity) { - return HeapBuffer.allocate(capacity, maxCapacity); - } - - @Test - public void testByteBufferToHeapBuffer() { - ByteBuffer byteBuffer = ByteBuffer.allocate(8); - byteBuffer.putLong(10); - byteBuffer.rewind(); - - HeapBuffer directBuffer = HeapBuffer.wrap(byteBuffer.array()); - assertEquals(directBuffer.readLong(), byteBuffer.getLong()); - } - - @Test - public void testDirectToHeapBuffer() { - DirectBuffer directBuffer = DirectBuffer.allocate(8); - directBuffer.writeLong(10); - directBuffer.flip(); - - byte[] bytes = new byte[8]; - directBuffer.read(bytes); - directBuffer.rewind(); - - HeapBuffer heapBuffer = HeapBuffer.wrap(bytes); - assertEquals(directBuffer.readLong(), heapBuffer.readLong()); - - directBuffer.release(); - } - - @Test - public void testHeapToDirectBuffer() { - HeapBuffer heapBuffer = HeapBuffer.allocate(8); - heapBuffer.writeLong(10); - heapBuffer.flip(); - - DirectBuffer directBuffer = DirectBuffer.allocate(8); - directBuffer.write(heapBuffer.array()); - directBuffer.flip(); - - assertEquals(directBuffer.readLong(), heapBuffer.readLong()); - - directBuffer.release(); - } -} diff --git a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/MappedBufferTest.java b/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/MappedBufferTest.java deleted file mode 100644 index 2d9a2b25be..0000000000 --- a/third-party/atomix/storage/src/test/java/io/atomix/storage/buffer/MappedBufferTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2015-present Open Networking Foundation - * - * 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.storage.buffer; - -import org.junit.AfterClass; -import org.junit.Test; - -import java.io.File; -import java.nio.file.Files; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Mapped buffer test. - * - * @author Jordan Halterman - */ -public class MappedBufferTest extends BufferTest { - @AfterClass - public static void afterTest() { - FileTesting.cleanFiles(); - } - - @Override - protected Buffer createBuffer(int capacity) { - return MappedBuffer.allocate(FileTesting.createFile(), capacity); - } - - @Override - protected Buffer createBuffer(int capacity, int maxCapacity) { - return MappedBuffer.allocate(FileTesting.createFile(), capacity, maxCapacity); - } - - /** - * Rests reopening a file that has been closed. - */ - @Test - public void testPersist() { - File file = FileTesting.createFile(); - try (MappedBuffer buffer = MappedBuffer.allocate(file, 16)) { - buffer.writeLong(10).writeLong(11).flip(); - assertEquals(10, buffer.readLong()); - assertEquals(11, buffer.readLong()); - } - try (MappedBuffer buffer = MappedBuffer.allocate(file, 16)) { - assertEquals(10, buffer.readLong()); - assertEquals(11, buffer.readLong()); - } - } - - /** - * Tests deleting a file. - */ - @Test - public void testDelete() { - File file = FileTesting.createFile(); - MappedBuffer buffer = MappedBuffer.allocate(file, 16); - buffer.writeLong(10).writeLong(11).flip(); - assertEquals(10, buffer.readLong()); - assertEquals(11, buffer.readLong()); - assertTrue(Files.exists(file.toPath())); - buffer.delete(); - assertFalse(Files.exists(file.toPath())); - } - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceCounted.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceCounted.java deleted file mode 100644 index 447e4cf5e8..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceCounted.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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.utils.concurrent; - -/** - * Reference counting interface. - *

- * Types that implement {@code ReferenceCounted} can be counted for references and thus used to clean up resources once - * a given instance of an object is no longer in use. - * - * @author Jordan Halterman - */ -public interface ReferenceCounted extends AutoCloseable { - - /** - * Acquires a reference. - * - * @return The acquired reference. - */ - T acquire(); - - /** - * Releases a reference. - * - * @return Indicates whether all references to the object have been released. - */ - boolean release(); - - /** - * Returns the number of open references. - * - * @return The number of open references. - */ - int references(); - - /** - * Defines an exception free close implementation. - */ - @Override - void close(); - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceFactory.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceFactory.java deleted file mode 100644 index 05454abc41..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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.utils.concurrent; - -/** - * Reference factory. - * - * @author Jordan Halterman - */ -public interface ReferenceFactory> { - - /** - * Creates a new reference. - * - * @param manager The reference manager. - * @return The created reference. - */ - T createReference(ReferenceManager manager); - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceManager.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceManager.java deleted file mode 100644 index 0c78d0d9b4..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferenceManager.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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.utils.concurrent; - -/** - * Reference manager. Manages {@link ReferenceCounted} objects. - * - * @author Jordan Halterman - */ -public interface ReferenceManager { - - /** - * Releases the given reference. - *

- * This method should be called with a {@link ReferenceCounted} object that contains no - * additional references. This allows, for instance, pools to recycle dereferenced objects. - * - * @param reference The reference to release. - */ - void release(T reference); - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferencePool.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferencePool.java deleted file mode 100644 index 0006814aca..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/ReferencePool.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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.utils.concurrent; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; - -/** - * Pool of reference counted objects. - * - * @author Jordan Halterman - */ -public class ReferencePool> implements ReferenceManager, AutoCloseable { - private final ReferenceFactory factory; - private final Queue pool = new ConcurrentLinkedQueue<>(); - private volatile boolean closed; - - public ReferencePool(ReferenceFactory factory) { - if (factory == null) { - throw new NullPointerException("factory cannot be null"); - } - this.factory = factory; - } - - /** - * Acquires a reference. - * - * @return The acquired reference. - */ - public T acquire() { - if (closed) { - throw new IllegalStateException("pool closed"); - } - - T reference = pool.poll(); - if (reference == null) { - reference = factory.createReference(this); - } - reference.acquire(); - return reference; - } - - @Override - public void release(T reference) { - if (!closed) { - pool.add(reference); - } - } - - @Override - public synchronized void close() { - if (closed) { - throw new IllegalStateException("pool closed"); - } - - closed = true; - for (T reference : pool) { - reference.close(); - } - } - -} diff --git a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/package-info.java b/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/package-info.java deleted file mode 100644 index 2843b2cce6..0000000000 --- a/third-party/atomix/utils/src/main/java/io/atomix/utils/concurrent/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2017-present Open Networking Foundation - * - * 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. - */ - -/** - * Provides classes and interfaces for managing concurrency. - */ -package io.atomix.utils.concurrent;