diff --git a/datastructures-bytes/NOTICE.txt b/datastructures-bytes/NOTICE.txt deleted file mode 100644 index 6463e93..0000000 --- a/datastructures-bytes/NOTICE.txt +++ /dev/null @@ -1,5 +0,0 @@ -This work is derived from - -https://github.com/OpenHFT/Chronicle-Bytes - -Licensed under the Apache License, Version 2.0 diff --git a/datastructures-bytes/build.gradle b/datastructures-bytes/build.gradle deleted file mode 100644 index 071a026..0000000 --- a/datastructures-bytes/build.gradle +++ /dev/null @@ -1,6 +0,0 @@ -dependencies { - api libs.chronicle.core - testImplementation libs.junit.vintage.engine - testImplementation libs.junit4 - testImplementation libs.affinity -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AbstractBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AbstractBytes.java deleted file mode 100644 index 70f4c65..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AbstractBytes.java +++ /dev/null @@ -1,1056 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.algo.BytesStoreHash; -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.bytes.util.DecoratedBufferUnderflowException; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.annotation.UsedViaReflection; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.UnsafeText; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -@SuppressWarnings("rawtypes") -public abstract class AbstractBytes - extends AbstractReferenceCounted - implements Bytes { - private static final boolean BYTES_BOUNDS_UNCHECKED = Jvm.getBoolean("bytes.bounds.unchecked", false); - // used for debugging - @UsedViaReflection - private final String name; - - protected BytesStore, Underlying> bytesStore; - protected long readPosition; - protected long writePosition; - protected long writeLimit; - protected boolean isPresent; - private int lastDecimalPlaces = 0; - private boolean lenient = false; - - AbstractBytes( BytesStore, Underlying> bytesStore, long writePosition, long writeLimit) - throws IllegalStateException { - this(bytesStore, writePosition, writeLimit, ""); - } - - AbstractBytes( BytesStore, Underlying> bytesStore, long writePosition, long writeLimit, String name) - throws IllegalStateException { - super(bytesStore.isDirectMemory()); - this.bytesStore(bytesStore); - bytesStore.reserve(this); - readPosition = bytesStore.readPosition(); - this.uncheckedWritePosition(writePosition); - this.writeLimit = writeLimit; - // used for debugging - this.name = name; - } - - @Override - public boolean isDirectMemory() { - return bytesStore.isDirectMemory(); - } - - @Override - public boolean canReadDirect(long length) { - long remaining = writePosition - readPosition; - return bytesStore.isDirectMemory() && remaining >= length; - } - - @Override - public void move(long from, long to, long length) throws BufferUnderflowException { - long start = start(); - bytesStore.move(from - start, to - start, length); - } - - - @Override - public Bytes compact() { - long start = start(); - long readRemaining = readRemaining(); - if ((readRemaining > 0) && (start < readPosition)) { - bytesStore.move(readPosition, start, readRemaining); - readPosition = start; - uncheckedWritePosition(readPosition + readRemaining); - } - return this; - } - - @Override - - public Bytes clear() { - long start = start(); - readPosition = start; - uncheckedWritePosition(start); - writeLimit = capacity(); - return this; - } - - - @Override - public Bytes clearAndPad(long length) throws BufferOverflowException { - if ((start() + length) > capacity()) { - throw newBOERange(start(), length, "clearAndPad failed. Start: %d + length: %d > capacity: %d", capacity()); - } - long l = start() + length; - readPosition = l; - uncheckedWritePosition(l); - writeLimit = capacity(); - return this; - } - - @Override - public long readLimit() { - return writePosition; - } - - @Override - public long writeLimit() { - return writeLimit; - } - - @Override - public long realCapacity() { - return bytesStore.capacity(); - } - - @Override - public long realWriteRemaining() { - return bytesStore.capacity() - writePosition; - } - - @Override - public boolean canWriteDirect(long count) { - return isDirectMemory() && - Math.min(writeLimit, bytesStore.realCapacity()) - >= count + writePosition; - } - - @Override - public long capacity() { - return bytesStore.capacity(); - } - - - @Override - public Underlying underlyingObject() { - return bytesStore.underlyingObject(); - } - - @Override - public long start() { - return bytesStore.start(); - } - - @Override - public long readPosition() { - return readPosition; - } - - @Override - public long writePosition() { - return writePosition; - } - - @Override - public boolean compareAndSwapInt(long offset, int expected, int value) throws BufferOverflowException { - writeCheckOffset(offset, 4); - return bytesStore.compareAndSwapInt(offset, expected, value); - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - writeCheckOffset(offset, 4); - bytesStore.testAndSetInt(offset, expected, value); - } - - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) throws BufferOverflowException { - writeCheckOffset(offset, 8); - return bytesStore.compareAndSwapLong(offset, expected, value); - } - - public AbstractBytes append(double d) throws BufferOverflowException { - boolean fits = canWriteDirect(380); - double ad = Math.abs(d); - if (ad < 1e-18) { - append(Double.toString(d)); - return this; - } - if (!fits) { - fits = 1e-6 <= ad && ad < 1e20 && canWriteDirect(24); - } - if (fits) { - long address = addressForWrite(writePosition); - long address2 = UnsafeText.appendDouble(address, d); - writeSkip(address2 - address); - return this; - } - BytesInternal.append(this, d); - return this; - } - - - @Override - public Bytes readPosition(long position) throws BufferUnderflowException { - if (position < start()) { - throw new DecoratedBufferUnderflowException(String.format("readPosition failed. Position: %d < start: %d", position, start())); - } - if (position > readLimit()) { - throw new DecoratedBufferUnderflowException( - String.format("readPosition failed. Position: %d > readLimit: %d", position, readLimit())); - } - this.readPosition = position; - return this; - } - - - @Override - public Bytes readLimit(long limit) throws BufferUnderflowException { - if (limit < start()) - throw limitLessThanStart(limit); - - if (limit > writeLimit()) - throw limitGreaterThanWriteLimit(limit); - - uncheckedWritePosition(limit); - return this; - } - - private DecoratedBufferUnderflowException limitGreaterThanWriteLimit(long limit) { - return new DecoratedBufferUnderflowException(String.format("readLimit failed. Limit: %d > writeLimit: %d", limit, writeLimit())); - } - - private DecoratedBufferUnderflowException limitLessThanStart(long limit) { - return new DecoratedBufferUnderflowException(String.format("readLimit failed. Limit: %d < start: %d", limit, start())); - } - - - @Override - public Bytes writePosition(long position) throws BufferOverflowException { - if (position > writeLimit()) - throw writePositionTooLarge(position); - - if (position < start()) - throw writePositionTooSmall(position); - - if (position < readPosition()) - this.readPosition = position; - - uncheckedWritePosition(position); - return this; - } - - - private DecoratedBufferOverflowException writePositionTooSmall(long position) { - return new DecoratedBufferOverflowException(String.format("writePosition failed. Position: %d < start: %d", position, start())); - } - - private DecoratedBufferOverflowException writePositionTooLarge(long position) { - return new DecoratedBufferOverflowException( - String.format("writePosition failed. Position: %d > writeLimit: %d", position, writeLimit())); - } - - - @Override - public Bytes readSkip(long bytesToSkip) throws BufferUnderflowException { - if (lenient) { - bytesToSkip = Math.min(bytesToSkip, readRemaining()); - } - readOffsetPositionMoved(bytesToSkip); - return this; - } - - @Override - public void uncheckedReadSkipOne() { - readPosition++; - } - - @Override - public void uncheckedReadSkipBackOne() { - readPosition--; - } - - - @Override - public Bytes writeSkip(long bytesToSkip) throws BufferOverflowException { - writeCheckOffset(writePosition, bytesToSkip); - uncheckedWritePosition(writePosition + bytesToSkip); - return this; - } - - - @Override - public Bytes writeLimit(long limit) throws BufferOverflowException { - if (limit < start()) { - throw writeLimitTooSmall(limit); - } - long capacity = capacity(); - if (limit > capacity) { - throw writeLimitTooBig(limit, capacity); - } - this.writeLimit = limit; - return this; - } - - - private DecoratedBufferOverflowException writeLimitTooBig(long limit, long capacity) { - return new DecoratedBufferOverflowException(String.format("writeLimit failed. Limit: %d > capacity: %d", limit, capacity)); - } - - - private DecoratedBufferOverflowException writeLimitTooSmall(long limit) { - return new DecoratedBufferOverflowException(String.format("writeLimit failed. Limit: %d < start: %d", limit, start())); - } - - @Override - protected void performRelease() throws IllegalStateException { - try { - this.bytesStore.release(this); - } finally { - this.bytesStore(ReleasedBytesStore.releasedBytesStore()); - } - } - - @Override - public int readUnsignedByte() { - try { - long offset = readOffsetPositionMoved(1); - return bytesStore.readUnsignedByte(offset); - - } catch (BufferUnderflowException e) { - return -1; - } - } - - public int readUnsignedByte(long offset) throws BufferUnderflowException { - return readByte(offset) & 0xFF; - } - - @Override - public int uncheckedReadUnsignedByte() { - try { - int unsignedByte = bytesStore.readUnsignedByte(readPosition); - readPosition++; - return unsignedByte; - } catch (BufferUnderflowException e) { - return -1; - } - } - - @Override - public byte readByte() { - try { - long offset = readOffsetPositionMoved(1); - return bytesStore.readByte(offset); - - } catch (BufferUnderflowException e) { - return 0; - } - } - - @Override - public int peekUnsignedByte() { - return readPosition >= writePosition ? -1 : bytesStore.readUnsignedByte(readPosition); - } - - @Override - public short readShort() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(2); - return bytesStore.readShort(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - @Override - public int readInt() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(4); - return bytesStore.readInt(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 1, true); - return bytesStore.readVolatileByte(offset); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 2, true); - return bytesStore.readVolatileShort(offset); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 4, true); - return bytesStore.readVolatileInt(offset); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 8, true); - return bytesStore.readVolatileLong(offset); - } - - @Override - public long readLong() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(8); - return bytesStore.readLong(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - @Override - public float readFloat() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(4); - return bytesStore.readFloat(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - @Override - public double readDouble() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(8); - return bytesStore.readDouble(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - @Override - public int readVolatileInt() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(4); - return bytesStore.readVolatileInt(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - @Override - public long readVolatileLong() throws BufferUnderflowException { - try { - long offset = readOffsetPositionMoved(8); - return bytesStore.readVolatileLong(offset); - } catch (BufferUnderflowException e) { - if (lenient) { - return 0; - } - throw e; - } - } - - protected long readOffsetPositionMoved(long adding) throws BufferUnderflowException { - long offset = readPosition; - readCheckOffset(readPosition, Math.toIntExact(adding), false); - readPosition += adding; - assert readPosition <= readLimit(); - return offset; - } - - - @Override - public Bytes writeByte(long offset, byte i) throws BufferOverflowException { - writeCheckOffset(offset, 1); - bytesStore.writeByte(offset, i); - return this; - } - - - @Override - public Bytes writeShort(long offset, short i) throws BufferOverflowException { - writeCheckOffset(offset, 2); - bytesStore.writeShort(offset, i); - return this; - } - - - @Override - public Bytes writeInt(long offset, int i) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - public Bytes writeOrderedInt(long offset, int i) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeOrderedInt(offset, i); - return this; - } - - - @Override - public Bytes writeLong(long offset, long i) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeLong(offset, i); - return this; - } - - - @Override - public Bytes writeOrderedLong(long offset, long i) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeOrderedLong(offset, i); - return this; - } - - - @Override - public Bytes writeFloat(long offset, float d) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeFloat(offset, d); - return this; - } - - - @Override - public Bytes writeDouble(long offset, double d) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeDouble(offset, d); - return this; - } - - - @Override - public Bytes writeVolatileByte(long offset, byte i8) throws BufferOverflowException { - writeCheckOffset(offset, 1); - bytesStore.writeVolatileByte(offset, i8); - return this; - } - - - @Override - public Bytes writeVolatileShort(long offset, short i16) throws BufferOverflowException { - writeCheckOffset(offset, 2); - bytesStore.writeVolatileShort(offset, i16); - return this; - } - - - @Override - public Bytes writeVolatileInt(long offset, int i32) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeVolatileInt(offset, i32); - return this; - } - - - @Override - public Bytes writeVolatileLong(long offset, long i64) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeVolatileLong(offset, i64); - return this; - } - - @Override - - public Bytes write( RandomDataInput bytes) { - assert bytes != this : "you should not write to yourself !"; - - try { - return write(bytes, bytes.readPosition(), Math.min(writeLimit() - writePosition(), bytes.readRemaining())); - } catch (BufferOverflowException | BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - @Override - - public Bytes write(long offsetInRDO, byte[] bytes, int offset, int length) throws BufferOverflowException { - long remaining = length; - while (remaining > 0) { - int copy = (int) Math.min(remaining, safeCopySize()); // copy 64 KB at a time. - writeCheckOffset(offsetInRDO, copy); - bytesStore.write(offsetInRDO, bytes, offset, copy); - offsetInRDO += copy; - offset += copy; - remaining -= copy; - } - return this; - } - - @Override - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) throws BufferOverflowException { - writeCheckOffset(offsetInRDO, length); - bytesStore.write(offsetInRDO, bytes, offset, length); - } - - @Override - - public Bytes write(long writeOffset, RandomDataInput bytes, long readOffset, long length) - throws BufferOverflowException, BufferUnderflowException { - - long remaining = length; - while (remaining > 0) { - int copy = (int) Math.min(remaining, safeCopySize()); // copy 64 KB at a time. - writeCheckOffset(writeOffset, copy); - bytesStore.write(writeOffset, bytes, readOffset, copy); - writeOffset += copy; - readOffset += copy; - remaining -= copy; - } - return this; - } - - void writeCheckOffset(long offset, long adding) throws BufferOverflowException { - if (BYTES_BOUNDS_UNCHECKED) - return; - writeCheckOffset0(offset, adding); - } - - private void writeCheckOffset0(long offset, long adding) { - if (offset < start()) { - throw newBOELower(offset); - } - if ((offset + adding) > writeLimit()) { - throw newBOERange(offset, adding, "writeCheckOffset failed. Offset: %d + adding %d> writeLimit: %d", writeLimit()); - } - } - - - private DecoratedBufferOverflowException newBOERange(long offset, long adding, String msg, long limit) { - return new DecoratedBufferOverflowException( - String.format(msg, offset, adding, limit)); - } - - - private DecoratedBufferOverflowException newBOELower(long offset) { - return new DecoratedBufferOverflowException(String.format("writeCheckOffset failed. Offset: %d < start: %d", offset, start())); - } - - @Override - public byte readByte(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 1, true); - return bytesStore.readByte(offset); - } - - @Override - public int peekUnsignedByte(long offset) throws BufferUnderflowException { - return offset >= readLimit() ? -1 : bytesStore.peekUnsignedByte(offset); - } - - @Override - public short readShort(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 2, true); - return bytesStore.readShort(offset); - } - - @Override - public int readInt(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 4, true); - return bytesStore.readInt(offset); - } - - @Override - public long readLong(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 8, true); - return bytesStore.readLong(offset); - } - - @Override - public float readFloat(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 4, true); - return bytesStore.readFloat(offset); - } - - @Override - public double readDouble(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 8, true); - return bytesStore.readDouble(offset); - } - - void readCheckOffset(long offset, long adding, boolean given) throws BufferUnderflowException { - if (BYTES_BOUNDS_UNCHECKED) - return; - readCheckOffset0(offset, adding, given); - } - - private void readCheckOffset0(long offset, long adding, boolean given) { - if (offset < start()) { - throw newBOEReadLower(offset); - } - long limit0 = given ? writeLimit() : readLimit(); - if ((offset + adding) > limit0) { - throw newBOEReadUpper(offset, adding, given); - } - } - - - private DecoratedBufferUnderflowException newBOEReadUpper(long offset, long adding, boolean given) { - long limit2 = given ? writeLimit() : readLimit(); - DecoratedBufferUnderflowException e = new DecoratedBufferUnderflowException(String - .format("readCheckOffset0 failed. Offset: %d + adding: %d > limit: %d (given: %s)", offset, adding, limit2, given)); - return e; - } - - - private DecoratedBufferUnderflowException newBOEReadLower(long offset) { - return new DecoratedBufferUnderflowException(String.format("readCheckOffset0 failed. Offset: %d < start: %d", offset, start())); - } - - void prewriteCheckOffset(long offset, long subtracting) throws BufferOverflowException { - if (BYTES_BOUNDS_UNCHECKED) - return; - prewriteCheckOffset0(offset, subtracting); - } - - private void prewriteCheckOffset0(long offset, long subtracting) { - if ((offset - subtracting) < start()) { - throw newBOERange(offset, subtracting, "prewriteCheckOffset0 failed. Offset: %d - subtracting: %d < start: %d", start()); - } - long limit0 = readLimit(); - if (offset > limit0) { - // assert false : "can't read bytes past the limit : limit=" + limit0 + ",offset=" + - // offset + - // ",adding=" + adding; - throw new DecoratedBufferOverflowException( - String.format("prewriteCheckOffset0 failed. Offset: %d > readLimit: %d", offset, limit0)); - } - } - - - @Override - public Bytes writeByte(byte i8) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(1, 1); - bytesStore.writeByte(offset, i8); - return this; - } - - - @Override - public Bytes prewrite( byte[] bytes) throws BufferOverflowException { - long offset = prewriteOffsetPositionMoved(bytes.length); - bytesStore.write(offset, bytes); - return this; - } - - - @Override - public Bytes prewrite( BytesStore bytes) throws BufferOverflowException { - long offset = prewriteOffsetPositionMoved(bytes.readRemaining()); - bytesStore.write(offset, bytes); - return this; - } - - - @Override - public Bytes prewriteByte(byte i8) throws BufferOverflowException { - long offset = prewriteOffsetPositionMoved(1); - bytesStore.writeByte(offset, i8); - return this; - } - - - @Override - public Bytes prewriteInt(int i) throws BufferOverflowException { - long offset = prewriteOffsetPositionMoved(4); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - public Bytes prewriteShort(short i) throws BufferOverflowException { - long offset = prewriteOffsetPositionMoved(2); - bytesStore.writeShort(offset, i); - return this; - } - - - @Override - public Bytes prewriteLong(long l) throws BufferOverflowException { - long offset = prewriteOffsetPositionMoved(8); - bytesStore.writeLong(offset, l); - return this; - } - - protected final long writeOffsetPositionMoved(long adding) throws BufferOverflowException { - return writeOffsetPositionMoved(adding, adding); - } - - protected long writeOffsetPositionMoved(long adding, long advance) throws BufferOverflowException { - long oldPosition = writePosition; - writeCheckOffset(writePosition, adding); - uncheckedWritePosition(writePosition + advance); - return oldPosition; - } - - void uncheckedWritePosition(long writePosition) { - this.writePosition = writePosition; - } - - protected long prewriteOffsetPositionMoved(long subtracting) throws BufferOverflowException { - prewriteCheckOffset(readPosition, subtracting); - return readPosition -= subtracting; - } - - - @Override - public Bytes writeShort(short i16) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(2); - bytesStore.writeShort(offset, i16); - return this; - } - - - @Override - public Bytes writeInt(int i) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(4); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - public Bytes writeIntAdv(int i, int advance) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(4, advance); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - public Bytes writeLong(long i64) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(8); - bytesStore.writeLong(offset, i64); - return this; - } - - - @Override - public Bytes writeLongAdv(long i64, int advance) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(8, advance); - bytesStore.writeLong(offset, i64); - return this; - } - - - @Override - public Bytes writeFloat(float f) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(4); - bytesStore.writeFloat(offset, f); - return this; - } - - - @Override - public Bytes writeDouble(double d) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(8); - bytesStore.writeDouble(offset, d); - return this; - } - - - @Override - public Bytes writeDoubleAndInt(double d, int i) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(12); - bytesStore.writeDouble(offset, d); - bytesStore.writeInt(offset + 8, i); - return this; - } - - - @Override - public Bytes write( byte[] bytes, int offset, int length) throws BufferOverflowException { - if ((length + offset) > bytes.length) { - throw new ArrayIndexOutOfBoundsException("bytes.length=" + bytes.length + ", " + "length=" + length + ", offset=" + offset); - } - if (length > writeRemaining()) { - throw new DecoratedBufferOverflowException( - String.format("write failed. Length: %d > writeRemaining: %d", length, writeRemaining())); - } - int remaining = length; - while (remaining > 0) { - int copy = Math.min(remaining, safeCopySize()); // copy 64 KB at a time. - long offsetInRDO = writeOffsetPositionMoved(copy); - bytesStore.write(offsetInRDO, bytes, offset, copy); - offset += copy; - remaining -= copy; - } - return this; - } - - protected int safeCopySize() { - return 64 << 10; - } - - - @Override - public Bytes writeSome( ByteBuffer buffer) throws BufferOverflowException { - int length = (int) Math.min(buffer.remaining(), writeRemaining()); - ensureCapacity(length); - bytesStore.write(writePosition, buffer, buffer.position(), length); - uncheckedWritePosition(writePosition + length); - buffer.position(buffer.position() + length); - return this; - } - - - @Override - public Bytes writeOrderedInt(int i) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(4); - bytesStore.writeOrderedInt(offset, i); - return this; - } - - - @Override - public Bytes writeOrderedLong(long i) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(8); - bytesStore.writeOrderedLong(offset, i); - return this; - } - - @Override - public long addressForRead(long offset) throws BufferUnderflowException { - return bytesStore.addressForRead(offset); - } - - @Override - public long addressForWrite(long offset) throws UnsupportedOperationException, BufferOverflowException { - return bytesStore.addressForWrite(offset); - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - return bytesStore.addressForWrite(writePosition); - } - - @Override - public int hashCode() { - return BytesStoreHash.hash32(this); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof BytesStore)) { - return false; - } - BytesStore bs = (BytesStore) obj; - long remaining = readRemaining(); - return (bs.readRemaining() == remaining) && - BytesInternal.contentEqual(this, bs); - } - - - @Override - public String toString() { - try { - return BytesInternal.toString(this); - } catch (Exception e) { - return e.toString(); - } - } - - @Override - public void nativeRead(long position, long address, long size) throws BufferUnderflowException { - bytesStore.nativeRead(position, address, size); - } - - @Override - public void nativeWrite(long address, long position, long size) throws BufferOverflowException { - bytesStore.nativeWrite(address, position, size); - } - - - @Override - public BytesStore bytesStore() { - return bytesStore; - } - - protected void bytesStore(BytesStore, Underlying> bytesStore) { - this.bytesStore = bytesStore; - } - - @Override - public int lastDecimalPlaces() { - return lastDecimalPlaces; - } - - @Override - public void lastDecimalPlaces(int lastDecimalPlaces) { - this.lastDecimalPlaces = Math.max(0, lastDecimalPlaces); - } - - @Override - public void lenient(boolean lenient) { - this.lenient = lenient; - } - - @Override - public boolean lenient() { - return lenient; - } - - @Override - public int byteCheckSum() throws IORuntimeException { - return byteCheckSum(readPosition(), readLimit()); - } - - @Override - public int byteCheckSum(long start, long end) { - if (end < Integer.MAX_VALUE && isDirectMemory()) - return byteCheckSum((int) start, (int) end); - return Bytes.super.byteCheckSum(start, end); - } - - public int byteCheckSum(int start, int end) { - int sum = 0; - for (int i = start; i < end; i++) { - sum += readByte(i); - } - return sum & 0xFF; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AbstractBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AbstractBytesStore.java deleted file mode 100644 index 6f2e6e3..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AbstractBytesStore.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.algo.BytesStoreHash; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; - -import java.nio.BufferUnderflowException; - -public abstract class AbstractBytesStore, Underlying> - extends AbstractReferenceCounted - implements BytesStore { - - protected AbstractBytesStore() { - } - - protected AbstractBytesStore(boolean monitored) { - super(monitored); - } - - @Override - public int peekUnsignedByte(long offset) throws BufferUnderflowException { - return offset >= readLimit() ? -1 : readUnsignedByte(offset); - } - - @Override - public int hashCode() { - return BytesStoreHash.hash32(this); - } - - @Override - public long readPosition() { - return 0L; - } - - @Override - public long readRemaining() { - return readLimit() - readPosition(); - } - - @Override - public long writeRemaining() { - return writeLimit() - writePosition(); - } - - @Override - public long start() { - return 0L; - } - - @Override - protected boolean performReleaseInBackground() { - return isDirectMemory(); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AppendableUtil.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AppendableUtil.java deleted file mode 100644 index 496284c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/AppendableUtil.java +++ /dev/null @@ -1,332 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.annotation.Java9; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.IOException; -import java.io.UTFDataFormatException; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -@SuppressWarnings("rawtypes") -public enum AppendableUtil { - ; - - public static void setCharAt( Appendable sb, int index, char ch) - throws IllegalArgumentException, BufferOverflowException { - if (sb instanceof StringBuilder) - ((StringBuilder) sb).setCharAt(index, ch); - else if (sb instanceof Bytes) - ((Bytes) sb).writeByte(index, ch); - else - throw new IllegalArgumentException("" + sb.getClass()); - } - - public static void parseUtf8( BytesStore bs, StringBuilder sb, boolean utf, int length) throws UTFDataFormatRuntimeException { - BytesInternal.parseUtf8(bs, bs.readPosition(), sb, utf, length); - } - - @ForceInline - public static void setLength( Appendable sb, int newLength) - throws BufferUnderflowException, IllegalArgumentException { - if (sb instanceof StringBuilder) - ((StringBuilder) sb).setLength(newLength); - else if (sb instanceof Bytes) - ((Bytes) sb).readPositionRemaining(0, newLength); - else - throw new IllegalArgumentException("" + sb.getClass()); - } - - public static void append( Appendable sb, double value) - throws IllegalArgumentException, BufferOverflowException { - if (sb instanceof StringBuilder) - ((StringBuilder) sb).append(value); - else if (sb instanceof Bytes) - ((Bytes) sb).append(value); - else - throw new IllegalArgumentException("" + sb.getClass()); - } - - public static void append( Appendable sb, long value) - throws IllegalArgumentException, BufferOverflowException { - if (sb instanceof StringBuilder) - ((StringBuilder) sb).append(value); - else if (sb instanceof Bytes) - ((Bytes) sb).append(value); - else - throw new IllegalArgumentException("" + sb.getClass()); - } - - public static void append( ACS sb, String str) { - try { - sb.append(str); - } catch (IOException e) { - throw new AssertionError(e); - } - } - - public static void read8bitAndAppend( StreamingDataInput bytes, - StringBuilder appendable, - StopCharsTester tester) { - while (true) { - int c = bytes.readUnsignedByte(); - if (tester.isStopChar(c, bytes.peekUnsignedByte())) - return; - appendable.append((char) c); - if (bytes.readRemaining() == 0) - return; - } - } - - public static void readUTFAndAppend( StreamingDataInput bytes, - Appendable appendable, - StopCharsTester tester) - throws BufferUnderflowException { - try { - readUtf8AndAppend(bytes, appendable, tester); - } catch (IOException e) { - throw new AssertionError(e); - } - } - - public static void readUtf8AndAppend( StreamingDataInput bytes, - Appendable appendable, - StopCharsTester tester) - throws BufferUnderflowException, IOException { - while (true) { - int c = bytes.readUnsignedByte(); - if (c >= 128) { - bytes.readSkip(-1); - break; - } - // this is used for array class such as !type byte[] - if (c == '[' && bytes.peekUnsignedByte() == ']') { - appendable.append((char) c); - appendable.append((char) bytes.readUnsignedByte()); - if (bytes.readRemaining() == 0) - return; - continue; - } - - if (tester.isStopChar(c, bytes.peekUnsignedByte())) - return; - appendable.append((char) c); - if (bytes.readRemaining() == 0) - return; - } - - for (int c; (c = bytes.readUnsignedByte()) >= 0; ) { - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - if (tester.isStopChar(c, bytes.peekUnsignedByte())) - return; - appendable.append((char) c); - break; - - case 12: - case 13: { - /* 110x xxxx 10xx xxxx */ - int char2 = bytes.readUnsignedByte(); - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatException( - "malformed input around byte " + Integer.toHexString(char2)); - int c2 = (char) (((c & 0x1F) << 6) | - (char2 & 0x3F)); - if (tester.isStopChar(c2, bytes.peekUnsignedByte())) - return; - appendable.append((char) c2); - break; - } - - case 14: { - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - int char2 = bytes.readUnsignedByte(); - int char3 = bytes.readUnsignedByte(); - - if (((char2 & 0xC0) != 0x80)) - throw new UTFDataFormatException( - "malformed input around byte " + Integer.toHexString(char2)); - if ((char3 & 0xC0) != 0x80) - throw new UTFDataFormatException( - "malformed input around byte " + Integer.toHexString(char3)); - int c3 = (char) (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - (char3 & 0x3F)); - if (tester.isStopChar(c3, bytes.peekUnsignedByte())) - return; - appendable.append((char) c3); - break; - } - - default: - /* 10xx xxxx, 1111 xxxx */ - throw new UTFDataFormatException( - "malformed input around byte " + Integer.toHexString(c)); - } - } - } - - public static void parse8bit_SB1( Bytes bytes, StringBuilder sb, int length) - throws BufferUnderflowException { - if (length > bytes.readRemaining()) - throw new BufferUnderflowException(); - NativeBytesStore nbs = (NativeBytesStore) bytes.bytesStore(); - long offset = bytes.readPosition(); - int count = BytesInternal.parse8bit_SB1(offset, nbs, sb, length); - bytes.readSkip(count); - } - - public static void parse8bit( StreamingDataInput bytes, Appendable appendable, int utflen) - throws BufferUnderflowException, IOException { - if (appendable instanceof StringBuilder) { - final StringBuilder sb = (StringBuilder) appendable; - if (bytes instanceof Bytes && ((Bytes) bytes).bytesStore() instanceof NativeBytesStore) { - parse8bit_SB1((Bytes) bytes, sb, utflen); - } else { - BytesInternal.parse8bit1(bytes, sb, utflen); - } - } else { - BytesInternal.parse8bit1(bytes, appendable, utflen); - } - } - - public static void append(ACS a, CharSequence cs, long start, long len) { - if (a instanceof StringBuilder) { - if (cs instanceof Bytes) - ((StringBuilder) a).append(Bytes.toString(((Bytes) cs), start, len)); - else - ((StringBuilder) a).append(cs.subSequence(Maths.toInt32(start), Maths.toInt32(len))); - } else if (a instanceof Bytes) { - ((Bytes) a).appendUtf8(cs, Maths.toInt32(start), Maths.toInt32(len)); - } else { - throw new UnsupportedOperationException(); - } - } - - public static long findUtf8Length( CharSequence str) throws IndexOutOfBoundsException { - int strlen = str.length(); - long utflen = strlen;/* use charAt instead of copying String to char array */ - for (int i = 0; i < strlen; i++) { - char c = str.charAt(i); - if (c <= 0x007F) { - continue; - } - if (c <= 0x07FF) { - utflen++; - - } else { - utflen += 2; - } - } - return utflen; - } - - @Java9 - public static long findUtf8Length( byte[] bytes, byte coder) { - long utflen; - - if (coder == 0) { - int strlen = bytes.length; - utflen = bytes.length; - - //noinspection ForLoopReplaceableByForEach - for (int i = 0; i < strlen; i++) { - int b = (bytes[i] & 0xFF); - - if (b > 0x007F) { - utflen++; - } - } - } else { - int strlen = bytes.length; - utflen = 0;/* use charAt instead of copying String to char array */ - for (int i = 0; i < strlen; i += 2) { - char c = (char) (((bytes[i + 1] & 0xFF) << 8) | (bytes[i] & 0xFF)); - - if (c <= 0x007F) { - utflen += 1; - continue; - } - if (c <= 0x07FF) { - utflen += 2; - } else { - utflen += 3; - } - } - } - - return utflen; - } - - @Java9 - public static long findUtf8Length( byte[] chars) { - long utflen = 0; /* use charAt instead of copying String to char array */ - int strlen = chars.length; - for (int i = 0; i < strlen; i++) { - int c = chars[i] & 0xFF; // unsigned byte - - if (c == 0) { // we have hit end of string - break; - } - - if (c >= 0xF0) { - utflen += 4; - i += 3; - } else if (c >= 0xE0) { - utflen += 3; - i += 2; - } else if (c >= 0xC0) { - utflen += 2; - i += 1; - } else { - utflen += 1; - } - } - return utflen; - } - - public static long findUtf8Length( char[] chars) { - long utflen = chars.length;/* use charAt instead of copying String to char array */ - for (char c : chars) { - if (c <= 0x007F) { - continue; - } - if (c <= 0x07FF) { - utflen++; - - } else { - utflen += 2; - } - } - return utflen; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BinaryBytesMethodWriterInvocationHandler.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BinaryBytesMethodWriterInvocationHandler.java deleted file mode 100644 index 7ae6828..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BinaryBytesMethodWriterInvocationHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.util.AbstractInvocationHandler; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.function.Function; - -public class BinaryBytesMethodWriterInvocationHandler extends AbstractInvocationHandler implements BytesMethodWriterInvocationHandler { - private final Function methodToId; - @SuppressWarnings("rawtypes") - private final BytesOut out; - private final Map methodToIdMap = new LinkedHashMap<>(); - - @SuppressWarnings("rawtypes") - public BinaryBytesMethodWriterInvocationHandler(Function methodToId, BytesOut out) { - super(HashMap::new); - this.methodToId = methodToId; - this.out = out; - } - - @Override - protected Object doInvoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException { - MethodEncoder info = methodToIdMap.computeIfAbsent(method, methodToId); - if (info == null) { - Jvm.warn().on(getClass(), "Unknown method " + method + " ignored"); - return null; - } - out.comment(method.getName()); - out.writeStopBit(info.messageId()); - info.encode(args, out); - return null; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BinaryWireCode.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BinaryWireCode.java deleted file mode 100644 index b4daf2f..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BinaryWireCode.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.lang.reflect.Field; - -public interface BinaryWireCode { - - // sequence of length 0 - 255 bytes - int BYTES_LENGTH8 = 0x80; - // sequence of length 0 - 2^16-1 bytes - int BYTES_LENGTH16 = 0x81; - // sequence of length 0 - 2^32-1 - int BYTES_LENGTH32 = 0x82; - // sequence of length 0 - 255 -// public static final int BYTES_LENGTH64 = 0x83; - - int FIELD_ANCHOR = 0x87; - int ANCHOR = 0x88; - int UPDATED_ALIAS = 0x89; - - // an array of unsigned bytes - int U8_ARRAY = 0x8A; - // public static final int U16_ARRAY = 0x8B; -// public static final int I32_ARRAY = 0x8C; - int I64_ARRAY = 0x8D; - int PADDING32 = 0x8E; - int PADDING = 0x8F; - - int FLOAT32 = 0x90; - int FLOAT64 = 0x91; - int FLOAT_STOP_2 = 0x92; - int FLOAT_STOP_4 = 0x94; - int FLOAT_STOP_6 = 0x96; - int FLOAT_SET_LOW_0 = 0x9A; - int FLOAT_SET_LOW_2 = 0x9B; - int FLOAT_SET_LOW_4 = 0x9C; - // 0x98 - 0x9F - - int UUID = 0xA0; - int UINT8 = 0xA1; - int UINT16 = 0xA2; - int UINT32 = 0xA3; - int INT8 = 0xA4; - int INT16 = 0xA5; - int INT32 = 0xA6; - int INT64 = 0xA7; - int SET_LOW_INT8 = 0xA8; - int SET_LOW_INT16 = 0xA9; - // public static final int FIXED_5 = 0xAA; -// public static final int FIXED_4 = 0xAB; -// public static final int FIXED_3 = 0xAC; -// public static final int FIXED_2 = 0xAD; - int STOP_BIT = 0xAE; - int INT64_0x = 0xAF; - - int FALSE = 0xB0; - int TRUE = 0xB1; - int TIME = 0xB2; - int DATE = 0xB3; - int DATE_TIME = 0xB4; - int ZONED_DATE_TIME = 0xB5; - int TYPE_PREFIX = 0xB6; - int FIELD_NAME_ANY = 0xB7; - int STRING_ANY = 0xB8; - int EVENT_NAME = 0xB9; - int FIELD_NUMBER = 0xBA; - int NULL = 0xBB; - int TYPE_LITERAL = 0xBC; - int EVENT_OBJECT = 0xBD; - int COMMENT = 0xBE; - int HINT = 0xBF; - - int FIELD_NAME0 = 0xC0; - // ... - int FIELD_NAME31 = 0xDF; - - int STRING_0 = 0xE0; - // ... - int STRING_31 = 0xFF; - - String[] STRING_FOR_CODE = _stringForCode(BinaryWireCode.class); - - static String[] _stringForCode(Class clazz) { - String[] stringForCode = new String[256]; - try { - for ( Field field : clazz.getDeclaredFields()) { - if (field.getType() == int.class) - stringForCode[field.getInt(null)] = field.getName(); - else if (field.getType() == byte.class) - stringForCode[field.getByte(null) & 0xFF] = field.getName(); - } - for (int i = FIELD_NAME0; i <= FIELD_NAME31; i++) - stringForCode[i] = "FIELD_" + i; - for (int i = STRING_0; i <= STRING_31; i++) - stringForCode[i] = "STRING_" + i; - for (int i = 0; i < stringForCode.length; i++) { - if (stringForCode[i] == null) - if (i <= ' ' || i >= 127) - stringForCode[i] = "Unknown_0x" + Integer.toHexString(i).toUpperCase(); - else - stringForCode[i] = "Unknown_" + (char) i; - } - } catch (IllegalAccessException e) { - throw new AssertionError(e); - } - return stringForCode; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringAppender.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringAppender.java deleted file mode 100644 index 3b2ea1d..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringAppender.java +++ /dev/null @@ -1,308 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.UnsafeText; -import org.jetbrains.annotations.NotNull; - -import java.io.Writer; -import java.math.BigDecimal; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -/** - * Methods to append text to a Bytes. This extends the Appendable interface. - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface ByteStringAppender> extends StreamingDataOutput, Appendable { - - /** - * @return these Bytes as a Writer - */ - - default Writer writer() { - return new ByteStringWriter(this); - } - - /** - * Append a char in UTF-8 - * - * @param ch to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - */ - @Override - - default B append(char ch) throws BufferOverflowException { - BytesInternal.appendUtf8Char(this, ch); - return (B) this; - } - - /** - * Append a characters in UTF-8 - * - * @param cs to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - */ - @Override - - default B append( CharSequence cs) throws BufferOverflowException { - if (cs.length() == 0) - return (B) this; - try { - return append(cs, 0, cs.length()); - } catch (IndexOutOfBoundsException e) { - throw new AssertionError(e); - } - } - - /** - * Append a boolean as T or F - * - * @param flag to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B append(boolean flag) throws BufferOverflowException { - return append(flag ? 'T' : 'F'); - } - - /** - * Append an int in decimal - * - * @param value to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B append(int value) throws BufferOverflowException { - BytesInternal.appendBase10(this, value); - return (B) this; - } - - /** - * Append a long in decimal - * - * @param value to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B append(long value) throws BufferOverflowException { - if (value == (int) value) - BytesInternal.appendBase10(this, (int) value); - else - BytesInternal.appendBase10(this, value); - return (B) this; - } - - - default B appendBase(long value, int base) throws BufferOverflowException { - BytesInternal.append(this, value, base); - return (B) this; - } - - - default B appendBase16(long value) throws BufferOverflowException { - BytesInternal.appendBase16(this, value, 1); - return (B) this; - } - - - default B appendBase16(long value, int minDigits) throws BufferOverflowException { - BytesInternal.appendBase16(this, value, minDigits); - return (B) this; - } - - /** - * Append a long in decimal with a given number of decimal places. Print value * 10^-decimalPlaces - * - * @param value to append - * @param decimalPlaces to shift the decimal place. - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B appendDecimal(long value, int decimalPlaces) throws BufferOverflowException { - BytesInternal.appendDecimal(this, value, decimalPlaces); - return (B) this; - } - - /** - * Append a float in decimal notation - * - * @param f to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B append(float f) throws BufferOverflowException { - float f2 = Math.abs(f); - if (f2 > 1e6 || f2 < 1e-3) { - return append(Float.toString(f)); - } - int precision = (int) Math.floor(6 - Math.log10(f2)); - long tens = Maths.tens(precision); - return append((double) Math.round(f * tens) / tens); - } - - /** - * Append a double in decimal notation - * - * @param d to append - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B append(double d) throws BufferOverflowException { - BytesInternal.append(this, d); - return (B) this; - } - - /** - * Append a double in decimal notation to a specific number of decimal places. Trailing zeros are not truncated. - *

- * If the number would normally be printed with more decimal places, the number is rounded. - * - * @param d to append - * @param decimalPlaces to always produce - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - - default B append(double d, int decimalPlaces) throws BufferOverflowException { - if (decimalPlaces < 0) - throw new IllegalArgumentException(); - if (decimalPlaces < 18) { - double d2 = d * Maths.tens(decimalPlaces); - if (d2 < Long.MAX_VALUE && d2 > Long.MIN_VALUE) { - // changed from java.lang.Math.round(d2) as this was shown up to cause latency - long round = d2 > 0.0 ? (long) (d2 + 0.5) : (long) (d2 - 0.5); - if (canWriteDirect(20 + decimalPlaces)) { - long address = addressForWritePosition(); - long address2 = UnsafeText.appendBase10d(address, round, decimalPlaces); - writeSkip(address2 - address); - } else { - appendDecimal(round, decimalPlaces); - } - return (B) this; - } - } - return append(d); - } - - /** - * Append a portion of a String to the Bytes in UTF-8. - * - * @param cs to copy - * @param start index of the first char inclusive - * @param end index of the last char exclusive. - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - */ - @Override - - default B append( CharSequence cs, int start, int end) - throws IndexOutOfBoundsException, BufferOverflowException { - BytesInternal.appendUtf8(this, cs, start, end - start); - return (B) this; - } - - /** - * Append a String to the Bytes in ISO-8859-1 - * - * @param cs to write - * @return this - * @throws BufferOverflowException If the string as too large to write in the capacity available - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - */ - - default B append8bit( CharSequence cs) - throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - return append8bit(cs, 0, cs.length()); - } - - default B append8bit( BytesStore bs) - throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - return write(bs, 0L, bs.readRemaining()); - } - - default B append8bit( String cs) - throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - return append8bit(cs, 0, cs.length()); - } - - /** - * Append a portion of a String to the Bytes in ISO-8859-1 - * - * @param cs to copy - * @param start index of the first char inclusive - * @param end index of the last char exclusive. - * @return this - * @throws BufferOverflowException If the string as too large to write in the capacity available - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IndexOutOfBoundsException if the start or the end are not valid for the CharSequence - */ - default B append8bit( CharSequence cs, int start, int end) - throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - if (cs instanceof BytesStore) { - return write((BytesStore) cs, (long) start, end); - } - for (int i = start; i < end; i++) { - char c = cs.charAt(i); - if (c > 255) c = '?'; - writeByte((byte) c); - } - return (B) this; - } - - default B append8bit( BytesStore bs, long start, long end) - throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - return write(bs, start, end); - } - - - default B appendDateMillis(long dateInMillis) { - BytesInternal.appendDateMillis(this, dateInMillis); - return (B) this; - } - - - default B appendTimeMillis(long timeOfDayInMillis) { - BytesInternal.appendTimeMillis(this, timeOfDayInMillis % 86400_000L); - return (B) this; - } - - - default B append( BigDecimal bigDecimal) { - append(bigDecimal.toString()); - return (B) this; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringParser.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringParser.java deleted file mode 100644 index 894d967..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringParser.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.io.IORuntimeException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.Reader; -import java.math.BigDecimal; -import java.nio.BufferUnderflowException; - -/** - * Supports parsing bytes as text. You can parse them as special or white space terminated text. - */ -interface ByteStringParser> extends StreamingDataInput { - /** - * Access these bytes as an ISO-8859-1 encoded Reader - * - * @return as a Reader - */ - - default Reader reader() { - return new ByteStringReader(this); - } - - /** - * Return true or false, or null if it could not be detected - * as true or false. Case is not important - *

- *

false: f, false, n, no, 0 - *

- *

true: t, true, y, yes, 1 - * - * @param tester to detect the end of the text. - * @return true, false, or null if neither. - */ - - default Boolean parseBoolean( StopCharTester tester) { - return BytesInternal.parseBoolean(this, tester); - } - - - default Boolean parseBoolean() { - return BytesInternal.parseBoolean(this, StopCharTesters.NON_ALPHA_DIGIT); - } - - /** - * parse text with UTF-8 decoding as character terminated. - * - * @param stopCharTester to check if the end has been reached. - * @return the text as a String. - */ - - @ForceInline - default String parseUtf8( StopCharTester stopCharTester) { - return BytesInternal.parseUtf8(this, stopCharTester); - } - - - @Deprecated(/* to be removed in x.22 */) - default String parseUTF( StopCharTester stopCharTester) { - return parseUtf8(stopCharTester); - } - - /** - * parse text with UTF-8 decoding as character terminated. - * - * @param buffer to populate - * @param stopCharTester to check if the end has been reached. - */ - @ForceInline - default void parseUtf8( Appendable buffer, StopCharTester stopCharTester) throws BufferUnderflowException { - BytesInternal.parseUtf8(this, buffer, stopCharTester); - } - - @Deprecated(/* to be removed in x.22 */) - default void parseUTF( Appendable buffer, StopCharTester stopCharTester) throws BufferUnderflowException { - parseUtf8(buffer, stopCharTester); - } - - /** - * parse text with UTF-8 decoding as one or two character terminated. - * - * @param buffer to populate - * @param stopCharsTester to check if the end has been reached. - */ - @ForceInline - default void parseUtf8( Appendable buffer, StopCharsTester stopCharsTester) - throws BufferUnderflowException, IORuntimeException { - BytesInternal.parseUtf8(this, buffer, stopCharsTester); - } - - @Deprecated(/* to be removed in x.22 */) - default void parseUTF( Appendable buffer, StopCharsTester stopCharsTester) - throws BufferUnderflowException, IORuntimeException { - parseUtf8(buffer, stopCharsTester); - } - - /** - * parse text with ISO-8859-1 decoding as character terminated. - * - * @param buffer to populate - * @param stopCharTester to check if the end has been reached. - */ - @SuppressWarnings("rawtypes") - @ForceInline - default void parse8bit(Appendable buffer, StopCharTester stopCharTester) - throws BufferUnderflowException { - if (buffer instanceof StringBuilder) - BytesInternal.parse8bit(this, (StringBuilder) buffer, stopCharTester); - else - BytesInternal.parse8bit(this, (Bytes) buffer, stopCharTester); - } - - /** - * parse text with ISO-8859-1 decoding as character terminated. - * - * @param stopCharTester to check if the end has been reached. - */ - default String parse8bit( StopCharTester stopCharTester) - throws BufferUnderflowException { - return BytesInternal.parse8bit(this, stopCharTester); - } - - /** - * parse text with ISO-8859-1 decoding as character terminated. - * - * @param buffer to populate - * @param stopCharsTester to check if the end has been reached. - */ - @SuppressWarnings("rawtypes") - @ForceInline - default void parse8bit(Appendable buffer, StopCharsTester stopCharsTester) - throws BufferUnderflowException { - if (buffer instanceof StringBuilder) - BytesInternal.parse8bit(this, (StringBuilder) buffer, stopCharsTester); - else - BytesInternal.parse8bit(this, (Bytes) buffer, stopCharsTester); - } - - @SuppressWarnings("rawtypes") - default void parse8bit(Bytes buffer, StopCharsTester stopCharsTester) - throws BufferUnderflowException { - BytesInternal.parse8bit(this, buffer, stopCharsTester); - } - - default void parse8bit(StringBuilder buffer, StopCharsTester stopCharsTester) - throws BufferUnderflowException { - BytesInternal.parse8bit(this, buffer, stopCharsTester); - } - - /** - * parse text as an int. The terminating character is consumed. - * - * @return an int. - */ - @ForceInline - default int parseInt() throws BufferUnderflowException { - return Maths.toInt32(BytesInternal.parseLong(this)); - } - - /** - * parse text as a long integer. The terminating character is consumed. - * - * @return a long. - */ - @ForceInline - default long parseLong() throws BufferUnderflowException { - return BytesInternal.parseLong(this); - } - - /** - * parse text as a float decimal. The terminating character is consumed. - *

- * The number of decimal places can be retrieved with lastDecimalPlaces() - * - * @return a float. - */ - default float parseFloat() throws BufferUnderflowException { - return (float) BytesInternal.parseDouble(this); - } - - /** - * parse text as a double decimal. The terminating character is consumed. - *

- * The number of decimal places can be retrieved with lastDecimalPlaces() - * - * @return a double. - */ - default double parseDouble() throws BufferUnderflowException { - return BytesInternal.parseDouble(this); - } - - /** - * Parse the significant digits of a decimal number. - *

- * The number of decimal places can be retrieved with lastDecimalPlaces() - * - * @return the significant digits - */ - default long parseLongDecimal() throws BufferUnderflowException { - return BytesInternal.parseLongDecimal(this); - } - - /** - * @return the last number of decimal places for parseDouble or parseLongDecimal - */ - int lastDecimalPlaces(); - - /** - * Store the last number of decimal places. If - * - * @param lastDecimalPlaces set the number of decimal places if positive, otherwise 0. - */ - void lastDecimalPlaces(int lastDecimalPlaces); - - /** - * Skip text until a terminating character is reached. - * - * @param tester to stop at - * @return true if a terminating character was found, false if the end of the buffer was reached. - */ - @ForceInline - default boolean skipTo( StopCharTester tester) { - return BytesInternal.skipTo(this, tester); - } - - - default BigDecimal parseBigDecimal() { - return new BigDecimal(parseUtf8(StopCharTesters.NUMBER_END)); - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringReader.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringReader.java deleted file mode 100644 index aaa59e9..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringReader.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import java.io.IOException; -import java.io.Reader; -import java.nio.BufferUnderflowException; - -/** - * A Reader wrapper for Bytes. This Reader moves the readPosition() of the underlying Bytes up to the readLimit() - */ -@SuppressWarnings("rawtypes") -public class ByteStringReader extends Reader { - private final ByteStringParser in; - - public ByteStringReader(ByteStringParser in) { - this.in = in; - } - - @Override - public int read() { - return in.readRemaining() > 0 ? in.readUnsignedByte() : -1; - } - - @Override - public long skip(long n) throws IOException { - long len = Math.min(in.readRemaining(), n); - try { - in.readSkip(len); - - } catch (BufferUnderflowException e) { - throw new IOException(e); - } - return len; - } - - @Override - public int read(char[] cbuf, int off, int len) throws IOException { - try { - return in.read(cbuf, off, len); - - } catch (BufferUnderflowException e) { - throw new IOException(e); - } - } - - @Override - public void close() { - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringWriter.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringWriter.java deleted file mode 100644 index 8dffbe1..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ByteStringWriter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.io.Writer; -import java.nio.BufferOverflowException; - -/** - * A Writer for an underlying Bytes. This moves the writePosition() up to the writeLimit(); - */ -@SuppressWarnings("rawtypes") -class ByteStringWriter extends Writer { - private final ByteStringAppender out; - - ByteStringWriter(ByteStringAppender out) { - this.out = out; - } - - @Override - public void write(int c) throws IOException { - try { - out.append((char) c); - - } catch ( BufferOverflowException e) { - throw new IOException(e); - } - } - - @Override - public void write( String str) throws IOException { - out.append(str); - } - - @Override - public void write( String str, int off, int len) throws IOException { - out.append(str, off, off + len); - } - - - @Override - public Writer append( CharSequence csq) throws IOException { - out.append(csq); - return this; - } - - - @Override - public Writer append( CharSequence csq, int start, int end) throws IOException { - out.append(csq, start, end); - return this; - } - - - @Override - public Writer append(char c) throws IOException { - out.append(c); - return this; - } - - @Override - public void flush() { - - } - - @Override - public void close() { - - } - - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - for (int i = 0; i < len; i++) - out.append(cbuf[i + off]); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Byteable.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Byteable.java deleted file mode 100644 index a5c6a8d..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Byteable.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -/** - * This Interface allows a reference to off heap memory to be reassigned. - *

- * A reference to off heap memory is a proxy for some memory which sits outside the heap. - */ -public interface Byteable, Underlying> { - /** - * This setter for a data type which points to an underlying ByteStore. - * - * @param bytesStore the fix point ByteStore - * @param offset the offset within the ByteStore - * @param length the length in the ByteStore - */ - void bytesStore(BytesStore bytesStore, long offset, long length) - throws IllegalStateException, IllegalArgumentException, BufferOverflowException, - BufferUnderflowException; - - - BytesStore bytesStore(); - - long offset(); - - /** - * @return the maximum size in byte for this reference. - */ - long maxSize(); -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Bytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Bytes.java deleted file mode 100644 index 7cebc69..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Bytes.java +++ /dev/null @@ -1,944 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.annotation.UsedViaReflection; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.util.ObjectUtils; -import net.openhft.chronicle.core.util.StringUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; - -/** - * Bytes is a pointer to a region of memory within a BytesStore. It can be for a fixed region of - * memory or an "elastic" buffer which can be resized, but not for a fixed region.

This is a - * BytesStore which is mutable and not thread safe. It has a write position and read position which - * must follow these constraints

start() <= readPosition() <= writePosition() <= - * writeLimit() <= capacity()

Also readLimit() == writePosition() and readPosition() - * <= safeLimit();

- */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface Bytes extends - BytesStore, Underlying>, - BytesIn, - BytesOut { - - long MAX_CAPACITY = Long.MAX_VALUE & ~0xF; // 8 EiB - 16 - int MAX_HEAP_CAPACITY = Integer.MAX_VALUE & ~0xF; // 2 GiB - 16 - @Deprecated(/* to be removed in x.22 */) - int MAX_BYTE_BUFFER_CAPACITY = MAX_HEAP_CAPACITY; - int DEFAULT_BYTE_BUFFER_CAPACITY = 256; - - /** - * Creates and returns a new elastic wrapper for a direct (off-heap) ByteBuffer with a default capacity - * which will be resized as required. - * - * @return a new elastic wrapper for a direct (off-heap) ByteBuffer with a default capacity - * which will be resized as required - */ - - static Bytes elasticByteBuffer() { - return elasticByteBuffer(DEFAULT_BYTE_BUFFER_CAPACITY); - } - - /** - * Creates and returns a new elastic wrapper for a direct (off-heap) ByteBuffer with - * the given {@code initialCapacity} which will be resized as required. - * - * @param initialCapacity the initial non-negative capacity given in bytes - * @return a new elastic wrapper for a direct (off-heap) ByteBuffer with - * the given {@code initialCapacity} which will be resized as required - */ - - static Bytes elasticByteBuffer(int initialCapacity) { - return elasticByteBuffer(initialCapacity, MAX_HEAP_CAPACITY); - } - - /** - * Creates and returns a new elastic wrapper for a direct (off-heap) ByteBuffer with - * the given {@code initialCapacity} which will be resized as required up - * to the given {@code maxSize}. - * - * @param initialCapacity the initial non-negative capacity given in bytes - * @param maxCapacity the max capacity given in bytes equal or greater than initialCapacity - * @return a new elastic wrapper for a direct (off-heap) ByteBuffer with - * the given {@code initialCapacity} which will be resized as required up - * to the given {@code maxCapacity} - */ - - static Bytes elasticByteBuffer(int initialCapacity, int maxCapacity) { - NativeBytesStore bs = NativeBytesStore.elasticByteBuffer(initialCapacity, maxCapacity); - try { - return bs.bytesForWrite(); - } finally { - bs.release(ReferenceOwner.INIT); - } - } - - /** - * Creates and returns a new elastic wrapper for a heap ByteBuffer with - * the given {@code initialCapacity} which will be resized as required. - * - * @param initialCapacity the initial non-negative capacity given in bytes - * @return a new elastic wrapper for a heap ByteBuffer with - * the given {@code initialCapacity} which will be resized as required - */ - - static Bytes elasticHeapByteBuffer(int initialCapacity) { - HeapBytesStore bs = HeapBytesStore.wrap(ByteBuffer.allocate(initialCapacity)); - try { - return NativeBytes.wrapWithNativeBytes(bs, Bytes.MAX_HEAP_CAPACITY); - } finally { - bs.release(INIT); - } - } - - - static Bytes elasticHeapByteBuffer() { - return elasticHeapByteBuffer(128); - } - - /** - * Wrap the ByteBuffer ready for reading - * Method for convenience only - might not be ideal for performance (creates garbage). - * To avoid garbage, use something like this example: - *
{@code
-     * import net.openhft.chronicle.bytes.Bytes;
-     * import java.nio.ByteBuffer;
-     *
-     * public class ChronicleBytesWithByteBufferExampleTest {
-     *     private static final String HELLO_WORLD = "hello world";
-     *
-     *     public static void main(String[] args) throws InterruptedException {
-     *         //setup Bytes and ByteBuffer to write from
-     *         Bytes b = Bytes.elasticByteBuffer();
-     *         ByteBuffer toWriteFrom = ByteBuffer.allocate(HELLO_WORLD.length());
-     *         toWriteFrom.put(HELLO_WORLD.getBytes(), 0, HELLO_WORLD.length());
-     *         toWriteFrom.flip();
-     *         byte[] toReadTo = new byte[HELLO_WORLD.length()];
-     *
-     *         doWrite(b, toWriteFrom);
-     *         ByteBuffer byteBuffer = doRead(b);
-     *
-     *         //check result
-     *         final StringBuilder sb = new StringBuilder();
-     *         for (int i = 0; i < HELLO_WORLD.length(); i++) {
-     *             sb.append((char) byteBuffer.get());
-     *         }
-     *         assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
-     *     }
-     *
-     *     private static void doWrite(Bytes b, ByteBuffer toWrite) {
-     *         //no garbage when writing to Bytes from ByteBuffer
-     *         b.clear();
-     *         b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit());
-     *     }
-     *
-     *     private static ByteBuffer doRead(Bytes b) {
-     *         //no garbage when getting the underlying ByteBuffer
-     *         assert b.underlyingObject() instanceof ByteBuffer;
-     *         ByteBuffer byteBuffer = (ByteBuffer) b.underlyingObject();
-     *         return byteBuffer;
-     *     }
-     * }
-     * }
- * - * @param byteBuffer to wrap - * @return a Bytes which wraps the provided ByteBuffer and is ready for reading. - */ - - static Bytes wrapForRead( ByteBuffer byteBuffer) { - BytesStore bs = BytesStore.wrap(byteBuffer); - try { - Bytes bbb = bs.bytesForRead(); - bbb.readLimit(byteBuffer.limit()); - bbb.readPosition(byteBuffer.position()); - return bbb; - } finally { - bs.release(INIT); - } - } - - /** - * Wrap the ByteBuffer ready for writing - * Method for convenience only - might not be ideal for performance (creates garbage). - * To avoid garbage, use something like this example: - *
{@code
-     * import net.openhft.chronicle.bytes.Bytes;
-     * import java.nio.ByteBuffer;
-     *
-     * public class ChronicleBytesWithByteBufferExampleTest {
-     *     private static final String HELLO_WORLD = "hello world";
-     *
-     *     public static void main(String[] args) throws InterruptedException {
-     *         //setup Bytes and ByteBuffer to write from
-     *         Bytes b = Bytes.elasticByteBuffer();
-     *         ByteBuffer toWriteFrom = ByteBuffer.allocate(HELLO_WORLD.length());
-     *         toWriteFrom.put(HELLO_WORLD.getBytes(), 0, HELLO_WORLD.length());
-     *         toWriteFrom.flip();
-     *         byte[] toReadTo = new byte[HELLO_WORLD.length()];
-     *
-     *         doWrite(b, toWriteFrom);
-     *         ByteBuffer byteBuffer = doRead(b);
-     *
-     *         //check result
-     *         final StringBuilder sb = new StringBuilder();
-     *         for (int i = 0; i < HELLO_WORLD.length(); i++) {
-     *             sb.append((char) byteBuffer.get());
-     *         }
-     *         assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
-     *     }
-     *
-     *     private static void doWrite(Bytes b, ByteBuffer toWrite) {
-     *         //no garbage when writing to Bytes from ByteBuffer
-     *         b.clear();
-     *         b.write(b.writePosition(), toWrite, toWrite.position(), toWrite.limit());
-     *     }
-     *
-     *     private static ByteBuffer doRead(Bytes b) {
-     *         //no garbage when getting the underlying ByteBuffer
-     *         assert b.underlyingObject() instanceof ByteBuffer;
-     *         ByteBuffer byteBuffer = (ByteBuffer) b.underlyingObject();
-     *         return byteBuffer;
-     *     }
-     * }
-     * }
- * - * @param byteBuffer to wrap - * @return a Bytes which wraps the provided ByteBuffer and is ready for writing. - */ - - static Bytes wrapForWrite( ByteBuffer byteBuffer) { - BytesStore bs = BytesStore.wrap(byteBuffer); - try { - Bytes bbb = bs.bytesForWrite(); - bbb.writePosition(byteBuffer.position()); - bbb.writeLimit(byteBuffer.limit()); - return bbb; - } finally { - bs.release(INIT); - } - } - - /** - * Wrap the byte[] ready for reading - * Method for convenience only - might not be ideal for performance (creates garbage). - * To avoid garbage, use something like this example: - *
{@code
-     * import net.openhft.chronicle.bytes.Bytes;
-     * import java.nio.charset.Charset;
-     *
-     * public class ChronicleBytesWithPrimByteArrayExampleTest {
-     *     private static final Charset ISO_8859 = Charset.forName("ISO-8859-1");
-     *     private static final String HELLO_WORLD = "hello world";
-     *
-     *     public static void main(String[] args) {
-     *         //setup Bytes and byte[]s to write from and read to
-     *         Bytes b = Bytes.elasticByteBuffer();
-     *         byte[] toWriteFrom = HELLO_WORLD.getBytes(ISO_8859);
-     *         byte[] toReadTo = new byte[HELLO_WORLD.length()];
-     *
-     *         doWrite(b, toWriteFrom);
-     *         doRead(b, toReadTo);
-     *
-     *         //check result
-     *         final StringBuilder sb = new StringBuilder();
-     *         for (int i = 0; i < HELLO_WORLD.length(); i++) {
-     *             sb.append((char) toReadTo[i]);
-     *         }
-     *         assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
-     *     }
-     *
-     *     private static void doWrite(Bytes b, byte[] toWrite) {
-     *         //no garbage when writing to Bytes from byte[]
-     *         b.clear();
-     *         b.write(toWrite);
-     *     }
-     *
-     *     private static void doRead(Bytes b, byte[] toReadTo) {
-     *         //no garbage when reading from Bytes into byte[]
-     *         b.read( toReadTo, 0, HELLO_WORLD.length());
-     *     }
-     * }
-     * }
- * - * @param byteArray to wrap - * @return the Bytes ready for reading. - */ - - static Bytes wrapForRead( byte[] byteArray) { - HeapBytesStore bs = BytesStore.wrap(byteArray); - try { - return bs.bytesForRead(); - } finally { - bs.release(INIT); - } - } - - /** - * Wrap the byte[] ready for writing - * Method for convenience only - might not be ideal for performance (creates garbage). - * To avoid garbage, use something like this example: - *
{@code
-     * import net.openhft.chronicle.bytes.Bytes;
-     * import java.nio.charset.Charset;
-     *
-     * public class ChronicleBytesWithPrimByteArrayExampleTest {
-     *     private static final Charset ISO_8859 = Charset.forName("ISO-8859-1");
-     *     private static final String HELLO_WORLD = "hello world";
-     *
-     *     public static void main(String[] args) {
-     *         //setup Bytes and byte[]s to write from and read to
-     *         Bytes b = Bytes.elasticByteBuffer();
-     *         byte[] toWriteFrom = HELLO_WORLD.getBytes(ISO_8859);
-     *         byte[] toReadTo = new byte[HELLO_WORLD.length()];
-     *
-     *         doWrite(b, toWriteFrom);
-     *         doRead(b, toReadTo);
-     *
-     *         //check result
-     *         final StringBuilder sb = new StringBuilder();
-     *         for (int i = 0; i < HELLO_WORLD.length(); i++) {
-     *             sb.append((char) toReadTo[i]);
-     *         }
-     *         assert sb.toString().equals(HELLO_WORLD): "Failed - strings not equal!";
-     *     }
-     *
-     *     private static void doWrite(Bytes b, byte[] toWrite) {
-     *         //no garbage when writing to Bytes from byte[]
-     *         b.clear();
-     *         b.write(toWrite);
-     *     }
-     *
-     *     private static void doRead(Bytes b, byte[] toReadTo) {
-     *         //no garbage when reading from Bytes into byte[]
-     *         b.read( toReadTo, 0, HELLO_WORLD.length());
-     *     }
-     * }
-     * }
- * - * @param byteArray to wrap - * @return the Bytes ready for writing. - */ - - static Bytes wrapForWrite( byte[] byteArray) { - BytesStore bs = BytesStore.wrap(byteArray); - try { - return bs.bytesForWrite(); - } finally { - bs.release(INIT); - } - } - - /** - * Convert text to bytes using ISO-8859-1 encoding and return a Bytes ready for reading. - *

- * Note: this returns a direct Bytes now - * - * @param text to convert - * @return Bytes ready for reading. - */ - - static Bytes from( CharSequence text) { - return from(text.toString()); - } - - @Deprecated(/* to be removed in x.22 */) - static Bytes fromString(String text) { - return from(text); - } - - /** - * Convert text to bytes using ISO-8859-1 encoding and return a Bytes ready for reading. - *

- * Note: this returns a direct Bytes now - * - * @param text to convert - * @return Bytes ready for reading. - */ - - static Bytes directFrom( String text) { - NativeBytesStore from = NativeBytesStore.from(text); - try { - return from.bytesForRead(); - } finally { - from.release(INIT); - } - } - - /** - * Convert text to bytes using ISO-8859-1 encoding and return a Bytes ready for reading. - *

- * Note: this returns a heap Bytes - * - * @param text to convert - * @return Bytes ready for reading. - */ - - static Bytes from( String text) throws IllegalArgumentException, IllegalStateException { - return wrapForRead(text.getBytes(StandardCharsets.ISO_8859_1)); - } - - @UsedViaReflection - static Bytes valueOf(String text) { - return from(text); - } - - /** - * Creates and returns a new fix sized wrapper for native (64-bit address) - * memory with the given {@code capacity}. - * - * @param capacity the non-negative capacity given in bytes - * @return a new fix sized wrapper for native (64-bit address) - * memory with the given {@code capacity} - */ - - static VanillaBytes allocateDirect(long capacity) throws IllegalArgumentException { - NativeBytesStore bs = NativeBytesStore.nativeStoreWithFixedCapacity(capacity); - try { - return new VanillaBytes<>(bs); - } finally { - bs.release(INIT); - } - } - - /** - * Creates and returns a new elastic wrapper for native (64-bit address) - * memory with zero initial capacity which will be resized as required. - * - * @return a new elastic wrapper for native (64-bit address) - * memory with zero initial capacity which will be resized as required - */ - - static NativeBytes allocateElasticDirect() { - return NativeBytes.nativeBytes(); - } - - /** - * Creates and returns a new elastic wrapper for native (64-bit address) - * memory with the given {@code initialCapacity} which will be resized as required. - * - * @param initialCapacity the initial non-negative capacity given in bytes - * @return a new elastic wrapper for native (64-bit address) - * memory with the given {@code initialCapacity} which will be resized as required - */ - - static NativeBytes allocateElasticDirect(long initialCapacity) throws IllegalArgumentException { - return NativeBytes.nativeBytes(initialCapacity); - } - - - static OnHeapBytes allocateElasticOnHeap() { - return allocateElasticOnHeap(32); - } - - - static OnHeapBytes allocateElasticOnHeap(int initialCapacity) { - HeapBytesStore wrap = HeapBytesStore.wrap(new byte[initialCapacity]); - try { - return new OnHeapBytes(wrap, true); - } finally { - wrap.release(INIT); - } - } - - /** - * Creates a string from the {@code position} to the {@code limit}, The buffer is not modified - * by this call - * - * @param buffer the buffer to use - * @return a string contain the text from the {@code position} to the {@code limit} - */ - - static String toString( final Bytes buffer) throws BufferUnderflowException { - return toString(buffer, MAX_HEAP_CAPACITY); - } - - /** - * Creates a string from the {@code position} to the {@code limit}, The buffer is not modified - * by this call - * - * @param buffer the buffer to use - * @param maxLen of the result returned - * @return a string contain the text from the {@code position} to the {@code limit} - */ - - static String toString( final Bytes buffer, long maxLen) throws - BufferUnderflowException { - - if (buffer.refCount() < 1) - // added because something is crashing the JVM - return ""; - - ReferenceOwner toString = ReferenceOwner.temporary("toString"); - buffer.reserve(toString); - try { - - if (buffer.readRemaining() == 0) - return ""; - - final long length = Math.min(maxLen + 1, buffer.readRemaining()); - - final StringBuilder builder = new StringBuilder(); - try { - buffer.readWithLength(length, b -> { - while (buffer.readRemaining() > 0) { - if (builder.length() >= maxLen) { - builder.append("..."); - break; - } - builder.append((char) buffer.readByte()); - } - }); - } catch (Exception e) { - builder.append(' ').append(e); - } - return builder.toString(); - } finally { - buffer.release(toString); - } - } - - /** - * The buffer is not modified by this call - * - * @param buffer the buffer to use - * @param position the position to create the string from - * @param len the number of characters to show in the string - * @return a string contain the text from offset {@code position} - */ - - static String toString( final Bytes buffer, long position, long len) - throws BufferUnderflowException { - final long pos = buffer.readPosition(); - final long limit = buffer.readLimit(); - buffer.readPositionRemaining(position, len); - - try { - - final StringBuilder builder = new StringBuilder(); - while (buffer.readRemaining() > 0) { - builder.append((char) buffer.readByte()); - } - - // remove the last comma - return builder.toString(); - } finally { - buffer.readLimit(limit); - buffer.readPosition(pos); - } - } - - /** - * Creates and returns a new fix sized wrapper for native (64-bit address) - * memory with the contents copied from the given {@code bytes} array. - *

- * Changes in the given {@code bytes} will not be affected by writes in - * the returned wrapper or vice versa. - * - * @param bytes array to copy - * @return a new fix sized wrapper for native (64-bit address) - * memory with the contents copied from the given {@code bytes} array - */ - - static VanillaBytes allocateDirect( byte[] bytes) throws IllegalArgumentException { - VanillaBytes result = allocateDirect(bytes.length); - try { - result.write(bytes); - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - return result; - } - - - static Bytes fromHexString( String s) { - return BytesInternal.fromHexString(s); - } - - /** - * Code shared by String and StringBuffer to do searches. The - * source is the character array being searched, and the target - * is the string being searched for. - * - * @param source the read bytes being searched. - * @param target the read bytes being searched for. - * @param fromIndex the index to begin searching from, - * @return the index of where the text was found. - */ - static int indexOf( BytesStore source, BytesStore target, int fromIndex) { - - long sourceOffset = source.readPosition(); - long targetOffset = target.readPosition(); - long sourceCount = source.readRemaining(); - long targetCount = target.readRemaining(); - - if (fromIndex >= sourceCount) { - return Math.toIntExact(targetCount == 0 ? sourceCount : -1); - } - if (fromIndex < 0) { - fromIndex = 0; - } - if (targetCount == 0) { - return fromIndex; - } - - byte firstByte = target.readByte(targetOffset); - long max = sourceOffset + (sourceCount - targetCount); - - for (long i = sourceOffset + fromIndex; i <= max; i++) { - /* Look for first character. */ - if (source.readByte(i) != firstByte) { - while (++i <= max && source.readByte(i) != firstByte) ; - } - - /* Found first character, now look at the rest of v2 */ - if (i <= max) { - long j = i + 1; - long end = j + targetCount - 1; - for (long k = targetOffset + 1; j < end && source.readByte(j) == target.readByte(k); j++, k++) { - } - - if (j == end) { - /* Found whole string. */ - return Math.toIntExact(i - sourceOffset); - } - } - } - return -1; - } - - /** - * Return a Bytes which is optionally unchecked. This allows bounds checks to be turned off. - * Note: this means that the result is no longer elastic, even if this is elastic. - * - * @param unchecked if true, minimal bounds checks will be performed. - * @return Bytes without bounds checking. - * @throws IllegalStateException if the underlying BytesStore has been released - */ - - default Bytes unchecked(boolean unchecked) throws IllegalStateException { - if (unchecked) { - if (isElastic()) - BytesUtil.WarnUncheckedElasticBytes.warn(); - Bytes underlyingBytes = start() == 0 && bytesStore().isDirectMemory() ? - new UncheckedNativeBytes<>(this) : - new UncheckedBytes<>(this); - release(INIT); - return underlyingBytes; - } - return this; - } - - default boolean unchecked() { - return false; - } - - /** - * @inheritDoc

- * If this Bytes {@link #isElastic()} the {@link #safeLimit()} can be - * lower than the point it can safely write. - */ - @Override - default long safeLimit() { - return bytesStore().safeLimit(); - } - - @Override - default boolean isClear() { - return start() == readPosition() && writeLimit() == capacity(); - } - - /** - * @inheritDoc

- * If this Bytes {@link #isElastic()} the {@link #realCapacity()} can be - * lower than the virtual {@link #capacity()}. - */ - @Override - default long realCapacity() { - return BytesStore.super.realCapacity(); - } - - /** - * @return a copy of this Bytes from position() to limit(). - */ - @Override - BytesStore, Underlying> copy(); - - - default String toHexString() { - return toHexString(1024); - } - - /** - * display the hex data of {@link Bytes} from the position() to the limit() - * - * @param maxLength limit the number of bytes to be dumped. - * @return hex representation of the buffer, from example [0D ,OA, FF] - */ - - default String toHexString(long maxLength) { - return toHexString(readPosition(), maxLength); - } - - /** - * display the hex data of {@link Bytes} from the position() to the limit() - * - * @param maxLength limit the number of bytes to be dumped. - * @return hex representation of the buffer, from example [0D ,OA, FF] - */ - - default String toHexString(long offset, long maxLength) { -// if (Jvm.isDebug() && Jvm.stackTraceEndsWith("Bytes", 3)) -// return "Not Available"; - - long maxLength2 = Math.min(maxLength, readLimit() - offset); - String ret = BytesInternal.toHexString(this, offset, maxLength2); - return maxLength2 < readLimit() - offset ? ret + "... truncated" : ret; - } - - /** - * Returns if this Bytes is elastic. I.e. it can resize when more data is written - * than it's {@link #realCapacity()}. - * - * @return if this Bytes is elastic - */ - boolean isElastic(); - - /** - * grow the buffer if the buffer is elastic, if the buffer is not elastic and there is not - * enough capacity then this method will throws {@link java.nio.BufferOverflowException} - * - * @param size the capacity that you required - * @throws IllegalArgumentException if the buffer is not elastic and there is not enough space - */ - default void ensureCapacity(long size) throws IllegalArgumentException { - if (size > capacity()) - throw new IllegalArgumentException(isElastic() ? "todo" : "not elastic"); - } - - /** - * Creates a slice of the current Bytes based on its position() and limit(). As a sub-section - * of a Bytes it cannot be elastic. - * - * @return a slice of the existing Bytes where the start is moved to the position and the - * current limit determines the capacity. - * @throws IllegalStateException if the underlying BytesStore has been released - */ - - @Override - default Bytes bytesForRead() throws IllegalStateException { - return isClear() ? BytesStore.super.bytesForRead() : new SubBytes<>(this, readPosition(), readLimit() + start()); - } - - /** - * @return the ByteStore this Bytes wraps. - */ - @Override - - BytesStore bytesStore(); - - default boolean isEqual(String s) { - return StringUtils.isEqual(this, s); - } - - /** - * Compact these Bytes by moving the readPosition to the start. - * - * @return this - */ - - Bytes compact(); - - /** - * copy bytes from one ByteStore to another - * - * @param store to copy to - * @return the number of bytes copied. - */ - @Override - default long copyTo( BytesStore store) { - return BytesStore.super.copyTo(store); - } - - @Override - default void copyTo( OutputStream out) throws IOException { - BytesStore.super.copyTo(out); - } - - @Override - default boolean sharedMemory() { - return bytesStore().sharedMemory(); - } - - /** - * will unwrite from the offset upto the current write position of the destination bytes - * - * @param fromOffset the offset from the target byytes - * @param count the number of bytes to un-write - */ - default void unwrite(long fromOffset, int count) { - long wp = writePosition(); - - if (wp < fromOffset) - return; - - write(fromOffset, this, fromOffset + count, wp - fromOffset - count); - writeSkip(-count); - } - - - default BigDecimal readBigDecimal() { - return new BigDecimal(readBigInteger(), Maths.toUInt31(readStopBit())); - } - - - default BigInteger readBigInteger() { - int length = Maths.toUInt31(readStopBit()); - if (length == 0) - if (lenient()) - return BigInteger.ZERO; - else - throw new BufferUnderflowException(); - byte[] bytes = new byte[length]; - read(bytes); - return new BigInteger(bytes); - } - - /** - * Returns the index within this bytes of the first occurrence of the - * specified sub-bytes. - *

- *

The returned index is the smallest value k for which: - *

-     * this.startsWith(bytes, k)
-     * 
- * If no such value of k exists, then {@code -1} is returned. - * - * @param source the sub-bytes to search for. - * @return the index of the first occurrence of the specified sub-bytes, - * or {@code -1} if there is no such occurrence. - */ - default long indexOf( Bytes source) { - return indexOf(source, 0); - } - - /** - * Returns the index within this bytes of the first occurrence of the - * specified subbytes. - *

- *

The returned index is the smallest value k for which: - *

-     * this.startsWith(bytes, k)
-     * 
- * If no such value of k exists, then {@code -1} is returned. - * - * @param source the sub-bytes to search for. - * @param fromIndex start the seach from this offset - * @return the index of the first occurrence of the specified sub-bytes, - * or {@code -1} if there is no such occurrence. - */ - default int indexOf( BytesStore source, int fromIndex) { - return indexOf(this, source, fromIndex); - } - - @Deprecated(/* to be removed in x.22 */) - default long indexOf( Bytes source, int fromIndex) { - return indexOf(this, source, fromIndex); - } - - @Override - - Bytes clear(); - - @Override - default boolean readWrite() { - return bytesStore().readWrite(); - } - - default void readWithLength(long length, BytesOut bytesOut) - throws BufferUnderflowException, IORuntimeException { - if (length > readRemaining()) - throw new BufferUnderflowException(); - long limit0 = readLimit(); - long limit = readPosition() + length; - boolean lenient = lenient(); - try { - lenient(true); - readLimit(limit); - bytesOut.write(this); - } finally { - readLimit(limit0); - readPosition(limit); - lenient(lenient); - } - } - - default T readMarshallableLength16(Class tClass, T object) { - if (object == null) object = ObjectUtils.newInstance(tClass); - int length = readUnsignedShort(); - long limit = readLimit(); - long end = readPosition() + length; - boolean lenient = lenient(); - try { - lenient(true); - readLimit(end); - object.readMarshallable(this); - } finally { - readPosition(end); - readLimit(limit); - lenient(lenient); - } - return object; - } - - default void writeMarshallableLength16(WriteBytesMarshallable marshallable) { - long position = writePosition(); - writeUnsignedShort(0); - marshallable.writeMarshallable(this); - long length = writePosition() - position - 2; - if (length >= 1 << 16) - throw new IllegalStateException("Marshallable " + marshallable.getClass() + " too long was " + length); - writeUnsignedShort(position, (int) length); - } - - default Bytes write(final InputStream inputStream) throws IOException { - for (; ; ) { - int read; - read = inputStream.read(); - if (read == -1) - break; - writeByte((byte) read); - } - return this; - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesComment.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesComment.java deleted file mode 100644 index c4e408a..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesComment.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -public interface BytesComment> { - /** - * Do these Bytes support saving comments - * - * @return true if comments are kept - */ - default boolean retainsComments() { - return false; - } - - /** - * Add comment as approriate for the toHexString format - * - * @param comment to add (or ignore) - * @return this - */ - @SuppressWarnings("unchecked") - default B comment(CharSequence comment) { - return (B) this; - } - - /** - * Adjust the indent for nested data - * - * @param n +1 indent in, -1 reduce indenting - * @return this. - */ - @SuppressWarnings("unchecked") - default B indent(int n) { - return (B) this; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesConsumer.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesConsumer.java deleted file mode 100644 index 648474e..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesConsumer.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import java.nio.BufferOverflowException; - -@FunctionalInterface -public interface BytesConsumer { - - /** - * Retrieves and removes the head of this queue, or returns {@code true} if this queue is - * empty. - * - * @param bytes to read into - * @return false if this queue is empty - */ - @SuppressWarnings("rawtypes") - boolean read(BytesOut bytes) throws BufferOverflowException; - -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesContext.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesContext.java deleted file mode 100644 index 1a5865a..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesContext.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; - -public interface BytesContext extends Closeable { - - /** - * @return a bytes to write to - */ - @SuppressWarnings("rawtypes") - Bytes bytes(); - - /** - * @return the key to be written to - */ - int key(); - - @Override - default boolean isClosed() { - throw new UnsupportedOperationException("todo"); - } - - default void rollbackOnClose() { - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesIn.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesIn.java deleted file mode 100644 index 8c32d5f..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesIn.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.util.ObjectUtils; -import org.jetbrains.annotations.NotNull; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface BytesIn extends - RandomDataInput, - ByteStringParser> { - /** - * Reads messages from this tails as methods. It returns a BooleanSupplier which returns - * - * @param objects which implement the methods serialized to the file. - * @return a reader which will read one Excerpt at a time - */ - - default MethodReader bytesMethodReader(Object... objects) { - return new BytesMethodReaderBuilder(this).build(objects); - } - - - default BytesMethodReaderBuilder bytesMethodReaderBuilder() { - return new BytesMethodReaderBuilder(this); - } - - T readMarshallableLength16(Class tClass, T object); - - default T readObject(Class componentType0) { - Class componentType = ObjectUtils.implementationToUse(componentType0); - if (BytesMarshallable.class.isAssignableFrom(componentType)) { - BytesMarshallable bm = (BytesMarshallable) ObjectUtils.newInstance(componentType); - bm.readMarshallable(this); - return (T) bm; - } - if (Enum.class.isAssignableFrom(componentType)) { - return (T) readEnum((Class) componentType); - } - switch (componentType.getName()) { - case "java.lang.String": - return (T) readUtf8(); - case "java.lang.Double": - return (T) (Double) readDouble(); - case "java.lang.Long": - return (T) (Long) readLong(); - case "java.lang.Integer": - return (T) (Integer) readInt(); - - default: - if (BytesMarshallable.class.isAssignableFrom(componentType)) { - BytesMarshallable bm = (BytesMarshallable) ObjectUtils.newInstance(componentType); - bm.readMarshallable(this); - return (T) bm; - } - throw new UnsupportedOperationException("Unsupported " + componentType); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesInternal.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesInternal.java deleted file mode 100644 index 9cd4cdb..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesInternal.java +++ /dev/null @@ -1,2702 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.pool.BytesPool; -import net.openhft.chronicle.bytes.util.DecoratedBufferUnderflowException; -import net.openhft.chronicle.bytes.util.StringInternerBytes; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.Memory; -import net.openhft.chronicle.core.UnsafeMemory; -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.io.UnsafeText; -import net.openhft.chronicle.core.pool.ClassAliasPool; -import net.openhft.chronicle.core.pool.EnumInterner; -import net.openhft.chronicle.core.pool.StringBuilderPool; -import net.openhft.chronicle.core.util.ByteBuffers; -import net.openhft.chronicle.core.util.Histogram; -import net.openhft.chronicle.core.util.StringUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UTFDataFormatException; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimeZone; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.StreamingDataOutput.JAVA9_STRING_CODER_LATIN; -import static net.openhft.chronicle.bytes.StreamingDataOutput.JAVA9_STRING_CODER_UTF16; -import static net.openhft.chronicle.core.io.ReferenceOwner.temporary; -import static net.openhft.chronicle.core.util.StringUtils.*; - -/** - * Utility methods to support common functionality in this package. This is not intended to be - * accessed directly. - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -enum BytesInternal { - ; - static final char[] HEXADECIMAL = "0123456789abcdef".toCharArray(); - static final ThreadLocal BYTE_BUFFER_TL = ThreadLocal.withInitial(() -> ByteBuffer.allocateDirect(0)); - static final ThreadLocal BYTE_BUFFER2_TL = ThreadLocal.withInitial(() -> ByteBuffer.allocateDirect(0)); - private static final byte[] MIN_VALUE_TEXT = ("" + Long.MIN_VALUE).getBytes(ISO_8859_1); - private static final StringBuilderPool SBP = new StringBuilderPool(); - private static final BytesPool BP = new BytesPool(); - private static final StringInternerBytes SI = new StringInternerBytes(4096); - private static final byte[] Infinity = "Infinity".getBytes(ISO_8859_1); - private static final byte[] NaN = "NaN".getBytes(ISO_8859_1); - private static final long MAX_VALUE_DIVIDE_5 = Long.MAX_VALUE / 5; - private static final ThreadLocal NUMBER_BUFFER = ThreadLocal.withInitial(() -> new byte[20]); - private static final long MAX_VALUE_DIVIDE_10 = Long.MAX_VALUE / 10; - private static final ThreadLocal dateCacheTL = new ThreadLocal<>(); - private static final int MAX_STRING_LEN = Integer.getInteger("bytes.max-string-len", 128 * 1024); - - static { - try { - ClassAliasPool.CLASS_ALIASES.addAlias(BytesStore.class, "!binary"); - } catch (Exception e) { - throw new AssertionError(e); - } - } - - public static boolean contentEqual( BytesStore a, BytesStore b) { - if (a == null) return b == null; - if (b == null) return false; - if (a.readRemaining() != b.readRemaining()) - return false; - // assume a >= b - if (a.realCapacity() < b.realCapacity()) - return contentEqual(b, a); - long aPos = a.readPosition(); - long bPos = b.readPosition(); - long aLength = Math.min(a.realCapacity(), a.readRemaining()); - long bLength = Math.min(b.realCapacity(), b.readRemaining()); - long i; - for (i = 0; i < bLength - 7; i += 8) { - if (a.readLong(aPos + i) != b.readLong(bPos + i)) - return false; - } - for (; i < bLength; i++) { - if (a.readByte(aPos + i) != b.readByte(bPos + i)) - return false; - } - // check for zeros - for (; i < aLength - 7; i += 8) { - if (a.readLong(aPos + i) != 0L) - return false; - } - for (; i < aLength; i++) { - if (a.readByte(aPos + i) != 0) - return false; - } - - return true; - } - - static boolean startsWith( BytesStore a, BytesStore b) { - if (a.readRemaining() < b.readRemaining()) - return false; - long aPos = a.readPosition(); - long bPos = b.readPosition(); - long length = b.readRemaining(); - long i; - for (i = 0; i < length - 7; i += 8) { - if (a.readLong(aPos + i) != b.readLong(bPos + i)) - return false; - } - if (i < length - 3) { - if (a.readInt(aPos + i) != b.readInt(bPos + i)) - return false; - i += 4; - } - if (i < length - 1) { - if (a.readShort(aPos + i) != b.readShort(bPos + i)) - return false; - i += 2; - } - if (i < length) { - return a.readByte(aPos + i) == b.readByte(bPos + i); -// i ++; - } - return true; - } - - public static void parseUtf8( - StreamingDataInput bytes, Appendable appendable, boolean utf, int length) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - if (appendable instanceof StringBuilder - && bytes.isDirectMemory() - && length < 1 << 20 - && utf) { - // todo fix, a problem with very long sequences. #35 - parseUtf8_SB1((Bytes) bytes, (StringBuilder) appendable, utf, length); - } else { - parseUtf81(bytes, appendable, utf, length); - } - } - - public static void parseUtf8( - RandomDataInput input, long offset, Appendable appendable, boolean utf, int length) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - - assert utf; - if (appendable instanceof StringBuilder) { - if (input instanceof NativeBytesStore) { - parseUtf8_SB1((NativeBytesStore) input, offset, (StringBuilder) appendable, length); - return; - } else if (input instanceof Bytes - && ((Bytes) input).bytesStore() instanceof NativeBytesStore) { - NativeBytesStore bs = (NativeBytesStore) ((Bytes) input).bytesStore(); - parseUtf8_SB1(bs, offset, (StringBuilder) appendable, length); - return; - } - } - parseUtf81(input, offset, appendable, length); - } - - public static boolean compareUtf8( RandomDataInput input, long offset, CharSequence other) - throws IORuntimeException, BufferUnderflowException, IndexOutOfBoundsException { - long utfLen; - if ((utfLen = input.readByte(offset++)) < 0) { - utfLen &= 0x7FL; - long b; - int count = 7; - while ((b = input.readByte(offset++)) < 0) { - utfLen |= (b & 0x7FL) << count; - count += 7; - } - if (b != 0) { - if (count > 56) - throw new IORuntimeException( - "Cannot read more than 9 stop bits of positive value"); - utfLen |= (b << count); - } else { - if (count > 63) - throw new IORuntimeException( - "Cannot read more than 10 stop bits of negative value"); - utfLen = ~utfLen; - } - } - if (utfLen == -1) - return other == null; - return other != null && compareUtf8(input, offset, utfLen, other); - } - - private static boolean compareUtf8( - RandomDataInput input, long offset, long utfLen, CharSequence other) - throws UTFDataFormatRuntimeException, BufferUnderflowException, IndexOutOfBoundsException { - if (offset + utfLen > input.realCapacity()) - throw new BufferUnderflowException(); - int i = 0; - while (i < utfLen && i < other.length()) { - int c = input.readByte(offset + i); - if (c < 0) - break; - if ((char) c != other.charAt(i)) - return false; - i++; - } - if (i < utfLen && i < other.length()) - return compareUtf82(input, offset + i, i, utfLen, other); - return utfLen == other.length(); - } - - private static boolean compareUtf82( - RandomDataInput input, long offset, int charI, long utfLen, CharSequence other) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - long limit = offset + utfLen; - while (offset < limit && charI < other.length()) { - int c = input.readUnsignedByte(offset++); - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - if ((char) c != other.charAt(charI)) - return false; - break; - - case 12: - case 13: { - /* 110x xxxx 10xx xxxx */ - if (offset == limit) - throw new UTFDataFormatRuntimeException( - "malformed input: partial character at end"); - int char2 = input.readUnsignedByte(offset++); - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (offset - limit + utfLen) + - " was " + char2); - int c2 = (char) (((c & 0x1F) << 6) | - (char2 & 0x3F)); - if ((char) c2 != other.charAt(charI)) - return false; - break; - } - - case 14: { - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - if (offset + 2 > limit) - throw new UTFDataFormatRuntimeException( - "malformed input: partial character at end"); - int char2 = input.readUnsignedByte(offset++); - int char3 = input.readUnsignedByte(offset++); - - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (offset - limit + utfLen - 1) + - " was " + char2 + " " + char3); - int c3 = (char) (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - (char3 & 0x3F)); - if ((char) c3 != other.charAt(charI)) - return false; - break; - } - // TODO add code point of characters > 0xFFFF support. - default: - /* 10xx xxxx, 1111 xxxx */ - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (offset - limit + utfLen)); - } - charI++; - } - return offset == limit && charI == other.length(); - } - - public static void parse8bit(long offset, RandomDataInput bytesStore, Appendable appendable, int utflen) - throws BufferUnderflowException, IOException { - if (bytesStore instanceof NativeBytesStore - && appendable instanceof StringBuilder) { - parse8bit_SB1(offset, (NativeBytesStore) bytesStore, (StringBuilder) appendable, utflen); - } else { - parse8bit1(offset, bytesStore, appendable, utflen); - } - } - - public static void parseUtf81( - StreamingDataInput bytes, Appendable appendable, boolean utf, int length) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - try { - int count = 0; - assert bytes.readRemaining() >= length; - while (count < length) { - int c = bytes.readUnsignedByte(); - if (c >= 128) { - bytes.readSkip(-1); - break; - - } else if (c < 0) { - break; - } - count++; - appendable.append((char) c); - } - - if (length > count) - parseUtf82(bytes, appendable, utf, length, count); - } catch (IOException e) { - throw Jvm.rethrow(e); - } - } - - public static void parseUtf81( RandomDataInput input, long offset, - Appendable appendable, int utflen) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - try { - assert input.realCapacity() >= offset + utflen; - long limit = offset + utflen; - while (offset < limit) { - int c = input.readUnsignedByte(offset++); - if (c >= 128) { - offset--; - break; - - } else if (c < 0) { - break; - } - appendable.append((char) c); - } - - if (limit > offset) - parseUtf82(input, offset, limit, appendable, utflen); - } catch (IOException e) { - throw Jvm.rethrow(e); - } - } - - public static void parse8bit1( StreamingDataInput bytes, StringBuilder sb, int utflen) { - assert bytes.readRemaining() >= utflen; - sb.ensureCapacity(utflen); - - if (Jvm.isJava9Plus()) { - byte[] sbBytes = extractBytes(sb); - for (int count = 0; count < utflen; count++) { - int c = bytes.readUnsignedByte(); - sbBytes[count] = (byte) c; - } - } else { - char[] chars = StringUtils.extractChars(sb); - for (int count = 0; count < utflen; count++) { - int c = bytes.readUnsignedByte(); - chars[count] = (char) c; - } - } - StringUtils.setLength(sb, utflen); - } - - public static void parse8bit1( StreamingDataInput bytes, Appendable appendable, int utflen) throws IOException { - - assert bytes.readRemaining() >= utflen; - for (int count = 0; count < utflen; count++) { - int c = bytes.readUnsignedByte(); - appendable.append((char) c); - } - } - - public static void parse8bit1(long offset, RandomDataInput bytes, Appendable appendable, int utflen) - throws BufferUnderflowException, IOException { - - if (bytes.realCapacity() < utflen + offset) - throw new DecoratedBufferUnderflowException(bytes.realCapacity() + " < " + utflen + " +" + offset); - for (int count = 0; count < utflen; count++) { - int c = bytes.readUnsignedByte(offset + count); - appendable.append((char) c); - } - } - - public static void parseUtf8_SB1( Bytes bytes, StringBuilder sb, boolean utf, int utflen) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - try { - assert utf; - int count = 0; - if (utflen > bytes.readRemaining()) { - final BufferUnderflowException bue = new BufferUnderflowException(); - bue.initCause(new IllegalStateException("utflen: " + utflen + ", readRemaining: " + bytes.readRemaining())); - throw bue; - } - long readPosition = bytes.readPosition(); - sb.ensureCapacity(utflen); - - if (Jvm.isJava9Plus()) { - sb.setLength(utflen); - while (count < utflen) { - byte c = bytes.readByte(readPosition + count); - if (c < 0) - break; - sb.setCharAt(count++, (char) c); // This is not as fast as it could be. - } - } else { - char[] chars = extractChars(sb); - while (count < utflen) { - int c = bytes.readByte(readPosition + count); - if (c < 0) - break; - chars[count++] = (char) c; - } - } - bytes.readSkip(count); - setCount(sb, count); - if (count < utflen) { - - long rp0 = bytes.readPosition(); - try { - parseUtf82(bytes, sb, utf, utflen, count); - } catch (UTFDataFormatRuntimeException e) { - long rp = Math.max(rp0 - 128, 0); - throw new UTFDataFormatRuntimeException(Long.toHexString(rp0) + "\n" + bytes.toHexString(rp, 200), e); - } - } - } catch (IOException e) { - - throw Jvm.rethrow(e); - } - } - - public static void parseUtf8_SB1( NativeBytesStore bytes, long offset, - StringBuilder sb, int utflen) - throws UTFDataFormatRuntimeException, BufferUnderflowException { - try { - if (offset + utflen > bytes.realCapacity()) - throw new BufferUnderflowException(); - long address = bytes.address + bytes.translate(offset); - Memory memory = bytes.memory; - sb.ensureCapacity(utflen); - int count = 0; - - if (Jvm.isJava9Plus()) { - sb.setLength(utflen); - while (count < utflen) { - byte c = memory.readByte(address + count); - if (c < 0) - break; - sb.setCharAt(count++, (char) c); - } - } else { - char[] chars = extractChars(sb); - while (count < utflen) { - int c = memory.readByte(address + count); - if (c < 0) - break; - chars[count++] = (char) c; - } - } - setCount(sb, count); - if (count < utflen) - parseUtf82(bytes, offset + count, offset + utflen, sb, utflen); - assert bytes.memory != null; - } catch (IOException e) { - throw Jvm.rethrow(e); - } - } - - public static int parse8bit_SB1(long offset, NativeBytesStore nbs, StringBuilder sb, int length) { - long address = nbs.address + nbs.translate(offset); - Memory memory = nbs.memory; - sb.ensureCapacity(length); - int count = 0; - - if (Jvm.isJava9Plus()) { - byte coder = getStringCoder(sb); - - if (coder == JAVA9_STRING_CODER_LATIN) { - byte[] bytes = extractBytes(sb); - while (count < length) { - byte b = memory.readByte(address + count); - bytes[count++] = b; - } - } else { - assert coder == JAVA9_STRING_CODER_UTF16; - sb.setLength(length); - while (count < length) { - byte b = memory.readByte(address + count); - sb.setCharAt(count++, (char) b); - } - } - } else { - char[] chars = extractChars(sb); - while (count < length) { - int c = memory.readByte(address + count) & 0xFF; - chars[count++] = (char) c; - } - } - setCount(sb, count); - return count; - } - - static void parseUtf82( StreamingDataInput bytes, Appendable appendable, boolean utf, int length, int count) - throws IOException, UTFDataFormatRuntimeException { - while (count < length) { - int c = bytes.readUnsignedByte(); - if (c < 0) - break; - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - count++; - appendable.append((char) c); - break; - - case 12: - case 13: { - /* 110x xxxx 10xx xxxx */ - count += utf ? 2 : 1; - if (count > length) - throw new UTFDataFormatRuntimeException( - "malformed input: partial character at end"); - int char2 = bytes.readUnsignedByte(); - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + count + " was " + char2); - int c2 = (char) (((c & 0x1F) << 6) | - (char2 & 0x3F)); - appendable.append((char) c2); - break; - } - - case 14: { - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - count += utf ? 3 : 1; - if (count > length) - throw new UTFDataFormatRuntimeException( - "malformed input: partial character at end"); - int char2 = bytes.readUnsignedByte(); - int char3 = bytes.readUnsignedByte(); - - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (count - 1) + " was " + char2 + " " + char3); - int c3 = (char) (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - (char3 & 0x3F)); - appendable.append((char) c3); - break; - } - // TODO add code point of characters > 0xFFFF support. - default: - /* 10xx xxxx, 1111 xxxx */ - - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + count); - } - } - } - - static void parseUtf82( RandomDataInput input, long offset, long limit, - Appendable appendable, int utflen) - throws IOException, UTFDataFormatRuntimeException, BufferUnderflowException { - while (offset < limit) { - int c = input.readUnsignedByte(offset++); - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - appendable.append((char) c); - break; - - case 12: - case 13: { - /* 110x xxxx 10xx xxxx */ - if (offset == limit) - throw new UTFDataFormatRuntimeException( - "malformed input: partial character at end"); - int char2 = input.readUnsignedByte(offset++); - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (offset - limit + utflen) + - " was " + char2); - int c2 = (char) (((c & 0x1F) << 6) | - (char2 & 0x3F)); - appendable.append((char) c2); - break; - } - - case 14: { - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - if (offset + 2 > limit) - throw new UTFDataFormatRuntimeException( - "malformed input: partial character at end"); - int char2 = input.readUnsignedByte(offset++); - int char3 = input.readUnsignedByte(offset++); - - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (offset - limit + utflen - 1) + - " was " + char2 + " " + char3); - int c3 = (char) (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - (char3 & 0x3F)); - appendable.append((char) c3); - break; - } - // TODO add code point of characters > 0xFFFF support. - default: - /* 10xx xxxx, 1111 xxxx */ - throw new UTFDataFormatRuntimeException( - "malformed input around byte " + (offset - limit + utflen)); - } - } - } - - public static void writeUtf8( StreamingDataOutput bytes, String str) - throws BufferOverflowException { - if (str == null) { - bytes.writeStopBit(-1); - return; - } - - try { - if (Jvm.isJava9Plus()) { - byte[] strBytes = extractBytes(str); - byte coder = StringUtils.getStringCoder(str); - long utfLength = AppendableUtil.findUtf8Length(strBytes, coder); - bytes.writeStopBit(utfLength); - bytes.appendUtf8(strBytes, 0, str.length(), coder); - } else { - char[] chars = extractChars(str); - long utfLength = AppendableUtil.findUtf8Length(chars); - bytes.writeStopBit(utfLength); - bytes.appendUtf8(chars, 0, chars.length); - } - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - @ForceInline - public static void writeUtf8( StreamingDataOutput bytes, CharSequence str) - throws BufferOverflowException { - if (str instanceof String) { - writeUtf8(bytes, (String) str); - return; - } - if (str == null) { - bytes.writeStopBit(-1); - - } else { - try { - long utfLength = AppendableUtil.findUtf8Length(str); - bytes.writeStopBit(utfLength); - appendUtf8(bytes, str, 0, str.length()); - } catch (IndexOutOfBoundsException e) { - throw new AssertionError(e); - } - } - } - - @ForceInline - public static long writeUtf8( RandomDataOutput out, long offset, - CharSequence str) - throws BufferOverflowException { - if (str == null) { - offset = writeStopBit(out, offset, -1); - - } else { - try { - int strLength = str.length(); - if (strLength < 32) { - long lenOffset = offset; - offset = appendUtf8(out, offset + 1, str, 0, strLength); - long utfLength = offset - lenOffset - 1; - assert utfLength <= 127; - writeStopBit(out, lenOffset, utfLength); - } else { - long utfLength = AppendableUtil.findUtf8Length(str); - offset = writeStopBit(out, offset, utfLength); - if (utfLength == strLength) { - append8bit(offset, out, str, 0, strLength); - offset += utfLength; - } else { - offset = appendUtf8(out, offset, str, 0, strLength); - } - } - } catch (IndexOutOfBoundsException | BufferUnderflowException | IllegalArgumentException e) { - throw new AssertionError(e); - } - } - return offset; - } - - @ForceInline - public static long writeUtf8( RandomDataOutput out, long offset, - CharSequence str, int maxUtf8Len) - throws BufferOverflowException { - if (str == null) { - offset = writeStopBit(out, offset, -1); - - } else { - try { - int strLength = str.length(); - long utfLength = AppendableUtil.findUtf8Length(str); - if (utfLength > maxUtf8Len) { - throw new IllegalArgumentException("Attempted to write a char sequence of " + - "utf8 size " + utfLength + ": \"" + str + - "\", when only " + maxUtf8Len + " allowed"); - } - offset = writeStopBit(out, offset, utfLength); - if (utfLength == strLength) { - append8bit(offset, out, str, 0, strLength); - offset += utfLength; - } else { - offset = appendUtf8(out, offset, str, 0, strLength); - } - } catch (IndexOutOfBoundsException | IllegalArgumentException | BufferUnderflowException e) { - throw new AssertionError(e); - } - } - return offset; - } - - - public static Bytes asBytes( RandomDataOutput bytes, long position, long limit) - throws IllegalStateException, BufferOverflowException, BufferUnderflowException { - Bytes sbytes = bytes.bytesForWrite(); - sbytes.writeLimit(limit); - sbytes.readLimit(limit); - sbytes.readPosition(position); - return sbytes; - } - - public static void appendUtf8( StreamingDataOutput bytes, - CharSequence str, int offset, int length) - throws IndexOutOfBoundsException, BufferOverflowException { - int i; - for (i = 0; i < length; i++) { - char c = str.charAt(offset + i); - if (c > 0x007F) - break; - bytes.rawWriteByte((byte) c); - } - appendUtf82(bytes, str, offset, length, i); - } - - private static void appendUtf82( StreamingDataOutput bytes, - CharSequence str, int offset, int length, int i) - throws IndexOutOfBoundsException, BufferOverflowException { - for (; i < length; i++) { - char c = str.charAt(offset + i); - appendUtf8Char(bytes, c); - } - } - - public static long appendUtf8( RandomDataOutput out, long outOffset, - CharSequence str, int strOffset, int length) - throws IndexOutOfBoundsException, BufferOverflowException { - int i; - for (i = 0; i < length; i++) { - char c = str.charAt(strOffset + i); - if (c > 0x007F) - break; - out.writeByte(outOffset++, (byte) c); - } - return appendUtf82(out, outOffset, str, strOffset, length, i); - } - - private static long appendUtf82( RandomDataOutput out, long outOffset, - CharSequence str, int strOffset, int length, int i) - throws IndexOutOfBoundsException, BufferOverflowException { - for (; i < length; i++) { - char c = str.charAt(strOffset + i); - outOffset = appendUtf8Char(out, outOffset, c); - } - return outOffset; - } - - public static void append8bit(long offsetInRDO, RandomDataOutput bytes, CharSequence str, int offset, int length) - throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, - IndexOutOfBoundsException { - if (bytes instanceof VanillaBytes) { - VanillaBytes vb = (VanillaBytes) bytes; - if (str instanceof RandomDataInput) { - vb.write(offsetInRDO, (RandomDataInput) str, offset, length); - return; - } - if (str instanceof String) { - vb.write(offsetInRDO, str, offset, length); - return; - } - } - for (int i = 0; i < length; i++) { - char c = str.charAt(offset + i); - if (c > 255) c = '?'; - bytes.writeUnsignedByte(offsetInRDO + i, c); - } - } - - public static void appendUtf8Char( StreamingDataOutput bytes, int c) - throws BufferOverflowException { - if (c <= 0x007F) { - bytes.rawWriteByte((byte) c); - - } else if (c <= 0x07FF) { - bytes.rawWriteByte((byte) (0xC0 | ((c >> 6) & 0x1F))); - bytes.rawWriteByte((byte) (0x80 | c & 0x3F)); - - } else if (c <= 0xFFFF) { - bytes.rawWriteByte((byte) (0xE0 | ((c >> 12) & 0x0F))); - bytes.rawWriteByte((byte) (0x80 | ((c >> 6) & 0x3F))); - bytes.rawWriteByte((byte) (0x80 | (c & 0x3F))); - - } else { - bytes.rawWriteByte((byte) (0xF0 | ((c >> 18) & 0x07))); - bytes.rawWriteByte((byte) (0x80 | ((c >> 12) & 0x3F))); - bytes.rawWriteByte((byte) (0x80 | ((c >> 6) & 0x3F))); - bytes.rawWriteByte((byte) (0x80 | (c & 0x3F))); - } - } - - public static long appendUtf8Char( RandomDataOutput out, long offset, int c) - throws BufferOverflowException { - if (c <= 0x007F) { - out.writeByte(offset++, (byte) c); - - } else if (c <= 0x07FF) { - out.writeByte(offset++, (byte) (0xC0 | ((c >> 6) & 0x1F))); - out.writeByte(offset++, (byte) (0x80 | c & 0x3F)); - - } else if (c <= 0xFFFF) { - out.writeByte(offset++, (byte) (0xE0 | ((c >> 12) & 0x0F))); - out.writeByte(offset++, (byte) (0x80 | ((c >> 6) & 0x3F))); - out.writeByte(offset++, (byte) (0x80 | (c & 0x3F))); - - } else { - out.writeByte(offset++, (byte) (0xF0 | ((c >> 18) & 0x07))); - out.writeByte(offset++, (byte) (0x80 | ((c >> 12) & 0x3F))); - out.writeByte(offset++, (byte) (0x80 | ((c >> 6) & 0x3F))); - out.writeByte(offset++, (byte) (0x80 | (c & 0x3F))); - } - return offset; - } - - public static void writeStopBit( StreamingDataOutput out, char n) - throws BufferOverflowException { - if ((n & ~0x7F) == 0) { - out.rawWriteByte((byte) (n & 0x7f)); - return; - } - if ((n & ~0x3FFF) == 0) { - out.rawWriteByte((byte) ((n & 0x7f) | 0x80)); - out.rawWriteByte((byte) (n >> 7)); - return; - } - writeStopBit0(out, n); - } - - public static void writeStopBit( StreamingDataOutput out, long n) - throws BufferOverflowException { - if ((n & ~0x7F) == 0) { - out.rawWriteByte((byte) (n & 0x7f)); - return; - } - if ((n & ~0x3FFF) == 0) { - out.rawWriteByte((byte) ((n & 0x7f) | 0x80)); - out.rawWriteByte((byte) (n >> 7)); - return; - } - writeStopBit0(out, n); - } - - public static long writeStopBit( RandomDataOutput out, long offset, long n) - throws BufferOverflowException { - if ((n & ~0x7F) == 0) { - out.writeByte(offset++, (byte) (n & 0x7f)); - return offset; - } - if ((n & ~0x3FFF) == 0) { - out.writeByte(offset++, (byte) ((n & 0x7f) | 0x80)); - out.writeByte(offset++, (byte) (n >> 7)); - return offset; - } - return writeStopBit0(out, offset, n); - } - - @SuppressWarnings("ShiftOutOfRange") - public static void writeStopBit( StreamingDataOutput out, double d) - throws BufferOverflowException { - long n = Double.doubleToRawLongBits(d); - while ((n & (~0L >>> 7)) != 0) { - out.rawWriteByte((byte) (((n >>> -7) & 0x7F) | 0x80)); - n <<= 7; - } - out.rawWriteByte((byte) ((n >>> -7) & 0x7F)); - } - - public static double readStopBitDouble( StreamingDataInput in) { - long n = 0; - int shift = 64 - 7; - int b; - do { - b = in.readByte(); - n |= shift > 0 ? (long) (b & 0x7F) << shift : b >> -shift; - shift -= 7; - } while (b < 0); - return Double.longBitsToDouble(n); - } - - static void writeStopBit0( StreamingDataOutput out, long n) - throws BufferOverflowException { - boolean neg = false; - if (n < 0) { - neg = true; - n = ~n; - } - - long n2; - while ((n2 = n >>> 7) != 0) { - out.rawWriteByte((byte) (0x80L | n)); - n = n2; - } - // final byte - if (!neg) { - out.rawWriteByte((byte) n); - - } else { - out.rawWriteByte((byte) (0x80L | n)); - out.rawWriteByte((byte) 0); - } - } - - static long writeStopBit0( RandomDataOutput out, long offset, long n) - throws BufferOverflowException { - boolean neg = false; - if (n < 0) { - neg = true; - n = ~n; - } - - long n2; - while ((n2 = n >>> 7) != 0) { - out.writeByte(offset++, (byte) (0x80L | n)); - n = n2; - } - // final byte - if (!neg) { - out.writeByte(offset++, (byte) n); - - } else { - out.writeByte(offset++, (byte) (0x80L | n)); - out.writeByte(offset++, (byte) 0); - } - return offset; - } - - static int stopBitLength0(long n) { - int len = 0; - if (n < 0) { - len = 1; - n = ~n; - } - - while ((n >>>= 7) != 0) len++; - return len + 1; - } - - public static String toDebugString( RandomDataInput bytes, long maxLength) { - if (bytes.refCount() < 1) - // added because something is crashing the JVM - return ""; - ReferenceOwner toDebugString = temporary("toDebugString"); - bytes.reserve(toDebugString); - try { - int len = Maths.toUInt31(maxLength + 40); - StringBuilder sb = new StringBuilder(len); - long readPosition = bytes.readPosition(); - long readLimit = bytes.readLimit(); - sb.append("[") - .append("pos: ").append(readPosition) - .append(", rlim: ").append(readLimit) - .append(", wlim: ").append(asSize(bytes.writeLimit())) - .append(", cap: ").append(asSize(bytes.capacity())) - .append(" ] "); - try { - long start = Math.max(bytes.start(), readPosition - 64); - long end = Math.min(readLimit + 64, start + maxLength); - // should never try to read past the end of the buffer - end = Math.min(end, bytes.realCapacity()); - try { - for (; end >= start + 16 && end >= readLimit + 16; end -= 8) { - if (bytes.readLong(end - 8) != 0) - break; - } - } catch ( UnsupportedOperationException | BufferUnderflowException ignored) { - // ignore - } - toString(bytes, sb, start, readPosition, bytes.writePosition(), end); - if (end < bytes.readLimit()) - sb.append("..."); - } catch (Exception e) { - sb.append(' ').append(e); - } - return sb.toString(); - - } finally { - bytes.release(toDebugString); - } - } - - - public static Object asSize(long size) { - return size == Bytes.MAX_CAPACITY ? "8EiB" : size; - } - - public static String to8bitString( BytesStore bytes) - throws IllegalArgumentException { - long pos = bytes.readPosition(); - int len = Maths.toInt32(bytes.readRemaining()); - char[] chars = new char[len]; - if (bytes instanceof VanillaBytes) { - ((VanillaBytes) bytes).read8Bit(chars, len); - } else { - for (int i = 0; i < len; i++) - try { - chars[i] = (char) bytes.readUnsignedByte(pos + i); - } catch (Exception e) { - return new String(chars, 0, len) + ' ' + e; - } - } - return StringUtils.newString(chars); - } - - - public static String toString( RandomDataInput bytes) - throws IllegalStateException { - - // the output will be no larger than this - final long available = - Math.min(bytes.realCapacity() - bytes.start(), bytes.readRemaining()); - final int size = (int) Math.min(available, MAX_STRING_LEN - 3); - final StringBuilder sb = new StringBuilder(size); - - if (bytes.readRemaining() > size) { - final Bytes bytes1 = bytes.bytesForRead(); - try { - bytes1.readLimit(bytes1.readPosition() + size); - toString(bytes1, sb); - if (size < available) - sb.append("..."); - return sb.toString(); - } finally { - bytes1.releaseLast(); - } - } else { - toString(bytes, sb); - return sb.toString(); - } - } - - private static void toString( RandomDataInput bytes, - Appendable sb, - long start, - long readPosition, - long writePosition, - long end) throws BufferUnderflowException { - try { - // before - if (start < bytes.start()) start = bytes.start(); - long realCapacity = bytes.realCapacity(); - if (end > realCapacity) end = realCapacity; - if (readPosition >= start && bytes instanceof Bytes) { - long last = Math.min(readPosition, end); - toString(bytes, sb, start, last); - sb.append('\u01C1'); - } - toString(bytes, sb, Math.max(readPosition, start), Math.min(writePosition, end)); - if (writePosition <= end) { - if (bytes instanceof Bytes) - sb.append('\u2021'); - toString(bytes, sb, writePosition, end); - } - } catch (Exception e) { - try { - sb.append(' ').append(e.toString()); - } catch (IOException e1) { - throw new AssertionError(e); - } - } - } - - private static void toString( RandomDataInput bytes, Appendable sb, long start, long last) - throws IOException { - for (long i = start; i < last; i++) { - sb.append(bytes.printable(i)); - } - } - - private static void toString( RandomDataInput bytes, StringBuilder sb) - throws IllegalStateException { - ReferenceOwner toString = temporary("toString"); - bytes.reserve(toString); - assert bytes.start() <= bytes.readPosition(); - assert bytes.readPosition() <= bytes.readLimit(); - long limit = Math.min(bytes.readLimit(), bytes.realCapacity()); - - try { - for (long i = bytes.readPosition(); i < limit; i++) { - sb.append((char) bytes.readUnsignedByte(i)); - } - } catch (BufferUnderflowException e) { - sb.append(' ').append(e); - - } finally { - bytes.release(toString); - } - } - - @ForceInline - public static char readStopBitChar( StreamingDataInput in) - throws IORuntimeException { - int l; - if ((l = in.rawReadByte()) >= 0) - return (char) l; - return (char) readStopBit0(in, l); - } - - @ForceInline - public static long readStopBit( StreamingDataInput in) - throws IORuntimeException { - long l; - if ((l = in.rawReadByte()) >= 0) - return l; - return readStopBit0(in, l); - } - - static long readStopBit0( StreamingDataInput in, long l) - throws IORuntimeException { - l &= 0x7FL; - long b; - int count = 7; - while ((b = in.rawReadByte()) < 0) { - l |= (b & 0x7FL) << count; - count += 7; - } - if (b != 0) { - if (count > 56) - throw new IORuntimeException( - "Cannot read more than 9 stop bits of positive value"); - return l | (b << count); - - } else { - if (count > 63) - throw new IORuntimeException( - "Cannot read more than 10 stop bits of negative value"); - return ~l; - } - } - - public static void append( ByteStringAppender out, long num, int base) - throws IllegalArgumentException, BufferOverflowException { - if (num < 0) { - if (num == Long.MIN_VALUE) { - if (base == 10) - out.write(MIN_VALUE_TEXT); - else - out.write(Long.toString(Long.MIN_VALUE, base)); - return; - } - out.rawWriteByte((byte) '-'); - num = -num; - } - if (num == 0) { - out.rawWriteByte((byte) '0'); - - } else { - switch (base) { - case 10: - appendBase10(out, num); - break; - case 16: - appendBase16(out, num, 1); - break; - default: - out.write(Long.toString(num, base)); - break; - } - } - } - - public static void appendBase10( ByteStringAppender out, int num) - throws IllegalArgumentException, BufferOverflowException { - appendBase10(out, (long) num); - } - - public static void appendBase10( ByteStringAppender out, long num) - throws IllegalArgumentException, BufferOverflowException { - if (out.canWriteDirect(20)) { - long address = out.addressForWrite(out.writePosition()); - long address2 = UnsafeText.appendFixed(address, num); - out.writeSkip(address2 - address); - } else { - appendLong0(out, num); - } - } - - public static void appendBase16( ByteStringAppender out, long num, int minDigits) - throws IllegalArgumentException, BufferOverflowException { - byte[] numberBuffer = NUMBER_BUFFER.get(); - int len = 0; - do { - int digit = (int) (num & 0xF); - num >>>= 4; - numberBuffer[len++] = (byte) HEXADECIMAL[digit]; - } while (--minDigits > 0 | num > 0); - for (int i = len - 1; i >= 0; i--) - out.rawWriteByte(numberBuffer[i]); - } - - public static void appendDecimal( ByteStringAppender out, long num, int decimalPlaces) - throws IllegalArgumentException, BufferOverflowException { - if (decimalPlaces == 0) { - appendBase10(out, num); - return; - } - - byte[] numberBuffer = NUMBER_BUFFER.get(); - int endIndex; - if (num < 0) { - if (num == Long.MIN_VALUE) { - numberBuffer = MIN_VALUE_TEXT; - endIndex = MIN_VALUE_TEXT.length; - } else { - out.rawWriteByte((byte) '-'); - num = -num; - endIndex = appendLong1(numberBuffer, num); - } - } else { - endIndex = appendLong1(numberBuffer, num); - } - int digits = numberBuffer.length - endIndex; - if (decimalPlaces >= digits) { - out.writeUnsignedByte('0'); - out.writeUnsignedByte('.'); - while (decimalPlaces-- > digits) - out.writeUnsignedByte('0'); - out.write(numberBuffer, endIndex, digits); - return; - } - - int decimalLength = numberBuffer.length - endIndex - decimalPlaces; - out.write(numberBuffer, endIndex, decimalLength); - out.writeUnsignedByte('.'); - out.write(numberBuffer, endIndex + decimalLength, digits - decimalLength); - } - - public static void prepend( BytesPrepender out, long num) - throws IllegalArgumentException, BufferOverflowException { - boolean neg = false; - if (num < 0) { - if (num == Long.MIN_VALUE) { - out.prewrite(MIN_VALUE_TEXT); - return; - } - neg = true; - num = -num; - } - do { - out.prewriteByte((byte) (num % 10 + '0')); - num /= 10; - } while (num > 0); - if (neg) - out.prewriteByte((byte) '-'); - } - - /** - * The length of the number must be fixed otherwise short numbers will not overwrite longer - * numbers - */ - public static void append( RandomDataOutput out, long offset, long num, int digits) - throws BufferOverflowException, IllegalArgumentException { - boolean negative = num < 0; - num = Math.abs(num); - - for (int i = digits - 1; i > 0; i--) { - out.writeByte(offset + i, (byte) (num % 10 + '0')); - num /= 10; - } - if (negative) { - if (num != 0) - numberTooLarge(digits); - out.writeByte(offset, '-'); - - } else { - if (num > 9) - numberTooLarge(digits); - out.writeByte(offset, (byte) (num % 10 + '0')); - } - } - - /** - * Appends given long value with given decimalPlaces to RandomDataOutput out - * - * @param width Maximum width. I will be padded with zeros to the left if necessary - */ - public static void appendDecimal( RandomDataOutput out, long num, long offset, int decimalPlaces, int width) - throws IORuntimeException, IllegalArgumentException, BufferOverflowException { - if (decimalPlaces == 0) { - append(out, offset, num, width); - return; - } - - byte[] numberBuffer = NUMBER_BUFFER.get(); - int endIndex; - if (num < 0) { - if (num == Long.MIN_VALUE) { - numberBuffer = MIN_VALUE_TEXT; - endIndex = MIN_VALUE_TEXT.length; - } else { - out.writeByte(offset++, (byte) '-'); - num = -num; - endIndex = appendLong1(numberBuffer, num); - } - } else { - endIndex = appendLong1(numberBuffer, num); - } - int digits = numberBuffer.length - endIndex; - - if (decimalPlaces >= digits) { - int numDigitsRequired = 2 + decimalPlaces; - if (numDigitsRequired > width) - throw new IllegalArgumentException("Value do not fit in " + width + " digits"); - out.writeUnsignedByte(offset++, '0'); - out.writeUnsignedByte(offset++, '.'); - while (decimalPlaces-- > digits) - out.writeUnsignedByte(offset++, '0'); - out.write(offset++, numberBuffer, endIndex, digits); - return; - } else { - int numDigitsRequired = digits + 1; - if (numDigitsRequired > width) - throw new IllegalArgumentException("Value do not fit in " + width + " digits"); - } - - while (width-- > (digits + 1)) { - out.writeUnsignedByte(offset++, '0'); - } - - int decimalLength = numberBuffer.length - endIndex - decimalPlaces; - out.write(offset, numberBuffer, endIndex, decimalLength); - offset += decimalLength; - out.writeUnsignedByte(offset++, '.'); - out.write(offset++, numberBuffer, endIndex + decimalLength, digits - decimalLength); - } - - private static void numberTooLarge(int digits) throws IllegalArgumentException { - throw new IllegalArgumentException("Number too large for " + digits + "digits"); - } - - private static void appendInt0( StreamingDataOutput out, int num) - throws IllegalArgumentException, BufferOverflowException { - if (num < 10) { - out.rawWriteByte((byte) ('0' + num)); - - } else if (num < 100) { - out.writeShort((short) (num / 10 + (num % 10 << 8) + '0' * 0x101)); - - } else { - byte[] numberBuffer = NUMBER_BUFFER.get(); - // Extract digits into the end of the numberBuffer - int endIndex = appendInt1(numberBuffer, num); - - // Bulk copy the digits into the front of the buffer - out.write(numberBuffer, endIndex, numberBuffer.length - endIndex); - } - } - - private static void appendLong0( StreamingDataOutput out, long num) - throws IllegalArgumentException, BufferOverflowException { - if (num < 0) { - if (num == Long.MIN_VALUE) { - out.write(MIN_VALUE_TEXT); - return; - } - out.rawWriteByte((byte) '-'); - num = -num; - } - if (num < 10) { - out.rawWriteByte((byte) ('0' + num)); - - } else if (num < 100) { - out.writeShort((short) (num / 10 + (num % 10 << 8) + '0' * 0x101)); - - } else { - byte[] numberBuffer = NUMBER_BUFFER.get(); - // Extract digits into the end of the numberBuffer - int endIndex = appendLong1(numberBuffer, num); - - // Bulk copy the digits into the front of the buffer - out.write(numberBuffer, endIndex, numberBuffer.length - endIndex); - } - } - - private static int appendInt1(byte[] numberBuffer, int num) { - numberBuffer[19] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 19; - numberBuffer[18] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 18; - numberBuffer[17] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 17; - numberBuffer[16] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 16; - numberBuffer[15] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 15; - numberBuffer[14] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 14; - numberBuffer[13] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 13; - numberBuffer[12] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 12; - numberBuffer[11] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 11; - numberBuffer[10] = (byte) (num % 10L + '0'); - return 10; - } - - private static int appendLong1(byte[] numberBuffer, long num) { - numberBuffer[19] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 19; - numberBuffer[18] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 18; - numberBuffer[17] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 17; - numberBuffer[16] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 16; - numberBuffer[15] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 15; - numberBuffer[14] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 14; - numberBuffer[13] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 13; - numberBuffer[12] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 12; - numberBuffer[11] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 11; - numberBuffer[10] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 10; - numberBuffer[9] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 9; - numberBuffer[8] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 8; - numberBuffer[7] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 7; - numberBuffer[6] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 6; - numberBuffer[5] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 5; - numberBuffer[4] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 4; - numberBuffer[3] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 3; - numberBuffer[2] = (byte) (num % 10L + '0'); - num /= 10; - if (num <= 0) - return 2; - numberBuffer[1] = (byte) (num % 10L + '0'); - return 1; - } - - public static void append( StreamingDataOutput out, double d) - throws BufferOverflowException, IllegalArgumentException { - long val = Double.doubleToRawLongBits(d); - int sign = (int) (val >>> 63); - int exp = (int) ((val >>> 52) & 2047); - long mantissa = val & ((1L << 52) - 1); - if (sign != 0) { - out.rawWriteByte((byte) '-'); - } - if (exp == 0 && mantissa == 0) { - out.rawWriteByte((byte) '0'); - out.rawWriteByte((byte) '.'); - out.rawWriteByte((byte) '0'); - return; - - } else if (exp == 2047) { - if (mantissa == 0) { - out.write(Infinity); - - } else { - out.write(NaN); - } - return; - - } else if (exp > 0) { - mantissa += 1L << 52; - } - final int shift = (1023 + 52) - exp; - if (shift > 0) { - // integer and faction - if (shift < 53) { - long intValue = mantissa >> shift; - appendLong0(out, intValue); - mantissa -= intValue << shift; - if (mantissa > 0) { - out.rawWriteByte((byte) '.'); - mantissa <<= 1; - mantissa++; - int precision = shift + 1; - long error = 1; - - long value = intValue; - int decimalPlaces = 0; - while (mantissa > error) { - // times 5*2 = 10 - mantissa *= 5; - error *= 5; - precision--; - long num = (mantissa >> precision); - value = value * 10 + num; - out.rawWriteByte((byte) ('0' + num)); - mantissa -= num << precision; - - final double parsedValue = asDouble(value, 0, sign != 0, ++decimalPlaces); - if (parsedValue == d) - break; - } - } else { - out.rawWriteByte((byte) '.'); - out.rawWriteByte((byte) '0'); - } - return; - - } else { - // faction. - out.rawWriteByte((byte) '0'); - out.rawWriteByte((byte) '.'); - mantissa <<= 6; - mantissa += (1 << 5); - int precision = shift + 6; - - long error = (1 << 5); - - long value = 0; - int decimalPlaces = 0; - while (mantissa > error) { - while (mantissa > MAX_VALUE_DIVIDE_5) { - mantissa >>>= 1; - error = (error + 1) >>> 1; - precision--; - } - // times 5*2 = 10 - mantissa *= 5; - error *= 5; - precision--; - if (precision >= 64) { - decimalPlaces++; - out.rawWriteByte((byte) '0'); - continue; - } - long num = (mantissa >>> precision); - value = value * 10 + num; - final char c = (char) ('0' + num); - assert !(c < '0' || c > '9'); - out.rawWriteByte((byte) c); - mantissa -= num << precision; - final double parsedValue = asDouble(value, 0, sign != 0, ++decimalPlaces); - if (parsedValue == d) - break; - } - return; - } - } - // large number - mantissa <<= 10; - int precision = -10 - shift; - int digits = 0; - while ((precision > 53 || mantissa > Long.MAX_VALUE >> precision) && precision > 0) { - digits++; - precision--; - long mod = mantissa % 5; - mantissa /= 5; - int modDiv = 1; - while (mantissa < MAX_VALUE_DIVIDE_5 && precision > 1) { - precision -= 1; - mantissa <<= 1; - modDiv <<= 1; - } - mantissa += modDiv * mod / 5; - } - long val2 = precision > 0 ? mantissa << precision : mantissa >>> -precision; - - appendLong0(out, val2); - for (int i = 0; i < digits; i++) - out.rawWriteByte((byte) '0'); - } - - private static double asDouble(long value, int exp, boolean negative, int deci) { - // these numbers were determined empirically. - int leading = - Long.numberOfLeadingZeros(value) - 1; - if (leading > 9) - leading = (27 + leading) >>> 2; - - int scale2 = 0; - if (leading > 0) { - scale2 = leading; - value <<= scale2; - } - double d; - if (deci > 0) { - if (deci < 28) { - long fives = Maths.fives(deci); - long whole = value / fives; - long rem = value % fives; - d = whole + (double) rem / fives; - } else { - d = value / Math.pow(5, deci); - } - } else if (deci < -27) { - d = value * Math.pow(5, -deci); - - } else if (deci < 0) { - double fives = Maths.fives(-deci); - d = value * fives; - - } else { - d = value; - } - - double scalb = Math.scalb(d, exp - deci - scale2); - return negative ? -scalb : scalb; - } - - - @ForceInline - public static String readUtf8( StreamingDataInput in) - throws BufferUnderflowException, IllegalArgumentException, IORuntimeException { - StringBuilder sb = acquireStringBuilder(); - return in.readUtf8(sb) ? SI.intern(sb) : null; - } - - - @ForceInline - public static String readUtf8( RandomDataInput in, long offset, int maxUtf8Len) - throws BufferUnderflowException, IllegalArgumentException, - IllegalStateException, IORuntimeException { - StringBuilder sb = acquireStringBuilder(); - return in.readUtf8Limited(offset, sb, maxUtf8Len) > 0 ? SI.intern(sb) : null; - } - - public static StringBuilder acquireStringBuilder() { - return SBP.acquireStringBuilder(); - } - - public static Bytes acquireBytes() { - return BP.acquireBytes(); - } - - - @ForceInline - public static String read8bit( StreamingDataInput in) - throws BufferUnderflowException, IORuntimeException { - Bytes bytes = acquireBytes(); - return in.read8bit(bytes) ? SI.intern(bytes) : null; - } - - - @ForceInline - public static String parseUtf8( StreamingDataInput bytes, StopCharTester tester) throws BufferUnderflowException { - StringBuilder utfReader = acquireStringBuilder(); - parseUtf8(bytes, utfReader, tester); - return SI.intern(utfReader); - } - - @ForceInline - public static void parseUtf8( StreamingDataInput bytes, Appendable builder, - StopCharTester tester) - throws BufferUnderflowException { - try { - if (builder instanceof StringBuilder - && bytes.isDirectMemory()) { - Bytes vb = (Bytes) bytes; - StringBuilder sb = (StringBuilder) builder; - sb.setLength(0); - readUtf8_SB1(vb, sb, tester); - } else { - AppendableUtil.setLength(builder, 0); - readUtf81(bytes, builder, tester); - } - } catch (UTFDataFormatException e) { - UTFDataFormatRuntimeException e2 = new UTFDataFormatRuntimeException("Unable to parse invalid UTF-8 code"); - e2.initCause(e); - throw e2; - - } catch (IOException e) { - throw Jvm.rethrow(e); - } - } - - private static void readUtf8_SB1( - Bytes bytes, StringBuilder appendable, StopCharTester tester) - throws IOException, BufferUnderflowException { - NativeBytesStore nb = (NativeBytesStore) bytes.bytesStore(); - int i = 0, len = Maths.toInt32(bytes.readRemaining()); - long address = nb.address + nb.translate(bytes.readPosition()); - Memory memory = nb.memory; - - if (Jvm.isJava9Plus()) { - int appendableLength = appendable.capacity(); - for (; i < len && i < appendableLength; i++) { - int c = memory.readByte(address + i); - if (c < 0) // we have hit a non-ASCII character. - break; - if (tester.isStopChar(c)) { - bytes.readSkip(i + 1); - StringUtils.setCount(appendable, i); - return; - } - appendable.append((char) c); - } - } else { - final char[] chars = StringUtils.extractChars(appendable); - for (; i < len && i < chars.length; i++) { - int c = memory.readByte(address + i); - if (c < 0) // we have hit a non-ASCII character. - break; - if (tester.isStopChar(c)) { - bytes.readSkip(i + 1); - StringUtils.setCount(appendable, i); - return; - } - chars[i] = (char) c; -// appendable.appendDouble((char) c); - } - } - StringUtils.setCount(appendable, i); - bytes.readSkip(i); - if (i < len) { - readUtf8_SB2(bytes, appendable, tester); - } - } - - private static void readUtf8_SB2( StreamingDataInput bytes, StringBuilder appendable, StopCharTester tester) - throws UTFDataFormatException { - while (true) { - int c = bytes.readUnsignedByte(); - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - if (tester.isStopChar(c)) - return; - appendable.append((char) c); - break; - - case 12: - case 13: { - /* 110x xxxx 10xx xxxx */ - int char2 = bytes.readUnsignedByte(); - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatException( - "malformed input around byte"); - int c2 = (char) (((c & 0x1F) << 6) | - (char2 & 0x3F)); - if (tester.isStopChar(c2)) - return; - appendable.append((char) c2); - break; - } - - case 14: { - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - int char2 = bytes.readUnsignedByte(); - int char3 = bytes.readUnsignedByte(); - - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw new UTFDataFormatException( - "malformed input around byte "); - int c3 = (char) (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - (char3 & 0x3F)); - if (tester.isStopChar(c3)) - return; - appendable.append((char) c3); - break; - } - - default: - /* 10xx xxxx, 1111 xxxx */ - throw new UTFDataFormatException( - "malformed input around byte "); - } - } - } - - private static void readUtf81( StreamingDataInput bytes, Appendable appendable, StopCharTester tester) - throws IOException, BufferUnderflowException { - int len = Maths.toInt32(bytes.readRemaining()); - while (len-- > 0) { - int c = bytes.rawReadByte() & 0xff; - if (c >= 128) { - bytes.readSkip(-1); - break; - } - if (tester.isStopChar(c)) - return; - appendable.append((char) c); - } - if (len <= 0) - return; - - readUtf82(bytes, appendable, tester); - } - - private static void readUtf82( StreamingDataInput bytes, Appendable appendable, StopCharTester tester) - throws IOException { - while (true) { - int c = bytes.readUnsignedByte(); - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - /* 0xxxxxxx */ - if (tester.isStopChar(c)) - return; - appendable.append((char) c); - break; - - case 12: - case 13: { - /* 110x xxxx 10xx xxxx */ - int char2 = bytes.readUnsignedByte(); - if ((char2 & 0xC0) != 0x80) - throw new UTFDataFormatException( - "malformed input around byte"); - int c2 = (char) (((c & 0x1F) << 6) | - (char2 & 0x3F)); - if (tester.isStopChar(c2)) - return; - appendable.append((char) c2); - break; - } - - case 14: { - /* 1110 xxxx 10xx xxxx 10xx xxxx */ - int char2 = bytes.readUnsignedByte(); - int char3 = bytes.readUnsignedByte(); - - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) - throw new UTFDataFormatException( - "malformed input around byte "); - int c3 = (char) (((c & 0x0F) << 12) | - ((char2 & 0x3F) << 6) | - (char3 & 0x3F)); - if (tester.isStopChar(c3)) - return; - appendable.append((char) c3); - break; - } - - default: - /* 10xx xxxx, 1111 xxxx */ - throw new UTFDataFormatException( - "malformed input around byte "); - } - } - } - - @ForceInline - public static void parseUtf8( StreamingDataInput bytes, Appendable builder, StopCharsTester tester) - throws IllegalArgumentException, BufferUnderflowException, IORuntimeException { - AppendableUtil.setLength(builder, 0); - try { - AppendableUtil.readUtf8AndAppend(bytes, builder, tester); - } catch (IOException e) { - throw Jvm.rethrow(e); - } - } - - @ForceInline - public static void parse8bit( StreamingDataInput bytes, StringBuilder builder, StopCharsTester tester) { - builder.setLength(0); - AppendableUtil.read8bitAndAppend(bytes, builder, tester); - } - - @ForceInline - public static void parse8bit( StreamingDataInput bytes, Bytes builder, StopCharsTester tester) - throws BufferUnderflowException, BufferOverflowException { - builder.readPosition(0); - - read8bitAndAppend(bytes, builder, tester); - } - - @ForceInline - public static void parse8bit( StreamingDataInput bytes, StringBuilder builder, StopCharTester tester) { - builder.setLength(0); - read8bitAndAppend(bytes, builder, tester); - } - - @ForceInline - public static void parse8bit( StreamingDataInput bytes, Bytes builder, StopCharTester tester) - throws BufferUnderflowException, BufferOverflowException { - builder.clear(); - - read8bitAndAppend(bytes, builder, tester); - } - - private static void read8bitAndAppend( StreamingDataInput bytes, StringBuilder appendable, StopCharTester tester) { - while (true) { - int c = bytes.readUnsignedByte(); - if (tester.isStopChar(c)) - return; - appendable.append((char) c); - if (bytes.readRemaining() == 0) - return; - } - } - - private static void read8bitAndAppend( StreamingDataInput bytes, Bytes bytes2, StopCharTester tester) - throws BufferUnderflowException, BufferOverflowException { - try { - while (true) { - int c = bytes.readUnsignedByte(); - if (tester.isStopChar(c)) - return; - bytes2.writeUnsignedByte(c); - if (bytes.readRemaining() == 0) - return; - } - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - private static void read8bitAndAppend( StreamingDataInput bytes, Bytes bytes2, StopCharsTester tester) - throws BufferUnderflowException, BufferOverflowException { - try { - int ch = bytes.readUnsignedByte(); - do { - int next = bytes.readUnsignedByte(); - if (tester.isStopChar(ch, next)) { - bytes.readSkip(-1); - return; - } - bytes2.writeUnsignedByte(ch); - ch = next; - } while (bytes.readRemaining() > 1); - - if (tester.isStopChar(ch, -1)) { - bytes.readSkip(-1); - return; - } - bytes2.writeUnsignedByte(ch); - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - public static double parseDouble( StreamingDataInput in) - throws BufferUnderflowException { - long value = 0; - int exp = 0; - boolean negative = false; - int decimalPlaces = Integer.MIN_VALUE; - int ch; - do { - ch = in.rawReadByte() & 0xFF; - } while (ch == ' ' && in.readRemaining() > 0); - - try { - switch (ch) { - case 'N': - if (compareRest(in, "aN")) - return Double.NaN; - in.readSkip(-1); - - return Double.NaN; - case 'I': - //noinspection SpellCheckingInspection - if (compareRest(in, "nfinity")) - return Double.POSITIVE_INFINITY; - in.readSkip(-1); - return Double.NaN; - case '-': - if (compareRest(in, "Infinity")) - return Double.NEGATIVE_INFINITY; - negative = true; - ch = in.rawReadByte(); - break; - } - int tens = 0; - while (true) { - if (ch >= '0' && ch <= '9') { - while (value >= MAX_VALUE_DIVIDE_10) { - value >>>= 1; - exp++; - } - value = value * 10 + (ch - '0'); - decimalPlaces++; - - } else if (ch == '.') { - decimalPlaces = 0; - - } else if (ch == 'E' || ch == 'e') { - tens = (int) parseLong(in); - break; - - } else { - break; - } - if (in.readRemaining() == 0) - break; - ch = in.rawReadByte(); - } - if (decimalPlaces < 0) - decimalPlaces = 0; - - decimalPlaces = decimalPlaces - tens; - - return asDouble(value, exp, negative, decimalPlaces); - } finally { - ((ByteStringParser) in).lastDecimalPlaces(decimalPlaces); - } - } - - static boolean compareRest( StreamingDataInput in, String s) - throws BufferUnderflowException { - if (s.length() > in.readRemaining()) - return false; - long position = in.readPosition(); - for (int i = 0; i < s.length(); i++) { - if (in.readUnsignedByte() != s.charAt(i)) { - in.readPosition(position); - return false; - } - } - int ch; - if (Character.isLetterOrDigit(ch = in.readUnsignedByte())) { - in.readPosition(position); - return false; - } - while (Character.isWhitespace(ch) && ch >= ' ') - ch = in.readUnsignedByte(); - return true; - } - - @ForceInline - public static long parseLong( StreamingDataInput in) - throws BufferUnderflowException { - long num = 0; - boolean negative = false; - int b = in.peekUnsignedByte(); - if (b == '0') { - in.readSkip(1); - b = in.peekUnsignedByte(); - if (b == 'x' || b == 'X') { - in.readSkip(1); - return parseLongHexaDecimal(in); - } - } - while (in.readRemaining() > 0) { - b = in.rawReadByte(); - // if (b >= '0' && b <= '9') - if ((b - ('0' + Integer.MIN_VALUE)) <= 9 + Integer.MIN_VALUE) { - num = num * 10 + b - '0'; - } else if (b == '-') { - negative = true; - } else if (b == ']' || b == '}') { - in.readSkip(-1); - break; - } else if (b == '.') { - consumeDecimals(in); - break; - } else if (b == '_') { - // ignore - } else { - break; - } - } - return negative ? -num : num; - } - - private static long parseLongHexaDecimal( StreamingDataInput in) { - long num = 0; - while (in.readRemaining() > 0) { - int b = in.readUnsignedByte(); - // if (b >= '0' && b <= '9') - if ((b - ('0' + Integer.MIN_VALUE)) <= 9 + Integer.MIN_VALUE) { - num = (num << 4) + b - '0'; - // if (b >= 'A' && b <= 'F') - } else if ((b - ('A' + Integer.MIN_VALUE)) < 6 + Integer.MIN_VALUE) { - num = (num << 4) + b - ('A' - 10); - // if (b >= 'a' && b <= 'f') - } else if ((b - ('a' + Integer.MIN_VALUE)) < 6 + Integer.MIN_VALUE) { - num = (num << 4) + b - ('a' - 10); - } else if (b == ']' || b == '}') { - in.readSkip(-1); - break; - } else if (b == '.') { - consumeDecimals(in); - break; - } else if (b == '_') { - // ignore - } else { - break; - } - } - return num; - } - - static void consumeDecimals( StreamingDataInput in) { - int b; - while (in.readRemaining() > 0) { - b = in.readUnsignedByte(); - if (b < '0' || b > '9') { - break; - } - } - } - - @ForceInline - public static long parseLongDecimal( StreamingDataInput in) - throws BufferUnderflowException { - long num = 0; - boolean negative = false; - int decimalPlaces = Integer.MIN_VALUE; - while (in.readRemaining() > 0) { - int b = in.readUnsignedByte(); - // if (b >= '0' && b <= '9') - if ((b - ('0' + Integer.MIN_VALUE)) <= 9 + Integer.MIN_VALUE) { - num = num * 10 + b - '0'; - decimalPlaces++; - } else if (b == '.') { - decimalPlaces = 0; - } else if (b == '-') { - negative = true; - } else if (b == ']' || b == '}') { - in.readSkip(-1); - break; - } else if (b == '_') { - // ignore - } else { - break; - } - } - ((ByteStringParser) in).lastDecimalPlaces(decimalPlaces); - return negative ? -num : num; - } - - @ForceInline - public static long parseHexLong( StreamingDataInput in) - throws BufferUnderflowException { - long num = 0; - while (in.readRemaining() > 0) { - int b = in.readUnsignedByte(); - // if (b >= '0' && b <= '9') - if ((b - ('0' + Integer.MIN_VALUE)) <= 9 + Integer.MIN_VALUE) { - num = num * 16 + b - '0'; - } else if ((b - ('A' + Integer.MIN_VALUE)) <= 6 + Integer.MIN_VALUE) { - num = num * 16 + b - 'A' + 10; - } else if ((b - ('a' + Integer.MIN_VALUE)) <= 6 + Integer.MIN_VALUE) { - num = num * 16 + b - 'a' + 10; - } else if (b == ']' || b == '}') { - in.readSkip(-1); - break; - } else if (b == '_') { - // ignore - } else { - break; - } - } - return num; - } - - public static long parseLong( RandomDataInput in, long offset) - throws BufferUnderflowException { - long num = 0; - boolean negative = false; - while (true) { - int b = in.peekUnsignedByte(offset++); - // if (b >= '0' && b <= '9') - if ((b - ('0' + Integer.MIN_VALUE)) <= 9 + Integer.MIN_VALUE) - num = num * 10 + b - '0'; - else if (b == '-') - negative = true; - else if (b != '_') - break; - } - return negative ? -num : num; - } - - public static boolean skipTo( ByteStringParser parser, StopCharTester tester) { - while (parser.readRemaining() > 0) { - int ch = parser.readUnsignedByte(); - if (tester.isStopChar(ch)) - return true; - } - return false; - } - - public static float addAndGetFloat( RandomDataInput in, long offset, float adding) - throws BufferUnderflowException { - try { - for (; ; ) { - int value = in.readVolatileInt(offset); - float value1 = Float.intBitsToFloat(value) + adding; - int value2 = Float.floatToRawIntBits(value1); - if (in.compareAndSwapInt(offset, value, value2)) - return value1; - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - public static double addAndGetDouble( RandomDataInput in, long offset, double adding) - throws BufferUnderflowException { - try { - for (; ; ) { - long value = in.readVolatileLong(offset); - double value1 = Double.longBitsToDouble(value) + adding; - long value2 = Double.doubleToRawLongBits(value1); - if (in.compareAndSwapLong(offset, value, value2)) - return value1; - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - public static int addAndGetInt( RandomDataInput in, long offset, int adding) - throws BufferUnderflowException { - try { - for (; ; ) { - int value = in.readVolatileInt(offset); - int value2 = value + adding; - if (in.compareAndSwapInt(offset, value, value2)) - return value2; - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - public static long addAndGetLong( RandomDataInput in, long offset, long adding) - throws BufferUnderflowException { - try { - for (; ; ) { - long value = in.readVolatileLong(offset); - long value2 = value + adding; - if (in.compareAndSwapLong(offset, value, value2)) - return value2; - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - /** - * display the hex data of Bytes from the position() to the limit() - * - * @param bytes the buffer you wish to toString() - * @return hex representation of the buffer, from example [0D ,OA, FF] - */ - public static String toHexString( final Bytes bytes, long offset, long len) - throws BufferUnderflowException { - if (len == 0) - return ""; - - int width = 16; - int[] lastLine = new int[width]; - String sep = ""; - long position = bytes.readPosition(); - long limit = bytes.readLimit(); - - try { - bytes.readPositionRemaining(offset, len); - - final StringBuilder builder = new StringBuilder(); - long start = offset / width * width; - long end = (offset + len + width - 1) / width * width; - for (long i = start; i < end; i += width) { - // check for duplicate rows - if (i + width < end) { - boolean same = true; - - for (int j = 0; j < width && i + j < offset + len; j++) { - int ch = bytes.readUnsignedByte(i + j); - same &= (ch == lastLine[j]); - lastLine[j] = ch; - } - if (i > start && same) { - sep = "........\n"; - continue; - } - } - builder.append(sep); - sep = ""; - String str = Long.toHexString(i); - for (int j = str.length(); j < 8; j++) - builder.append('0'); - builder.append(str); - for (int j = 0; j < width; j++) { - if (j == width / 2) - builder.append(' '); - if (i + j < offset || i + j >= offset + len) { - builder.append(" "); - - } else { - builder.append(' '); - int ch = bytes.readUnsignedByte(i + j); - builder.append(HEXADECIMAL[ch >> 4]); - builder.append(HEXADECIMAL[ch & 15]); - } - } - builder.append(' '); - for (int j = 0; j < width; j++) { - if (j == width / 2) - builder.append(' '); - if (i + j < offset || i + j >= offset + len) { - builder.append(' '); - - } else { - int ch = bytes.readUnsignedByte(i + j); - if (ch < ' ' || ch > 126) - ch = '\u00B7'; - builder.append((char) ch); - } - } - builder.append("\n"); - } - return builder.toString(); - } finally { - bytes.readLimit(limit); - bytes.readPosition(position); - } - } - - public static void appendTimeMillis( ByteStringAppender b, long timeInMS) - throws BufferOverflowException { - int hours = (int) (timeInMS / (60 * 60 * 1000)); - if (hours > 99) { - b.append(hours); // can have over 24 hours. - } else { - b.rawWriteByte((byte) (hours / 10 + '0')); - b.rawWriteByte((byte) (hours % 10 + '0')); - } - b.rawWriteByte((byte) ':'); - int minutes = (int) ((timeInMS / (60 * 1000)) % 60); - b.rawWriteByte((byte) (minutes / 10 + '0')); - b.rawWriteByte((byte) (minutes % 10 + '0')); - b.rawWriteByte((byte) ':'); - int seconds = (int) ((timeInMS / 1000) % 60); - b.rawWriteByte((byte) (seconds / 10 + '0')); - b.rawWriteByte((byte) (seconds % 10 + '0')); - b.rawWriteByte((byte) '.'); - int millis = (int) (timeInMS % 1000); - b.rawWriteByte((byte) (millis / 100 + '0')); - b.rawWriteByte((byte) (millis / 10 % 10 + '0')); - b.rawWriteByte((byte) (millis % 10 + '0')); - } - - public static boolean equalBytesAny( BytesStore b1, BytesStore b2, long readRemaining) - throws BufferUnderflowException { - BytesStore bs1 = b1;// OS.isWindows() ? b1 : b1.bytesStore(); - BytesStore bs2 = b2; //OS.isWindows() ? b2 : b2.bytesStore(); - - if (bs1.readRemaining() < readRemaining) - return false; - - long i = 0; - for (; i < readRemaining - 7 && - canReadBytesAt(bs1, b1.readPosition() + i, 8) && - canReadBytesAt(bs2, b2.readPosition() + i, 8); i += 8) { - long l1 = bs1.readLong(b1.readPosition() + i); - long l2 = bs2.readLong(b2.readPosition() + i); - if (l1 != l2) - return false; - } - for (; i < readRemaining && - canReadBytesAt(bs1, b1.readPosition() + i, 1) && - canReadBytesAt(bs2, b2.readPosition() + i, 1); i++) { - byte i1 = bs1.readByte(b1.readPosition() + i); - byte i2 = bs2.readByte(b2.readPosition() + i); - if (i1 != i2) - return false; - } - return true; - } - - public static void appendDateMillis( ByteStringAppender b, long timeInMS) - throws BufferOverflowException { - DateCache dateCache = dateCacheTL.get(); - if (dateCache == null) { - dateCacheTL.set(dateCache = new DateCache()); - } - long date = timeInMS / 86400000; - if (dateCache.lastDay != date) { - dateCache.lastDateStr = dateCache.dateFormat.format(new Date(timeInMS)).getBytes(ISO_8859_1); - dateCache.lastDay = date; - - } else { - assert dateCache.lastDateStr != null; - } - b.write(dateCache.lastDateStr); - } - - - public static , S extends StreamingDataInput> E readEnum( StreamingDataInput input, Class eClass) - throws BufferUnderflowException, IORuntimeException { - Bytes bytes = acquireBytes(); - input.read8bit(bytes); - - return (E) EnumInterner.ENUM_INTERNER.get(eClass).intern(bytes); - } - - public static void writeFully( RandomDataInput bytes, long offset, long length, StreamingDataOutput sdo) - throws BufferUnderflowException, BufferOverflowException { - long i = 0; - for (; i < length - 3; i += 4) - sdo.rawWriteInt(bytes.readInt(offset + i)); - for (; i < length; i++) - sdo.rawWriteByte(bytes.readByte(offset + i)); - } - - public static void copyMemory(long from, long to, int length) - throws BufferUnderflowException, BufferOverflowException { - UnsafeMemory.copyMemory(from, to, length); - } - - - public static byte[] toByteArray( RandomDataInput in) { - int len = (int) Math.min(Bytes.MAX_HEAP_CAPACITY, in.readRemaining()); - byte[] bytes = new byte[len]; - in.read(in.readPosition(), bytes, 0, bytes.length); - return bytes; - } - - public static void copy( RandomDataInput input, OutputStream output) - throws IOException { - byte[] bytes = new byte[512]; - long start = input.readPosition(); - long i = 0; - for (int len; (len = (int) input.read(start + i, bytes, 0, bytes.length)) > 0; i += len) { - output.write(bytes, 0, len); - } - } - - public static void copy( InputStream input, StreamingDataOutput output) - throws IOException, BufferOverflowException, IllegalArgumentException { - byte[] bytes = new byte[512]; - for (int len; (len = input.read(bytes)) > 0; ) { - output.write(bytes, 0, len); - } - } - - public static Boolean parseBoolean( ByteStringParser parser, StopCharTester tester) throws BufferUnderflowException { - Bytes sb = acquireBytes(); - parseUtf8(parser, sb, tester); - if (sb.length() == 0) - return null; - switch (sb.charAt(0)) { - case 't': - case 'T': - return sb.length() == 1 || StringUtils.equalsCaseIgnore(sb, "true") ? true : null; - case 'y': - case 'Y': - return sb.length() == 1 || StringUtils.equalsCaseIgnore(sb, "yes") ? true : null; - case '0': - return sb.length() == 1 ? false : null; - case '1': - return sb.length() == 1 ? true : null; - case 'f': - case 'F': - return sb.length() == 1 || StringUtils.equalsCaseIgnore(sb, "false") ? false : null; - case 'n': - case 'N': - return sb.length() == 1 || StringUtils.equalsCaseIgnore(sb, "no") ? false : null; - default: - return null; - } - } - - - public static BytesStore subBytes(RandomDataInput from, long start, long length) throws BufferUnderflowException { - try { - BytesStore ret = NativeBytesStore.nativeStore(Math.max(0, length)); - ret.write(0L, from, start, length); - return ret; - } catch (IllegalArgumentException | BufferOverflowException e) { - throw new AssertionError(e); - } - } - - public static long findByte( RandomDataInput bytes, byte stopByte) { - try { - long start = bytes.readPosition(); - long remaining = bytes.readRemaining(); - for (long i = 0; i < remaining; i++) { - if (bytes.readByte(start + i) == stopByte) - return i; - } - return -1; - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - - public static Bytes fromHexString( String s) { - Bytes in = Bytes.from(s); - try { - Bytes out = Bytes.elasticByteBuffer(); - OUTER: - while (in.readRemaining() > 0) { - in.parseHexLong(); - for (int i = 0; i < 16; i++) { - if (in.peekUnsignedByte() == ' ') { - in.readSkip(1); - if (in.peekUnsignedByte() == ' ') - break OUTER; - } - long value = in.parseHexLong(); - out.rawWriteByte((byte) value); - } - if (in.readByte(in.readPosition() - 1) <= ' ') - in.readSkip(-1); - in.skipTo(StopCharTesters.CONTROL_STOP); - } - return out; - } catch (BufferUnderflowException | BufferOverflowException e) { - throw new AssertionError(e); - } finally { - in.releaseLast(); - } - } - - public static void readHistogram( StreamingDataInput in, Histogram histogram) { - try { - int powersOf2 = Maths.toUInt31(in.readStopBit()); - int fractionBits = Maths.toUInt31(in.readStopBit()); - long overRange = in.readStopBit(); - long totalCount = in.readStopBit(); - long floor = in.readStopBit(); - histogram.init(powersOf2, fractionBits, overRange, totalCount, floor); - int length = Maths.toUInt31(in.readStopBit()); - int[] ints = histogram.sampleCount(); - for (int i = 0; i < length; i++) - ints[i] = Maths.toUInt31(in.readStopBit()); - } catch (IllegalArgumentException e) { - throw new IORuntimeException(e); - } - } - - public static void writeHistogram( StreamingDataOutput out, Histogram histogram) throws BufferOverflowException { - out.writeStopBit(histogram.powersOf2()); - out.writeStopBit(histogram.fractionBits()); - out.writeStopBit(histogram.overRange()); - out.writeStopBit(histogram.totalCount()); - out.writeStopBit(histogram.floor()); - int[] ints = histogram.sampleCount(); - out.writeStopBit(ints.length); - for (int i : ints) - out.writeStopBit(i); - } - - public static ByteBuffer asByteBuffer( BytesStore bytesStore) throws BufferUnderflowException { - return asByteBuffer(BYTE_BUFFER_TL, bytesStore); - } - - public static ByteBuffer asByteBuffer2( BytesStore bytesStore) throws BufferUnderflowException { - return asByteBuffer(BYTE_BUFFER2_TL, bytesStore); - } - - private static ByteBuffer asByteBuffer( ThreadLocal byteBufferTL, BytesStore bytesStore) throws BufferUnderflowException { - ByteBuffer byteBuffer = byteBufferTL.get(); - assignBytesStoreToByteBuffer(bytesStore, byteBuffer); - return byteBuffer; - } - - public static void assignBytesStoreToByteBuffer( BytesStore bytesStore, ByteBuffer byteBuffer) throws BufferUnderflowException { - long address = bytesStore.addressForRead(bytesStore.readPosition()); - long capacity = bytesStore.readRemaining(); - ByteBuffers.setAddressCapacity(byteBuffer, address, capacity); - byteBuffer.clear(); - } - - private static boolean canReadBytesAt( - final BytesStore bs, final long offset, final int length) { - return bs.readLimit() - offset >= length; - } - - public static String parse8bit(ByteStringParser bsp, StopCharTester stopCharTester) { - StringBuilder sb = BytesInternal.acquireStringBuilder(); - BytesInternal.parse8bit(bsp, sb, stopCharTester); - return BytesInternal.SI.intern(sb); - } - - static class DateCache { - final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); - private long lastDay = Long.MIN_VALUE; - - private byte[] lastDateStr = null; - - DateCache() { - dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMarshallable.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMarshallable.java deleted file mode 100644 index 6679481..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMarshallable.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.annotation.DontChain; -import net.openhft.chronicle.core.io.IORuntimeException; - -/** - * An object which can be read or written directly to Bytes in a streaming manner. - */ -@DontChain -public interface BytesMarshallable extends ReadBytesMarshallable, WriteBytesMarshallable { - @Override - @SuppressWarnings("rawtypes") - default void readMarshallable(BytesIn bytes) throws IORuntimeException { - BytesUtil.readMarshallable(this, bytes); - } - - @SuppressWarnings("rawtypes") - @Override - default void writeMarshallable(BytesOut bytes) { - BytesUtil.writeMarshallable(this, bytes); - } - - default String $toString() { - HexDumpBytes bytes = new HexDumpBytes(); - writeMarshallable(bytes); - String s = "# " + getClass().getName() + "\n" + bytes.toHexString(); - bytes.releaseLast(); - return s; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMarshaller.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMarshaller.java deleted file mode 100644 index 4979e17..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMarshaller.java +++ /dev/null @@ -1,759 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.ClassLocal; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.util.ObjectUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.*; -import java.util.*; -import java.util.function.Supplier; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public class BytesMarshaller { - public static final ClassLocal BYTES_MARSHALLER_CL - = ClassLocal.withInitial(BytesMarshaller::new); - private final FieldAccess[] fields; - - public BytesMarshaller( Class tClass) { - Map map = new LinkedHashMap<>(); - getAllField(tClass, map); - fields = map.values().stream() - .map(FieldAccess::create).toArray(FieldAccess[]::new); - } - - public static void getAllField( Class clazz, Map map) { - if (clazz != Object.class) - getAllField(clazz.getSuperclass(), map); - for ( Field field : clazz.getDeclaredFields()) { - if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0) - continue; - Jvm.setAccessible(field); - map.put(field.getName(), field); - } - } - - public void readMarshallable(ReadBytesMarshallable t, BytesIn in) { - for ( FieldAccess field : fields) { - field.read(t, in); - } - } - - public void writeMarshallable(WriteBytesMarshallable t, BytesOut out) { - out.indent(+1); - for ( FieldAccess field : fields) { - field.write(t, out); - } - out.indent(-1); - } - - static abstract class FieldAccess { - final Field field; - - FieldAccess(Field field) { - this.field = field; - } - - - public static Object create( Field field) { - Class type = field.getType(); - switch (type.getName()) { - case "boolean": - return new BooleanFieldAccess(field); - case "byte": - return new ByteFieldAccess(field); - case "char": - return new CharFieldAccess(field); - case "short": - return new ShortFieldAccess(field); - case "int": - return new IntegerFieldAccess(field); - case "float": - return new FloatFieldAccess(field); - case "long": - return new LongFieldAccess(field); - case "double": - return new DoubleFieldAccess(field); - default: - if (type.isArray()) { - if (type.getComponentType().isPrimitive()) { - if (type == byte[].class) - return new ByteArrayFieldAccess(field); - if (type == int[].class) - return new IntArrayFieldAccess(field); - if (type == float[].class) - return new FloatArrayFieldAccess(field); - if (type == long[].class) - return new LongArrayFieldAccess(field); - if (type == double[].class) - return new DoubleArrayFieldAccess(field); - throw new UnsupportedOperationException("TODO " + field.getType()); - } - return new ObjectArrayFieldAccess(field); - } - if (Collection.class.isAssignableFrom(type)) - return new CollectionFieldAccess(field); - if (Map.class.isAssignableFrom(type)) - return new MapFieldAccess(field); - if (BytesStore.class.isAssignableFrom(type)) - return new BytesFieldAccess(field); - if (BytesMarshallable.class.isAssignableFrom(type)) - return new BytesMarshallableFieldAccess(field); - return new ScalarFieldAccess(field); - } - } - - - static Class extractClass(Type type0) { - if (type0 instanceof Class) - return (Class) type0; - else if (type0 instanceof ParameterizedType) - return (Class) ((ParameterizedType) type0).getRawType(); - else - return Object.class; - } - - - @Override - public String toString() { - return getClass().getSimpleName() + "{" + - "field=" + field + - '}'; - } - - void write(Object o, BytesOut write) { - try { - write.comment(field.getName()); - getValue(o, write); - } catch (IllegalAccessException iae) { - throw new AssertionError(iae); - } - } - - protected abstract void getValue(Object o, BytesOut write) throws IllegalAccessException; - - void read(Object o, BytesIn read) { - try { - setValue(o, read); - } catch (IllegalAccessException iae) { - throw new AssertionError(iae); - } catch (IORuntimeException e) { - throw Jvm.rethrow(e); - } - } - - protected abstract void setValue(Object o, BytesIn read) throws IllegalAccessException, IORuntimeException; - - - protected Supplier newInstance( Class type) { - try { - return (Supplier) type.newInstance(); - } catch (IllegalAccessException e) { - throw new AssertionError(e); - } catch (InstantiationException e) { - throw Jvm.rethrow(e); - } - } - } - - static class ScalarFieldAccess extends FieldAccess { - public ScalarFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - Object o2 = field.get(o); - String s = o2 == null ? null : o2.toString(); - write.writeUtf8(s); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException, IORuntimeException { - String s = read.readUtf8(); - field.set(o, ObjectUtils.convertTo(field.getType(), s)); - } - } - - static class BytesMarshallableFieldAccess extends FieldAccess { - public BytesMarshallableFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - BytesMarshallable o2 = (BytesMarshallable) field.get(o); - assert o2 != null; - o2.writeMarshallable(write); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException, IORuntimeException { - BytesMarshallable o2 = (BytesMarshallable) field.get(o); - if (!field.getType().isInstance(o2)) - field.set(o, o2 = (BytesMarshallable) ObjectUtils.newInstance((Class) field.getType())); - - o2.readMarshallable(read); - } - } - - static class BytesFieldAccess extends FieldAccess { - public BytesFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - BytesStore bytes = (BytesStore) field.get(o); - if (bytes == null) { - write.writeStopBit(-1); - return; - } - long offset = bytes.readPosition(); - long length = bytes.readRemaining(); - write.writeStopBit(length); - write.write(bytes, offset, length); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException, IORuntimeException { - Bytes bytes = (Bytes) field.get(o); - long stopBit = read.readStopBit(); - if (stopBit == -1) { - if (bytes != null) - bytes.releaseLast(); - field.set(o, null); - return; - } - int length = Maths.toUInt31(stopBit); - Bytes bs; - if (bytes == null) { - bs = Bytes.allocateElasticOnHeap(length); - field.set(o, bs); - } else { - bs = bytes; - } - bs.clear(); - read.read(bs, length); - bs.readLimit(length); - } - } - -/* - static class ArrayFieldAccess extends FieldAccess { - private final Class componentType; - - public ArrayFieldAccess( Field field) { - super(field); - componentType = field.getType().getComponentType(); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - throw new UnsupportedOperationException("TODO"); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - throw new UnsupportedOperationException("TODO"); - } - } -*/ - - static class ObjectArrayFieldAccess extends FieldAccess { - Class componentType; - - public ObjectArrayFieldAccess(Field field) { - super(field); - componentType = field.getType().getComponentType(); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - Object[] c = (Object[]) field.get(o); - if (c == null) { - write.writeStopBit(-1); - return; - } - int size = c.length; - write.writeStopBit(size); - if (size == 0) - return; - for (int i = 0; i < size; i++) - write.writeObject(componentType, c[i]); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - Object[] c = (Object[]) field.get(o); - int length = Maths.toInt32(read.readStopBit()); - if (length < 0) { - if (c != null) - field.set(o, null); - return; - } - if (c == null) - field.set(o, c = (Object[]) Array.newInstance(field.getType().getComponentType(), length)); - else if (c.length != length) - field.set(o, c = Arrays.copyOf(c, length)); - for (int i = 0; i < length; i++) { - Object o2 = c[i]; - if (o2 instanceof BytesMarshallable) - ((BytesMarshallable) o2).readMarshallable(read); - else - c[i] = read.readObject(componentType); - } - } - } - - static class CollectionFieldAccess extends FieldAccess { - final Supplier collectionSupplier; - - private final Class componentType; - private final Class type; - - public CollectionFieldAccess( Field field) { - super(field); - type = field.getType(); - if (type == List.class || type == Collection.class) - collectionSupplier = ArrayList::new; - else if (type == SortedSet.class || type == NavigableSet.class) - collectionSupplier = TreeSet::new; - else if (type == Set.class) - collectionSupplier = LinkedHashSet::new; - else - collectionSupplier = newInstance(type); - Type genericType = field.getGenericType(); - if (genericType instanceof ParameterizedType) { - ParameterizedType pType = (ParameterizedType) genericType; - Type type0 = pType.getActualTypeArguments()[0]; - componentType = extractClass(type0); - } else { - componentType = Object.class; - } - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - Collection c = (Collection) field.get(o); - if (c == null) { - write.writeStopBit(-1); - return; - } - write.writeStopBit(c.size()); - if (c.isEmpty()) - return; - if (c instanceof RandomAccess && c instanceof List) { - List l = (List) c; - for (int i = 0, size = l.size(); i < size; i++) - write.writeObject(componentType, l.get(i)); - } else { - for (Object o2 : c) { - write.writeObject(componentType, o2); - } - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - Collection c = (Collection) field.get(o); - int length = Maths.toInt32(read.readStopBit()); - if (length < 0) { - if (c != null) - field.set(o, null); - return; - } - c.clear(); - for (int i = 0; i < length; i++) - c.add(read.readObject(componentType)); - } - } - - static class MapFieldAccess extends FieldAccess { - final Supplier collectionSupplier; - private final Class type; - - private final Class keyType; - - private final Class valueType; - - public MapFieldAccess( Field field) { - super(field); - type = field.getType(); - if (type == Map.class) - collectionSupplier = LinkedHashMap::new; - else if (type == SortedMap.class || type == NavigableMap.class) - collectionSupplier = TreeMap::new; - else - collectionSupplier = newInstance(type); - Type genericType = field.getGenericType(); - if (genericType instanceof ParameterizedType) { - ParameterizedType pType = (ParameterizedType) genericType; - Type[] actualTypeArguments = pType.getActualTypeArguments(); - keyType = extractClass(actualTypeArguments[0]); - valueType = extractClass(actualTypeArguments[1]); - - } else { - keyType = Object.class; - valueType = Object.class; - } - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - Map m = (Map) field.get(o); - if (m == null) { - write.writeStopBit(-1); - return; - } - write.writeStopBit(m.size()); - for (Map.Entry entry : m.entrySet()) { - write.writeObject(keyType, entry.getKey()); - write.writeObject(valueType, entry.getValue()); - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - Map m = (Map) field.get(o); - long length = read.readStopBit(); - if (length < 0) { - if (m != null) - field.set(o, null); - return; - } - if (m == null) { - field.set(o, m = new LinkedHashMap<>()); - } else { - m.clear(); - } - for (int i = 0; i < length; i++) { - m.put(read.readObject(keyType), read.readObject(valueType)); - } - } - } - - static class BooleanFieldAccess extends FieldAccess { - public BooleanFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeBoolean(field.getBoolean(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setBoolean(o, read.readBoolean()); - } - } - - static class ByteFieldAccess extends FieldAccess { - public ByteFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeByte(field.getByte(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setByte(o, read.readByte()); - } - } - - static class ByteArrayFieldAccess extends FieldAccess { - public ByteArrayFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - byte[] array = (byte[]) field.get(o); - if (array == null) { - write.writeInt(~0); - } else { - write.writeInt(array.length); - for (int i = 0; i < array.length; i++) - write.writeByte(array[i]); - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - int len = read.readInt(); - if (len == ~0) { - field.set(o, null); - } else if (len >= 0) { - byte[] array = (byte[]) field.get(o); - if (array == null || array.length != len) { - array = new byte[len]; - field.set(o, array); - } - for (int i = 0; i < len; i++) - array[i] = read.readByte(); - } - } - } - - static class CharFieldAccess extends FieldAccess { - public CharFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - char aChar = field.getChar(o); - if (aChar >= 65536 - 127) - write.writeStopBit(aChar - 65536); - else - write.writeStopBit(aChar); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setChar(o, read.readStopBitChar()); - } - } - - static class ShortFieldAccess extends FieldAccess { - public ShortFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeShort(field.getShort(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setShort(o, read.readShort()); - } - } - - static class IntegerFieldAccess extends FieldAccess { - public IntegerFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeInt(field.getInt(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setInt(o, read.readInt()); - } - } - - static class IntArrayFieldAccess extends FieldAccess { - public IntArrayFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - int[] array = (int[]) field.get(o); - if (array == null) { - write.writeInt(~0); - } else { - write.writeInt(array.length); - for (int i = 0; i < array.length; i++) - write.writeInt(array[i]); - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - int len = read.readInt(); - if (len == ~0) { - field.set(o, null); - } else if (len >= 0) { - int[] array = (int[]) field.get(o); - if (array == null || array.length != len) { - array = new int[len]; - field.set(o, array); - } - for (int i = 0; i < len; i++) - array[i] = read.readInt(); - } - } - } - - static class FloatFieldAccess extends FieldAccess { - public FloatFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeFloat(field.getFloat(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setFloat(o, read.readFloat()); - } - } - - static class FloatArrayFieldAccess extends FieldAccess { - public FloatArrayFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - float[] array = (float[]) field.get(o); - if (array == null) { - write.writeInt(~0); - } else { - write.writeInt(array.length); - for (int i = 0; i < array.length; i++) - write.writeFloat(array[i]); - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - int len = read.readInt(); - if (len == ~0) { - field.set(o, null); - } else if (len >= 0) { - float[] array = (float[]) field.get(o); - if (array == null || array.length != len) { - array = new float[len]; - field.set(o, array); - } - for (int i = 0; i < len; i++) - array[i] = read.readFloat(); - } - } - } - - static class LongFieldAccess extends FieldAccess { - public LongFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeLong(field.getLong(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setLong(o, read.readLong()); - } - } - - static class LongArrayFieldAccess extends FieldAccess { - public LongArrayFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - long[] array = (long[]) field.get(o); - if (array == null) { - write.writeInt(~0); - } else { - write.writeInt(array.length); - for (int i = 0; i < array.length; i++) - write.writeLong(array[i]); - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - int len = read.readInt(); - if (len == ~0) { - field.set(o, null); - } else if (len >= 0) { - long[] array = (long[]) field.get(o); - if (array == null || array.length != len) { - array = new long[len]; - field.set(o, array); - } - for (int i = 0; i < len; i++) - array[i] = read.readLong(); - } - } - } - - static class DoubleFieldAccess extends FieldAccess { - public DoubleFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - write.writeDouble(field.getDouble(o)); - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - field.setDouble(o, read.readDouble()); - } - } - - static class DoubleArrayFieldAccess extends FieldAccess { - public DoubleArrayFieldAccess(Field field) { - super(field); - } - - @Override - protected void getValue(Object o, BytesOut write) throws IllegalAccessException { - double[] array = (double[]) field.get(o); - if (array == null) { - write.writeInt(~0); - } else { - write.writeInt(array.length); - for (int i = 0; i < array.length; i++) - write.writeDouble(array[i]); - } - } - - @Override - protected void setValue(Object o, BytesIn read) throws IllegalAccessException { - int len = read.readInt(); - if (len == ~0) { - field.set(o, null); - } else if (len >= 0) { - double[] array = (double[]) field.get(o); - if (array == null || array.length != len) { - array = new double[len]; - field.set(o, array); - } - for (int i = 0; i < len; i++) - array[i] = read.readDouble(); - } - } - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodReader.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodReader.java deleted file mode 100644 index f9e9d36..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodReader.java +++ /dev/null @@ -1,94 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.io.SimpleCloseable; -import net.openhft.chronicle.core.util.InvocationTargetRuntimeException; -import net.openhft.chronicle.core.util.ObjectUtils; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.*; -import java.util.function.Consumer; - -@SuppressWarnings("rawtypes") -public class BytesMethodReader extends SimpleCloseable implements MethodReader { - private final BytesIn in; - private final BytesParselet defaultParselet; - private final List> methodEncoders = new ArrayList<>(); - private final Map> methodEncoderMap = new LinkedHashMap<>(); - - public BytesMethodReader(BytesIn in, - BytesParselet defaultParselet, - MethodEncoderLookup methodEncoderLookup, - Object[] objects) { - - this.in = in; - this.defaultParselet = defaultParselet; - - for (Object object : objects) { - for (Method method : object.getClass().getMethods()) { - MethodEncoder encoder = methodEncoderLookup.apply(method); - if (encoder != null) { - addEncoder(object, method, encoder); - } - } - } - } - - private void addEncoder(Object object, Method method, MethodEncoder encoder) { - Jvm.setAccessible(method); - Class[] parameterTypes = method.getParameterTypes(); - int count = parameterTypes.length; - BytesMarshallable[][] array = new BytesMarshallable[1][count]; - for (int i = 0; i < count; i++) { - array[0][i] = (BytesMarshallable) ObjectUtils.newInstance(parameterTypes[i]); - } - Consumer reader = in -> { - array[0] = (BytesMarshallable[]) encoder.decode(array[0], in); - try { - method.invoke(object, array[0]); - } catch (IllegalAccessException | InvocationTargetException e) { - Jvm.warn().on(getClass(), "Exception calling " + method + " " + Arrays.toString(array[0]), e); - } - }; - long messageId = encoder.messageId(); - if (messageId >= 0 && messageId < 1000) { - while (methodEncoders.size() <= messageId) - methodEncoders.add(null); - methodEncoders.set((int) messageId, reader); - } else { - methodEncoderMap.put(messageId, reader); - } - } - - @Override - public MethodReaderInterceptorReturns methodReaderInterceptorReturns() { - throw new UnsupportedOperationException(); - } - - public boolean readOne() throws InvocationTargetRuntimeException { - throwExceptionIfClosed(); - - if (in.readRemaining() < 1) - return false; - long messageId = in.readStopBit(); - Consumer consumer; - if (messageId >= 0 && messageId < methodEncoders.size()) - consumer = methodEncoders.get((int) messageId); - else - consumer = methodEncoderMap.get(messageId); - if (consumer == null) { - defaultParselet.accept(messageId, in); - } else { - consumer.accept(in); - } - - return true; - } - - @Override - public MethodReader closeIn(boolean closeIn) { - return this; - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodReaderBuilder.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodReaderBuilder.java deleted file mode 100644 index 87750cb..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodReaderBuilder.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -@SuppressWarnings("rawtypes") -public class BytesMethodReaderBuilder implements MethodReaderBuilder { - private final BytesIn in; - private BytesParselet defaultParselet = createDefaultParselet(); - private MethodEncoderLookup methodEncoderLookup = MethodEncoderLookup.BY_ANNOTATION; - - public BytesMethodReaderBuilder(BytesIn in) { - this.in = in; - } - - - static BytesParselet createDefaultParselet() { - return (msg, in) -> { - Bytes bytes = (Bytes) in; - throw new IllegalArgumentException("Unknown message type " + msg + " " + bytes.toHexString()); - }; - } - - @Override - public MethodReaderBuilder warnMissing(boolean warnMissing) { - // always true - return this; - } - - public MethodEncoderLookup methodEncoderLookup() { - return methodEncoderLookup; - } - - public BytesMethodReaderBuilder methodEncoderLookup(MethodEncoderLookup methodEncoderLookup) { - this.methodEncoderLookup = methodEncoderLookup; - return this; - } - - public BytesParselet defaultParselet() { - return defaultParselet; - } - - public BytesMethodReaderBuilder defaultParselet(BytesParselet defaultParselet) { - this.defaultParselet = defaultParselet; - return this; - } - - @Override - public MethodReaderBuilder methodReaderInterceptorReturns(MethodReaderInterceptorReturns methodReaderInterceptorReturns) { - throw new UnsupportedOperationException(); - } - - public BytesMethodReader build(Object... objects) { - return new BytesMethodReader(in, defaultParselet, methodEncoderLookup, objects); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodWriterInvocationHandler.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodWriterInvocationHandler.java deleted file mode 100644 index 2c7e611..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesMethodWriterInvocationHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; - -import java.lang.reflect.InvocationHandler; - -public interface BytesMethodWriterInvocationHandler extends InvocationHandler { - - void onClose(Closeable closeable); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesOut.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesOut.java deleted file mode 100644 index b75ed64..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesOut.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.util.ObjectUtils; -import org.jetbrains.annotations.NotNull; - -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import java.util.function.Function; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface BytesOut extends - ByteStringAppender>, - BytesPrepender>, - BytesComment> { - - /** - * Proxy an interface so each message called is written to a file for replay. - * - * @param tClass primary interface - * @param additional any additional interfaces - * @return a proxy which implements the primary interface (additional interfaces have to be - * cast) - */ - - default T bytesMethodWriter( Class tClass, Class... additional) { - Class[] interfaces = ObjectUtils.addAll(tClass, additional); - - //noinspection unchecked - return (T) Proxy.newProxyInstance(tClass.getClassLoader(), interfaces, - new BinaryBytesMethodWriterInvocationHandler(MethodEncoderLookup.BY_ANNOTATION, this)); - } - - void writeMarshallableLength16(WriteBytesMarshallable marshallable); - - /** - * Write a limit set of writeObject types. - * - * @param componentType expected. - * @param obj of componentType - */ - default void writeObject(Class componentType, Object obj) { - if (!componentType.isInstance(obj)) - throw new IllegalArgumentException("Cannot serialize " + obj.getClass() + " as an " + componentType); - if (obj instanceof BytesMarshallable) { - ((BytesMarshallable) obj).writeMarshallable(this); - return; - } - if (obj instanceof Enum) { - writeEnum((Enum) obj); - return; - } - if (obj instanceof BytesStore) { - BytesStore bs = (BytesStore) obj; - writeStopBit(bs.readRemaining()); - write(bs); - return; - } - switch (componentType.getName()) { - case "java.lang.String": - writeUtf8((String) obj); - return; - case "java.lang.Double": - writeDouble((Double) obj); - return; - case "java.lang.Long": - writeLong((Long) obj); - return; - case "java.lang.Integer": - writeInt((Integer) obj); - return; - - default: - throw new UnsupportedOperationException("Not supported " + componentType); - } - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesParselet.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesParselet.java deleted file mode 100644 index 4a875b9..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesParselet.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -@FunctionalInterface -public interface BytesParselet { - @SuppressWarnings("rawtypes") - void accept(long messageType, BytesIn in); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesPrepender.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesPrepender.java deleted file mode 100644 index 4ef0105..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesPrepender.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -public interface BytesPrepender> { - - /** - * Clear a buffer, with a given padding to allow for prepending later. clearAndPad(0) is the same as clear() - * - * @param length to pad - * @return this - * @throws BufferOverflowException if the length > capacity() - start() - */ - - B clearAndPad(long length) throws BufferOverflowException; - - /** - * Prepends a long in decimal, this method moves the readPosition() backwards. - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param value to prepend as text. - * @return this - * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded - * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer - */ - @SuppressWarnings("unchecked") - - default B prepend(long value) throws BufferOverflowException { - BytesInternal.prepend(this, value); - return (B) this; - } - - /** - * Write backward in binary a byte - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param bytes to prepend to. - */ - - B prewrite(byte[] bytes) throws BufferOverflowException; - - /** - * Write backward in binary a byte - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param bytes to prepend to. - */ - @SuppressWarnings("rawtypes") - - B prewrite(BytesStore bytes) throws BufferOverflowException; - - /** - * Write backward in binary a byte - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param b byte to prepend to. - */ - - B prewriteByte(byte b) throws BufferOverflowException; - - /** - * Write backward in binary a 2 byte int - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param i short to prepend to. - */ - - B prewriteShort(short i) throws BufferOverflowException; - - /** - * Write backward in binary a 4 byte int - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param i integer to prepend to. - */ - - B prewriteInt(int i) throws BufferOverflowException; - - /** - * Write backward in binary an 8 byte long - *

Note: it moves the readPosition not the writePosition / readLimit

- * - * @param l long to prepend to. - */ - - B prewriteLong(long l) throws BufferOverflowException; -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesRingBuffer.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesRingBuffer.java deleted file mode 100644 index f262e6c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesRingBuffer.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.io.Closeable; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.nio.BufferOverflowException; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface BytesRingBuffer extends BytesRingBufferStats, BytesConsumer, Closeable { - - Logger LOG = LoggerFactory.getLogger(BytesRingBuffer.class); - - - static BytesRingBuffer newInstance( NativeBytesStore bytesStore) { - return newInstance(bytesStore, 1); - } - - - static MultiReaderBytesRingBuffer newInstance( - NativeBytesStore bytesStore, - int numReaders) { - try { - final Class aClass = clazz(); - final Constructor constructor = aClass - .getDeclaredConstructor(BytesStore.class, int.class); - return constructor.newInstance(bytesStore, numReaders); - - } catch (Exception e) { - LOG.error("This is a a commercial feature, please contact " + - "sales@chronicle.software to unlock this feature."); - - throw Jvm.rethrow(e); - } - } - - - static Class clazz() throws ClassNotFoundException { - //noinspection AccessStaticViaInstance - return (Class) Class.forName( - "software.chronicle.enterprise.ring.EnterpriseRingBuffer"); - } - - static long sizeFor(long capacity) { - return sizeFor(capacity, 1); - } - - static long sizeFor(long capacity, int numReaders) { - try { - //noinspection AccessStaticViaInstance - final Method sizeFor = Class.forName( - "software.chronicle.enterprise.queue.ChronicleRingBuffer").getMethod("sizeFor", long.class, int.class); - return (long) sizeFor.invoke(null, capacity, numReaders); - - } catch (Exception e) { - LOG.error("This is a a commercial feature, please contact " + - "sales@chronicle.software to unlock this feature."); - - throw Jvm.rethrow(e); - } - } - - /** - * clears the ring buffer but moving the read position to the write position - */ - void clear(); - - /** - * Inserts the specified element at the tail of this queue if it is possible to do so - * immediately without exceeding the queue's capacity, - * - * @param bytes0 the {@code bytes0} that you wish to add to the ring buffer - * @return returning {@code true} upon success and {@code false} if this queue is full. - */ - boolean offer( BytesStore bytes0); - - /** - * Retrieves and removes the head of this queue, or returns {@code null} if this queue is - * empty. - * - * @param using Bytes to read into. - * @return false if this queue is empty, or a populated buffer if the element was retried - * @throws BufferOverflowException is the {@code using} buffer is not large enough - */ - @Override - boolean read( BytesOut using); - - long readRemaining(); - - boolean isEmpty(); - - BytesStore bytesStore(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesRingBufferStats.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesRingBufferStats.java deleted file mode 100644 index 4fc5d77..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesRingBufferStats.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import java.util.List; - -public interface BytesRingBufferStats { - /** - * each time the ring is read, this logs the number of bytes in the write buffer, calling this - * method resets these statistics, - * - * @return Long.MAX_VALUE if no read calls were made since the last time this method was called. - */ - long minNumberOfWriteBytesRemaining(); - - /** - * @return the total capacity in bytes - */ - long capacity(); - - long getAndClearWriteCount(); - - long getAndClearMissedWriteCount(); - - long getAndClearContentionCount(); - - List readers(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesStore.java deleted file mode 100644 index 38f421d..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesStore.java +++ /dev/null @@ -1,570 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceCounted; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.crypto.Cipher; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; - -import static java.lang.Math.min; - -/** - * A immutable reference to some bytes with fixed extents. This can be shared safely across thread - * provided the data referenced is accessed in a thread safe manner. Only offset access within the - * capacity is possible. - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface BytesStore, Underlying> - extends RandomDataInput, RandomDataOutput, ReferenceCounted, CharSequence { - - /** - * This method builds a BytesStore using the bytes in a CharSequence. This chars are encoded - * using ISO_8859_1 - * - * @param cs to convert - * @return BytesStore - */ - static BytesStore from( CharSequence cs) { - if (cs instanceof BytesStore) - return ((BytesStore) cs).copy(); - return HeapBytesStore.wrap(cs.toString().getBytes(StandardCharsets.ISO_8859_1)); - } - - /** - * Wraps a byte[]. This means there is one copy in memory. - * - * @param bytes to wrap - * @return BytesStore - */ - static HeapBytesStore wrap( byte[] bytes) { - return HeapBytesStore.wrap(bytes); - } - - /** - * Wraps a ByteBuffer which can be either on heap or off heap. - * - * @param bb to wrap - * @return BytesStore - */ - - static BytesStore wrap( ByteBuffer bb) { - return bb.isDirect() - ? NativeBytesStore.wrap(bb) - : HeapBytesStore.wrap(bb); - } - - /** - * @return a PointerBytesStore which can be set to any addressForRead - */ - - static PointerBytesStore nativePointer() { - return new PointerBytesStore(); - } - - /** - * Return the addressForRead and length as a BytesStore - * - * @param address for the start - * @param length of data - * @return as a BytesStore - */ - - static PointerBytesStore wrap(long address, long length) { - PointerBytesStore pbs = nativePointer(); - pbs.set(address, length); - return pbs; - } - - /** - * @return an empty, fixed sized Bytes - */ - static BytesStore empty() { - return NoBytesStore.noBytesStore(); - } - - /** - * @return whether it uses direct memory or not. - */ - @Override - boolean isDirectMemory(); - - /** - * @return a copy of this BytesStore. - */ - BytesStore copy(); - - /** - * @return a Bytes to wrap this ByteStore from the start() to the realCapacity(). - * @throws IllegalStateException if this Bytes has been released. - */ - @Override - - default Bytes bytesForRead() throws IllegalStateException { - try { - Bytes ret = bytesForWrite(); - ret.readLimit(writeLimit()); - ret.writeLimit(realCapacity()); - ret.readPosition(start()); - return ret; - } catch (BufferUnderflowException e) { - throw new IllegalStateException(e); - } - } - - /** - * @return a Bytes for writing to this BytesStore - */ - @Override - - default Bytes bytesForWrite() throws IllegalStateException { - return new VanillaBytes<>(this, writePosition(), writeLimit()); - } - - /** - * Returns if the {@code readPosition} is at the {@code start} and - * the {@code writeLimit} is at the {@code end}. - *

- * I.e {@code start() == readPosition() && writeLimit() == capacity()} - * - * @return if the {@code readPosition} is at the {@code start} and - * the {@code writeLimit} is at the {@code end} - */ - default boolean isClear() { - return true; - } - - /** - * @return the actual capacity available before resizing. - */ - @Override - default long realCapacity() { - return capacity(); - } - - /** - * @return The maximum limit you can set. - */ - @Override - long capacity(); - - /** - * @return the underlying object being wrapped, if there is one, or null if not. - */ - - Underlying underlyingObject(); - - /** - * Use this test to determine if an offset is considered safe. - */ - default boolean inside(long offset) { - return start() <= offset && offset < safeLimit(); - } - - default boolean inside(long offset, long buffer) { - return start() <= offset && offset + buffer < safeLimit(); - } - - /** - * @return how many bytes can be safely read, i.e. what is the real capacity of the underlying data. - */ - default long safeLimit() { - return capacity(); - } - - /** - * Copy the data to another BytesStore as long as there is space available in the destination store. - * - * @param store to copy to - * @return how many bytes were copied - */ - default long copyTo( BytesStore store) { - long readPos = readPosition(); - long writePos = store.writePosition(); - long copy = min(readRemaining(), store.capacity()); - long i = 0; - try { - for (; i < copy - 7; i += 8) - store.writeLong(writePos + i, readLong(readPos + i)); - for (; i < copy; i++) - store.writeByte(writePos + i, readByte(readPos + i)); - } catch (BufferOverflowException | BufferUnderflowException e) { - throw new AssertionError(e); - } - return copy; - } - - default void copyTo( OutputStream out) throws IOException { - BytesInternal.copy(this, out); - } - - /** - * Fill the BytesStore with zeros - * - * @param start first byte inclusive - * @param end last byte exclusive. - * @return this. - */ - @Override - - default B zeroOut(long start, long end) { - if (end <= start) - return (B) this; - if (start < start()) - start = start(); - if (end > capacity()) - end = capacity(); - long i = start; - try { - for (; i < end - 7; i += 8L) - writeLong(i, 0L); - for (; i < end; i++) - writeByte(i, 0); - } catch (BufferOverflowException | IllegalArgumentException e) { - throw new AssertionError(e); - } - return (B) this; - } - - /** - * This method is inherited from CharSequence so result should be the length of the contained - * chars sequence although it actually returns the number of underlying bytes. These 2 numbers are only the same - * if the encoding we are using is single char for single byte. - * - * @return length in bytes to read or Integer.MAX_VALUE if longer. - */ - @Override - default int length() { - return (int) Math.min(Integer.MAX_VALUE, readRemaining()); - } - - /** - * Assume ISO-8859-1 encoding, subclasses can override this. - */ - @Override - default char charAt(int index) throws IndexOutOfBoundsException { - try { - return (char) readUnsignedByte(readPosition() + index); - - } catch (BufferUnderflowException e) { - throw new IndexOutOfBoundsException((readPosition() + index) + " >= " + readLimit()); - } - } - - /** - * Not supported. - */ - - @Override - default CharSequence subSequence(int start, int end) { - throw new UnsupportedOperationException("todo"); - } - - /** - * By default the maximum length of data shown is 256 characters. Use toDebugString(long) if you want more. - * - * @return This BytesStore as a DebugString. - */ - - default String toDebugString() { - return toDebugString(512); - } - - /** - * @param maxLength the maximum len of the output - * @return This BytesStore as a DebugString. - */ - - default String toDebugString(long maxLength) { - return BytesInternal.toDebugString(this, maxLength); - } - - /** - * @return the underlying BytesStore - */ - - default BytesStore bytesStore() { - return this; - } - - /** - * Check if a portion of a BytesStore matches this one. - * - * @param bytesStore to match against - * @param length to match. - * @return true if the bytes up to min(length, this.length(), bytesStore.length()) matched. - */ - default boolean equalBytes( BytesStore bytesStore, long length) - throws BufferUnderflowException { - return length == 8 && bytesStore.length() >= 8 - ? readLong(readPosition()) == bytesStore.readLong(bytesStore.readPosition()) - : BytesInternal.equalBytesAny(this, bytesStore, length); - } - - /** - * Return the bytes sum of the readable bytes. - * - * @return unsigned byte sum. - */ - default int byteCheckSum() throws IORuntimeException { - return byteCheckSum(readPosition(), readLimit()); - } - - default int byteCheckSum(long start, long end) { - int sum = 0; - for (long i = start; i < end; i++) { - sum += readByte(i); - } - return sum & 0xFF; - } - - - /** - * Does the BytesStore end with a character? - * - * @param c to look for - * @return true if its the last character. - */ - default boolean endsWith(char c) { - try { - return readRemaining() > 0 && readUnsignedByte(readLimit() - 1) == c; - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - /** - * Does the BytesStore start with a character? - * - * @param c to look for - * @return true if its the last character. - */ - default boolean startsWith(char c) { - try { - return readRemaining() > 0 && readUnsignedByte(readPosition()) == c; - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - /** - * Compare the contents of the BytesStores. - * - * @param bytesStore to compare with - * @return true if they contain the same data. - */ - default boolean contentEquals( BytesStore bytesStore) { - return BytesInternal.contentEqual(this, bytesStore); - } - - default boolean startsWith( BytesStore bytesStore) { - return BytesInternal.startsWith(this, bytesStore); - } - - - default String to8bitString() throws IllegalArgumentException { - return BytesInternal.to8bitString(this); - } - - - /** - * Perform a not atomic add and get operation for an unsigned byte value. This method - * does not check for unsigned byte overflow. - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - */ - default int addAndGetUnsignedByteNotAtomic(long offset, int adding) throws BufferUnderflowException { - try { - int r = (readUnsignedByte(offset) + adding) & 0xFF; - writeByte(offset, (byte) r); - return r; - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - /** - * Perform a not atomic add and get operation for a short value. - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - */ - default short addAndGetShortNotAtomic(long offset, short adding) throws BufferUnderflowException { - try { - short r = (short) (readShort(offset) + adding); - writeByte(offset, r); - return r; - } catch (BufferOverflowException | IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - /** - * Perform a not atomic add and get operation for an int value. - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - */ - default int addAndGetIntNotAtomic(long offset, int adding) throws BufferUnderflowException { - try { - int r = readInt(offset) + adding; - writeInt(offset, r); - return r; - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - /** - * Perform a not atomic add and get operation for a float value. - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - */ - default double addAndGetDoubleNotAtomic(long offset, double adding) throws BufferUnderflowException { - try { - double r = readDouble(offset) + adding; - writeDouble(offset, r); - return r; - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - /** - * Perform a not atomic add and get operation for a float value. - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - */ - default float addAndGetFloatNotAtomic(long offset, float adding) throws BufferUnderflowException { - try { - float r = readFloat(offset) + adding; - writeFloat(offset, r); - return r; - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - void move(long from, long to, long length) throws BufferUnderflowException; - - /** - * Write a value which is not smaller. - * - * @param offset to write to - * @param atLeast value it is at least. - */ - default void writeMaxLong(long offset, long atLeast) throws BufferUnderflowException { - try { - for (; ; ) { - long v = readVolatileLong(offset); - if (v >= atLeast) - return; - if (compareAndSwapLong(offset, v, atLeast)) - return; - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - /** - * Write a value which is not smaller. - * - * @param offset to write to - * @param atLeast value it is at least. - */ - default void writeMaxInt(long offset, int atLeast) throws BufferUnderflowException { - try { - for (; ; ) { - int v = readVolatileInt(offset); - if (v >= atLeast) - return; - if (compareAndSwapInt(offset, v, atLeast)) - return; - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - default boolean isEmpty() { - return readRemaining() == 0; - } - - default void cipher( Cipher cipher, Bytes outBytes, ByteBuffer using1, ByteBuffer using2) throws IllegalStateException { - long readPos = outBytes.readPosition(); - try { - long writePos = outBytes.writePosition(); - BytesStore inBytes; - long size = readRemaining(); - if (this.isDirectMemory()) { - inBytes = this; - } else { - inBytes = NativeBytesStore.nativeStore(size); - this.copyTo(inBytes); - } - BytesInternal.assignBytesStoreToByteBuffer(inBytes, using1); - int outputSize = cipher.getOutputSize(Math.toIntExact(size)); - outBytes.ensureCapacity(writePos + outputSize); - outBytes.readPositionRemaining(writePos, outputSize); - BytesInternal.assignBytesStoreToByteBuffer(outBytes, using2); - int len = cipher.update(using1, using2); - len += cipher.doFinal(using1, using2); - assert len == using2.position(); - outBytes.writePosition(writePos + using2.position()); - - } catch ( Exception e) { - throw new IllegalStateException(e); - } finally { - try { - outBytes.readPosition(readPos); - } catch (BufferUnderflowException e) { - //noinspection ThrowFromFinallyBlock - throw new IllegalStateException(e); - } - } - } - - default void cipher( Cipher cipher, Bytes outBytes) throws IllegalStateException { - cipher(cipher, outBytes, BytesInternal.BYTE_BUFFER_TL.get(), BytesInternal.BYTE_BUFFER2_TL.get()); - } - - /** - * @return whether this BytesStore is writable. - */ - default boolean readWrite() { - return true; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesTextMethodTester.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesTextMethodTester.java deleted file mode 100644 index 9423da1..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesTextMethodTester.java +++ /dev/null @@ -1,115 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.util.function.Function; - -@SuppressWarnings("rawtypes") -public class BytesTextMethodTester { - private final String input; - private final Class outputClass; - private final String output; - private final Function componentFunction; - - private String setup; - private Function afterRun; - - private String expected; - private String actual; - - public BytesTextMethodTester(String input, Function componentFunction, Class outputClass, String output) { - this.input = input; - this.outputClass = outputClass; - this.output = output; - this.componentFunction = componentFunction; - } - - public String setup() { - return setup; - } - - - public BytesTextMethodTester setup(String setup) { - this.setup = setup; - return this; - } - - public Function afterRun() { - return afterRun; - } - - - public BytesTextMethodTester afterRun(Function afterRun) { - this.afterRun = afterRun; - return this; - } - - - public BytesTextMethodTester run() throws IOException { - - Bytes bytes2 = new HexDumpBytes(); - T writer = bytes2.bytesMethodWriter(outputClass); - - Object component = componentFunction.apply(writer); - Object[] components = component instanceof Object[] - ? (Object[]) component - : new Object[]{component}; - - if (setup != null) { - Bytes bytes0 = HexDumpBytes.fromText(BytesUtil.readFile(setup)); - - BytesMethodReader reader0 = bytes0.bytesMethodReaderBuilder() - .defaultParselet(this::unknownMessageId) - .build(components); - while (reader0.readOne()) { - bytes2.clear(); - } - bytes2.clear(); - } - - // expected - expected = BytesUtil.readFile(output).toString().trim().replace("\r", ""); - - Bytes text = BytesUtil.readFile(input); - for (String text2 : text.toString().split("###[^\n]*\n")) { - if (text2.trim().length() <= 0) - continue; - Bytes bytes = HexDumpBytes.fromText(text2); - - BytesMethodReader reader = bytes.bytesMethodReaderBuilder() - .defaultParselet(this::unknownMessageId) - .build(components); - - while (reader.readOne()) { - if (bytes.readRemaining() > 1) - bytes2.comment("## End Of Message"); - } - bytes.releaseLast(); - bytes2.comment("## End Of Block"); - } - bytes2.comment("## End Of Test"); - - actual = bytes2.toHexString().trim(); - if (afterRun != null) { - expected = afterRun.apply(expected); - actual = afterRun.apply(actual); - } - bytes2.releaseLast(); - return this; - } - - private void unknownMessageId(long id, BytesIn b) { - Jvm.warn().on(getClass(), "Unknown message id " + Long.toHexString(id)); - b.readPosition(b.readLimit()); - } - - public String expected() { - return expected; - } - - public String actual() { - return actual; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesUtil.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesUtil.java deleted file mode 100644 index b740d2b..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/BytesUtil.java +++ /dev/null @@ -1,331 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.ClassLocal; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.UnsafeMemory; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.IOTools; -import net.openhft.chronicle.core.util.StringUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import sun.misc.Unsafe; - -import java.io.*; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.net.URL; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static net.openhft.chronicle.core.io.IOTools.*; - -@SuppressWarnings("rawtypes") -public enum BytesUtil { - ; - private static final int[] NO_INTS = {}; - private static final ClassLocal TRIVIALLY_COPYABLE = ClassLocal.withInitial(BytesUtil::isTriviallyCopyable0); - - /** - * Is the whole class trivially copyable - * - * @param clazz to check - * @return true if the whole class is trivially copyable - */ - public static boolean isTriviallyCopyable(Class clazz) { - return TRIVIALLY_COPYABLE.get(clazz).length > 0; - } - - static int[] isTriviallyCopyable0(Class clazz) { - if (clazz.isArray()) { - Class componentType = clazz.getComponentType(); - if (componentType.isPrimitive()) - return new int[]{UnsafeMemory.UNSAFE.arrayBaseOffset(clazz)}; - return NO_INTS; - } - List fields = new ArrayList<>(); - while (clazz != null && clazz != Object.class) { - Collections.addAll(fields, clazz.getDeclaredFields()); - clazz = clazz.getSuperclass(); - } - int min = Integer.MAX_VALUE; - int max = Integer.MIN_VALUE; - for (Field field : fields) { - int modifiers = field.getModifiers(); - if (Modifier.isStatic(modifiers)) - continue; - long offset2 = UnsafeMemory.UNSAFE.objectFieldOffset(field); - int size = sizeOf(field.getType()); - min = (int) Math.min(min, offset2); - max = (int) Math.max(max, offset2 + size); - if (Modifier.isTransient(modifiers)) - return NO_INTS; - if (!field.getType().isPrimitive()) - return NO_INTS; - } - return new int[]{min, max}; - } - - /** - * Are all the fields in the range given trivially copyable - * - * @param clazz to check - * @return true if the fields in range are trivially copyable. - */ - public static boolean isTriviallyCopyable(Class clazz, int offset, int length) { - int[] ints = TRIVIALLY_COPYABLE.get(clazz); - if (ints.length == 0) - return false; - return offset >= ints[0] && (ints.length == 1 || offset + length <= ints[1]); - } - - public static int[] triviallyCopyableRange(Class clazz) { - return TRIVIALLY_COPYABLE.get(clazz); - } - - private static int sizeOf(Class type) { - return type == boolean.class || type == byte.class ? 1 - : type == short.class || type == char.class ? 2 - : type == int.class || type == float.class ? 4 - : type == long.class || type == double.class ? 8 - : Unsafe.ARRAY_OBJECT_INDEX_SCALE; - } - - public static String findFile( String name) throws FileNotFoundException { - File file = new File(name); - URL url = null; - if (!file.exists()) { - url = urlFor(name); - String file2 = url.getFile() - .replace("target/test-classes", "src/test/resources"); - file = new File(file2); - } - if (!file.exists()) - throw new FileNotFoundException(name); - return file.getAbsolutePath(); - } - - public static Bytes readFile( String name) throws IOException { - if (name.startsWith("=")) { - return Bytes.from(name.substring(1)); - } - File file = new File(name); - URL url = null; - if (!file.exists()) { - url = urlFor(name); - file = new File(url.getFile()); - } - return // name.endsWith(".gz") || !file.exists() || OS.isWindows() ? - Bytes.wrapForRead(readAsBytes(url == null ? new FileInputStream(file) : open(url))); - //: MappedFile.readOnly(file).acquireBytesForRead(0); - - } - - public static void writeFile(String file, Bytes bytes) throws IOException { - try (OutputStream os = new FileOutputStream(file)) { - os.write(bytes.underlyingObject()); - } - } - - public static boolean bytesEqual( - RandomDataInput a, long offset, - RandomDataInput second, long secondOffset, long len) - throws BufferUnderflowException { - long i = 0; - while (len - i >= 8L) { - if (a.readLong(offset + i) != second.readLong(secondOffset + i)) - return false; - i += 8L; - } - if (len - i >= 4L) { - if (a.readInt(offset + i) != second.readInt(secondOffset + i)) - return false; - i += 4L; - } - if (len - i >= 2L) { - if (a.readShort(offset + i) != second.readShort(secondOffset + i)) - return false; - i += 2L; - } - if (i < len) - return a.readByte(offset + i) == second.readByte(secondOffset + i); - return true; - } - - public static boolean bytesEqual( CharSequence cs, RandomDataInput bs, long offset, int length) { - if (cs == null || cs.length() != length) - return false; - for (int i = 0; i < length; i++) { - if (cs.charAt(i) != bs.readUnsignedByte(offset + i)) - return false; - } - return true; - } - - public static boolean equals(Object o1, Object o2) { - if (o1 == o2) return true; - if (o1 instanceof CharSequence && o2 instanceof CharSequence) - return StringUtils.isEqual((CharSequence) o1, (CharSequence) o2); - return o1 != null && o1.equals(o2); - } - - public static int asInt( String str) { - ByteBuffer bb = ByteBuffer.wrap(str.getBytes(StandardCharsets.ISO_8859_1)).order(ByteOrder.nativeOrder()); - return bb.getInt(); - } - - public static int stopBitLength(long n) { - if ((n & ~0x7F) == 0) { - return 1; - } - if ((n & ~0x3FFF) == 0) { - return 2; - } - return BytesInternal.stopBitLength0(n); - } - - - public static char[] toCharArray( Bytes bytes) { - final char[] chars = new char[Maths.toUInt31(bytes.readRemaining())]; - - for (int i = 0; i < bytes.readRemaining(); i++) { - chars[i] = (char) bytes.readUnsignedByte(i + bytes.readPosition()); - } - return chars; - } - - - public static char[] toCharArray( Bytes bytes, long position, int length) { - final char[] chars = new char[length]; - - int j = 0; - for (int i = 0; i < length; i++) { - chars[j++] = (char) bytes.readUnsignedByte(position + i); - } - return chars; - } - - public static long readStopBit( StreamingDataInput in) throws IORuntimeException { - return BytesInternal.readStopBit(in); - } - - public static void writeStopBit( StreamingDataOutput out, long n) { - BytesInternal.writeStopBit(out, n); - } - - public static void parseUtf8( - StreamingDataInput in, Appendable appendable, int utflen) - throws UTFDataFormatRuntimeException { - BytesInternal.parseUtf8(in, appendable, true, utflen); - } - - public static void appendUtf8( StreamingDataOutput out, CharSequence cs) { - BytesInternal.appendUtf8(out, cs, 0, cs.length()); - } - - // used by Chronicle FIX. - public static void appendBytesFromStart( Bytes bytes, long startPosition, StringBuilder sb) { - try { - BytesInternal.parse8bit(startPosition, bytes, sb, (int) (bytes.readPosition() - startPosition)); - sb.append('\u2016'); - sb.append(bytes); - } catch (IOException e) { - throw new IORuntimeException(e); - } - } - - public static void readMarshallable( ReadBytesMarshallable marshallable, BytesIn bytes) { - BytesMarshaller.BYTES_MARSHALLER_CL.get(marshallable.getClass()) - .readMarshallable(marshallable, bytes); - } - - public static void writeMarshallable( WriteBytesMarshallable marshallable, BytesOut bytes) { - BytesMarshaller.BYTES_MARSHALLER_CL.get(marshallable.getClass()) - .writeMarshallable(marshallable, bytes); - } - - @Deprecated(/* to be removed in x.22 */) - public static long utf8Length( CharSequence toWrite) { - return AppendableUtil.findUtf8Length(toWrite); - } - - static String asString(String s, Throwable t) { - StringWriter sw = new StringWriter(); - sw.append(s).append("\n"); - t.printStackTrace(new PrintWriter(sw)); - return sw.toString(); - } - - // calls the BackgroundResourceReleaser and AbstractCloseable.assertCloseableClose first. - public static void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - public static boolean byteToBoolean(byte b) { - return b != 0 && b != 'N' && b != 'n'; - } - - public static long roundUpTo64ByteAlign(long x) { - return (x + 63L) & ~63L; - } - - public static long roundUpTo8ByteAlign(long x) { - return (x + 7L) & ~7L; - } - - public static void read8ByteAlignPadding(Bytes bytes) { - bytes.readPosition(roundUpTo8ByteAlign(bytes.readPosition())); - } - - public static void write8ByteAlignPadding(Bytes bytes) { - long start = bytes.writePosition(); - long end = roundUpTo8ByteAlign(start); - bytes.writePosition(end); - bytes.zeroOut(start, end); - } - - public static String toDebugString( RandomDataInput bytes, long start, long maxLength) { - BytesStore bytes2 = bytes.subBytes(start, maxLength); - return bytes2.toDebugString(maxLength); - } - - @Deprecated(/* to be removed in x.22 */) - public static boolean unregister(BytesStore bs) { - IOTools.unmonitor(bs); - return true; - } - - static class WarnUncheckedElasticBytes { - static { - Jvm.debug().on(WarnUncheckedElasticBytes.class, "Wrapping elastic bytes with unchecked() will require calling ensureCapacity() as needed!"); - } - - static void warn() { - // static block does the work. - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/CommonMarshallable.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/CommonMarshallable.java deleted file mode 100644 index 1e53ba2..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/CommonMarshallable.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.annotation.DontChain; - -@DontChain -public interface CommonMarshallable { - /** - * @return whether this message should be written as self describing - */ - default boolean usesSelfDescribingMessage() { - return false; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ConnectionDroppedException.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ConnectionDroppedException.java deleted file mode 100644 index 386ccab..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ConnectionDroppedException.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; - -/** - * thrown when the TcpChannelHub drops its connection to the server - */ -// TODO Move to network where it is used. -public class ConnectionDroppedException extends IORuntimeException { - public ConnectionDroppedException(String message) { - super(message); - } - - public ConnectionDroppedException(Throwable e) { - super(e); - } - - public ConnectionDroppedException(String s, Throwable e) { - super(s, e); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/DynamicallySized.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/DynamicallySized.java deleted file mode 100644 index 1c4353b..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/DynamicallySized.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -/** - * Marker interface for implementors of {@link Byteable} that indicates - * that size requirements can only be calculated on a user-supplied instance. - */ -public interface DynamicallySized { -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/GuardedNativeBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/GuardedNativeBytes.java deleted file mode 100644 index 611a8da..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/GuardedNativeBytes.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -import static net.openhft.chronicle.bytes.BinaryWireCode.*; - -public class GuardedNativeBytes extends NativeBytes { - static final byte BYTE_T = (byte) INT8; - static final byte SHORT_T = (byte) INT16; - static final byte INT_T = (byte) INT32; - static final byte LONG_T = (byte) INT64; - static final byte STOP_T = (byte) STOP_BIT; - static final byte FLOAT_T = (byte) FLOAT32; - static final byte DOUBLE_T = (byte) FLOAT64; - - private static final String[] STRING_FOR_CODE = _stringForCode(GuardedNativeBytes.class); - - public GuardedNativeBytes( BytesStore store, long capacity) throws IllegalStateException { - super(store, capacity); - } - - - @Override - public Bytes writeByte(byte i8) throws BufferOverflowException { - super.writeByte(BYTE_T); - return super.writeByte(i8); - } - - @Override - public Bytes rawWriteByte(byte i8) throws BufferOverflowException { - return super.writeByte(i8); - } - - @Override - public Bytes rawWriteInt(int i) throws BufferOverflowException { - return super.writeInt(i); - } - - @Override - public byte readByte() { - expectByte(BYTE_T); - return super.readByte(); - } - - @Override - public byte rawReadByte() { - return super.readByte(); - } - - @Override - public int rawReadInt() { - return super.readInt(); - } - - @Override - public int readUnsignedByte() { - expectByte(BYTE_T); - return super.readUnsignedByte(); - } - - - @Override - public Bytes writeShort(short i16) throws BufferOverflowException { - super.writeByte(SHORT_T); - return super.writeShort(i16); - } - - @Override - public short readShort() throws BufferUnderflowException { - expectByte(SHORT_T); - return super.readShort(); - } - - - @Override - public Bytes writeStopBit(char x) throws BufferOverflowException { - super.writeByte(STOP_T); - return super.writeStopBit(x); - } - - - @Override - public Bytes writeStopBit(long x) throws BufferOverflowException { - super.writeByte(STOP_T); - return super.writeStopBit(x); - } - - @Override - public long readStopBit() throws IORuntimeException { - expectByte(STOP_T); - return super.readStopBit(); - } - - @Override - public char readStopBitChar() throws IORuntimeException { - expectByte(STOP_T); - return super.readStopBitChar(); - } - - - @Override - public Bytes writeInt(int i) throws BufferOverflowException { - super.writeByte(INT_T); - return super.writeInt(i); - } - - @Override - public int readInt() throws BufferUnderflowException { - expectByte(INT_T); - return super.readInt(); - } - - - @Override - public Bytes writeLong(long i64) throws BufferOverflowException { - super.writeByte(LONG_T); - return super.writeLong(i64); - } - - @Override - public long readLong() throws BufferUnderflowException { - expectByte(LONG_T); - return super.readLong(); - } - - - @Override - public Bytes writeFloat(float f) throws BufferOverflowException { - super.writeByte(FLOAT_T); - return super.writeFloat(f); - } - - @Override - public float readFloat() throws BufferUnderflowException { - expectByte(FLOAT_T); - return super.readFloat(); - } - - - @Override - public Bytes writeDouble(double d) throws BufferOverflowException { - super.writeByte(DOUBLE_T); - return super.writeDouble(d); - } - - @Override - public double readDouble() throws BufferUnderflowException { - expectByte(DOUBLE_T); - return super.readDouble(); - } - - private void expectByte(byte expected) { - byte type = super.readByte(); - if (type != expected) - throw new IllegalStateException("Expected " + STRING_FOR_CODE[expected & 0xFF] - + " but was " + STRING_FOR_CODE[type & 0xFF]); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/HeapBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/HeapBytesStore.java deleted file mode 100644 index 1d2058c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/HeapBytesStore.java +++ /dev/null @@ -1,402 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.bytes.util.DecoratedBufferUnderflowException; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.Memory; -import net.openhft.chronicle.core.OS; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.util.function.Function; - -/** - * Wrapper for Heap ByteBuffers and arrays. - */ -@SuppressWarnings("restriction") -public class HeapBytesStore - extends AbstractBytesStore, Underlying> { - - private static final Memory MEMORY = OS.memory(); - - private final Object realUnderlyingObject; - private final int dataOffset; - private final long capacity; - - private final Underlying underlyingObject; - - private HeapBytesStore( ByteBuffer byteBuffer) { - super(false); - //noinspection unchecked - this.underlyingObject = (Underlying) byteBuffer; - this.realUnderlyingObject = byteBuffer.array(); - this.dataOffset = Jvm.arrayByteBaseOffset() + byteBuffer.arrayOffset(); - this.capacity = byteBuffer.capacity(); - } - - private HeapBytesStore( byte [] byteArray) { - super(false); - //noinspection unchecked - this.underlyingObject = (Underlying) byteArray; - this.realUnderlyingObject = byteArray; - this.dataOffset = Jvm.arrayByteBaseOffset(); - this.capacity = byteArray.length; - } - - // Used by Chronicle-Map. - - public static HeapBytesStore wrap( byte[] byteArray) { - return new HeapBytesStore<>(byteArray); - } - - // Used by Chronicle-Map. - - public static HeapBytesStore wrap( ByteBuffer bb) { - return new HeapBytesStore<>(bb); - } - - @Override - public boolean isDirectMemory() { - return false; - } - - @Override - public void move(long from, long to, long length) { - if (from < 0 || to < 0) throw new BufferUnderflowException(); - //noinspection SuspiciousSystemArraycopy - System.arraycopy(realUnderlyingObject, Maths.toUInt31(from), realUnderlyingObject, Maths.toUInt31(to), Maths.toUInt31(length)); - } - - - @Override - public String toString() { - try { - return BytesInternal.toString(this); - - } catch (IllegalStateException e) { - return e.toString(); - } - } - - - @Override - public BytesStore, Underlying> copy() { - throw new UnsupportedOperationException("todo"); - } - - @Override - protected void performRelease() { - // nothing to do - } - - @Override - public long capacity() { - return capacity; - } - - - @Override - public Underlying underlyingObject() { - return underlyingObject; - } - - @Override - public boolean compareAndSwapInt(long offset, int expected, int value) { - return MEMORY.compareAndSwapInt(realUnderlyingObject, dataOffset + offset, expected, value); - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - MEMORY.testAndSetInt(realUnderlyingObject, dataOffset + offset, expected, value); - } - - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) { - return MEMORY.compareAndSwapLong( - realUnderlyingObject, dataOffset + offset, expected, value); - } - - @Override - public byte readByte(long offset) throws BufferUnderflowException { - checkOffset(offset, 1); - return MEMORY.readByte(realUnderlyingObject, dataOffset + offset); - } - - @Override - public short readShort(long offset) throws BufferUnderflowException { - checkOffset(offset, 2); - return MEMORY.readShort(realUnderlyingObject, dataOffset + offset); - } - - @Override - public int readInt(long offset) throws BufferUnderflowException { - checkOffset(offset, 4); - return MEMORY.readInt(realUnderlyingObject, dataOffset + offset); - } - - @Override - public long readLong(long offset) throws BufferUnderflowException { - checkOffset(offset, 8); - return MEMORY.readLong(realUnderlyingObject, dataOffset + offset); - } - - @Override - public float readFloat(long offset) throws BufferUnderflowException { - checkOffset(offset, 4); - return MEMORY.readFloat(realUnderlyingObject, dataOffset + offset); - } - - @Override - public double readDouble(long offset) throws BufferUnderflowException { - checkOffset(offset, 8); - return MEMORY.readDouble(realUnderlyingObject, dataOffset + offset); - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - checkOffset(offset, 1); - return MEMORY.readVolatileByte(realUnderlyingObject, dataOffset + offset); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - checkOffset(offset, 2); - return MEMORY.readVolatileShort(realUnderlyingObject, dataOffset + offset); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - checkOffset(offset, 4); - return MEMORY.readVolatileInt(realUnderlyingObject, dataOffset + offset); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - checkOffset(offset, 8); - return MEMORY.readVolatileLong(realUnderlyingObject, dataOffset + offset); - } - - - @Override - public HeapBytesStore writeByte(long offset, byte b) - throws BufferOverflowException { - writeCheckOffset(offset, 1); - MEMORY.writeByte(realUnderlyingObject, dataOffset + offset, b); - return this; - } - - - @Override - public HeapBytesStore writeShort(long offset, short i16) - throws BufferOverflowException { - writeCheckOffset(offset, 2); - MEMORY.writeShort(realUnderlyingObject, dataOffset + offset, i16); - return this; - } - - - @Override - public HeapBytesStore writeInt(long offset, int i32) - throws BufferOverflowException { - writeCheckOffset(offset, 4); - MEMORY.writeInt(realUnderlyingObject, dataOffset + offset, i32); - return this; - } - - - @Override - public HeapBytesStore writeOrderedInt(long offset, int i32) - throws BufferOverflowException { - writeCheckOffset(offset, 4); - MEMORY.writeOrderedInt(realUnderlyingObject, dataOffset + offset, i32); - return this; - } - - - @Override - public HeapBytesStore writeLong(long offset, long i64) - throws BufferOverflowException { - writeCheckOffset(offset, 8); - MEMORY.writeLong(realUnderlyingObject, dataOffset + offset, i64); - return this; - } - - - @Override - public HeapBytesStore writeOrderedLong(long offset, long i) - throws BufferOverflowException { - writeCheckOffset(offset, 8); - MEMORY.writeOrderedLong(realUnderlyingObject, dataOffset + offset, i); - return this; - } - - - @Override - public HeapBytesStore writeFloat(long offset, float f) - throws BufferOverflowException { - writeCheckOffset(offset, 4); - MEMORY.writeFloat(realUnderlyingObject, dataOffset + offset, f); - return this; - } - - - @Override - public HeapBytesStore writeDouble(long offset, double d) - throws BufferOverflowException { - writeCheckOffset(offset, 8); - MEMORY.writeDouble(realUnderlyingObject, dataOffset + offset, d); - return this; - } - - - @Override - public HeapBytesStore writeVolatileByte(long offset, byte i8) - throws BufferOverflowException { - writeCheckOffset(offset, 1); - MEMORY.writeVolatileByte(realUnderlyingObject, dataOffset + offset, i8); - return this; - } - - - @Override - public HeapBytesStore writeVolatileShort(long offset, short i16) - throws BufferOverflowException { - writeCheckOffset(offset, 2); - MEMORY.writeVolatileShort(realUnderlyingObject, dataOffset + offset, i16); - return this; - } - - - @Override - public HeapBytesStore writeVolatileInt(long offset, int i32) - throws BufferOverflowException { - writeCheckOffset(offset, 4); - MEMORY.writeVolatileInt(realUnderlyingObject, dataOffset + offset, i32); - return this; - } - - - @Override - public HeapBytesStore writeVolatileLong(long offset, long i64) - throws BufferOverflowException { - writeCheckOffset(offset, 8); - MEMORY.writeVolatileLong(realUnderlyingObject, dataOffset + offset, i64); - return this; - } - - - @Override - public HeapBytesStore write( - long offsetInRDO, byte[] bytes, int offset, int length) throws BufferOverflowException { - writeCheckOffset(offsetInRDO, length); - MEMORY.copyMemory( - bytes, offset, realUnderlyingObject, this.dataOffset + offsetInRDO, length); - return this; - } - - @Override - public void write( - long offsetInRDO, ByteBuffer bytes, int offset, int length) throws BufferOverflowException { - writeCheckOffset(offsetInRDO, length); - assert realUnderlyingObject == null || dataOffset >= (Jvm.is64bit() ? 12 : 8); - if (bytes.isDirect()) { - MEMORY.copyMemory(Jvm.address(bytes), realUnderlyingObject, - this.dataOffset + offsetInRDO, length); - - } else { - MEMORY.copyMemory(bytes.array(), offset, realUnderlyingObject, - this.dataOffset + offsetInRDO, length); - } - } - - - @Override - public HeapBytesStore write(long writeOffset, - RandomDataInput bytes, long readOffset, long length) { - long i; - for (i = 0; i < length - 7; i += 8) { - long x = bytes.readLong(readOffset + i); - writeLong(writeOffset + i, x); - } - for (; i < length; i++) { - byte x = bytes.readByte(readOffset + i); - writeByte(writeOffset + i, x); - } - return this; - } - - @Override - public long addressForRead(long offset) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - @Override - public long addressForWrite(long offset) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - throw new UnsupportedOperationException(); - } - - @Override - public void nativeRead(long position, long address, long size) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public void nativeWrite(long address, long position, long size) { - throw new UnsupportedOperationException("todo"); - } - - @SuppressWarnings("rawtypes") - @Override - public boolean equals(Object obj) { - return obj instanceof BytesStore && BytesInternal.contentEqual(this, (BytesStore) obj); - } - - @Override - public boolean sharedMemory() { - return false; - } - - private void checkOffset(long offset, int size) throws BufferUnderflowException { - checkBounds(offset, size, DecoratedBufferUnderflowException::new); - } - - private void writeCheckOffset(long offset, int size) throws BufferOverflowException { - checkBounds(offset, size, DecoratedBufferOverflowException::new); - } - - private void checkBounds(final long offset, final int size, - final Function exceptionFunction) { - if (offset < start() || offset + size > capacity) { - throw exceptionFunction.apply( - String.format("Offset: %d, start: %d, size: %d, capacity: %d", - offset, start(), size, capacity)); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/HexDumpBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/HexDumpBytes.java deleted file mode 100644 index 3b783b3..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/HexDumpBytes.java +++ /dev/null @@ -1,1543 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.util.Histogram; -import net.openhft.chronicle.core.util.ThrowingConsumer; -import net.openhft.chronicle.core.util.ThrowingConsumerNonCapturing; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; -import java.io.StringReader; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.util.Scanner; -import java.util.regex.Pattern; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public class HexDumpBytes - implements Bytes { - - private static final char[] HEXADECIMAL = "0123456789abcdef".toCharArray(); - private static final Pattern HEX_PATTERN = Pattern.compile("[0-9a-fA-F]{1,2}"); - - private final NativeBytes base = Bytes.allocateElasticDirect(256); - private final Bytes text = Bytes.allocateElasticOnHeap(1024); - private final Bytes comment = Bytes.allocateElasticOnHeap(64); - private OffsetFormat offsetFormat = null; - private long startOfLine = 0; - private int indent = 0; - private int numberWrap = 16; - - public HexDumpBytes() { - } - - HexDumpBytes(BytesStore base, Bytes text) { - try { - this.base.write(base); - this.text.write(text); - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - public static HexDumpBytes fromText(Reader reader) { - HexDumpBytes tb = new HexDumpBytes(); - Reader reader2 = new TextBytesReader(reader, tb.text); - try (Scanner sc = new Scanner(reader2)) { - while (sc.hasNext()) { - if (sc.hasNext(HEX_PATTERN)) - tb.base.rawWriteByte((byte) Integer.parseInt(sc.next(), 16)); - else - sc.nextLine(); // assume it's a comment - } - } catch (BufferOverflowException | IllegalArgumentException e) { - throw new AssertionError(e); - } - return tb; - } - - public static HexDumpBytes fromText(CharSequence text) { - return fromText(new StringReader(text.toString())); - } - - public HexDumpBytes offsetFormat(OffsetFormat offsetFormat) { - this.offsetFormat = offsetFormat; - return this; - } - - public int numberWrap() { - return numberWrap; - } - - public HexDumpBytes numberWrap(int numberWrap) { - this.numberWrap = numberWrap; - return this; - } - - @Override - public long readRemaining() { - return base.readRemaining(); - } - - @Override - public long writeRemaining() { - return base.writeRemaining(); - } - - @Override - public long readLimit() { - return base.readLimit(); - } - - @Override - public long writeLimit() { - return base.writeLimit(); - } - - - @Override - public String toHexString() { - if (lineLength() > 0) - newLine(); - return text.toString(); - } - - @Override - public int hashCode() { - return base.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return base.equals(obj); - } - - @Override - - public String toString() { - return base.toString(); - } - - @Override - public boolean retainsComments() { - return true; - } - - @Override - public Bytes comment(CharSequence comment) { - if (this.comment.readRemaining() > 0) - newLine(); - if (comment.length() > 0 && comment.charAt(0) == '#') { - indent = 0; - this.text.append('#').append(comment).append('\n'); - startOfLine = this.text.writePosition(); - } else { - this.comment.clear().append(comment); - } - return this; - } - - @Override - public BytesOut indent(int n) { - indent += n; - if (lineLength() > 0) { - newLine(); - } - return this; - } - - private long lineLength() { - return this.text.writePosition() - startOfLine; - } - - private void newLine() { - if (this.comment.readRemaining() > 0) { - while (lineLength() < numberWrap * 3 - 3) - this.text.append(" "); - while (lineLength() < numberWrap * 3) - this.text.append(' '); - this.text.append("# "); - this.text.append(comment); - comment.clear(); - } - this.text.append('\n'); - startOfLine = this.text.writePosition(); - } - - private void appendOffset(long offset) { - if (offsetFormat == null) return; - offsetFormat.append(offset, this.text); - long wp = text.writePosition(); - if (wp > 0 && text.peekUnsignedByte(wp - 1) > ' ') - text.append(' '); - startOfLine = text.writePosition(); - } - - @Override - public BytesStore copy() { - return new HexDumpBytes(base, text); - } - - @Override - public boolean isElastic() { - return base.isElastic(); - } - - @Override - public void ensureCapacity(long size) throws IllegalArgumentException { - base.ensureCapacity(size); - } - - @Override - - public BytesStore bytesStore() { - return base; - } - - @Override - - public Bytes compact() { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes clear() { - return base.clear(); - } - - @Override - public boolean isDirectMemory() { - return false; - } - - @Override - public long capacity() { - return base.capacity(); - } - - @Override - public long addressForRead(long offset) throws UnsupportedOperationException { - return base.addressForRead(offset); - } - - @Override - public long addressForWrite(long offset) throws UnsupportedOperationException { - throw new UnsupportedOperationException(); - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - throw new UnsupportedOperationException(); - } - - @Override - public boolean compareAndSwapInt(long offset, int expected, int value) throws BufferOverflowException { - if (base.compareAndSwapInt(offset & 0xFFFFFFFFL, expected, value)) { - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 4); - return true; - } - return false; - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - long off = offset & 0xFFFFFFFFL; - base.testAndSetInt(off, expected, value); - copyToText(off, offset >>> 32, 4); - } - - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) throws BufferOverflowException { - if (base.compareAndSwapLong(offset & 0xFFFFFFFFL, expected, value)) { - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 8); - return true; - } - return false; - } - - @Override - - public Void underlyingObject() { - throw new UnsupportedOperationException(); - } - - @Override - public void move(long from, long to, long length) { - throw new UnsupportedOperationException(); - } - - @Override - public void reserve(ReferenceOwner owner) throws IllegalStateException { - base.reserve(owner); - } - - @Override - public void release(ReferenceOwner owner) throws IllegalStateException { - base.release(owner); - if (base.refCount() == 0) { - text.releaseLast(); - comment.releaseLast(); - } - } - - @Override - public void releaseLast(ReferenceOwner owner) throws IllegalStateException { - base.releaseLast(owner); - if (base.refCount() == 0) { - text.releaseLast(); - comment.releaseLast(); - } - } - - @Override - public int refCount() { - return base.refCount(); - } - - @Override - public boolean tryReserve(ReferenceOwner owner) { - return base.tryReserve(owner); - } - - @Override - public boolean reservedBy(ReferenceOwner owner) { - return base.reservedBy(owner); - } - - @Override - - public Bytes writeByte(long offset, byte i8) throws BufferOverflowException { - base.writeByte(offset & 0xFFFFFFFFL, i8); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 1); - return this; - } - - @Override - - public Bytes writeShort(long offset, short i) throws BufferOverflowException { - base.writeShort(offset & 0xFFFFFFFFL, i); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 2); - return this; - } - - @Override - - public Bytes writeInt24(long offset, int i) throws BufferOverflowException { - base.writeInt24(offset & 0xFFFFFFFFL, i); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 3); - return this; - } - - @Override - - public Bytes writeInt(long offset, int i) throws BufferOverflowException { - return writeOrderedInt(offset, i); - } - - @Override - - public Bytes writeOrderedInt(long offset, int i) throws BufferOverflowException { - base.writeOrderedInt(offset & 0xFFFFFFFFL, i); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 4); - return this; - } - - @Override - - public Bytes writeLong(long offset, long i) throws BufferOverflowException { - return writeOrderedLong(offset, i); - } - - @Override - - public Bytes writeOrderedLong(long offset, long i) throws BufferOverflowException { - base.writeOrderedLong(offset & 0xFFFFFFFFL, i); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 8); - return this; - } - - @Override - - public Bytes writeFloat(long offset, float d) throws BufferOverflowException { - base.writeFloat(offset & 0xFFFFFFFFL, d); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 4); - return this; - } - - @Override - - public Bytes writeDouble(long offset, double d) throws BufferOverflowException { - base.writeDouble(offset & 0xFFFFFFFFL, d); - copyToText(offset & 0xFFFFFFFFL, offset >>> 32, 8); - return this; - } - - @Override - - public Bytes writeVolatileByte(long offset, byte i8) throws BufferOverflowException { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes writeVolatileShort(long offset, short i16) throws BufferOverflowException { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes writeVolatileInt(long offset, int i32) throws BufferOverflowException { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes writeVolatileLong(long offset, long i64) throws BufferOverflowException { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes write(long offsetInRDO, byte[] bytes, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - throw new UnsupportedOperationException(); - } - - @Override - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes write(long writeOffset, RandomDataInput bytes, long readOffset, long length) throws BufferOverflowException, IllegalArgumentException, BufferUnderflowException { - throw new UnsupportedOperationException(); - } - - @Override - public void nativeWrite(long address, long position, long size) { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes readPosition(long position) throws BufferUnderflowException { - base.readPosition(position); - return this; - } - - @Override - - public Bytes readLimit(long limit) throws BufferUnderflowException { - base.readLimit(limit); - return this; - } - - @Override - - public Bytes readSkip(long bytesToSkip) throws BufferUnderflowException { - base.readSkip(bytesToSkip); - return this; - } - - @Override - public void uncheckedReadSkipOne() { - base.uncheckedReadSkipOne(); - } - - @Override - public void uncheckedReadSkipBackOne() { - base.uncheckedReadSkipBackOne(); - } - - @Override - public long readStopBit() throws IORuntimeException { - return base.readStopBit(); - } - - @Override - public char readStopBitChar() throws IORuntimeException { - return base.readStopBitChar(); - } - - @Override - public double readStopBitDouble() { - return base.readStopBitDouble(); - } - - @Override - public double readStopBitDecimal() throws BufferOverflowException { - return base.readStopBitDecimal(); - } - - @Override - public byte readByte() { - return base.readByte(); - } - - @Override - public int readUnsignedByte() { - return base.readUnsignedByte(); - } - - @Override - public int uncheckedReadUnsignedByte() { - return base.uncheckedReadUnsignedByte(); - } - - @Override - public short readShort() throws BufferUnderflowException { - return base.readShort(); - } - - @Override - public int readInt() throws BufferUnderflowException { - return base.readInt(); - } - - @Override - public long readLong() throws BufferUnderflowException { - return base.readLong(); - } - - @Override - public float readFloat() throws BufferUnderflowException { - return base.readFloat(); - } - - @Override - public double readDouble() throws BufferUnderflowException { - return base.readDouble(); - } - - @Override - public int readVolatileInt() throws BufferUnderflowException { - return base.readVolatileInt(); - } - - @Override - public long readVolatileLong() throws BufferUnderflowException { - return base.readVolatileLong(); - } - - @Override - public int peekUnsignedByte() { - return base.peekUnsignedByte(); - } - - - @Override - public int lastDecimalPlaces() { - return base.lastDecimalPlaces(); - } - - @Override - public void lastDecimalPlaces(int lastDecimalPlaces) { - base.lastDecimalPlaces(lastDecimalPlaces); - } - - - @Override - public BigDecimal readBigDecimal() { - return base.readBigDecimal(); - } - - - @Override - public BigInteger readBigInteger() { - return base.readBigInteger(); - } - - @Override - public void readWithLength(long length, BytesOut bytesOut) throws BufferUnderflowException, IORuntimeException { - base.readWithLength(length, bytesOut); - } - - @Override - public T readMarshallableLength16(Class tClass, T object) { - return base.readMarshallableLength16(tClass, object); - } - - - @Override - public Bytes readPositionUnlimited(long position) throws BufferUnderflowException { - return base.readPositionUnlimited(position); - - } - - - @Override - public Bytes readPositionRemaining(long position, long remaining) throws BufferUnderflowException { - return base.readPositionRemaining(position, remaining); - - } - - @Override - public void readWithLength0(long length, ThrowingConsumerNonCapturing, IORuntimeException, BytesOut> bytesConsumer, StringBuilder sb, BytesOut toBytes) throws BufferUnderflowException, IORuntimeException { - base.readWithLength0(length, bytesConsumer, sb, toBytes); - - } - - @Override - public void readWithLength(long length, ThrowingConsumer, IORuntimeException> bytesConsumer) throws BufferUnderflowException, IORuntimeException { - base.readWithLength(length, bytesConsumer); - - } - - @Override - public boolean readBoolean() { - return base.readBoolean(); - - } - - @Override - public int readUnsignedShort() throws BufferUnderflowException { - return base.readUnsignedShort(); - - } - - @Override - public int readInt24() throws BufferUnderflowException { - return base.readInt24(); - - } - - @Override - public int readUnsignedInt24() throws BufferUnderflowException { - return base.readUnsignedInt24(); - - } - - @Override - public long readUnsignedInt() throws BufferUnderflowException { - return base.readUnsignedInt(); - - } - - - @Override - public String readUtf8() throws BufferUnderflowException, IORuntimeException, IllegalArgumentException { - return base.readUtf8(); - - } - - - @Override - public String readUTFΔ() throws IORuntimeException, BufferUnderflowException, IllegalArgumentException { - return base.readUTFΔ(); - - } - - - @Override - public String read8bit() throws IORuntimeException, BufferUnderflowException { - return base.read8bit(); - - } - - @Override - public boolean readUtf8( ACS sb) throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - return base.readUtf8(sb); - - } - - @Override - public boolean readUTFΔ( ACS sb) throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - return base.readUTFΔ(sb); - - } - - @Override - public boolean read8bit( Bytes b) throws BufferUnderflowException, IllegalStateException, BufferOverflowException { - return base.read8bit(b); - - } - - @Override - public boolean read8bit( ACS sb) throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - return base.read8bit(sb); - - } - - @Override - public boolean read8bit( StringBuilder sb) throws IORuntimeException, BufferUnderflowException { - return base.read8bit(sb); - - } - - @Override - public int read( byte[] bytes) { - return base.read(bytes); - - } - - @Override - public int read( byte[] bytes, int off, int len) { - return base.read(bytes, off, len); - - } - - @Override - public int read( char[] bytes, int off, int len) { - return base.read(bytes, off, len); - - } - - @Override - public void read( ByteBuffer buffer) { - base.read(buffer); - - } - - @Override - public void read( Bytes bytes, int length) { - base.read(bytes, length); - - } - - - @Override - public > E readEnum( Class eClass) throws IORuntimeException, BufferUnderflowException { - return base.readEnum(eClass); - - } - - @Override - public void readHistogram( Histogram histogram) { - base.readHistogram(histogram); - - } - - @Override - public void readWithLength(Bytes bytes) { - base.readWithLength(bytes); - - } - - @Override - - public Bytes writePosition(long position) throws BufferOverflowException { - base.writePosition(position & 0xFFFFFFFFL); - text.writePosition(position >>> 32); - return this; - } - - @Override - - public Bytes writeLimit(long limit) throws BufferOverflowException { - base.writeLimit(limit); - return this; - } - - @Override - - public Bytes writeSkip(long bytesToSkip) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeSkip(bytesToSkip); - return this; - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeByte(byte i8) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeByte(i8); - return this; - } finally { - copyToText(pos); - } - } - - /** - * For HexDumpBytes it needs to remember the writePosition for the underlying bytes as well as the text hex dump, so it encodes both in one number so you can call writePosition later. - * - * @return the base and text writePositions. - */ - @Override - public long writePosition() { - return base.writePosition() | (text.writePosition() << 32); - } - - private void copyToText(long pos) { - if (lineLength() == 0 && offsetFormat != null) { - appendOffset(pos); - startOfLine = text.writePosition(); - } - - long end = base.writePosition(); - if (pos < end) { - doIndent(); - do { - int value = base.readUnsignedByte(pos); - long ll = lineLength(); - if (ll >= numberWrap * 3 - 1) { - newLine(); - appendOffset(pos); - doIndent(); - startOfLine = text.writePosition(); - } - pos++; - long wp = text.writePosition(); - if (wp > 0 && text.peekUnsignedByte(wp - 1) > ' ') - text.append(' '); - text.appendBase16(value, 2); - } while (pos < end); - } - } - - private void copyToText(long pos, long tpos, int length) { - if (tpos > 0 && text.readUnsignedByte(tpos) <= ' ') - tpos++; - while (length-- > 0) { - int value = base.readUnsignedByte(pos++); - text.writeUnsignedByte(tpos++, HEXADECIMAL[value >> 4]); - text.writeUnsignedByte(tpos++, HEXADECIMAL[value & 0xF]); - if (length > 0) - text.writeUnsignedByte(tpos++, ' '); - } - } - - private void doIndent() { - if (lineLength() == 0 && indent > 0) { - for (int i = 0; i < indent; i++) - text.append(" "); - startOfLine = text.writePosition(); - } - } - - @Override - - public Bytes writeShort(short i16) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeShort(i16); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeInt(int i) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeInt(i); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeIntAdv(int i, int advance) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeIntAdv(i, advance); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeLong(long i64) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeLong(i64); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeLongAdv(long i64, int advance) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeLongAdv(i64, advance); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeFloat(float f) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeFloat(f); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeDouble(double d) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeDouble(d); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeDoubleAndInt(double d, int i) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeDouble(d); - base.writeInt(i); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - public long realWriteRemaining() { - return base.realWriteRemaining(); - } - - @Override - - public Bytes write(byte[] bytes, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - long pos = base.writePosition(); - try { - base.write(bytes, offset, length); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeSome(ByteBuffer buffer) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeSome(buffer); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeOrderedInt(int i) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeOrderedInt(i); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes writeOrderedLong(long i) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeOrderedLong(i); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes clearAndPad(long length) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.clearAndPad(length); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - - public Bytes prewrite(byte[] bytes) { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes prewrite(BytesStore bytes) { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes prewriteByte(byte b) { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes prewriteShort(short i) { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes prewriteInt(int i) { - throw new UnsupportedOperationException(); - } - - @Override - - public Bytes prewriteLong(long l) { - throw new UnsupportedOperationException(); - } - - @Override - public byte readByte(long offset) throws BufferUnderflowException { - return base.readByte(offset); - } - - @Override - public int peekUnsignedByte(long offset) { - return base.peekUnsignedByte(offset); - } - - @Override - public short readShort(long offset) throws BufferUnderflowException { - return base.readShort(offset); - } - - @Override - public int readInt(long offset) throws BufferUnderflowException { - return base.readInt(offset); - } - - @Override - public long readLong(long offset) throws BufferUnderflowException { - return base.readLong(offset); - } - - @Override - public float readFloat(long offset) throws BufferUnderflowException { - return base.readFloat(offset); - } - - @Override - public double readDouble(long offset) throws BufferUnderflowException { - return base.readDouble(offset); - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - return base.readVolatileByte(offset); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - return base.readVolatileShort(offset); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - return base.readVolatileInt(offset); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - return base.readVolatileLong(offset); - } - - @Override - public void nativeRead(long position, long address, long size) throws BufferUnderflowException { - base.nativeRead(position, address, size); - } - - @Override - public long readPosition() { - return base.readPosition() | (text.readPosition() << 32); - } - - @Override - public void lenient(boolean lenient) { - base.lenient(lenient); - } - - @Override - public boolean lenient() { - return base.lenient(); - } - - @Override - public void writeMarshallableLength16(WriteBytesMarshallable marshallable) { - long pos = base.writePosition(); - try { - base.writeMarshallableLength16(marshallable); - } finally { - copyToText(pos); - } - } - - @Override - public Bytes write(InputStream inputStream) throws IOException { - long pos = base.writePosition(); - try { - base.write(inputStream); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeStopBit(long x) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeStopBit(x); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeStopBit(char x) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeStopBit(x); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeStopBit(double d) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeStopBit(d); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeStopBitDecimal(double d) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeStopBitDecimal(d); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUtf8(CharSequence cs) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeUtf8(cs); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUtf8(String s) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeUtf8(s); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUTFΔ(CharSequence cs) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeUTFΔ(cs); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write8bit( CharSequence cs) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.write8bit(cs); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write8bit( CharSequence s, int start, int length) throws BufferOverflowException, IllegalArgumentException, IndexOutOfBoundsException { - long pos = base.writePosition(); - try { - base.write8bit(s, start, length); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write(CharSequence cs) throws BufferOverflowException, BufferUnderflowException, IllegalArgumentException { - long pos = base.writePosition(); - try { - base.write(cs); - return this; - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write( CharSequence s, int start, int length) throws BufferOverflowException, IllegalArgumentException, IndexOutOfBoundsException { - long pos = base.writePosition(); - try { - base.write(s, start, length); - return this; - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write8bit( String s) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.write8bit(s); - return this; - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write8bit( BytesStore bs) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.write8bit(bs); - return this; - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUnsignedByte(int i) throws BufferOverflowException, IllegalArgumentException { - long pos = base.writePosition(); - try { - base.writeUnsignedByte(i); - return this; - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUnsignedShort(int u16) throws BufferOverflowException, IllegalArgumentException { - long pos = base.writePosition(); - try { - base.writeUnsignedShort(u16); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeInt24(int i) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeInt24(i); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUnsignedInt24(int i) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeUnsignedInt24(i); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeUnsignedInt(long i) throws BufferOverflowException, IllegalArgumentException { - long pos = base.writePosition(); - try { - base.writeUnsignedInt(i); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write( RandomDataInput bytes) { - long pos = base.writePosition(); - try { - - base.write(bytes); - return this; - } finally { - copyToText(pos); - } - } - - @Override - public Bytes write( BytesStore bytes) { - long pos = base.writePosition(); - try { - base.write(bytes); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeSome( Bytes bytes) { - long pos = base.writePosition(); - try { - base.writeSome(bytes); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write( RandomDataInput bytes, long offset, long length) throws BufferOverflowException, BufferUnderflowException { - long pos = base.writePosition(); - try { - base.write(bytes, offset, length); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write( BytesStore bytes, long offset, long length) throws BufferOverflowException, BufferUnderflowException { - long pos = base.writePosition(); - try { - base.write(bytes, offset, length); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes write( byte[] bytes) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.write(bytes); - return this; - - } finally { - copyToText(pos); - } - } - - - @Override - public Bytes writeBoolean(boolean flag) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeBoolean(flag); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - public > Bytes writeEnum( E e) throws BufferOverflowException { - long pos = base.writePosition(); - try { - base.writeEnum(e); - return this; - - } finally { - copyToText(pos); - } - } - - @Override - public void writePositionRemaining(long position, long length) { - writePosition(position); - writeLimit(base.writePosition + length); - } - - @Override - public void writeHistogram( Histogram histogram) { - long pos = base.writePosition(); - try { - base.writeHistogram(histogram); - } finally { - copyToText(pos); - } - } - - @Override - public void writeBigDecimal( BigDecimal bd) { - long pos = base.writePosition(); - try { - base.writeBigDecimal(bd); - } finally { - copyToText(pos); - } - } - - @Override - public void writeBigInteger( BigInteger bi) { - long pos = base.writePosition(); - try { - base.writeBigInteger(bi); - } finally { - copyToText(pos); - } - } - - @Override - public void writeWithLength(RandomDataInput bytes) { - long pos = base.writePosition(); - try { - base.writeWithLength(bytes); - } finally { - copyToText(pos); - } - } - - private static class TextBytesReader extends Reader { - private final Reader reader; - private final Bytes base; - - public TextBytesReader(Reader reader, Bytes base) { - this.reader = reader; - this.base = base; - } - - @Override - public int read( char[] cbuf, int off, int len) throws IOException { - int len2 = reader.read(cbuf, off, len); - base.append(new String(cbuf, off, len)); // TODO Optimise - return len2; - } - - @Override - public void close() throws IOException { - reader.close(); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Invocation.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Invocation.java deleted file mode 100644 index d8123b7..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/Invocation.java +++ /dev/null @@ -1,9 +0,0 @@ -package net.openhft.chronicle.bytes; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -@FunctionalInterface -public interface Invocation { - Object invoke(Method m, Object o, Object[] args) throws InvocationTargetException; -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytes.java deleted file mode 100644 index 176e67c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytes.java +++ /dev/null @@ -1,1056 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Memory; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.UnsafeMemory; -import net.openhft.chronicle.core.io.Closeable; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.util.ObjectUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -import static net.openhft.chronicle.core.util.StringUtils.*; - -/** - * Bytes to wrap memory mapped data. - *

- * NOTE These Bytes are single Threaded as are all Bytes. - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public class MappedBytes extends AbstractBytes implements Closeable { - - private static final boolean TRACE = Jvm.getBoolean("trace.mapped.bytes"); - - private MappedBytesStore bytesStore; - private final MappedFile mappedFile; - private final boolean backingFileIsReadOnly; - private boolean closed; - - // assume the mapped file is reserved already. - protected MappedBytes( final MappedFile mappedFile) throws IllegalStateException { - this(mappedFile, ""); - } - - protected MappedBytes( final MappedFile mappedFile, final String name) throws IllegalStateException { - super(NoBytesStore.noBytesStore(), - NoBytesStore.noBytesStore().writePosition(), - NoBytesStore.noBytesStore().writeLimit(), - name); - - assert mappedFile != null; - this.mappedFile = mappedFile; - mappedFile.reserve(this); - this.backingFileIsReadOnly = !mappedFile.file().canWrite(); - assert !mappedFile.isClosed(); - clear(); - } - - - public static MappedBytes mappedBytes( final String filename, final long chunkSize) - throws FileNotFoundException, IllegalStateException { - return mappedBytes(new File(filename), chunkSize); - } - - - public static MappedBytes mappedBytes( final File file, final long chunkSize) - throws FileNotFoundException, IllegalStateException { - return mappedBytes(file, chunkSize, OS.pageSize()); - } - - - public static MappedBytes mappedBytes( final File file, final long chunkSize, final long overlapSize) - throws FileNotFoundException, IllegalStateException { - final MappedFile rw = MappedFile.of(file, chunkSize, overlapSize, false); - try { - return mappedBytes(rw); - } finally { - rw.release(INIT); - } - } - - - public static MappedBytes mappedBytes( final File file, - final long chunkSize, - final long overlapSize, - final boolean readOnly) throws FileNotFoundException, - IllegalStateException { - final MappedFile rw = MappedFile.of(file, chunkSize, overlapSize, readOnly); - try { - return mappedBytes(rw); - } finally { - rw.release(INIT); - } - } - - - public static MappedBytes mappedBytes( final MappedFile rw) { - return new MappedBytes(rw); - } - - - public static MappedBytes readOnly( final File file) throws FileNotFoundException { - MappedFile mappedFile = MappedFile.readOnly(file); - try { - return new MappedBytes(mappedFile); - } finally { - mappedFile.release(INIT); - } - } - - @Override - protected void bytesStore(BytesStore bytesStore) { - super.bytesStore(bytesStore); - if (bytesStore instanceof MappedBytesStore) - this.bytesStore = (MappedBytesStore) bytesStore; - else - this.bytesStore = null; - } - - - @Override - public String toString() { - if (!TRACE) - return super.toString(); - return "MappedBytes{" + "\n" + - "refCount=" + refCount() + ",\n" + - "mappedFile=" + mappedFile.file().getAbsolutePath() + ",\n" + - "mappedFileRefCount=" + mappedFile.refCount() + ",\n" + - "mappedFileIsClosed=" + mappedFile.isClosed() + ",\n" + - "mappedFileRafIsClosed=" + Jvm.getValue(mappedFile.raf(), "closed") + ",\n" + - "mappedFileRafChannelIsClosed=" + !mappedFile.raf().getChannel().isOpen() + ",\n" + - "isClosed=" + isClosed() + - '}'; - } - - public MappedBytes write( final byte[] bytes, - final int offset, - final int length) { - throwExceptionIfClosed(); - - write(writePosition, bytes, offset, length); - writePosition += Math.min(length, bytes.length - offset); - return this; - } - - public MappedBytes write(final long offsetInRDO, - final byte[] bytes, - int offset, - final int length) { - throwExceptionIfClosed(); - - long wp = offsetInRDO; - if ((length + offset) > bytes.length) - throw new ArrayIndexOutOfBoundsException("bytes.length=" + bytes.length + ", " + "length=" + length + ", offset=" + offset); - - if (length > writeRemaining()) - throw new DecoratedBufferOverflowException( - String.format("write failed. Length: %d > writeRemaining: %d", length, writeRemaining())); - - int remaining = length; - - acquireNextByteStore(wp, false); - - while (remaining > 0) { - - long safeCopySize = copySize(wp); - - if (safeCopySize + mappedFile.overlapSize() >= remaining) { - bytesStore.write(wp, bytes, offset, remaining); - return this; - } - - bytesStore.write(wp, bytes, offset, (int) safeCopySize); - - offset += safeCopySize; - wp += safeCopySize; - remaining -= safeCopySize; - - // move to the next chunk - acquireNextByteStore0(wp, false); - - } - return this; - - } - - public MappedBytes write(final long writeOffset, - final RandomDataInput bytes, - long readOffset, - final long length) - throws BufferOverflowException, BufferUnderflowException { - throwExceptionIfClosed(); - - long wp = writeOffset; - - if (length > writeRemaining()) - throw new DecoratedBufferOverflowException( - String.format("write failed. Length: %d > writeRemaining: %d", length, writeRemaining())); - - long remaining = length; - - acquireNextByteStore(wp, false); - - while (remaining > 0) { - - long safeCopySize = copySize(wp); - - if (safeCopySize + mappedFile.overlapSize() >= remaining) { - bytesStore.write(wp, bytes, readOffset, remaining); - return this; - } - - bytesStore.write(wp, bytes, readOffset, safeCopySize); - - readOffset += safeCopySize; - wp += safeCopySize; - remaining -= safeCopySize; - - // move to the next chunk - acquireNextByteStore0(wp, false); - } - return this; - } - - - @Override - public MappedBytes write( final RandomDataInput bytes) - throws BufferOverflowException { - throwExceptionIfClosed(); - - assert bytes != this : "you should not write to yourself !"; - final long remaining = bytes.readRemaining(); - write(writePosition, bytes); - writePosition += remaining; - return this; - } - - - public MappedBytes write(final long offsetInRDO, final RandomDataInput bytes) - throws BufferOverflowException { - throwExceptionIfClosed(); - - write(offsetInRDO, bytes, bytes.readPosition(), bytes.readRemaining()); - return this; - } - - private long copySize(final long writePosition) { - long size = mappedFile.chunkSize(); - return size - writePosition % size; - } - - public void setNewChunkListener(final NewChunkListener listener) { - mappedFile.setNewChunkListener(listener); - } - - - public MappedFile mappedFile() { - return mappedFile; - } - - @Override - public BytesStore, Void> copy() { - return NativeBytes.copyOf(this); - } - - @Override - public long capacity() { - return mappedFile.capacity(); - } - - @Override - public Bytes readLimitToCapacity() throws BufferUnderflowException { - this.writePosition = mappedFile.capacity(); - return this; - } - - @Override - public long realCapacity() { - throwExceptionIfClosed(); - - try { - return mappedFile.actualSize(); - - } catch (IORuntimeException e) { - Jvm.warn().on(getClass(), "Unable to obtain the real size for " + mappedFile.file(), e); - return 0; - } - } - - - @Override - public Bytes readPositionRemaining(final long position, final long remaining) throws BufferUnderflowException { -// throwExceptionIfClosed(); - - final long limit = position + remaining; - acquireNextByteStore(position, true); - - if (writeLimit < limit) - writeLimit(limit); - - boolean debug = false; - assert debug = true; - if (debug) - readLimit(limit); - else - uncheckedWritePosition(limit); - - return readPosition(position); - } - - - @Override - public Bytes readPosition(final long position) throws BufferUnderflowException { -// throwExceptionIfClosed(); - - if (bytesStore != null && bytesStore.inside(position)) { - return super.readPosition(position); - } else { - acquireNextByteStore0(position, true); - return this; - } - } - - @Override - public long addressForRead(final long offset) throws BufferUnderflowException { -// throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset)) - acquireNextByteStore0(offset, true); - return bytesStore.addressForRead(offset); - } - - @Override - public long addressForRead(final long offset, final int buffer) throws UnsupportedOperationException, BufferUnderflowException { -// throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset, buffer)) - acquireNextByteStore0(offset, true); - return bytesStore.addressForRead(offset); - } - - @Override - public long addressForWrite(final long offset) throws UnsupportedOperationException, BufferOverflowException { -// throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset)) - acquireNextByteStore0(offset, true); - return bytesStore.addressForWrite(offset); - } - - @Override - protected void readCheckOffset(final long offset, - final long adding, - final boolean given) throws BufferUnderflowException { - final long check = adding >= 0 ? offset : offset + adding; - //noinspection StatementWithEmptyBody - if (bytesStore != null && bytesStore.inside(check, adding)) { - // nothing. - } else { - acquireNextByteStore0(offset, false); - } - super.readCheckOffset(offset, adding, given); - } - - - @Override - public String read8bit() throws IORuntimeException, BufferUnderflowException { -// throwExceptionIfClosed(); - - return BytesInternal.read8bit(this); - } - - @Override - protected void writeCheckOffset(final long offset, final long adding) throws BufferOverflowException { - - if (offset < 0 || offset > mappedFile.capacity() - adding) - throw writeBufferOverflowException(offset); - if (bytesStore == null || !bytesStore.inside(offset, Math.toIntExact(adding))) { - acquireNextByteStore0(offset, false); - } -// super.writeCheckOffset(offset, adding); - } - - @Override - public void ensureCapacity(final long size) throws IllegalArgumentException { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(writePosition, Math.toIntExact(size))) { - acquireNextByteStore0(writePosition, false); - } - } - - - private BufferOverflowException writeBufferOverflowException(final long offset) { - BufferOverflowException exception = new BufferOverflowException(); - exception.initCause(new IllegalArgumentException("Offset out of bound " + offset)); - return exception; - } - - private void acquireNextByteStore(final long offset, final boolean set) throws BufferOverflowException, IllegalStateException { - // if in the same chunk, can continue even if closed, but not released. - if (bytesStore != null && bytesStore.inside(offset)) - return; - - // not allowed if closed. - throwExceptionIfReleased(); - - acquireNextByteStore0(offset, set); - } - - // DON'T call this directly. - // TODO Check whether we need synchronized; original comment; require protection from concurrent mutation to bytesStore field - private synchronized void acquireNextByteStore0(final long offset, final boolean set) { - throwExceptionIfClosed(); - - final BytesStore oldBS = this.bytesStore; - try { - final MappedBytesStore newBS = mappedFile.acquireByteStore(this, offset, oldBS); - if (newBS != oldBS) { - this.bytesStore(newBS); - if (oldBS != null) - oldBS.release(this); - } - assert this.bytesStore.reservedBy(this); - - } catch ( IOException | IllegalArgumentException e) { - final BufferOverflowException boe = new BufferOverflowException(); - boe.initCause(e); - throw boe; - } - if (set) { - if (writeLimit() < readPosition) - writeLimit(readPosition); - if (readLimit() < readPosition) - readLimit(readPosition); - readPosition = offset; - } - } - - - @Override - public Bytes readSkip(final long bytesToSkip) - throws BufferUnderflowException { - // called often so skip this check for performance - // throwExceptionIfClosed(); - - if (readPosition + bytesToSkip > readLimit()) throw new BufferUnderflowException(); - long check = bytesToSkip >= 0 ? this.readPosition : this.readPosition + bytesToSkip; - if (bytesStore == null || - bytesToSkip != (int) bytesToSkip || - !bytesStore.inside(readPosition, (int) bytesToSkip)) { - acquireNextByteStore0(check, false); - } - this.readPosition += bytesToSkip; - return this; - } - - @Override - public MappedBytesStore bytesStore() { -// throwExceptionIfClosed(); - - return (MappedBytesStore) super.bytesStore(); - } - - @Override - public long start() { -// throwExceptionIfClosed(); - - return 0L; - } - - - @Override - public Bytes writePosition(final long position) throws BufferOverflowException { -// throwExceptionIfClosed(); - - if (position > writeLimit) - throw new BufferOverflowException(); - if (position < 0L) - throw new BufferUnderflowException(); - if (position < readPosition) - this.readPosition = position; - this.writePosition = position; - return this; - } - - - @Override - public Bytes clear() { - // typically only used at the start of an operation so reject if closed. - throwExceptionIfClosed(); - - long start = 0L; - readPosition = start; - this.writePosition = start; - writeLimit = mappedFile.capacity(); - return this; - } - - - @Override - public Bytes writeByte(final byte i8) throws BufferOverflowException { - throwExceptionIfClosed(); - - final long oldPosition = writePosition; - if (writePosition < 0 || writePosition > capacity() - (long) 1) - throw writeBufferOverflowException(writePosition); - if (bytesStore == null || !bytesStore.inside(writePosition, 1)) { - // already determined we need it - acquireNextByteStore0(writePosition, false); - } - this.writePosition = writePosition + (long) 1; - bytesStore.writeByte(oldPosition, i8); - return this; - } - - @Override - protected void performRelease() throws IllegalStateException { - super.performRelease(); - mappedFile.release(this); - } - - @Override - protected boolean performReleaseInBackground() { - return true; - } - - @Override - public boolean isElastic() { - return true; - } - - public boolean isBackingFileReadOnly() { -// throwExceptionIfClosed(); - - return backingFileIsReadOnly; - } - - @Override - - public Bytes write( final RandomDataInput bytes, - final long offset, - final long length) throws BufferUnderflowException, BufferOverflowException { - throwExceptionIfClosed(); - - if (bytes instanceof BytesStore) - write((BytesStore) bytes, offset, length); - else if (length == 8) - writeLong(bytes.readLong(offset)); - else if (length > 0) - BytesInternal.writeFully(bytes, offset, length, this); - return this; - } - - - @Override - public Bytes write( final BytesStore bytes, - final long offset, - final long length) - throws BufferUnderflowException, BufferOverflowException { - throwExceptionIfClosed(); - - if (length == 8) { - writeLong(bytes.readLong(offset)); - } else if (length > 0) { - if (bytes.isDirectMemory()) { - // need to check this to pull in the right bytesStore() - long fromAddress = bytes.addressForRead(offset); - if (length <= bytes.bytesStore().realCapacity() - offset) { - this.acquireNextByteStore(writePosition(), false); - // can we do a direct copy of raw memory? - if (bytesStore.realCapacity() - writePosition >= length) { - rawCopy(length, fromAddress); - return this; - } - } - } - BytesInternal.writeFully(bytes, offset, length, this); - } - - return this; - } - - void rawCopy(final long length, final long fromAddress) - throws BufferOverflowException, BufferUnderflowException { - this.throwExceptionIfReleased(); - OS.memory().copyMemory(fromAddress, addressForWritePosition(), length); - uncheckedWritePosition(writePosition() + length); - } - - - @Override - public Bytes append8bit( CharSequence cs, int start, int end) - throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, - IndexOutOfBoundsException { - throwExceptionIfClosed(); - - // check the start. - long pos = writePosition(); - writeCheckOffset(pos, 0); - if (!(cs instanceof String) || pos + (end - start) * 3 + 5 >= safeLimit()) { - return super.append8bit(cs, start, end); - } - return append8bit0((String) cs, start, end - start); - } - - @Override - - public MappedBytes write8bit( CharSequence s, int start, int length) { - throwExceptionIfClosed(); - - ObjectUtils.requireNonNull(s); - - // check the start. - long pos = writePosition(); - writeCheckOffset(pos, 0); - if (!(s instanceof String) || pos + length * 3 + 5 >= safeLimit()) { - super.write8bit(s, start, length); - return this; - } - - writeStopBit(length); - return append8bit0((String) s, start, length); - } - - - private MappedBytes append8bit0( String s, int start, int length) throws BufferOverflowException { - - if (Jvm.isJava9Plus()) { - byte[] bytes = extractBytes(s); - long address = addressForWritePosition(); - Memory memory = bytesStore().memory; - int i = 0; - for (; i < length - 3; i += 4) { - int c0 = bytes[i + start] & 0xff; - int c1 = bytes[i + start + 1] & 0xff; - int c2 = bytes[i + start + 2] & 0xff; - int c3 = bytes[i + start + 3] & 0xff; - memory.writeInt(address, (c3 << 24) | (c2 << 16) | (c1 << 8) | c0); - address += 4; - } - for (; i < length; i++) { - byte c = bytes[i + start]; - memory.writeByte(address++, c); - } - writeSkip(length); - } else { - char[] chars = extractChars(s); - long address = addressForWritePosition(); - Memory memory = bytesStore().memory; - int i = 0; - for (; i < length - 3; i += 4) { - int c0 = chars[i + start] & 0xff; - int c1 = chars[i + start + 1] & 0xff; - int c2 = chars[i + start + 2] & 0xff; - int c3 = chars[i + start + 3] & 0xff; - memory.writeInt(address, (c3 << 24) | (c2 << 16) | (c1 << 8) | c0); - address += 4; - } - for (; i < length; i++) { - char c = chars[i + start]; - memory.writeByte(address++, (byte) c); - } - writeSkip(length); - } - return this; - } - - - @Override - public Bytes appendUtf8( CharSequence cs, int start, int length) - throws BufferOverflowException, IllegalArgumentException { - throwExceptionIfClosed(); - - // check the start. - long pos = writePosition(); - writeCheckOffset(pos, 0); - if (!(cs instanceof String) || pos + length * 3 + 5 >= safeLimit()) { - super.appendUtf8(cs, start, length); - return this; - } - - if (Jvm.isJava9Plus()) { - // byte[] bytes = extractBytes((String) cs); - final String str = (String) cs; - long address = addressForWrite(pos); - Memory memory = OS.memory(); - int i = 0; - non_ascii: - { - for (; i < length; i++) { - char c = str.charAt(i + start); - //byte c = bytes[i + start]; - if (c > 127) { - writeSkip(i); - break non_ascii; - } - memory.writeByte(address++, (byte) c); - } - writeSkip(length); - return this; - } - for (; i < length; i++) { - char c = str.charAt(i + start); - appendUtf8(c); - } - } else { - char[] chars = extractChars((String) cs); - long address = addressForWrite(pos); - Memory memory = OS.memory(); - int i = 0; - non_ascii: - { - for (; i < length; i++) { - char c = chars[i + start]; - if (c > 127) { - writeSkip(i); - break non_ascii; - } - memory.writeByte(address++, (byte) c); - } - writeSkip(length); - return this; - } - for (; i < length; i++) { - char c = chars[i + start]; - appendUtf8(c); - } - } - return this; - } - - @Override - public boolean sharedMemory() { - return true; - } - - @Override - - public Bytes writeOrderedInt(long offset, int i) throws BufferOverflowException { - throwExceptionIfClosed(); - - writeCheckOffset(offset, 4); - if (bytesStore == null || !bytesStore.inside(offset, 4)) { - acquireNextByteStore0(offset, false); - } - bytesStore.writeOrderedInt(offset, i); - return this; - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset, 1)) { - acquireNextByteStore0(offset, false); - } - return bytesStore.readVolatileByte(offset); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset, 2)) { - acquireNextByteStore0(offset, false); - } - return bytesStore.readVolatileShort(offset); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset, 4)) { - acquireNextByteStore0(offset, false); - } - return bytesStore.readVolatileInt(offset); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset, 8)) { - acquireNextByteStore0(offset, false); - } - return bytesStore.readVolatileLong(offset); - } - - @Override - public int peekUnsignedByte() { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(readPosition, 1)) { - acquireNextByteStore0(readPosition, false); - } - return super.peekUnsignedByte(); - } - - @Override - public int peekUnsignedByte(final long offset) throws BufferUnderflowException { - throwExceptionIfClosed(); - - if (bytesStore == null || !bytesStore.inside(offset, 1)) { - acquireNextByteStore0(offset, false); - } - return super.peekUnsignedByte(offset); - } - - @SuppressWarnings("restriction") - @Override - public int peekVolatileInt() { - - if (bytesStore == null || !bytesStore.inside(readPosition, 4)) { - acquireNextByteStore0(readPosition, true); - } - - MappedBytesStore bytesStore = this.bytesStore; - long address = bytesStore.address + bytesStore.translate(readPosition); - Memory memory = bytesStore.memory; - - // are we inside a cache line? - if ((address & 63) <= 60) { - ObjectUtils.requireNonNull(memory); - UnsafeMemory.unsafeLoadFence(); - return UnsafeMemory.unsafeGetInt(address); - } else { - return memory.readVolatileInt(address); - } - } - - @Override - public void release(ReferenceOwner id) throws IllegalStateException { - super.release(id); - closed |= refCount() <= 0; - } - - @Override - public void releaseLast(ReferenceOwner id) throws IllegalStateException { - super.releaseLast(id); - closed = true; - } - - @Override - public void close() { - if (closed) - return; - release(INIT); - closed = true; - } - - @Override - public boolean isClosed() { - return closed; - } - - - @Override - public Bytes writeUtf8(CharSequence str) throws BufferOverflowException { - throwExceptionIfClosed(); - - if (str instanceof String) { - writeUtf8((String) str); - return this; - } - if (str == null) { - this.writeStopBit(-1); - - } else { - try { - long utfLength = AppendableUtil.findUtf8Length(str); - this.writeStopBit(utfLength); - BytesInternal.appendUtf8(this, str, 0, str.length()); - } catch (IndexOutOfBoundsException e) { - throw new AssertionError(e); - } - } - return this; - } - - @Override - public Bytes writeUtf8(String str) throws BufferOverflowException { - throwExceptionIfClosed(); - - if (str == null) { - writeStopBit(-1); - return this; - } - - try { - if (Jvm.isJava9Plus()) { - byte[] strBytes = extractBytes(str); - byte coder = getStringCoder(str); - long utfLength = AppendableUtil.findUtf8Length(strBytes, coder); - writeStopBit(utfLength); - appendUtf8(strBytes, 0, str.length(), coder); - } else { - char[] chars = extractChars(str); - long utfLength = AppendableUtil.findUtf8Length(chars); - writeStopBit(utfLength); - appendUtf8(chars, 0, chars.length); - } - return this; - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - - @Override - public Bytes appendUtf8(char[] chars, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - throwExceptionIfClosed(); - - if (writePosition < 0 || writePosition > capacity() - (long) 1 + length) - throw writeBufferOverflowException(writePosition); - int i; - ascii: - { - for (i = 0; i < length; i++) { - char c = chars[offset + i]; - if (c > 0x007F) - break ascii; - long oldPosition = writePosition; - if (bytesStore == null || ((writePosition & 0xff) == 0 && !bytesStore.inside(writePosition, (length - i) * 3))) { - acquireNextByteStore0(writePosition, false); - } - this.writePosition = writePosition + (long) 1; - bytesStore.writeByte(oldPosition, (byte) c); - } - return this; - } - for (; i < length; i++) { - char c = chars[offset + i]; - BytesInternal.appendUtf8Char(this, c); - } - return this; - } - - @Override - public long readStopBit() throws IORuntimeException { - throwExceptionIfClosed(); - - long offset = readOffsetPositionMoved(1); - byte l = bytesStore.readByte(offset); - - if (l >= 0) - return l; - return BytesInternal.readStopBit0(this, l); - } - - @Override - public char readStopBitChar() throws IORuntimeException { - throwExceptionIfClosed(); - - long offset = readOffsetPositionMoved(1); - byte l = bytesStore.readByte(offset); - - if (l >= 0) - return (char) l; - return (char) BytesInternal.readStopBit0(this, l); - } - - - @Override - public Bytes writeStopBit(long n) throws BufferOverflowException { - throwExceptionIfClosed(); - - if ((n & ~0x7F) == 0) { - writeByte((byte) (n & 0x7f)); - return this; - } - if ((~n & ~0x7F) == 0) { - writeByte((byte) (0x80L | ~n)); - writeByte((byte) 0); - return this; - } - - if ((n & ~0x3FFF) == 0) { - writeByte((byte) ((n & 0x7f) | 0x80)); - writeByte((byte) (n >> 7)); - return this; - } - BytesInternal.writeStopBit0(this, n); - return this; - } - - - @Override - public Bytes writeStopBit(char n) throws BufferOverflowException { - throwExceptionIfClosed(); - - if ((n & ~0x7F) == 0) { - writeByte((byte) (n & 0x7f)); - return this; - } - - if ((n & ~0x3FFF) == 0) { - writeByte((byte) ((n & 0x7f) | 0x80)); - writeByte((byte) (n >> 7)); - return this; - } - BytesInternal.writeStopBit0(this, n); - return this; - } - - @Override - public boolean isDirectMemory() { - return true; - } - - public MappedBytes write8bit( BytesStore bs) - throws BufferOverflowException { - throwExceptionIfClosed(); - - if (bs == null) { - writeStopBit(-1); - } else { - long offset = bs.readPosition(); - long readRemaining = Math.min(writeRemaining(), bs.readLimit() - offset); - writeStopBit(readRemaining); - write(bs, offset, readRemaining); - } - return this; - } - - // used by the Pretoucher, don't change this without considering the impact. - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) throws BufferOverflowException { - throwExceptionIfClosed(); - - if (offset < 0 || offset > mappedFile.capacity() - (long) 8) - throw writeBufferOverflowException(offset); - if (bytesStore == null || bytesStore.start() > offset || offset + 8 >= bytesStore.safeLimit()) { - acquireNextByteStore0(offset, false); - } -// super.writeCheckOffset(offset, adding); - return bytesStore.compareAndSwapLong(offset, expected, value); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytesStore.java deleted file mode 100644 index 324a5ac..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytesStore.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.ReferenceOwner; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferUnderflowException; - -/** - * BytesStore to wrap memory mapped data. - */ -public class MappedBytesStore extends NativeBytesStore { - private final MappedFile mappedFile; - private final long start; - private final long safeLimit; - - protected MappedBytesStore(ReferenceOwner owner, MappedFile mappedFile, long start, long address, long capacity, long safeCapacity) throws IllegalStateException { - super(address, start + capacity, new OS.Unmapper(address, capacity), false); - this.mappedFile = mappedFile; - this.start = start; - this.safeLimit = start + safeCapacity; - reserveTransfer(INIT, owner); - } - - /** - * Fetch the capacity of the underlying file - * This can differ from the exposed capacity() of this bytes store (which has been page aligned) - * - * @return - capacity of the underlying file - */ - public long underlyingCapacity() { - return mappedFile.capacity(); - } - - @Override - public Bytes bytesForRead() throws IllegalStateException { - try { - return new VanillaBytes(this) - .readLimit(writeLimit()) - .readPosition(start()); - } catch (BufferUnderflowException e) { - throw new IllegalStateException(e); - } - } - - - @Override - public VanillaBytes bytesForWrite() throws IllegalStateException { - return new VanillaBytes<>(this); - } - - @Override - public boolean inside(long offset) { - return start <= offset && offset < safeLimit; - } - - @Override - public boolean inside(long offset, long buffer) { - // this is correct that it uses the maximumLimit, yes it is different than the method above. - return start <= offset && offset + buffer < limit; - } - - @Override - public long safeLimit() { - return safeLimit; - } - - @Override - public byte readByte(long offset) { - return memory.readByte(address - start + offset); - } - - - @Override - public NativeBytesStore writeOrderedInt(long offset, int i) { - memory.writeOrderedInt(address - start + offset, i); - return this; - } - - @Override - protected long translate(long offset) { - assert offset >= start; - assert offset < limit; - - return offset - start; - } - - @Override - public long start() { - return start; - } - - @Override - public long readPosition() { - return start(); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytesStoreFactory.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytesStoreFactory.java deleted file mode 100644 index 49c276b..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedBytesStoreFactory.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.ReferenceOwner; -import org.jetbrains.annotations.NotNull; - -@FunctionalInterface -public interface MappedBytesStoreFactory { - - MappedBytesStore create(ReferenceOwner owner, MappedFile mappedFile, long start, long address, long capacity, long safeCapacity); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedFile.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedFile.java deleted file mode 100644 index a4b9382..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedFile.java +++ /dev/null @@ -1,541 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.CleaningRandomAccessFile; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.AbstractCloseableReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.onoes.Slf4jExceptionHandler; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.channels.ClosedByInterruptException; -import java.nio.channels.FileChannel; -import java.nio.channels.FileChannel.MapMode; -import java.nio.channels.FileLock; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.List; - -import static net.openhft.chronicle.core.io.Closeable.closeQuietly; - -/** - * A memory mapped files which can be randomly accessed in chunks. It has overlapping regions to - * avoid wasting bytes at the end of chunks. - */ -@SuppressWarnings({"rawtypes", "unchecked", "restriction"}) -public class MappedFile extends AbstractCloseableReferenceCounted { - static final boolean RETAIN = Jvm.getBoolean("mappedFile.retain"); - private static final long DEFAULT_CAPACITY = 128L << 40; - - - private final RandomAccessFile raf; - private final FileChannel fileChannel; - private final long chunkSize; - private final long overlapSize; - private final List stores = new ArrayList<>(); - private final long capacity; - - private final File file; - private final String canonicalPath; - private final boolean readOnly; - private NewChunkListener newChunkListener = MappedFile::logNewChunk; - - protected MappedFile( final File file, - final RandomAccessFile raf, - final long chunkSize, - final long overlapSize, - final long capacity, - final boolean readOnly) { - this.file = file; - try { - this.canonicalPath = file.getCanonicalPath().intern(); - } catch (IOException ioe) { - throw new IllegalStateException("Unable to obtain the canonical path for " + file.getAbsolutePath(), ioe); - } - this.raf = raf; - this.fileChannel = raf.getChannel(); - this.chunkSize = OS.mapAlign(chunkSize); - this.overlapSize = overlapSize > 0 && overlapSize < 64 << 10 ? chunkSize : OS.mapAlign(overlapSize); - this.capacity = capacity; - this.readOnly = readOnly; - - Jvm.doNotCloseOnInterrupt(getClass(), this.fileChannel); - } - - private static void logNewChunk(final String filename, - final int chunk, - final long delayMicros) { - if (delayMicros < 100 || !Jvm.isDebugEnabled(MappedFile.class)) - return; - - // avoid a GC while trying to memory map. - final String message = BytesInternal.acquireStringBuilder() - .append("Allocation of ").append(chunk) - .append(" chunk in ").append(filename) - .append(" took ").append(delayMicros / 1e3).append(" ms.") - .toString(); - Jvm.perf().on(MappedFile.class, message); - } - - - public static MappedFile of( final File file, - final long chunkSize, - final long overlapSize, - final boolean readOnly) throws FileNotFoundException { -// if (readOnly && OS.isWindows()) { -// Jvm.warn().on(MappedFile.class, "Read only mode not supported on Windows, defaulting to read/write"); -// readOnly = false; -// } - - RandomAccessFile raf = new CleaningRandomAccessFile(file, readOnly ? "r" : "rw"); -// try { - final long capacity = /*readOnly ? raf.length() : */DEFAULT_CAPACITY; - return new MappedFile(file, raf, chunkSize, overlapSize, capacity, readOnly); -/* - } catch (IOException e) { - Closeable.closeQuietly(raf); - FileNotFoundException fnfe = new FileNotFoundException("Unable to open " + file); - fnfe.initCause(e); - throw fnfe; - } -*/ - } - - - public static MappedFile mappedFile( final File file, final long chunkSize) throws FileNotFoundException { - return mappedFile(file, chunkSize, OS.pageSize()); - } - -/* - private void check(Throwable throwable, int[] count) { - for (int i = 0; i < stores.size(); i++) { - WeakReference storeRef = stores.get(i); - if (storeRef == null) - continue; - MappedBytesStore mbs = storeRef.get(); - if (mbs != null && mbs.refCount() > 0) { - mbs.releaseLast(); - throwable.printStackTrace(); - count[0]++; - } - } - } -*/ - - - public static MappedFile mappedFile( final String filename, final long chunkSize) throws FileNotFoundException { - return mappedFile(filename, chunkSize, OS.pageSize()); - } - - - public static MappedFile mappedFile( final String filename, - final long chunkSize, - final long overlapSize) throws FileNotFoundException { - return mappedFile(new File(filename), chunkSize, overlapSize); - } - - - public static MappedFile mappedFile( final File file, - final long chunkSize, - final long overlapSize) throws FileNotFoundException { - return mappedFile(file, chunkSize, overlapSize, false); - } - - - public static MappedFile mappedFile( final File file, - final long chunkSize, - final long overlapSize, - final boolean readOnly) throws FileNotFoundException { - return MappedFile.of(file, chunkSize, overlapSize, readOnly); - } - - - public static MappedFile readOnly( final File file) throws FileNotFoundException { - long chunkSize = file.length(); - long overlapSize = 0; - // Chunks of 4 GB+ not supported on Windows. - if (OS.isWindows() && chunkSize > 2L << 30) { - chunkSize = 2L << 30; - overlapSize = OS.pageSize(); - } - return MappedFile.of(file, chunkSize, overlapSize, true); - } - - - public static MappedFile mappedFile( final File file, - final long capacity, - final long chunkSize, - final long overlapSize, - final boolean readOnly) throws IOException { - final RandomAccessFile raf = new CleaningRandomAccessFile(file, readOnly ? "r" : "rw"); - // Windows throws an exception when setting the length when you re-open - if (raf.length() < capacity) - raf.setLength(capacity); - return new MappedFile(file, raf, chunkSize, overlapSize, capacity, readOnly); - } - - public static void warmup() { - final List errorsDuringWarmup = new ArrayList<>(); - try { - Jvm.setExceptionHandlers(Slf4jExceptionHandler.FATAL, null, null); - - final File file = File.createTempFile("delete_warming_up", "me"); - file.deleteOnExit(); - final long mapAlignment = OS.mapAlignment(); - final int chunks = 64; - final int compileThreshold = Jvm.compileThreshold(); - for (int j = 0; j <= compileThreshold; j += chunks) { - try { - try ( RandomAccessFile raf = new CleaningRandomAccessFile(file, "rw")) { - try (final MappedFile mappedFile = new MappedFile(file, raf, mapAlignment, 0, mapAlignment * chunks, false)) { - warmup0(mapAlignment, chunks, mappedFile); - } - } - Thread.yield(); - } catch (IOException e) { - errorsDuringWarmup.add(e); - } - } - Thread.yield(); - Files.delete(file.toPath()); - } catch (IOException e) { - Jvm.resetExceptionHandlers(); - Jvm.warn().on(MappedFile.class, "Error during warmup", e); - } finally { - Jvm.resetExceptionHandlers(); - if (errorsDuringWarmup.size() > 0) - Jvm.warn().on(MappedFile.class, errorsDuringWarmup.size() + " errors during warmup: " + errorsDuringWarmup); - } - } - - private static void warmup0(final long mapAlignment, - final int chunks, - final MappedFile mappedFile) throws IOException { - ReferenceOwner warmup = ReferenceOwner.temporary("warmup"); - for (int i = 0; i < chunks; i++) { - mappedFile.acquireBytesForRead(warmup, i * mapAlignment) - .release(warmup); - mappedFile.acquireBytesForWrite(warmup, i * mapAlignment) - .release(warmup); - } - } - - - public File file() { - return file; - } - - /** - * @throws IllegalStateException if closed. - */ - - public MappedBytesStore acquireByteStore( - ReferenceOwner owner, - final long position) - throws IOException, IllegalArgumentException, IllegalStateException { - return acquireByteStore(owner, position, null, readOnly ? ReadOnlyMappedBytesStore::new : MappedBytesStore::new); - } - - - public MappedBytesStore acquireByteStore( - ReferenceOwner owner, - final long position, - BytesStore oldByteStore) - throws IOException, IllegalArgumentException, IllegalStateException { - return acquireByteStore(owner, position, oldByteStore, readOnly ? ReadOnlyMappedBytesStore::new : MappedBytesStore::new); - } - - - public MappedBytesStore acquireByteStore( - ReferenceOwner owner, - final long position, - BytesStore oldByteStore, - final MappedBytesStoreFactory mappedBytesStoreFactory) - throws IOException, - IllegalArgumentException, - IllegalStateException { - - throwExceptionIfClosed(); - - if (position < 0) - throw new IOException("Attempt to access a negative position: " + position); - final int chunk = (int) (position / chunkSize); - Jvm.safepoint(); - - final MappedBytesStore mbs; - synchronized (stores) { - while (stores.size() <= chunk) - stores.add(null); - mbs = stores.get(chunk); - } - if (mbs != null) { - // don't reserve it again if we are already holding it. - if (mbs == oldByteStore) { - return mbs; - } - if (mbs.tryReserve(owner)) { - return mbs; - } - } - - // its important we perform this outside the synchronized below, as this operation can take a while and if synchronized can block slow tailer - // from acquiring the next block - resizeRafIfTooSmall(chunk); - - synchronized (stores) { - - // We are back, protected by synchronized, and need to - // update our view on previous existence (we might have been stalled - // for a long time since we last checked dues to resizing and another - // thread might have added a MappedByteStore (very unlikely but still possible)) - final MappedBytesStore mbs1 = stores.get(chunk); - if (mbs1 != null && mbs1.tryReserve(owner)) { - return mbs1; - } - // *** THIS CAN TAKE A LONG TIME IF A RESIZE HAS TO OCCUR *** - // let double check it to make sure no other thread change it in the meantime. - //resizeRafIfTooSmall(chunk); - - final long mappedSize = chunkSize + overlapSize; - final MapMode mode = readOnly ? MapMode.READ_ONLY : MapMode.READ_WRITE; - final long startOfMap = chunk * chunkSize; - - final long beginNs = System.nanoTime(); - - final long address = OS.map(fileChannel, mode, startOfMap, mappedSize); - final MappedBytesStore mbs2 = mappedBytesStoreFactory.create(owner, this, chunk * this.chunkSize, address, mappedSize, this.chunkSize); - if (RETAIN) - mbs2.reserve(this); - stores.set(chunk, mbs2); - - final long elapsedNs = System.nanoTime() - beginNs; - if (newChunkListener != null) - newChunkListener.onNewChunk(file.getPath(), chunk, elapsedNs / 1000); - - if (elapsedNs >= 2_000_000L) - Jvm.perf().on(getClass(), "Took " + elapsedNs / 1_000_000L + " ms to add mapping for " + file()); - - return mbs2; - } - } - - private void resizeRafIfTooSmall(final int chunk) throws IOException { - Jvm.safepoint(); - - final long minSize = (chunk + 1L) * chunkSize + overlapSize; - long size = fileChannel.size(); - Jvm.safepoint(); - if (size >= minSize || readOnly) - return; - - // handle a possible race condition between processes. - try { - // A single JVM cannot lock a distinct canonical file more than once. - - // We might have several MappedFile objects that maps to - // the same underlying file (possibly via hard or soft links) - // so we use the canonical path as a lock key - - // Ensure exclusivity for any and all MappedFile objects handling - // the same canonical file. - synchronized (canonicalPath) { - size = fileChannel.size(); - if (size < minSize) { - final long beginNs = System.nanoTime(); - try (FileLock ignore = fileChannel.lock()) { - size = fileChannel.size(); - if (size < minSize) { - Jvm.safepoint(); - raf.setLength(minSize); - Jvm.safepoint(); - } - } - final long elapsedNs = System.nanoTime() - beginNs; - if (elapsedNs >= 1_000_000L) { - Jvm.perf().on(getClass(), "Took " + elapsedNs / 1000L + " us to grow file " + file()); - } - } - } - } catch (IOException ioe) { - throw new IOException("Failed to resize to " + minSize, ioe); - } - } - - /** - * Convenience method so you don't need to release the BytesStore - */ - - public Bytes acquireBytesForRead(ReferenceOwner owner, final long position) - throws IOException, IllegalStateException, IllegalArgumentException { - throwExceptionIfClosed(); - - final MappedBytesStore mbs = acquireByteStore(owner, position, null); - final Bytes bytes = mbs.bytesForRead(); - bytes.readPositionUnlimited(position); - bytes.reserveTransfer(INIT, owner); - mbs.release(owner); - return bytes; - } - - public void acquireBytesForRead(ReferenceOwner owner, final long position, final VanillaBytes bytes) - throws IOException, IllegalStateException, IllegalArgumentException { - throwExceptionIfClosed(); - - final MappedBytesStore mbs = acquireByteStore(owner, position, null); - bytes.bytesStore(mbs, position, mbs.capacity() - position); - } - - - public Bytes acquireBytesForWrite(ReferenceOwner owner, final long position) - throws IOException, IllegalStateException, IllegalArgumentException { - throwExceptionIfClosed(); - - MappedBytesStore mbs = acquireByteStore(owner, position, null); - Bytes bytes = mbs.bytesForWrite(); - bytes.writePosition(position); - bytes.reserveTransfer(INIT, owner); - mbs.release(owner); - return bytes; - } - - public void acquireBytesForWrite(ReferenceOwner owner, final long position, final VanillaBytes bytes) - throws IOException, IllegalStateException, IllegalArgumentException { - throwExceptionIfClosed(); - - final MappedBytesStore mbs = acquireByteStore(owner, position, null); - bytes.bytesStore(mbs, position, mbs.capacity() - position); - bytes.writePosition(position); - } - - @Override - protected boolean performReleaseInBackground() { - // don't perform the close in the background as that just sets a flag. This does the real work. - return true; - } - - protected void performRelease() { - try { - synchronized (stores) { - for (int i = 0; i < stores.size(); i++) { - final MappedBytesStore mbs = stores.get(i); - if (mbs != null && RETAIN) { - // this MappedFile is the only referrer to the MappedBytesStore at this point, - // so ensure that it is released - mbs.release(this); - } - // Dereference released entities - stores.set(i, null); - } - } - } finally { - closeQuietly(raf); - setClosed(); - } - } - - - public String referenceCounts() { - final StringBuilder sb = new StringBuilder(); - sb.append("refCount: ").append(refCount()); - for ( final MappedBytesStore mbs : stores) { - long count = 0; - if (mbs != null) - count = mbs.refCount(); - sb.append(", ").append(count); - } - return sb.toString(); - } - - public long capacity() { - return capacity; - } - - public long chunkSize() { - return chunkSize; - } - - public long overlapSize() { - return overlapSize; - } - - public NewChunkListener getNewChunkListener() { - return newChunkListener; - } - - public void setNewChunkListener(final NewChunkListener listener) { - throwExceptionIfClosedInSetter(); - - this.newChunkListener = listener; - } - - public long actualSize() throws IORuntimeException { - - boolean interrupted = Thread.interrupted(); - try { - return fileChannel.size(); - - // this was seen once deep in the JVM. - } catch (ArrayIndexOutOfBoundsException aiooe) { - // try again. - return actualSize(); - - } catch (ClosedByInterruptException cbie) { - close(); - interrupted = true; - throw new IllegalStateException(cbie); - - } catch (IOException e) { - final boolean open = fileChannel.isOpen(); - if (open) { - throw new IORuntimeException(e); - } else { - close(); - throw new IllegalStateException(e); - } - } finally { - if (interrupted) - Thread.currentThread().interrupt(); - } - } - - - public RandomAccessFile raf() { - return raf; - } - - @Override - protected void finalize() throws Throwable { - warnAndReleaseIfNotReleased(); - super.finalize(); - } - - @Override - protected boolean threadSafetyCheck(boolean isUsed) { - // component is thread safe - return true; - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedUniqueMicroTimeProvider.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedUniqueMicroTimeProvider.java deleted file mode 100644 index 217b386..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedUniqueMicroTimeProvider.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.IOTools; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.time.SystemTimeProvider; -import net.openhft.chronicle.core.time.TimeProvider; - -import java.io.IOException; - -/** - * Timestamps are unique across threads/processes on a single machine. - * - * @deprecated Use {@link MappedUniqueTimeProvider} instead. - */ -@Deprecated(/* to be removed in x.23 */) -public enum MappedUniqueMicroTimeProvider implements TimeProvider { - INSTANCE; - - private static final int LAST_TIME = 128; - - private final MappedFile file; - @SuppressWarnings("rawtypes") - private final Bytes bytes; - private TimeProvider provider = SystemTimeProvider.INSTANCE; - - MappedUniqueMicroTimeProvider() { - try { - String user = System.getProperty("user.name", "unknown"); - file = MappedFile.mappedFile(OS.TMP + "/.time-stamp." + user + ".dat", OS.pageSize(), 0); - IOTools.unmonitor(file); - ReferenceOwner mumtp = ReferenceOwner.temporary("mumtp"); - bytes = file.acquireBytesForWrite(mumtp, 0); - bytes.append8bit("&TSF\nTime stamp file uses for sharing a unique id\n"); - IOTools.unmonitor(bytes); - } catch (IOException ioe) { - throw new IORuntimeException(ioe); - } - } - - public MappedUniqueMicroTimeProvider provider(TimeProvider provider) { - this.provider = provider; - return this; - } - - @Override - public long currentTimeMillis() { - return provider.currentTimeMillis(); - } - - @Override - public long currentTimeMicros() { - long timeus = provider.currentTimeMicros(); - while (true) { - long time0 = bytes.readVolatileLong(LAST_TIME); - long time0us = time0 / 1000; - long time; - if (time0us >= timeus) - time = (time0us + 1) * 1000; - else - time = timeus * 1000; - if (bytes.compareAndSwapLong(LAST_TIME, time0, time)) - return time / 1_000; - Jvm.nanoPause(); - } - } - - @Override - public long currentTimeNanos() { - long time = provider.currentTimeNanos(); - while (true) { - long time0 = bytes.readVolatileLong(LAST_TIME); - if (time0 >= time) - time = time0 + 1; - if (bytes.compareAndSwapLong(LAST_TIME, time0, time)) - return time; - Jvm.nanoPause(); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedUniqueTimeProvider.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedUniqueTimeProvider.java deleted file mode 100644 index 9350c39..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MappedUniqueTimeProvider.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.IOTools; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.time.SystemTimeProvider; -import net.openhft.chronicle.core.time.TimeProvider; - -import java.io.IOException; - -/** - * Timestamps are unique across threads/processes on a single machine. - */ -public enum MappedUniqueTimeProvider implements TimeProvider { - INSTANCE; - - private static final int LAST_TIME = 128; - - private final MappedFile file; - @SuppressWarnings("rawtypes") - private final Bytes bytes; - private TimeProvider provider = SystemTimeProvider.INSTANCE; - - MappedUniqueTimeProvider() { - try { - String user = System.getProperty("user.name", "unknown"); - file = MappedFile.mappedFile(OS.TMP + "/.time-stamp." + user + ".dat", OS.pageSize(), 0); - IOTools.unmonitor(file); - ReferenceOwner mumtp = ReferenceOwner.temporary("mumtp"); - bytes = file.acquireBytesForWrite(mumtp, 0); - bytes.append8bit("&TSF\nTime stamp file uses for sharing a unique id\n"); - IOTools.unmonitor(bytes); - } catch (IOException ioe) { - throw new IORuntimeException(ioe); - } - } - - public MappedUniqueTimeProvider provider(TimeProvider provider) { - this.provider = provider; - return this; - } - - @Override - public long currentTimeMillis() { - return provider.currentTimeMillis(); - } - - @Override - public long currentTimeMicros() { - long timeus = provider.currentTimeMicros(); - while (true) { - long time0 = bytes.readVolatileLong(LAST_TIME); - long time0us = time0 / 1000; - long time; - if (time0us >= timeus) - time = (time0us + 1) * 1000; - else - time = timeus * 1000; - if (bytes.compareAndSwapLong(LAST_TIME, time0, time)) - return time / 1_000; - Jvm.nanoPause(); - } - } - - @Override - public long currentTimeNanos() { - long time = provider.currentTimeNanos(); - long time5 = time >>> 5; - - long time0 = bytes.readVolatileLong(LAST_TIME); - long timeNanos5 = time0 >>> 5; - - if (time5 > timeNanos5) - if (bytes.compareAndSwapLong(LAST_TIME, time0, time)) - return time; - - while (true) { - time0 = bytes.readVolatileLong(LAST_TIME); - long next = (time0 + 0x20) & ~0x1f; - if (bytes.compareAndSwapLong(LAST_TIME, time0, next)) - return next; - Jvm.nanoPause(); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodEncoder.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodEncoder.java deleted file mode 100644 index 7a42e4d..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodEncoder.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -public interface MethodEncoder { - long messageId(); - - @SuppressWarnings("rawtypes") - void encode(Object[] objects, BytesOut out); - - @SuppressWarnings("rawtypes") - Object[] decode(Object[] lastObjects, BytesIn in); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodEncoderLookup.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodEncoderLookup.java deleted file mode 100644 index f9c900c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodEncoderLookup.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.util.Annotations; - -import java.lang.reflect.Method; -import java.util.function.Function; - -public enum MethodEncoderLookup implements Function { - BY_ANNOTATION; - - @Override - public MethodEncoder apply(Method method) { - MethodId methodId = Annotations.getAnnotation(method, MethodId.class); - if (methodId == null) return null; - long messageId = methodId.value(); - return new MethodEncoder() { - @Override - public long messageId() { - return messageId; - } - - @SuppressWarnings("rawtypes") - @Override - public void encode(Object[] objects, BytesOut out) { - for (Object object : objects) { - if (object instanceof BytesMarshallable) { - ((BytesMarshallable) object).writeMarshallable(out); - continue; - } - throw new IllegalArgumentException("Object type " + object + " not supported"); - } - } - - @SuppressWarnings("rawtypes") - @Override - public Object[] decode(Object[] lastObjects, BytesIn in) { - for (Object lastObject : lastObjects) - ((BytesMarshallable) lastObject).readMarshallable(in); - return lastObjects; - } - }; - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodId.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodId.java deleted file mode 100644 index 7a7da27..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodId.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import java.lang.annotation.*; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -@Inherited -public @interface MethodId { - long value(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReader.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReader.java deleted file mode 100644 index b29de27..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReader.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; -import net.openhft.chronicle.core.util.InvocationTargetRuntimeException; - -public interface MethodReader extends Closeable { - String HISTORY = "history"; - - MethodReaderInterceptorReturns methodReaderInterceptorReturns(); - - /** - * Moves the queue to read a message if there is one, but is more expensive than {@link #lazyReadOne()}. - * If there is an exception in the dispatching mechanics then this should be caught and Jvm.warn'd. - * If there is an exception in the invocation then this is wrapped in a {@link InvocationTargetRuntimeException} - * and thrown. - * - * @return true if there was a message, false if no more messages. - * @throws InvocationTargetRuntimeException if the receiver (target method) throws - */ - boolean readOne() throws InvocationTargetRuntimeException; - - /** - * Does a quick read which is simpler but might not read the next message. readOne() has to be called periodically. - * See also javadoc for {@link #readOne()} - * - * @return true if there was a message, false if there is probably not a message. - */ - default boolean lazyReadOne() { - return readOne(); - } - - /** - * Call close on the input when closed - */ - MethodReader closeIn(boolean closeIn); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderBuilder.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderBuilder.java deleted file mode 100644 index 3f0ca8a..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderBuilder.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -public interface MethodReaderBuilder { - default MethodReaderBuilder methodReaderInterceptor(MethodReaderInterceptor methodReaderInterceptor) { - return methodReaderInterceptorReturns((m, o, a, i) -> { - methodReaderInterceptor.intercept(m, o, a, i); - return null; - }); - } - - MethodReaderBuilder methodReaderInterceptorReturns(MethodReaderInterceptorReturns methodReaderInterceptorReturns); - - MethodReaderBuilder warnMissing(boolean warnMissing); - - MethodReader build(Object... components); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderInterceptor.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderInterceptor.java deleted file mode 100644 index 86d4f6b..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderInterceptor.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -/** - * @deprecated Use {@link MethodReaderInterceptorReturns} - */ -@Deprecated(/* to be removed in x.22 */) -@FunctionalInterface -public interface MethodReaderInterceptor { - void intercept(Method m, Object o, Object[] args, Invocation invocation) throws InvocationTargetException; -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderInterceptorReturns.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderInterceptorReturns.java deleted file mode 100644 index 5a38c84..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodReaderInterceptorReturns.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -@FunctionalInterface -public interface MethodReaderInterceptorReturns { - Object intercept(Method m, Object o, Object[] args, Invocation invocation) throws InvocationTargetException; -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterBuilder.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterBuilder.java deleted file mode 100644 index 82677cd..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterBuilder.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; - -import java.util.function.Supplier; - -public interface MethodWriterBuilder extends Supplier { - - MethodWriterBuilder genericEvent(String genericEvent); - - default MethodWriterBuilder metaData(boolean metaData) { - return this; - } - - @Deprecated(/* to be removed in x.23 */) - MethodWriterBuilder useMethodIds(boolean useMethodIds); - - MethodWriterBuilder onClose(Closeable closeable); - - // sourceId enables this, this isn't useful unless it's set. - @Deprecated(/* to be removed in x.22 */) - MethodWriterBuilder recordHistory(boolean recordHistory); - - default MethodWriterBuilder updateInterceptor(UpdateInterceptor updateInterceptor) { - throw new UnsupportedOperationException(); - } - - default T build() { - return get(); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInterceptor.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInterceptor.java deleted file mode 100644 index ebe02be..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInterceptor.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.Method; -import java.util.function.BiConsumer; - -/** - * Invoked around method writing allowing you to take action before or after method invocation, - * or even not to call the method - * - * @deprecated Use MethodWriterInterceptorReturns - */ -@FunctionalInterface -@Deprecated(/* to be removed in x.22 */) -public interface MethodWriterInterceptor { - - static MethodWriterInterceptor of( final MethodWriterListener methodWriterListener, final MethodWriterInterceptor interceptor) { - if (methodWriterListener == null && interceptor == null) - throw new IllegalArgumentException("both methodWriterListener and interceptor are NULL"); - - if (methodWriterListener == null) - return interceptor::intercept; - - if (interceptor == null) - return (method, args, invoker) -> { - methodWriterListener.onWrite(method.getName(), args); - invoker.accept(method, args); - }; - - return (method, args, invoker) -> { - interceptor.intercept(method, args, invoker); - methodWriterListener.onWrite(method.getName(), args); - }; - } - - void intercept(Method method, Object[] args, BiConsumer invoker); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInterceptorReturns.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInterceptorReturns.java deleted file mode 100644 index 1bd81c7..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInterceptorReturns.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.Method; -import java.util.function.BiFunction; - -/* - * Peter Lawrey so interceptors can return an object to use for chaining. - *

- * Invoked around method writing allowing you to take action before or after method invocation, - * or even not to call the method - */ -@FunctionalInterface -public interface MethodWriterInterceptorReturns { - static MethodWriterInterceptorReturns of(MethodWriterInterceptor interceptor) { - return (m, a, i) -> { - interceptor.intercept(m, a, i::apply); - return null; - }; - } - - static MethodWriterInterceptorReturns of( final MethodWriterListener methodWriterListener, final MethodWriterInterceptorReturns interceptor) { - if (methodWriterListener == null && interceptor == null) - throw new IllegalArgumentException("both methodWriterListener and interceptor are NULL"); - - if (methodWriterListener == null) - return interceptor; - - if (interceptor == null) - return (method, args, invoker) -> { - methodWriterListener.onWrite(method.getName(), args); - return invoker.apply(method, args); - }; - - return (method, args, invoker) -> { - methodWriterListener.onWrite(method.getName(), args); - return interceptor.intercept(method, args, invoker); - }; - } - - Object intercept(Method method, Object[] args, BiFunction invoker); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInvocationHandler.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInvocationHandler.java deleted file mode 100644 index a21520f..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterInvocationHandler.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.InvocationHandler; - -public interface MethodWriterInvocationHandler extends InvocationHandler { - void recordHistory(boolean recordHistory); - - void onClose(Closeable closeable); - - default void methodWriterInterceptorReturns(MethodWriterListener methodWriterListener, MethodWriterInterceptorReturns interceptor) { - if (methodWriterListener != null || interceptor != null) - methodWriterInterceptorReturns(MethodWriterInterceptorReturns.of(methodWriterListener, interceptor)); - } - - void methodWriterInterceptorReturns(MethodWriterInterceptorReturns methodWriterInterceptor); - - void genericEvent(String genericEvent); - - void useMethodIds(boolean useMethodIds); - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterListener.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterListener.java deleted file mode 100644 index 4396186..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MethodWriterListener.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -/** - * Invoked before writing out this method and args. - * - * @deprecated Use MethodWriterInterceptorReturns - */ -@Deprecated(/* to be removed in x.22 */) -@FunctionalInterface -public interface MethodWriterListener { - void onWrite(String name, Object[] args); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MultiReaderBytesRingBuffer.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MultiReaderBytesRingBuffer.java deleted file mode 100644 index f18ea3f..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/MultiReaderBytesRingBuffer.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -public interface MultiReaderBytesRingBuffer extends BytesRingBuffer { - - - default RingBufferReader createReader() { - return createReader(0); - } - - /** - * Create a reader - * - * @param id of reader as each reader has separate read position etc. - * @return reader - */ - - RingBufferReader createReader(int id); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NativeBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NativeBytes.java deleted file mode 100644 index 36976e4..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NativeBytes.java +++ /dev/null @@ -1,409 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.core.*; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -import static net.openhft.chronicle.bytes.NativeBytesStore.nativeStoreWithFixedCapacity; -import static net.openhft.chronicle.bytes.NoBytesStore.noBytesStore; - -/** - * Elastic memory accessor which can wrap either a ByteBuffer or malloc'ed memory. - *

- *

This class can wrap heap ByteBuffers, called NativeBytes for historical reasons. - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public class NativeBytes - extends VanillaBytes { - private static final boolean BYTES_GUARDED = Jvm.getBoolean("bytes.guarded"); - private static boolean s_newGuarded = BYTES_GUARDED; - private final long capacity; - - public NativeBytes( final BytesStore store, final long capacity) throws IllegalStateException { - super(store, 0, capacity); - this.capacity = capacity; - } - - public NativeBytes( final BytesStore store) throws IllegalStateException { - super(store, 0, store.capacity()); - capacity = store.capacity(); - } - - /** - * For testing - * - * @return will new NativeBytes be guarded. - */ - public static boolean areNewGuarded() { - return s_newGuarded; - } - - /** - * turn guarding on/off. Can be enabled by assertion with - * - * assert NativeBytes.setNewGuarded(true); - * - * - * @param guarded turn on if true - */ - public static boolean setNewGuarded(final boolean guarded) { - s_newGuarded = guarded; - return true; - } - - /** - * For testing - */ - public static void resetNewGuarded() { - s_newGuarded = BYTES_GUARDED; - } - - - public static NativeBytes nativeBytes() { - try { - return NativeBytes.wrapWithNativeBytes(noBytesStore(), Bytes.MAX_CAPACITY); - } catch (IllegalStateException e) { - throw new AssertionError(e); - } - } - - - public static NativeBytes nativeBytes(final long initialCapacity) throws IllegalArgumentException { - final NativeBytesStore store = nativeStoreWithFixedCapacity(initialCapacity); - try { - try { - return NativeBytes.wrapWithNativeBytes(store, Bytes.MAX_CAPACITY); - } finally { - store.release(INIT); - } - } catch (IllegalStateException e) { - throw new AssertionError(e); - } - } - - public static BytesStore, Void> copyOf( final Bytes bytes) { - final long remaining = bytes.readRemaining(); - - try { - final NativeBytes bytes2 = Bytes.allocateElasticDirect(remaining); - bytes2.write(bytes, 0, remaining); - return bytes2; - } catch (IllegalArgumentException | BufferOverflowException | BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - private static long alignToPageSize(final long size) { - long mask = OS.pageSize() - 1; - return (size + mask) & ~mask; - } - - - public static NativeBytes wrapWithNativeBytes( final BytesStore bs, long capacity) { - return s_newGuarded - ? new GuardedNativeBytes(bs, capacity) - : new NativeBytes<>(bs, capacity); - } - - protected static long maxCapacityFor( BytesStore bs) { - return bs.underlyingObject() instanceof ByteBuffer - || bs.underlyingObject() instanceof byte[] - ? MAX_HEAP_CAPACITY - : Bytes.MAX_CAPACITY; - } - - @Override - public long capacity() { - return capacity; - } - - @Override - protected void writeCheckOffset(final long offset, final long adding) throws BufferOverflowException { - if (offset >= bytesStore.start()) { - final long writeEnd = offset + adding; - if (writeEnd <= bytesStore.safeLimit()) { - return; // do nothing. - } - if (writeEnd >= capacity) - throw new BufferOverflowException(/*"Write exceeds capacity"*/); - checkResize(writeEnd); - } else { - throw new BufferOverflowException(); - } - } - - @Override - void prewriteCheckOffset(long offset, long subtracting) throws BufferOverflowException { - if (offset - subtracting >= bytesStore.start()) { - if (offset <= bytesStore.safeLimit()) { - return; // do nothing. - } - if (offset >= capacity) - throw new BufferOverflowException(/*"Write exceeds capacity"*/); - checkResize(offset); - } else { - throw new BufferOverflowException(); - } - } - - @Override - public void ensureCapacity(final long size) throws IllegalArgumentException { - try { - assert size >= 0; - writeCheckOffset(writePosition(), size); - } catch (BufferOverflowException e) { - IllegalArgumentException iae = new IllegalArgumentException("Bytes cannot be resized to " + size + " limit: " + capacity()); - iae.printStackTrace(); - throw iae; - } - } - - private void checkResize(final long endOfBuffer) throws BufferOverflowException { - if (isElastic()) - resize(endOfBuffer); - else - throw new BufferOverflowException(); - } - - @Override - public boolean isElastic() { - return true; - } - - // the endOfBuffer is the minimum capacity and one byte more than the last addressable byte. - private void resize(final long endOfBuffer) - throws BufferOverflowException { - throwExceptionIfReleased(); - if (endOfBuffer < 0) - throw new DecoratedBufferOverflowException(endOfBuffer + "< 0"); - if (endOfBuffer > capacity()) - throw new DecoratedBufferOverflowException(endOfBuffer + ">" + capacity()); - final long realCapacity = realCapacity(); - if (endOfBuffer <= realCapacity) { -// System.out.println("no resize " + endOfBuffer + " < " + realCapacity); - return; - } - - // Grow by 50% - long size = Math.max(endOfBuffer + 7, realCapacity * 3 / 2 + 32); - if (isDirectMemory() || size > MAX_HEAP_CAPACITY) { - // Allocate direct memory of page granularity - size = alignToPageSize(size); - } else { - size &= ~0x7; - } - // Cap the size with capacity() again - size = Math.min(size, capacity()); - - final boolean isByteBufferBacked = bytesStore.underlyingObject() instanceof ByteBuffer; - if (isByteBufferBacked && size > MAX_HEAP_CAPACITY) { - - // Add a stack trace to this relatively unusual event which will - // enable tracing of potentially derailed code or excessive buffer use. - final StackTrace stackTrace = new StackTrace(); - final String stack = BytesUtil.asString("Calling stack is", stackTrace); - - Jvm.warn().on(getClass(), "Going to try to replace ByteBuffer-backed BytesStore with " + - "raw NativeBytesStore to grow to " + size / 1024 + " KB. If later it is assumed that " + - "this bytes' underlyingObject() is ByteBuffer, NullPointerException is likely to be thrown. " + - stack); - } -// System.out.println("resize " + endOfBuffer + " to " + size); - if (endOfBuffer > 1 << 20) - Jvm.warn().on(getClass(), "Resizing buffer was " + realCapacity / 1024 + " KB, " + - "needs " + (endOfBuffer - realCapacity) + " bytes more, " + - "new-size " + size / 1024 + " KB"); - final BytesStore store; - int position = 0; - try { - if (isByteBufferBacked && size <= MAX_HEAP_CAPACITY) { - position = ((ByteBuffer) bytesStore.underlyingObject()).position(); - store = allocateNewByteBufferBackedStore(Maths.toInt32(size)); - } else { - store = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(size); - } - store.reserveTransfer(INIT, this); - } catch (IllegalArgumentException e) { - BufferOverflowException boe = new BufferOverflowException(); - boe.initCause(e); - throw boe; - } - - throwExceptionIfReleased(); - final BytesStore, Underlying> tempStore = this.bytesStore; - this.bytesStore.copyTo(store); - this.bytesStore(store); - try { - tempStore.release(this); - } catch (IllegalStateException e) { - Jvm.debug().on(getClass(), e); - } - - if (this.bytesStore.underlyingObject() instanceof ByteBuffer) { - final ByteBuffer byteBuffer = (ByteBuffer) this.bytesStore.underlyingObject(); - byteBuffer.position(0); - byteBuffer.limit(byteBuffer.capacity()); - byteBuffer.position(position); - } - } - - - private BytesStore allocateNewByteBufferBackedStore(final int size) { - if (isDirectMemory()) { - return NativeBytesStore.elasticByteBuffer(size, capacity()); - } else { - return HeapBytesStore.wrap(ByteBuffer.allocate(size)); - } - } - - - @Override - public Bytes write( final byte[] bytes, - final int offset, - final int length) throws BufferOverflowException, IllegalArgumentException { - if (length > writeRemaining()) - throw new BufferOverflowException(); - ensureCapacity(length); - super.write(bytes, offset, length); - return this; - } - - - public Bytes write( final BytesStore bytes, - final long offset, - final long length) throws BufferOverflowException, IllegalArgumentException, BufferUnderflowException { - ensureCapacity(length); - super.write(bytes, offset, length); - return this; - } - - @Override - - public NativeBytes writeSome( final Bytes bytes) { - try { - long length = Math.min(bytes.readRemaining(), writeRemaining()); - if (length + writePosition() >= 1 << 20) - length = Math.min(bytes.readRemaining(), realCapacity() - writePosition()); - final long offset = bytes.readPosition(); - ensureCapacity(length); - optimisedWrite(bytes, offset, length); - if (length == bytes.readRemaining()) { - bytes.clear(); - } else { - bytes.readSkip(length); - if (bytes.writePosition() > bytes.realCapacity() / 2) - bytes.compact(); - } - return this; - } catch (IllegalArgumentException | BufferUnderflowException | BufferOverflowException e) { - throw new AssertionError(e); - } - } - - @Override - protected long writeOffsetPositionMoved(final long adding, final long advance) throws BufferOverflowException { - final long oldPosition = writePosition; - if (writePosition < bytesStore.start()) - throw new BufferOverflowException(); - final long writeEnd = writePosition + adding; - if (writeEnd > writeLimit) - throwBeyondWriteLimit(advance, writeEnd); - else if (writeEnd > bytesStore.safeLimit()) - checkResize(writeEnd); - this.writePosition = writePosition + advance; - return oldPosition; - } - - private void throwBeyondWriteLimit(long advance, long writeEnd) { - throw new DecoratedBufferOverflowException("attempt to write " + advance + " bytes to " + writeEnd + " limit: " + writeLimit); - } - - - @Override - public Bytes writeByte(final byte i8) throws BufferOverflowException { - final long offset = writeOffsetPositionMoved(1, 1); - bytesStore.writeByte(offset, i8); - return this; - } - - - @Override - public Bytes write8bit( final BytesStore bs) throws BufferOverflowException { - if (bs == null) { - writeStopBit(-1); - } else { - final long offset = bs.readPosition(); - final long readRemaining = Math.min(writeRemaining(), bs.readLimit() - offset); - writeStopBit(readRemaining); - write(bs, offset, readRemaining); - } - return this; - } - - - @Override - public Bytes writeLong(final long i64) throws BufferOverflowException { - final long offset = writeOffsetPositionMoved(8L, 8L); - bytesStore.writeLong(offset, i64); - return this; - } - - @Override - public long readRemaining() { - return writePosition - readPosition; - } - - public final static class NativeSubBytes extends SubBytes { - private final NativeBytesStore nativeBytesStore; - - public NativeSubBytes( final BytesStore bytesStore, - final long start, - final long capacity) throws IllegalStateException { - super(bytesStore, start, capacity); - nativeBytesStore = (NativeBytesStore) this.bytesStore; - } - - @Override - public long read(final long offsetInRDI, - final byte[] bytes, - final int offset, - final int length) { - - final int len = (int) Math.min(length, readLimit() - offsetInRDI); - int i; - final long address = nativeBytesStore.address + nativeBytesStore.translate(offsetInRDI); - for (i = 0; i < len - 7; i += 8) - UnsafeMemory.unsafePutLong(bytes, i, nativeBytesStore.memory.readLong(address + i)); - if (i < len - 3) { - UnsafeMemory.unsafePutInt(bytes, i, nativeBytesStore.memory.readInt(address + i)); - i += 4; - } - for (; i < len; i++) - UnsafeMemory.unsafePutByte(bytes, i, nativeBytesStore.memory.readByte(address + i)); - return len; - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NativeBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NativeBytesStore.java deleted file mode 100644 index e09dc9c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NativeBytesStore.java +++ /dev/null @@ -1,852 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.*; -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.cleaner.CleanerServiceLocator; -import net.openhft.chronicle.core.cleaner.spi.ByteBufferCleanerService; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.util.SimpleCleaner; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.lang.reflect.Field; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; - -import static net.openhft.chronicle.bytes.Bytes.MAX_CAPACITY; - -@SuppressWarnings({"restriction", "rawtypes", "unchecked"}) -public class NativeBytesStore - extends AbstractBytesStore, Underlying> { - private static final long MEMORY_MAPPED_SIZE = 128 << 10; - private static final Field BB_ADDRESS, BB_CAPACITY, BB_ATT; - private static final ByteBufferCleanerService CLEANER_SERVICE = CleanerServiceLocator.cleanerService(); - // static MappedBytes last; - - static { - Class directBB = ByteBuffer.allocateDirect(0).getClass(); - BB_ADDRESS = Jvm.getField(directBB, "address"); - BB_CAPACITY = Jvm.getField(directBB, "capacity"); - BB_ATT = Jvm.getField(directBB, "att"); - } - - protected long address; - // on release, set this to null. - protected Memory memory = OS.memory(); - protected long limit, maximumLimit; - - private SimpleCleaner cleaner; - private boolean elastic; - - private Underlying underlyingObject; - private final Finalizer finalizer; - - private NativeBytesStore() { - finalizer = null; - } - - private NativeBytesStore( ByteBuffer bb, boolean elastic) { - this(bb, elastic, Bytes.MAX_HEAP_CAPACITY); - } - - public NativeBytesStore( ByteBuffer bb, boolean elastic, int maximumLimit) { - this(); - init(bb, elastic); - this.maximumLimit = elastic ? maximumLimit : Math.min(limit, maximumLimit); - } - - public NativeBytesStore( - long address, long limit) { - this(address, limit, null, false); - } - - public NativeBytesStore( - long address, long limit, Runnable deallocator, boolean elastic) { - this(address, limit, deallocator, elastic, false); - } - - protected NativeBytesStore( - long address, long limit, Runnable deallocator, boolean elastic, boolean monitored) { - super(monitored); - setAddress(address); - this.limit = limit; - this.maximumLimit = elastic ? MAX_CAPACITY : limit; - this.cleaner = deallocator == null ? null : new SimpleCleaner(deallocator); - underlyingObject = null; - this.elastic = elastic; - if (cleaner == null) - finalizer = null; - else - finalizer = new Finalizer(); - } - - - public static NativeBytesStore wrap( ByteBuffer bb) { - return new NativeBytesStore<>(bb, false); - } - - - public static NativeBytesStore uninitialized() { - return new NativeBytesStore<>(); - } - - /** - * this is an elastic native store - * - * @param capacity of the buffer. - */ - - public static NativeBytesStore nativeStore(long capacity) - throws IllegalArgumentException { - return of(capacity, true, true); - } - - - private static NativeBytesStore of(long capacity, boolean zeroOut, boolean elastic) - throws IllegalArgumentException { - if (capacity <= 0) - return new NativeBytesStore<>(NoBytesStore.NO_PAGE, 0, null, elastic); - Memory memory = OS.memory(); - long address = memory.allocate(capacity); - if (zeroOut || capacity < MEMORY_MAPPED_SIZE) { - memory.setMemory(address, capacity, (byte) 0); - memory.storeFence(); - } - Deallocator deallocator = new Deallocator(address, capacity); - return new NativeBytesStore<>(address, capacity, deallocator, elastic); - } - - - public static NativeBytesStore nativeStoreWithFixedCapacity(long capacity) - throws IllegalArgumentException { - return of(capacity, true, false); - } - - - public static NativeBytesStore lazyNativeBytesStoreWithFixedCapacity(long capacity) - throws IllegalArgumentException { - return of(capacity, false, false); - } - - - public static NativeBytesStore elasticByteBuffer() { - return elasticByteBuffer(OS.pageSize(), MAX_CAPACITY); - } - - - public static NativeBytesStore elasticByteBuffer(int size, long maxSize) { - return new NativeBytesStore<>(ByteBuffer.allocateDirect(size), true, Math.toIntExact(maxSize)); - } - - - public static NativeBytesStore from( String text) { - return from(text.getBytes(StandardCharsets.ISO_8859_1)); - } - - - public static NativeBytesStore from( byte[] bytes) { - try { - NativeBytesStore nbs = nativeStoreWithFixedCapacity(bytes.length); - Bytes bytes2 = Bytes.wrapForRead(bytes); - bytes2.copyTo(nbs); - bytes2.releaseLast(); - return nbs; - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - @Override - public boolean isDirectMemory() { - return true; - } - - @Override - public boolean canReadDirect(long length) { - return limit >= length; - } - - public void init( ByteBuffer bb, boolean elastic) { - this.elastic = elastic; - underlyingObject = (Underlying) bb; - setAddress(Jvm.address(bb)); - this.limit = bb.capacity(); - } - - public void uninit() { - underlyingObject = null; - address = 0; - limit = 0; - maximumLimit = 0; - cleaner = null; - } - - @Override - public void move(long from, long to, long length) throws BufferUnderflowException { - if (from < 0 || to < 0) throw new BufferUnderflowException(); - long address = this.address; - if (address == 0) throwException(null); - memoryCopyMemory(address + from, address + to, length); - } - - private void memoryCopyMemory(long fromAddress, long toAddress, long length) { - try { - memory.copyMemory(fromAddress, toAddress, length); - } catch (NullPointerException ifReleased) { - throwException(ifReleased); - } - } - - private void throwException(Throwable ifReleased) { - throwExceptionIfReleased(); - throw new IllegalStateException(ifReleased); - } - - - @Override - public BytesStore, Underlying> copy() { - try { - if (underlyingObject == null) { - NativeBytesStore copy = of(realCapacity(), false, true); - memoryCopyMemory(address, copy.address, capacity()); - return (BytesStore) copy; - - } else if (underlyingObject instanceof ByteBuffer) { - ByteBuffer bb = ByteBuffer.allocateDirect(Maths.toInt32(capacity())); - bb.put((ByteBuffer) underlyingObject); - bb.clear(); - return (BytesStore) wrap(bb); - - } else { - throw new UnsupportedOperationException(); - } - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - - @Override - public VanillaBytes bytesForWrite() throws IllegalStateException { - return elastic - ? NativeBytes.wrapWithNativeBytes(this, this.capacity()) - : new VanillaBytes<>(this); - } - - @Override - @ForceInline - public long realCapacity() { - return limit; - } - - @Override - @ForceInline - public long capacity() { - return maximumLimit; - } - - - @Override - @ForceInline - public Underlying underlyingObject() { - return underlyingObject; - } - - - @Override - @ForceInline - public NativeBytesStore zeroOut(long start, long end) { - if (end <= start) - return this; - if (start < start()) - start = start(); - if (end > capacity()) - end = capacity(); - - memory.setMemory(address + translate(start), end - start, (byte) 0); - return this; - } - - @Override - @ForceInline - public boolean compareAndSwapInt(long offset, int expected, int value) { - return memory.compareAndSwapInt(address + translate(offset), expected, value); - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - memory.testAndSetInt(address + translate(offset), offset, expected, value); - } - - @Override - @ForceInline - public boolean compareAndSwapLong(long offset, long expected, long value) { - return memory.compareAndSwapLong(address + translate(offset), expected, value); - } - - @Override - @ForceInline - public long addAndGetLong(long offset, long adding) throws BufferUnderflowException { - return memory.addLong(address + translate(offset), adding); - } - - @Override - @ForceInline - public int addAndGetInt(long offset, int adding) throws BufferUnderflowException { - return memory.addInt(address + translate(offset), adding); - } - - protected long translate(long offset) { - return offset; - } - - @Override - @ForceInline - public byte readByte(long offset) { - return memory.readByte(address + translate(offset)); - } - - public int readUnsignedByte(long offset) throws BufferUnderflowException { - return readByte(offset) & 0xFF; - } - - @Override - @ForceInline - public short readShort(long offset) { - return memory.readShort(address + translate(offset)); - } - - @Override - @ForceInline - public int readInt(long offset) { - return memory.readInt(address + translate(offset)); - } - - @Override - @ForceInline - public long readLong(long offset) { - long address = this.address; - assert address != 0; - return memory.readLong(address + translate(offset)); - } - - @Override - @ForceInline - public float readFloat(long offset) { - return memory.readFloat(address + translate(offset)); - } - - @Override - @ForceInline - public double readDouble(long offset) { - return memory.readDouble(address + translate(offset)); - } - - @Override - @ForceInline - public byte readVolatileByte(long offset) { - return memory.readVolatileByte(address + translate(offset)); - } - - @Override - @ForceInline - public short readVolatileShort(long offset) { - return memory.readVolatileShort(address + translate(offset)); - } - - @Override - @ForceInline - public int readVolatileInt(long offset) { - return memory.readVolatileInt(address + translate(offset)); - } - - @Override - @ForceInline - public long readVolatileLong(long offset) { - return memory.readVolatileLong(address + translate(offset)); - } - - - @Override - @ForceInline - public NativeBytesStore writeByte(long offset, byte i8) { - memory.writeByte(address + translate(offset), i8); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeShort(long offset, short i16) { - memory.writeShort(address + translate(offset), i16); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeInt(long offset, int i32) { - try { - memory.writeInt(address + translate(offset), i32); - return this; - } catch (NullPointerException e) { - throwExceptionIfReleased(); - throw e; - } - } - - - @Override - @ForceInline - public NativeBytesStore writeOrderedInt(long offset, int i) { - memory.writeOrderedInt(address + translate(offset), i); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeLong(long offset, long i64) { - memory.writeLong(address + translate(offset), i64); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeOrderedLong(long offset, long i) { - memory.writeOrderedLong(address + translate(offset), i); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeFloat(long offset, float f) { - memory.writeFloat(address + translate(offset), f); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeDouble(long offset, double d) { - memory.writeDouble(address + translate(offset), d); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeVolatileByte(long offset, byte i8) { - memory.writeVolatileByte(address + translate(offset), i8); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeVolatileShort(long offset, short i16) { - memory.writeVolatileShort(address + translate(offset), i16); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeVolatileInt(long offset, int i32) { - memory.writeVolatileInt(address + translate(offset), i32); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore writeVolatileLong(long offset, long i64) { - memory.writeVolatileLong(address + translate(offset), i64); - return this; - } - - - @Override - @ForceInline - public NativeBytesStore write( - long offsetInRDO, byte[] bytes, int offset, int length) { - memory.copyMemory(bytes, offset, address + translate(offsetInRDO), length); - return this; - } - - @Override - @ForceInline - public void write( - long offsetInRDO, ByteBuffer bytes, int offset, int length) { - if (bytes.isDirect()) { - memoryCopyMemory(Jvm.address(bytes) + offset, address + translate(offsetInRDO), length); - - } else { - memory.copyMemory(bytes.array(), offset, address + translate(offsetInRDO), length); - } - } - - - @Override - @ForceInline - public NativeBytesStore write( - long writeOffset, RandomDataInput bytes, long readOffset, long length) - throws BufferOverflowException, BufferUnderflowException { - if (bytes.isDirectMemory()) { - memoryCopyMemory(bytes.addressForRead(readOffset), addressForWrite(writeOffset), length); - } else { - write0(writeOffset, bytes, readOffset, length); - } - return this; - } - - public void write0(long offsetInRDO, RandomDataInput bytes, long offset, long length) throws BufferUnderflowException { - long i = 0; - for (; i < length - 7; i += 8) { - writeLong(offsetInRDO + i, bytes.readLong(offset + i)); - } - for (; i < length; i++) { - writeByte(offsetInRDO + i, bytes.readByte(offset + i)); - } - } - - @Override - public long addressForRead(long offset) throws BufferUnderflowException { - if (offset < start() || offset > realCapacity()) - throw new BufferUnderflowException(); - return address + translate(offset); - } - - @Override - public long addressForWrite(long offset) throws BufferOverflowException { - if (offset < start() || offset > realCapacity()) - throw new BufferOverflowException(); - return address + translate(offset); - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - return addressForWrite(start()); - } - - @Override - protected void performRelease() { - memory = null; - if (cleaner != null) { - cleaner.clean(); - - } else if (underlyingObject instanceof ByteBuffer) { - ByteBuffer underlyingObject = (ByteBuffer) this.underlyingObject; - if (underlyingObject.isDirect()) - CLEANER_SERVICE.clean(underlyingObject); - } - } - - - @Override - public String toString() { - try { - return BytesInternal.toString(this); - - } catch (IllegalStateException e) { - return e.toString(); - } - } - - @Override - @ForceInline - public void nativeRead(long position, long address, long size) throws BufferUnderflowException { - // TODO add bounds checking. - memoryCopyMemory(addressForRead(position), address, size); - } - - @Override - @ForceInline - public void nativeWrite(long address, long position, long size) throws BufferOverflowException { - // TODO add bounds checking. - memoryCopyMemory(address, addressForWrite(position), size); - } - - void write8bit(long position, char[] chars, int offset, int length) { - long addr = address + translate(position); - Memory memory = this.memory; - for (int i = 0; i < length; i++) - memory.writeByte(addr + i, (byte) chars[offset + i]); - } - - void read8bit(long position, char[] chars, int length) { - long addr = address + translate(position); - Memory memory = this.memory; - for (int i = 0; i < length; i++) - chars[i] = (char) (memory.readByte(addr + i) & 0xFF); - } - - @Override - public long readIncompleteLong(long offset) { - int remaining = (int) Math.min(8, readRemaining() - offset); - long l = 0; - for (int i = 0; i < remaining; i++) { - byte b = memory.readByte(address + offset + i); - l |= (long) (b & 0xFF) << (i * 8); - } - return l; - } - - @Override - public boolean equals(Object obj) { - return obj instanceof BytesStore && BytesInternal.contentEqual(this, (BytesStore) obj); - } - - public void setAddress(long address) { - if ((address & ~0x3FFF) == 0) - throw new AssertionError("Invalid addressForRead " + Long.toHexString(address)); - this.address = address; - } - - @Deprecated(/* to be removed in x.22 */) - public long appendUTF(long pos, char[] chars, int offset, int length) throws BufferOverflowException { - return appendUtf8(pos, chars, offset, length); - } - - public long appendUtf8(long pos, char[] chars, int offset, int length) throws BufferOverflowException { - if (pos + length > realCapacity()) - throw new BufferOverflowException(); - - long address = this.address + translate(0); - Memory memory = this.memory; - if (memory == null) throw new NullPointerException(); - int i; - ascii: - { - for (i = 0; i < length - 3; i += 4) { - final int i2 = offset + i; - char c0 = chars[i2]; - char c1 = chars[i2 + 1]; - char c2 = chars[i2 + 2]; - char c3 = chars[i2 + 3]; - if ((c0 | c1 | c2 | c3) > 0x007F) - break ascii; - final int value = (c0) | (c1 << 8) | (c2 << 16) | (c3 << 24); - UnsafeMemory.unsafePutInt(address + pos, value); - pos += 4; - } - for (; i < length; i++) { - char c = chars[offset + i]; - if (c > 0x007F) - break ascii; - UnsafeMemory.unsafePutByte(address + pos++, (byte) c); - } - - return pos; - } - return appendUtf8a(pos, chars, offset, length, i); - } - - private long appendUtf8a(long pos, char[] chars, int offset, int length, int i) { - for (; i < length; i++) { - char c = chars[offset + i]; - if (c <= 0x007F) { - writeByte(pos++, (byte) c); - - } else if (c <= 0x07FF) { - writeByte(pos++, (byte) (0xC0 | ((c >> 6) & 0x1F))); - writeByte(pos++, (byte) (0x80 | c & 0x3F)); - - } else { - writeByte(pos++, (byte) (0xE0 | ((c >> 12) & 0x0F))); - writeByte(pos++, (byte) (0x80 | ((c >> 6) & 0x3F))); - writeByte(pos++, (byte) (0x80 | (c & 0x3F))); - } - } - return pos; - } - - @Override - public long copyTo( BytesStore store) { - if (store.isDirectMemory()) - return copyToDirect(store); - else - return super.copyTo(store); - } - - public long copyToDirect( BytesStore store) { - long toCopy = Math.min(limit, store.safeLimit()); - if (toCopy > 0) { - try { - long addr = address; - long addr2 = store.addressForWrite(0); - memoryCopyMemory(addr, addr2, toCopy); - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - return toCopy; - } - - - @Override - public ByteBuffer toTemporaryDirectByteBuffer() { - ByteBuffer bb = ByteBuffer.allocateDirect(0); - try { - BB_ADDRESS.setLong(bb, address); - BB_CAPACITY.setInt(bb, Maths.toUInt31(readRemaining())); - BB_ATT.set(bb, this); - } catch (Exception e) { - throw new AssertionError(e); - } - bb.clear(); - return bb; - } - - @Override - public int byteCheckSum() throws IORuntimeException { - return byteCheckSum(start(), readLimit()); - } - - public int byteCheckSum(long position, long limit) { - Memory memory = this.memory; - assert memory != null; - int b = 0; - long ptr = address + position; - long end = address + limit; - for (; ptr < end - 7; ptr += 8) { - b += memory.readByte(ptr) - + memory.readByte(ptr + 1) - + memory.readByte(ptr + 2) - + memory.readByte(ptr + 3) - + memory.readByte(ptr + 4) - + memory.readByte(ptr + 5) - + memory.readByte(ptr + 6) - + memory.readByte(ptr + 7); - } - for (; ptr < end; ptr++) { - b += memory.readByte(ptr); - } - return b & 0xFF; - } - - @Override - public boolean sharedMemory() { - return false; - } - - @Override - public long read(long offsetInRDI, byte[] bytes, int offset, int length) { - int len = (int) Math.min(length, readLimit() - offsetInRDI); - int i; - final long address = this.address + translate(offsetInRDI); - for (i = 0; i < len - 7; i += 8) - UnsafeMemory.unsafePutLong(bytes, i, memory.readLong(address + i)); - if (i < len - 3) { - UnsafeMemory.unsafePutInt(bytes, i, memory.readInt(address + i)); - i += 4; - } - for (; i < len; i++) - UnsafeMemory.unsafePutByte(bytes, i, memory.readByte(address + i)); - return len; - } - - @Override - public int peekUnsignedByte(long offset) { - final long address = this.address; - final Memory memory = this.memory; - final long translate = translate(offset); -// assert translate >= 0; - final long address2 = address + translate; -// last.writeLong(8, Thread.currentThread().getId()); -// last.writeLong(0, offset); -// last.writeLong(16, translate); -// last.writeLong(32, maximumLimit); -// last.writeLong(48, addressForRead); -// last.writeLong(64, address2); -// last.writeBoolean(80, memory != null); -// last.writeVolatileByte(88, (byte) 1); - int ret = translate >= limit ? -1 : - memory.readByte(address2) & 0xFF; -// last.writeVolatileByte(88, (byte) 0xFF); -// last.writeLong(24, Thread.currentThread().getId()); - return ret; - } - - @Override - public int fastHash(long offset, int length) throws BufferUnderflowException { - long ret; - switch (length) { - case 0: - return 0; - case 1: - ret = readByte(offset); - break; - case 2: - ret = readShort(offset); - break; - case 4: - ret = readInt(offset); - break; - case 8: - ret = readInt(offset) * 0x6d0f27bdL + readInt(offset + 4); - break; - default: - return super.fastHash(offset, length); - } - long hash = ret * 0x855dd4db; - return (int) (hash ^ (hash >> 32)); - } - - @Override - public long safeLimit() { - return limit; - } - - static class Deallocator implements Runnable { - - private volatile long address; - private final long size; - - Deallocator(long address, long size) { - assert address != 0; - this.address = address; - this.size = size; - } - - @Override - public void run() { - // System.out.println("Release " + Long.toHexString(addressForRead)); - if (address == 0) - return; - long addressToFree = address; - address = 0; - OS.memory().freeMemory(addressToFree, size); - } - } - - private class Finalizer { - @Override - protected void finalize() throws Throwable { - super.finalize(); - warnAndReleaseIfNotReleased(); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NewChunkListener.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NewChunkListener.java deleted file mode 100644 index 4feb029..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NewChunkListener.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -@FunctionalInterface -public interface NewChunkListener { - void onNewChunk(String filename, int chunk, long delayMicros); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NoBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NoBytesStore.java deleted file mode 100644 index 0461cd6..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/NoBytesStore.java +++ /dev/null @@ -1,338 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.IOTools; -import net.openhft.chronicle.core.io.ReferenceOwner; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * This is a ByteStore which uses no space but could be resized to be larger (by replacing it with a ByteStire with space) - * - * @see ReleasedBytesStore - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public enum NoBytesStore implements BytesStore { - NO_BYTES_STORE; - - public static final long NO_PAGE; - - - public static final Bytes NO_BYTES; - - static { - try { - NO_PAGE = OS.memory().allocate(OS.pageSize()); - NO_BYTES = new VanillaBytes(noBytesStore()); - IOTools.unmonitor(NO_BYTES); - - } catch ( IllegalArgumentException | IllegalStateException e) { - throw new AssertionError(e); - } - } - - - public static > BytesStore noBytesStore() { - return NO_BYTES_STORE; - } - - @Override - public void reserve(ReferenceOwner owner) throws IllegalStateException { - } - - @Override - public void release(ReferenceOwner owner) throws IllegalStateException { - } - - @Override - public void releaseLast(ReferenceOwner id) throws IllegalStateException { - } - - @Override - public int refCount() { - return 1; - } - - @Override - public boolean tryReserve(ReferenceOwner owner) { - return false; - } - - @Override - public boolean reservedBy(ReferenceOwner owner) { - return true; - } - - - @Override - public RandomDataOutput writeByte(long offset, byte i8) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeShort(long offset, short i) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeInt(long offset, int i) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeOrderedInt(long offset, int i) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeLong(long offset, long i) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeOrderedLong(long offset, long i) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeFloat(long offset, float d) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeDouble(long offset, double d) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeVolatileByte(long offset, byte i8) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeVolatileShort(long offset, short i16) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeVolatileInt(long offset, int i32) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput writeVolatileLong(long offset, long i64) { - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput write(long offsetInRDO, byte[] bytes, int offset, int length) { - if (length != 0) - throw new UnsupportedOperationException(); - return this; - } - - @Override - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) { - if (length != 0) - throw new UnsupportedOperationException(); - } - - - @Override - public RandomDataOutput write(long writeOffset, RandomDataInput bytes, long readOffset, long length) { - if (length != 0) - throw new UnsupportedOperationException(); - return this; - } - - @Override - public byte readByte(long offset) { - throw new UnsupportedOperationException(); - } - - @Override - public int peekUnsignedByte(long offset) { - return -1; - } - - @Override - public short readShort(long offset) { - throw new UnsupportedOperationException(); - } - - @Override - public int readInt(long offset) { - throw new UnsupportedOperationException(); - } - - @Override - public long readLong(long offset) { - throw new UnsupportedOperationException(); - } - - @Override - public float readFloat(long offset) { - throw new UnsupportedOperationException(); - } - - @Override - public double readDouble(long offset) { - throw new UnsupportedOperationException(); - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - throw new UnsupportedOperationException(); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - throw new UnsupportedOperationException(); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - throw new UnsupportedOperationException(); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - throw new BufferUnderflowException(); - } - - @Override - public boolean isDirectMemory() { - return false; - } - - - @Override - public BytesStore copy() { - return this; - } - - @Override - public long capacity() { - return 0; - } - - @Override - public Void underlyingObject() { - return null; - } - - @Override - public boolean inside(long offset) { - return false; - } - - @Override - public boolean inside(long offset, long buffer) { - return false; - } - - @Override - public long copyTo( BytesStore store) { - // nothing to copy. - return 0L; - } - - @Override - public void nativeWrite(long address, long position, long size) { - throw new UnsupportedOperationException(); - } - - @Override - public void nativeRead(long position, long address, long size) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean compareAndSwapInt(long offset, int expected, int value) { - throw new UnsupportedOperationException(); - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean equalBytes( BytesStore bytesStore, long length) { - return length == 0; - } - - @Override - public void move(long from, long to, long length) { - throw new UnsupportedOperationException(); - } - - @Override - public long addressForRead(long offset) throws BufferUnderflowException { - if (offset != 0) - throw new BufferUnderflowException(); - return NO_PAGE; - } - - @Override - public long addressForWrite(long offset) throws BufferOverflowException { - if (offset != 0) - throw new BufferOverflowException(); - return NO_PAGE; - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - return NO_PAGE; - } - - - @Override - public Bytes bytesForWrite() { - throw new UnsupportedOperationException("todo"); - } - - @Override - public boolean sharedMemory() { - return false; - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/OffsetFormat.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/OffsetFormat.java deleted file mode 100644 index 9b8d856..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/OffsetFormat.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -@FunctionalInterface -public interface OffsetFormat { - @SuppressWarnings("rawtypes") - void append(long offset, Bytes bytes); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/OnHeapBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/OnHeapBytes.java deleted file mode 100644 index 07a629d..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/OnHeapBytes.java +++ /dev/null @@ -1,103 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.core.Jvm; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; - -public class OnHeapBytes extends VanillaBytes { - public static final int MAX_CAPACITY = Bytes.MAX_HEAP_CAPACITY; - private final boolean elastic; - private final long capacity; - - public OnHeapBytes( BytesStore bytesStore, boolean elastic) throws IllegalStateException { - super(bytesStore); - this.elastic = elastic; - this.capacity = elastic ? MAX_CAPACITY : bytesStore.capacity(); - - writePosition(0); - writeLimit(capacity()); - } - - @Override - public long capacity() { - return capacity; - } - - @Override - public boolean isElastic() { - return elastic; - } - - @Override - protected void writeCheckOffset(long offset, long adding) - throws BufferOverflowException { - if (offset >= bytesStore.start()) { - long writeEnd = offset + adding; - if (writeEnd > writeLimit) - throwBeyondWriteLimit(adding, writeEnd); - if (writeEnd <= bytesStore.safeLimit()) { - return; // do nothing. - } - checkResize(writeEnd); - } else { - throw new BufferOverflowException(); - } - } - - private void throwBeyondWriteLimit(long advance, long writeEnd) { - throw new DecoratedBufferOverflowException("attempt to write " + advance + " bytes to " + writeEnd + " limit: " + writeLimit); - } - - private void checkResize(long endOfBuffer) - throws BufferOverflowException { - if (isElastic()) - resize(endOfBuffer); - else - throw new BufferOverflowException(); - } - - // the endOfBuffer is the minimum capacity and one byte more than the last addressable byte. - private void resize(long endOfBuffer) - throws BufferOverflowException { - if (endOfBuffer < 0) - throw new BufferOverflowException(); - if (endOfBuffer > capacity()) - throw new BufferOverflowException(); - final long realCapacity = realCapacity(); - if (endOfBuffer <= realCapacity) { -// System.out.println("no resize " + endOfBuffer + " < " + realCapacity); - return; - } - - // Grow by 50% - long size0 = Math.max(endOfBuffer, realCapacity * 3 / 2); - // Size must not be more than capacity(), it may break some assumptions in BytesStore or elsewhere - int size = (int) Math.min(size0, capacity()); - - // System.out.println("resize " + endOfBuffer + " to " + size); - if (endOfBuffer > 1 << 20) - Jvm.warn().on(getClass(), "Resizing buffer was " + realCapacity / 1024 + " KB, " + - "needs " + (endOfBuffer - realCapacity) + " bytes more, " + - "new-size " + size / 1024 + " KB"); - HeapBytesStore store; - try { - store = HeapBytesStore.wrap(new byte[size]); - store.reserveTransfer(INIT, this); - } catch (IllegalArgumentException e) { - BufferOverflowException boe = new BufferOverflowException(); - boe.initCause(e); - throw boe; - } - - BytesStore, byte[]> tempStore = this.bytesStore; - this.bytesStore.copyTo(store); - this.bytesStore(store); - try { - tempStore.release(this); - } catch (IllegalStateException e) { - Jvm.debug().on(getClass(), e); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/PointerBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/PointerBytesStore.java deleted file mode 100644 index 7d31c06..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/PointerBytesStore.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -/** - * A BytesStore which can point to arbitrary memory. - */ -public class PointerBytesStore extends NativeBytesStore { - private boolean isPresent = false; - - public PointerBytesStore() { - super(NoBytesStore.NO_PAGE, 0, null, false, false); - } - - public void set(long address, long capacity) { - isPresent = true; - setAddress(address); - this.limit = maximumLimit = capacity; - } - - - @Override - public VanillaBytes bytesForWrite() throws IllegalStateException { - return new VanillaBytes<>(this); - } - - @Override - public long safeLimit() { - return limit; - } - - @Override - public long start() { - return 0; - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomCommon.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomCommon.java deleted file mode 100644 index 84e8b07..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomCommon.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.io.ReferenceCounted; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteOrder; - -interface RandomCommon extends ReferenceCounted { - /** - * @return The smallest position allowed in this buffer. - */ - @ForceInline - default long start() { - return 0L; - } - - /** - * @return the highest limit allowed for this buffer. - */ - @ForceInline - default long capacity() { - return Bytes.MAX_CAPACITY; - } - - /** - * @return the limit for this buffer without resizing - */ - default long realCapacity() { - return capacity(); - } - - /** - * The read position must be start() <= readPosition() && readPosition() <= readLimit() && readPosition < safeLimit() - * - * @return position to read from. - */ - @ForceInline - default long readPosition() { - return start(); - } - - /** - * The read position must be readPosition() <= writePosition() && writePosition() <= writeLimit() - * - * @return position to write to. - */ - @ForceInline - default long writePosition() { - return start(); - } - - /** - * @return How many more bytes can we read. - */ - @ForceInline - default long readRemaining() { - long remaining = readLimit() - readPosition(); - return remaining; - } - - /** - * @return How many more bytes can we written. - */ - @ForceInline - default long writeRemaining() { - return writeLimit() - writePosition(); - } - - /** - * @return the highest offset or position allowed for this buffer. - */ - @ForceInline - default long readLimit() { - return realCapacity(); - } - - @ForceInline - default long writeLimit() { - return realCapacity(); - } - - /** - * Obtain the underlying addressForRead. This is for expert users only. - * - * @param offset within this buffer. addressForRead(start()) is the actual addressForRead of the first byte. - * @return the underlying addressForRead of the buffer - * @throws UnsupportedOperationException if the underlying buffer is on the heap - * @throws BufferUnderflowException if the offset is before the start() or the after the capacity() - */ - long addressForRead(long offset) - throws UnsupportedOperationException, BufferUnderflowException; - - default long addressForRead(long offset, int buffer) - throws UnsupportedOperationException, BufferUnderflowException { - return addressForRead(offset); - } - - /** - * Obtain the underlying addressForRead. This is for expert users only. - * - * @param offset within this buffer. addressForRead(start()) is the actual addressForRead of the first byte. - * @return the underlying addressForRead of the buffer - * @throws UnsupportedOperationException if the underlying buffer is on the heap - * @throws BufferOverflowException if the offset is before the start() or the after the capacity() - */ - long addressForWrite(long offset) - throws UnsupportedOperationException, BufferOverflowException; - - long addressForWritePosition() - throws UnsupportedOperationException, BufferOverflowException; - - default ByteOrder byteOrder() { - return ByteOrder.nativeOrder(); - } - - /** - * @return the streaming bytes for reading. - */ - @SuppressWarnings("rawtypes") - - Bytes bytesForRead() throws IllegalStateException; - - /** - * @return the streaming bytes for writing. - */ - @SuppressWarnings("rawtypes") - - Bytes bytesForWrite() throws IllegalStateException; - - /** - * Perform a 32-bit CAS at a given offset. - * - * @param offset to perform CAS - * @param expected value - * @param value to set - * @return true, if successful. - */ - boolean compareAndSwapInt(long offset, int expected, int value) - throws BufferOverflowException; - - void testAndSetInt(long offset, int expected, int value); - - /** - * Perform a 64-bit CAS at a given offset. - * - * @param offset to perform CAS - * @param expected value - * @param value to set - * @return true, if successful. - */ - boolean compareAndSwapLong(long offset, long expected, long value) - throws BufferOverflowException; - - /** - * Perform a 32-bit float CAS at a given offset. - * - * @param offset to perform CAS - * @param expected value - * @param value to set - * @return true, if successful. - */ - default boolean compareAndSwapFloat(long offset, float expected, float value) - throws BufferOverflowException { - return compareAndSwapInt(offset, Float.floatToRawIntBits(expected), Float.floatToRawIntBits(value)); - } - - /** - * Perform a 64-bit double CAS at a given offset. - * - * @param offset to perform CAS - * @param expected value - * @param value to set - * @return true, if successful. - */ - default boolean compareAndSwapDouble(long offset, double expected, double value) - throws BufferOverflowException { - return compareAndSwapLong(offset, Double.doubleToRawLongBits(expected), Double.doubleToRawLongBits(value)); - } - - /** - * @return true if these Bytes use shared memory. - */ - boolean sharedMemory(); - - boolean isDirectMemory(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomDataInput.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomDataInput.java deleted file mode 100644 index bcca92a..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomDataInput.java +++ /dev/null @@ -1,578 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.io.IORuntimeException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * This allows random access to the underling bytes. This instance can be used across threads as it is stateless. - * The thread safety of the underlying data depends on how the methods are used. - */ -public interface RandomDataInput extends RandomCommon { - String[] charToString = createCharToString(); - - - static String[] createCharToString() { - String[] charToString = new String[256]; - charToString[0] = "\u0660"; - for (int i = 1; i < 21; i++) - charToString[i] = Character.toString((char) (i + 0x2487)); - for (int i = ' '; i < 256; i++) - charToString[i] = Character.toString((char) i); - for (int i = 21; i < ' '; i++) - charToString[i] = "\\u00" + Integer.toHexString(i).toUpperCase(); - for (int i = 0x80; i < 0xA0; i++) - charToString[i] = "\\u00" + Integer.toHexString(i).toUpperCase(); - return charToString; - } - - default int peekVolatileInt() throws BufferUnderflowException { - return readVolatileInt(readPosition()); - } - - /** - * Read boolean at an offset - * - * @param offset to read - * @return the boolean - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - @ForceInline - default boolean readBoolean(long offset) - throws BufferUnderflowException { - return BytesUtil.byteToBoolean(readByte(offset)); - } - - /** - * Read byte at an offset - * - * @param offset to read - * @return the byte - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - byte readByte(long offset) throws BufferUnderflowException; - - /** - * Read an unsigned byte at an offset - * - * @param offset to read - * @return the unsigned byte - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - @ForceInline - default int readUnsignedByte(long offset) throws BufferUnderflowException { - return readByte(offset) & 0xFF; - } - - /** - * Read an unsigned byte at an offset, or -1 - * - * @param offset to read - * @return the unsigned byte or -1 - */ - int peekUnsignedByte(long offset) throws BufferUnderflowException; - - /** - * Read a short at an offset - * - * @param offset to read - * @return the short - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - short readShort(long offset) throws BufferUnderflowException; - - /** - * Read an unsigned short at an offset - * - * @param offset to read - * @return the unsigned short - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - @ForceInline - default int readUnsignedShort(long offset) throws BufferUnderflowException { - return readShort(offset) & 0xFFFF; - } - - /** - * Read an unsigned int at an offset - * - * @param offset to read - * @return the int - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default int readUnsignedInt24(long offset) throws BufferUnderflowException { - return readUnsignedShort(offset) | (readUnsignedByte(offset) << 16); - } - - /** - * Read an int at an offset - * - * @param offset to read - * @return the int - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - int readInt(long offset) throws BufferUnderflowException; - - /** - * Read an unsigned int at an offset - * - * @param offset to read - * @return the unsigned int - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - @ForceInline - default long readUnsignedInt(long offset) throws BufferUnderflowException { - return readInt(offset) & 0xFFFFFFFFL; - } - - /** - * Read a long at an offset - * - * @param offset to read - * @return the long - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - long readLong(long offset) throws BufferUnderflowException; - - /** - * Read a float at an offset - * - * @param offset to read - * @return the float - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - float readFloat(long offset) throws BufferUnderflowException; - - /** - * Read a double at an offset - * - * @param offset to read - * @return the double - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - double readDouble(long offset) throws BufferUnderflowException; - - /** - * Read the byte at an offset and converts it into a printable - * - * @param offset to read - * @return the byte in a printable form. - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default String printable(long offset) throws BufferUnderflowException { - return charToString[readUnsignedByte(offset)]; - } - - /** - * Read a 8-bit byte from memory with a load barrier. - * - * @param offset to read - * @return the byte value - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - byte readVolatileByte(long offset) throws BufferUnderflowException; - - /** - * Read a 16-bit short from memory with a load barrier. - * - * @param offset to read - * @return the short value - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - short readVolatileShort(long offset) throws BufferUnderflowException; - - /** - * Read a 32-bit int from memory with a load barrier. - * - * @param offset to read - * @return the int value - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - int readVolatileInt(long offset) throws BufferUnderflowException; - - /** - * Read a float from memory with a load barrier. - * - * @param offset to read - * @return the float value - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default float readVolatileFloat(long offset) throws BufferUnderflowException { - return Float.intBitsToFloat(readVolatileInt(offset)); - } - - /** - * Read a 64-bit long from memory with a load barrier. - * - * @param offset to read - * @return the long value - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - long readVolatileLong(long offset) throws BufferUnderflowException; - - /** - * Read a 64-bit double from memory with a load barrier. - * - * @param offset to read - * @return the double value - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default double readVolatileDouble(long offset) throws BufferUnderflowException { - return Double.longBitsToDouble(readVolatileLong(offset)); - } - - default long parseLong(long offset) throws BufferUnderflowException { - return BytesInternal.parseLong(this, offset); - } - - /** - * expert level method for copying data to native memory. - * - * @param position within the ByteStore to copy. - * @param address in native memory - * @param size in bytes - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - void nativeRead(long position, long address, long size) throws BufferUnderflowException; - - /** - * Read a byte[] from memory. - * - * @return the length actually read. - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default int copyTo( byte[] bytes) throws BufferUnderflowException { - int len = (int) Math.min(bytes.length, readRemaining()); - for (int i = 0; i < len; i++) - bytes[i] = readByte(start() + i); - return len; - } - - /** - * Copy data from this RandomDataInput to the ByteBuffer. The minimum of {@link #readRemaining()} and - * {@link ByteBuffer#remaining()}. Starting from {@link #start()} in this RandomDataInput and from {@link - * ByteBuffer#position()} of the given bb. Does NOT change the position or limit or mark of the given ByteBuffer. - * Returns the number of the copied bytes. - */ - default int copyTo( ByteBuffer bb) throws BufferUnderflowException { - int pos = bb.position(); - int len = (int) Math.min(bb.remaining(), readRemaining()); - int i; - for (i = 0; i < len - 7; i += 8) - bb.putLong(pos + i, readLong(start() + i)); - for (; i < len; i++) - bb.put(pos + i, readByte(start() + i)); - return len; - } - - /** - * Read a long which is zero padded (high bytes) if the available bytes is less than 8. - * If the offset is at or beyond the readLimit, this will return 0L. - * - * @param offset to read from - * @return the long which might be padded. - */ - default long readIncompleteLong(long offset) { - long left = readLimit() - offset; - long l; - try { - if (left >= 8) - return readLong(offset); - if (left == 4) - return readInt(offset); - l = 0; - for (int i = 0, remaining = (int) left; i < remaining; i++) { - l |= (long) readUnsignedByte(offset + i) << (i * 8); - } - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - return l; - } - - /** - * Returns the actual capacity that can be potentially read. - * - * @return the actual capacity that can be potentially read. - */ - long realCapacity(); - - /** - * Perform an atomic add and get operation for a 32-bit int - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default int addAndGetInt(long offset, int adding) - throws BufferUnderflowException { - return BytesInternal.addAndGetInt(this, offset, adding); - } - - /** - * Perform an atomic add and get operation for a 64-bit long - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default long addAndGetLong(long offset, long adding) - throws BufferUnderflowException { - return BytesInternal.addAndGetLong(this, offset, adding); - } - - /** - * Perform an atomic add and get operation for a 32-bit float - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default float addAndGetFloat(long offset, float adding) - throws BufferUnderflowException { - return BytesInternal.addAndGetFloat(this, offset, adding); - } - - /** - * Perform an atomic add and get operation for a 64-bit double - * - * @param offset to add and get - * @param adding value to add, can be 1 - * @return the sum - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - default double addAndGetDouble(long offset, double adding) - throws BufferUnderflowException { - return BytesInternal.addAndGetDouble(this, offset, adding); - } - - /** - * Copy a sub sequence of bytes as a BytesStore. - * - * @param start of bytes - * @param length of bytes - * @return ByteStore copy. - */ - @SuppressWarnings("rawtypes") - - default BytesStore subBytes(long start, long length) throws BufferUnderflowException { - return BytesInternal.subBytes(this, start, length); - } - - default long findByte(byte stopByte) { - return BytesInternal.findByte(this, stopByte); - } - - /** - * Truncates {@code sb} (it must be a {@link StringBuilder} or {@link Bytes}) and reads a char - * sequence from the given {@code offset}, encoded as Utf8, into it. Returns offset after - * the read Utf8, if a normal char sequence was read, or {@code -1 - offset}, if {@code null} - * was observed (in this case, {@code sb} is truncated too, but not updated then, by querying - * {@code sb} only this case is indistinguishable from reading an empty char sequence). - * - * @param offset the offset in this {@code RandomDataInput} to read char sequence from - * @param sb the buffer to read char sequence into (truncated first) - * @param buffer type, must be {@code StringBuilder} or {@code Bytes} - * @return offset after the normal read char sequence, or -1 - offset, if char sequence is - * {@code null} - * @see RandomDataOutput#writeUtf8(long, CharSequence) - */ - default long readUtf8(long offset, ACS sb) - throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - AppendableUtil.setLength(sb, 0); - // TODO insert some bounds check here - - long utfLen; - if ((utfLen = readByte(offset++)) < 0) { - utfLen &= 0x7FL; - long b; - int count = 7; - while ((b = readByte(offset++)) < 0) { - utfLen |= (b & 0x7FL) << count; - count += 7; - } - if (b != 0) { - if (count > 56) - throw new IORuntimeException( - "Cannot read more than 9 stop bits of positive value"); - utfLen |= (b << count); - } else { - if (count > 63) - throw new IORuntimeException( - "Cannot read more than 10 stop bits of negative value"); - utfLen = ~utfLen; - } - } - if (utfLen == -1) - return ~offset; - int len = Maths.toUInt31(utfLen); - BytesInternal.parseUtf8(this, offset, sb, true, len); - return offset + utfLen; - } - - /** - * Truncates {@code sb} (it must be a {@link StringBuilder} or {@link Bytes}) and reads a char - * sequence from the given {@code offset}, encoded as Utf8, into it. Returns offset after - * the read Utf8, if a normal char sequence was read, or {@code -1 - offset}, if {@code null} - * was observed (in this case, {@code sb} is truncated too, but not updated then, by querying - * {@code sb} only this case is indistinguishable from reading an empty char sequence). If - * length of Utf8 encoding of the char sequence exceeds {@code maxUtf8Len}, - * {@code IllegalStateException} is thrown. - * - * @param offset the offset in this {@code RandomDataInput} to read char sequence from - * @param sb the buffer to read char sequence into (truncated first) - * @param maxUtf8Len the maximum allowed length of the char sequence in Utf8 encoding - * @param buffer type, must be {@code StringBuilder} or {@code Bytes} - * @return offset after the normal read char sequence, or -1 - offset, if char sequence is - * {@code null} - * @see RandomDataOutput#writeUtf8Limited(long, CharSequence, int) - */ - default long readUtf8Limited( - long offset, ACS sb, int maxUtf8Len) - throws IORuntimeException, IllegalArgumentException, BufferUnderflowException, - IllegalStateException { - AppendableUtil.setLength(sb, 0); - // TODO insert some bounds check here - - long utfLen; - if ((utfLen = readByte(offset++)) < 0) { - utfLen &= 0x7FL; - long b; - int count = 7; - while ((b = readByte(offset++)) < 0) { - utfLen |= (b & 0x7FL) << count; - count += 7; - } - if (b != 0) { - if (count > 56) - throw new IORuntimeException( - "Cannot read more than 9 stop bits of positive value"); - utfLen |= (b << count); - } else { - if (count > 63) - throw new IORuntimeException( - "Cannot read more than 10 stop bits of negative value"); - utfLen = ~utfLen; - } - } - if (utfLen == -1) - return ~offset; - if (utfLen > maxUtf8Len) - throw new IllegalStateException("Attempted to read a char sequence of " + - "utf8 size " + utfLen + ", when only " + maxUtf8Len + " allowed"); - BytesInternal.parseUtf8(this, offset, sb, true, (int) utfLen); - return offset + utfLen; - } - - /** - * Reads a char sequence from the given {@code offset}, encoded as Utf8. If length of Utf8 - * encoding of the char sequence exceeds {@code maxUtf8Len}, {@code IllegalStateException} - * is thrown. - * - * @param offset the offset in this {@code RandomDataInput} to read char sequence from - * @param maxUtf8Len the maximum allowed length of the char sequence in Utf8 encoding - * @return the char sequence was read - * @see RandomDataOutput#writeUtf8Limited(long, CharSequence, int) - */ - - default String readUtf8Limited(long offset, int maxUtf8Len) - throws BufferUnderflowException, IORuntimeException, IllegalArgumentException, - IllegalStateException { - return BytesInternal.readUtf8(this, offset, maxUtf8Len); - } - - /** - * Compares the UTF-8 encoded char sequence, written in this {@code RandomDataInput} at the - * given offset, with the given char sequence. Returns {@code true}, if they are equal. Both - * char sequences (encoded in bytes and the given) may be {@code null}. - * - * @param offset the offset in this {@code RandomDataInput} where the char sequence to compare - * is written - * @param other the second char sequence to compare - * @return {@code true} if two char sequences are equal - * @throws IORuntimeException if the contents are not a valid string. - */ - default boolean compareUtf8(long offset, CharSequence other) throws IORuntimeException { - return BytesInternal.compareUtf8(this, offset, other); - } - - - default byte[] toByteArray() throws IllegalArgumentException { - return BytesInternal.toByteArray(this); - } - - default long read(long offsetInRDI, byte[] bytes, int offset, int length) { - try { - int len = (int) Math.min(length, readLimit() - offsetInRDI); - for (int i = 0; i < len; i++) - bytes[offset + i] = readByte(offsetInRDI + i); - return len; - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - default ByteBuffer toTemporaryDirectByteBuffer() throws IllegalArgumentException { - int len = Maths.toUInt31(readRemaining()); - try { - ByteBuffer bb = ByteBuffer.allocateDirect(len); - copyTo(bb); - bb.clear(); - return bb; - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - default int fastHash(long offset, int length) throws BufferUnderflowException { - long hash = 0; - int i = 0; - if (length >= 4) { - hash = readInt(offset + i); - i += 4; - } - for (; i < length - 3; i += 4) { - hash *= 0x6d0f27bd; - hash += readInt(offset + i); - } - if (i < length - 1) { - hash *= 0x6d0f27bdL; - hash += readShort(offset + i); - i += 2; - } - if (i < length) - hash += readByte(offset + i); - hash *= 0x855dd4db; - return (int) (hash ^ (hash >> 32)); - } - - default boolean canReadDirect(long length) { - return isDirectMemory() && readRemaining() >= length; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomDataOutput.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomDataOutput.java deleted file mode 100644 index d2c240f..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RandomDataOutput.java +++ /dev/null @@ -1,351 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface RandomDataOutput> extends RandomCommon { - /** - * Write a byte at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - * @throws IllegalArgumentException if the value cannot be cast to the type without loss. - */ - - default R writeByte(long offset, int i) - throws BufferOverflowException, IllegalArgumentException { - return writeByte(offset, Maths.toInt8(i)); - } - - /** - * Write an unsigned byte at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - * @throws IllegalArgumentException if the value cannot be cast to the type without loss. - */ - - default R writeUnsignedByte(long offset, int i) - throws BufferOverflowException, IllegalArgumentException { - return writeByte(offset, (byte) Maths.toUInt8(i)); - } - - /** - * Write a boolean at an offset. - * - * @param offset to write to - * @param flag the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - default R writeBoolean(long offset, boolean flag) - throws BufferOverflowException { - try { - return writeByte(offset, flag ? 'Y' : 'N'); - - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - /** - * Write an unsigned byte at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - * @throws IllegalArgumentException if the value cannot be cast to the type without loss. - */ - - default R writeUnsignedShort(long offset, int i) - throws BufferOverflowException, IllegalArgumentException { - return writeShort(offset, (short) Maths.toUInt16(i)); - } - - /** - * Write an unsigned byte at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - * @throws IllegalArgumentException if the value cannot be cast to the type without loss. - */ - - default R writeUnsignedInt(long offset, long i) - throws BufferOverflowException, IllegalArgumentException { - return writeInt(offset, (int) Maths.toUInt32(i)); - } - - /** - * Write an unsigned byte at an offset. - * - * @param offset to write to - * @param i8 the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeByte(long offset, byte i8) throws BufferOverflowException; - - /** - * Write a short at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeShort(long offset, short i) throws BufferOverflowException; - - - default R writeInt24(long offset, int i) throws BufferOverflowException { - writeShort(offset, (short) i); - return writeByte(offset + 2, (byte) (i >> 16)); - } - - /** - * Write an int at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeInt(long offset, int i) throws BufferOverflowException; - - /** - * Perform a non stalling write with a store barrier. - * - * @param offset to write to - * @param i value to write - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeOrderedInt(long offset, int i) throws BufferOverflowException; - - /** - * Perform a non stalling write with a store barrier. - * - * @param offset to write to - * @param f value to write - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - default R writeOrderedFloat(long offset, float f) throws BufferOverflowException { - return writeOrderedInt(offset, Float.floatToRawIntBits(f)); - } - - /** - * Write a long at an offset. - * - * @param offset to write to - * @param i the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeLong(long offset, long i) throws BufferOverflowException, IllegalStateException; - - /** - * Perform a non stalling write with a store barrier. - * - * @param offset to write to - * @param i value to write - * @return this - */ - - R writeOrderedLong(long offset, long i) throws BufferOverflowException; - - /** - * Perform a non stalling write with a store barrier. - * - * @param offset to write to - * @param d value to write - * @return this - */ - - default R writeOrderedDouble(long offset, double d) throws BufferOverflowException { - return writeOrderedLong(offset, Double.doubleToRawLongBits(d)); - } - - /** - * Write a float at an offset. - * - * @param offset to write to - * @param d the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeFloat(long offset, float d) throws BufferOverflowException; - - /** - * Write a double at an offset. - * - * @param offset to write to - * @param d the value - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R writeDouble(long offset, double d) throws BufferOverflowException; - - - R writeVolatileByte(long offset, byte i8) throws BufferOverflowException; - - - R writeVolatileShort(long offset, short i16) throws BufferOverflowException; - - - R writeVolatileInt(long offset, int i32) throws BufferOverflowException; - - - R writeVolatileLong(long offset, long i64) throws BufferOverflowException; - - - default R writeVolatileFloat(long offset, float f) throws BufferOverflowException { - return writeVolatileInt(offset, Float.floatToRawIntBits(f)); - } - - - default R writeVolatileDouble(long offset, double d) throws BufferOverflowException { - return writeVolatileLong(offset, Double.doubleToRawLongBits(d)); - } - - - default R write(long offsetInRDO, byte[] bytes) throws BufferOverflowException { - return write(offsetInRDO, bytes, 0, bytes.length); - } - - - R write(long offsetInRDO, byte[] bytes, int offset, int length) - throws BufferOverflowException; - - void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) - throws BufferOverflowException; - - - default R write(long offsetInRDO, BytesStore bytes) - throws BufferOverflowException { - try { - return write(offsetInRDO, bytes, bytes.readPosition(), bytes.readRemaining()); - - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - - R write(long writeOffset, RandomDataInput bytes, long readOffset, long length) - throws BufferOverflowException, BufferUnderflowException; - - /** - * Zero out the bytes between the start and the end. - * - * @param start index of first byte inclusive - * @param end index of last byte exclusive - * @return this - * @throws BufferOverflowException if the capacity was exceeded - */ - - R zeroOut(long start, long end); - - - default R append(long offset, long value, int digits) - throws BufferOverflowException { - BytesInternal.append(this, offset, value, digits); - return (R) this; - } - - - default R append(long offset, double value, int decimalPlaces, int digits) throws BufferOverflowException { - if (decimalPlaces < 20) { - double d2 = value * Maths.tens(decimalPlaces); - if (d2 <= Long.MAX_VALUE && d2 >= Long.MIN_VALUE) { - BytesInternal.appendDecimal(this, Math.round(d2), offset, decimalPlaces, digits); - return (R) this; - } - } - BytesInternal.append((StreamingDataOutput) this, value); - return (R) this; - } - - /** - * expert level method to copy data from native memory into the BytesStore - * - * @param address in native memory to copy from - * @param position in BytesStore to copy to - * @param size in bytes - */ - void nativeWrite(long address, long position, long size) throws BufferOverflowException; - - /** - * Writes the given {@code cs} to this {@code RandomDataOutput} from the given {@code offset}, - * in Utf8 format. Returns the offset after the written char sequence. - * - * @param offset the offset to write char sequence from - * @param cs the char sequence to write, could be {@code null} - * @return the offset after the char sequence written, in this {@code RandomDataOutput} - * @see RandomDataInput#readUtf8(long, Appendable) - */ - default long writeUtf8(long offset, CharSequence cs) throws BufferOverflowException { - return BytesInternal.writeUtf8(this, offset, cs); - } - - /** - * Writes the given {@code cs} to this {@code RandomDataOutput} from the given {@code offset}, - * in Utf8 format, checking that the utf8 encoding size of the given char sequence is less or - * equal to the given {@code maxUtf8Len}, otherwise {@code IllegalArgumentException} is thrown, - * and no bytes of this {@code RandomDataOutput} are overwritten. Returns the offset after the - * written char sequence. - * - * @param offset the offset to write char sequence from - * @param cs the char sequence to write, could be {@code null} - * @param maxUtf8Len the maximum allowed length (in Utf8 encoding) of the given char sequence - * @return the offset after the char sequence written, in this {@code RandomDataOutput} - * @throws IllegalArgumentException if the given char sequence size in Utf8 encoding exceeds - * maxUtf8Len - * @see RandomDataInput#readUtf8Limited(long, Appendable, int) - * @see RandomDataInput#readUtf8Limited(long, int) - */ - default long writeUtf8Limited(long offset, CharSequence cs, int maxUtf8Len) - throws BufferOverflowException { - return BytesInternal.writeUtf8(this, offset, cs, maxUtf8Len); - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReadBytesMarshallable.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReadBytesMarshallable.java deleted file mode 100644 index c1994cf..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReadBytesMarshallable.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.annotation.DontChain; -import net.openhft.chronicle.core.io.IORuntimeException; - -/** - * Read data directly as Bytes. - */ -@FunctionalInterface -@DontChain -public interface ReadBytesMarshallable extends CommonMarshallable { - /** - * Bytes to read. This can be used as a method to implement or as a lambda. - * - * @param bytes to read. - */ - @SuppressWarnings("rawtypes") - void readMarshallable(BytesIn bytes) throws IORuntimeException; -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReadOnlyMappedBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReadOnlyMappedBytesStore.java deleted file mode 100644 index a081668..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReadOnlyMappedBytesStore.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.ReferenceOwner; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * BytesStore to wrap memory mapped data. - */ -public class ReadOnlyMappedBytesStore extends MappedBytesStore { - - public ReadOnlyMappedBytesStore(ReferenceOwner owner, MappedFile mappedFile, long start, long address, long capacity, long safeCapacity) - throws IllegalStateException { - super(owner, mappedFile, start, address, capacity, safeCapacity); - } - - - @Override - public NativeBytesStore zeroOut(long start, long end) { - return this; - } - - @Override - public boolean compareAndSwapInt(long offset, int expected, int value) { - throw checkReadOnly(); - } - - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeByte(long offset, byte i8) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeShort(long offset, short i16) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeInt(long offset, int i32) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeLong(long offset, long i64) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeOrderedLong(long offset, long i) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeFloat(long offset, float f) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeDouble(long offset, double d) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeVolatileByte(long offset, byte i8) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeVolatileShort(long offset, short i16) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeVolatileInt(long offset, int i32) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeVolatileLong(long offset, long i64) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore write(long offsetInRDO, byte[] bytes, int offset, int length) { - throw checkReadOnly(); - } - - @Override - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore write(long writeOffset, RandomDataInput bytes, long readOffset, long length) - throws BufferOverflowException, BufferUnderflowException { - throw checkReadOnly(); - } - - @Override - public void write0(long offsetInRDO, RandomDataInput bytes, long offset, long length) { - throw checkReadOnly(); - } - - @Override - public void nativeWrite(long address, long position, long size) { - throw checkReadOnly(); - } - - @Override - void write8bit(long position, char[] chars, int offset, int length) { - throw checkReadOnly(); - } - - @Override - public long appendUTF(long pos, char[] chars, int offset, int length) { - throw checkReadOnly(); - } - - @Override - public long appendUtf8(long pos, char[] chars, int offset, int length) { - throw checkReadOnly(); - } - - - @Override - public VanillaBytes bytesForWrite() throws IllegalStateException { - throw checkReadOnly(); - } - - - @Override - public NativeBytesStore writeOrderedInt(long offset, int i) { - throw checkReadOnly(); - } - - private IllegalStateException checkReadOnly() throws IllegalStateException { - throw new IllegalStateException("Read Only"); - } - - @Override - public boolean readWrite() { - return false; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReleasedBytesStore.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReleasedBytesStore.java deleted file mode 100644 index 95fdea0..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ReleasedBytesStore.java +++ /dev/null @@ -1,319 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.ReferenceOwner; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * applied after a Bytes has been released and cannot be used. - * - * @see NoBytesStore - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public enum ReleasedBytesStore implements BytesStore { - RELEASED_BYTES_STORE; - - - public static > BytesStore releasedBytesStore() { - return RELEASED_BYTES_STORE; - } - - @Override - public void reserve(ReferenceOwner id) throws IllegalStateException { - throw newIllegalStateException(); - } - - @Override - public void release(ReferenceOwner id) throws IllegalStateException { - throw newIllegalStateException(); - } - - @Override - public int refCount() { - return 0; - } - - @Override - public boolean tryReserve(ReferenceOwner id) throws IllegalStateException { - return false; - } - - @Override - public boolean reservedBy(ReferenceOwner owner) { - return false; - } - - @Override - public void releaseLast(ReferenceOwner id) throws IllegalStateException { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeByte(long offset, byte i8) { - throw newIllegalStateException(); - } - - - private IllegalStateException newIllegalStateException() { - return new IllegalStateException("released"); - } - - - @Override - public RandomDataOutput writeShort(long offset, short i) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeInt(long offset, int i) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeOrderedInt(long offset, int i) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeLong(long offset, long i) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeOrderedLong(long offset, long i) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeFloat(long offset, float d) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeDouble(long offset, double d) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeVolatileByte(long offset, byte i8) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeVolatileShort(long offset, short i16) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeVolatileInt(long offset, int i32) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput writeVolatileLong(long offset, long i64) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput write(long offsetInRDO, byte[] bytes, int offset, int length) { - if (length != 0) - throw newIllegalStateException(); - return this; - } - - @Override - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) { - throw newIllegalStateException(); - } - - - @Override - public RandomDataOutput write(long writeOffset, RandomDataInput bytes, long readOffset, long length) { - throw newIllegalStateException(); - } - - @Override - public byte readByte(long offset) { - throw newIllegalStateException(); - } - - @Override - public int peekUnsignedByte(long offset) { - return -1; - } - - @Override - public short readShort(long offset) { - throw newIllegalStateException(); - } - - @Override - public int readInt(long offset) { - throw newIllegalStateException(); - } - - @Override - public long readLong(long offset) { - throw newIllegalStateException(); - } - - @Override - public float readFloat(long offset) { - throw newIllegalStateException(); - } - - @Override - public double readDouble(long offset) { - throw newIllegalStateException(); - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - throw newIllegalStateException(); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - throw newIllegalStateException(); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - throw newIllegalStateException(); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - throw newIllegalStateException(); - } - - @Override - public boolean isDirectMemory() { - return false; - } - - - @Override - public BytesStore copy() { - return this; - } - - @Override - public long capacity() { - return 0; - } - - @Override - public Void underlyingObject() { - return null; - } - - @Override - public boolean inside(long offset) { - return false; - } - - @Override - public boolean inside(long offset, long buffer) { - return false; - } - - @Override - public long copyTo( BytesStore store) { - throw newIllegalStateException(); - } - - @Override - public void nativeWrite(long address, long position, long size) { - throw newIllegalStateException(); - } - - @Override - public void nativeRead(long position, long address, long size) { - throw newIllegalStateException(); - } - - @Override - public boolean compareAndSwapInt(long offset, int expected, int value) { - throw newIllegalStateException(); - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - throw newIllegalStateException(); - } - - @Override - public boolean compareAndSwapLong(long offset, long expected, long value) { - throw newIllegalStateException(); - } - - @Override - public boolean equalBytes( BytesStore bytesStore, long length) { - throw newIllegalStateException(); - } - - @Override - public void move(long from, long to, long length) { - throw newIllegalStateException(); - } - - @Override - public long addressForRead(long offset) throws BufferUnderflowException { - throw newIllegalStateException(); - } - - @Override - public long addressForWrite(long offset) throws BufferOverflowException { - throw newIllegalStateException(); - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - throw newIllegalStateException(); - } - - - @Override - public Bytes bytesForWrite() { - throw newIllegalStateException(); - } - - @Override - public boolean sharedMemory() { - return false; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RingBufferReader.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RingBufferReader.java deleted file mode 100644 index af457ae..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RingBufferReader.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; - -public interface RingBufferReader extends RingBufferReaderStats, Closeable { - long UNKNOWN_INDEX = -1; - - boolean isEmpty(); - - boolean isStopped(); - - /** - * stop the reader. After being stopped, the reader will not block writers. - * After being stopped the reader can be re-opened - */ - void stop(); - - /** - * the readPosition and readLimit will be adjusted so that the client can read the data - * - * @param bytes who's byteStore must be the ring buffer, - * @return nextReadPosition which should be passed to {@link RingBufferReader#afterRead(long)} - */ - @SuppressWarnings("rawtypes") - long beforeRead(Bytes bytes); - - void afterRead(long next); - - void afterRead(long next, long payloadStart, long underlyingIndex); - - long underlyingIndex(); - - /** - * Convenience method calls both {@link #beforeRead(Bytes)} and {@link #afterRead(long)} - * - * @param bytes - * @return whether read succeeded - */ - @SuppressWarnings("rawtypes") - boolean read(BytesOut bytes); - - /** - * @return the byteStore which backs the ring buffer - */ - @SuppressWarnings("rawtypes") - BytesStore byteStore(); - - /** - * Take reader to just past the end of the RB - */ - void toEnd(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RingBufferReaderStats.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RingBufferReaderStats.java deleted file mode 100644 index 1229741..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/RingBufferReaderStats.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -public interface RingBufferReaderStats { - - long getAndClearReadCount(); - - long getAndClearMissedReadCount(); - - long behind(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharTester.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharTester.java deleted file mode 100644 index b5a0719..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharTester.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.EscapingStopCharTester; -import org.jetbrains.annotations.NotNull; - -@FunctionalInterface -public interface StopCharTester { - /** - * Detect which byte stops the string to be parsed - *

- *

This should be changed to support char instead. - *

- *

Note: for safety reasons, you should stop on a 0 byte or throw an IllegalStateException. - * - * @param ch to test, 0 should return true or throw an exception. - * @return if this byte is a stop character. - */ - boolean isStopChar(int ch); - - - default StopCharTester escaping() { - return new EscapingStopCharTester(this); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharTesters.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharTesters.java deleted file mode 100644 index c59fa4f..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharTesters.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -public enum StopCharTesters implements StopCharTester { - COMMA_STOP { - @Override - public boolean isStopChar(int ch) { - return ch < ' ' || ch == ','; - } - }, - CURLY_STOP { - @Override - public boolean isStopChar(int ch) { - return ch < ' ' || ch == '}'; - } - }, - COMMA_SPACE_STOP { - @Override - public boolean isStopChar(int ch) { - return ch <= ' ' || ch == ','; - } - }, - CONTROL_STOP { - @Override - public boolean isStopChar(int ch) { - return ch < ' '; - } - }, - SPACE_STOP { - @Override - public boolean isStopChar(int ch) { - return Character.isWhitespace(ch) || ch == 0; - } - }, - QUOTES { - @Override - public boolean isStopChar(int ch) { - return ch == '"' || ch <= 0; - } - }, - SINGLE_QUOTES { - @Override - public boolean isStopChar(int ch) { - return ch == '\'' || ch <= 0; - } - }, - EQUALS { - @Override - public boolean isStopChar(int ch) { - return ch == '=' || ch <= 0; - } - }, - NUMBER_END { - @Override - public boolean isStopChar(int ch) { - switch (ch) { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '+': - case '-': - case '.': - case 'E': - case 'e': - return false; - default: - return true; - } - } - }, - NON_ALPHA_DIGIT { - @Override - public boolean isStopChar(int ch) { - return ch < '0' || !(Character.isAlphabetic(ch) || Character.isDigit(ch)); - } - }, - ALL { - @Override - public boolean isStopChar(int ch) { - return ch < 0; - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharsTester.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharsTester.java deleted file mode 100644 index 682d8fd..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StopCharsTester.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.EscapingStopCharsTester; -import org.jetbrains.annotations.NotNull; - -@FunctionalInterface -public interface StopCharsTester { - /** - * Detect which byte or bytes stops the string to be parsed - *

- *

This should be changed to support char instead. - *

- *

Note: for safety reasons, you should stop on a 0 byte or throw an IllegalStateException. - * - * @param ch to test, 0 should return true or throw an exception. - * @param ch2 to test, 0 should return true or throw an exception. - * @return if this byte is a stop character. - */ - boolean isStopChar(int ch, int ch2); - - - default StopCharsTester escaping() { - return new EscapingStopCharsTester(this); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingCommon.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingCommon.java deleted file mode 100644 index 334e4e9..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingCommon.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -public interface StreamingCommon> extends RandomCommon { - - /** - * Set the readPosition= writePosition = start, writeLimit = capacity - * - * @return this - */ - - S clear(); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingDataInput.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingDataInput.java deleted file mode 100644 index c406657..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingDataInput.java +++ /dev/null @@ -1,438 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.UnsafeMemory; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.util.Histogram; -import net.openhft.chronicle.core.util.ThrowingConsumer; -import net.openhft.chronicle.core.util.ThrowingConsumerNonCapturing; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * This data input has a a position() and a limit() - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface StreamingDataInput> extends StreamingCommon { - - S readPosition(long position) throws BufferUnderflowException; - - - default S readPositionUnlimited(long position) throws BufferUnderflowException { - return readLimitToCapacity().readPosition(position); - } - - - default S readPositionRemaining(long position, long remaining) throws BufferUnderflowException { - readLimit(position + remaining); - return readPosition(position); - } - - - S readLimit(long limit) throws BufferUnderflowException; - - default S readLimitToCapacity() throws BufferUnderflowException { - return readLimit(capacity()); - } - - /** - * Skip a number of bytes by moving the readPosition. Must be less than or equal to the readLimit. - * - * @param bytesToSkip bytes to skip. - * @return this - * @throws BufferUnderflowException if the offset is outside the limits of the Bytes - */ - - S readSkip(long bytesToSkip) throws BufferUnderflowException; - - /** - * Read skip 1 when you are sure this is safe. Use at your own risk when you find a performance problem. - */ - void uncheckedReadSkipOne(); - - /** - * Read skip -1 when you are sure this is safe. Use at your own risk when you find a performance problem. - */ - void uncheckedReadSkipBackOne(); - - /** - * Perform a set of actions with a temporary bounds mode. - */ - default void readWithLength0(long length, ThrowingConsumerNonCapturing bytesConsumer, StringBuilder sb, BytesOut toBytes) - throws BufferUnderflowException, IORuntimeException { - if (length > readRemaining()) - throw new BufferUnderflowException(); - long limit0 = readLimit(); - long limit = readPosition() + length; - try { - readLimit(limit); - bytesConsumer.accept((S) this, sb, toBytes); - } finally { - readLimit(limit0); - readPosition(limit); - } - } - - /** - * Perform a set of actions with a temporary bounds mode. - */ - default void readWithLength(long length, ThrowingConsumer bytesConsumer) - throws BufferUnderflowException, IORuntimeException { - if (length > readRemaining()) - throw new BufferUnderflowException(); - long limit0 = readLimit(); - long limit = readPosition() + length; - try { - readLimit(limit); - bytesConsumer.accept((S) this); - } finally { - readLimit(limit0); - readPosition(limit); - } - } - - - default InputStream inputStream() { - return new StreamingInputStream(this); - } - - default long readStopBit() throws IORuntimeException { - return BytesInternal.readStopBit(this); - } - - default char readStopBitChar() throws IORuntimeException { - return BytesInternal.readStopBitChar(this); - } - - default double readStopBitDouble() { - return BytesInternal.readStopBitDouble(this); - } - - default double readStopBitDecimal() throws BufferOverflowException { - long value = readStopBit(); - int scale = (int) (Math.abs(value) % 10); - value /= 10; - return (double) value / Maths.tens(scale); - } - - default boolean readBoolean() { - byte b = readByte(); - return BytesUtil.byteToBoolean(b); - } - - byte readByte(); - - default byte rawReadByte() { - return readByte(); - } - - default char readChar() { - return readStopBitChar(); - } - - /** - * @return the next unsigned 8 bit value or -1; - */ - int readUnsignedByte(); - - /** - * @return the next unsigned 8 bit value or -1; - */ - int uncheckedReadUnsignedByte(); - - short readShort() throws BufferUnderflowException; - - default int readUnsignedShort() throws BufferUnderflowException { - return readShort() & 0xFFFF; - } - - default int readInt24() throws BufferUnderflowException { - return readUnsignedShort() | (readUnsignedByte() << 24 >> 8); - } - - default int readUnsignedInt24() throws BufferUnderflowException { - return readUnsignedShort() | (readUnsignedByte() << 16); - } - - int readInt() throws BufferUnderflowException; - - default int rawReadInt() { - return readInt(); - } - - default long readUnsignedInt() - throws BufferUnderflowException { - return readInt() & 0xFFFFFFFFL; - } - - long readLong() throws BufferUnderflowException; - - default long rawReadLong() { - return readLong(); - } - - /** - * @return a long using the bytes remaining - */ - default long readIncompleteLong() { - long left = readRemaining(); - try { - if (left >= 8) - return readLong(); - if (left == 4) - return readInt(); - long l = 0; - for (int i = 0, remaining = (int) left; i < remaining; i++) { - l |= (long) readUnsignedByte() << (i * 8); - } - return l; - - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - float readFloat() throws BufferUnderflowException; - - double readDouble() throws BufferUnderflowException; - - /** - * The same as readUTF() except the length is stop bit encoded. This saves one byte for strings shorter than 128 - * chars. null values are also supported - * - * @return a Unicode string or null if writeUtf8(null) was called - */ - - default String readUtf8() - throws BufferUnderflowException, IORuntimeException, IllegalArgumentException { - return BytesInternal.readUtf8(this); - } - - - @Deprecated(/* to be removed in x.22 */) - default String readUTFΔ() - throws IORuntimeException, BufferUnderflowException, IllegalArgumentException { - return BytesInternal.readUtf8(this); - } - - - default String read8bit() throws IORuntimeException, BufferUnderflowException { - return BytesInternal.read8bit(this); - } - - /** - * The same as readUtf8() except the chars are copied to a truncated StringBuilder. - * - * @param sb to copy chars to - * @return true if there was a String, or false if it was null - */ - default boolean readUtf8( ACS sb) - throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - AppendableUtil.setLength(sb, 0); - if (readRemaining() <= 0) - // TODO throw BufferUnderflowException here? please review - return false; - long len0 = readStopBit(); - if (len0 == -1) - return false; - int len = Maths.toUInt31(len0); - if (len > 0) - BytesInternal.parseUtf8(this, sb, true, len); - return true; - } - - @Deprecated(/* to be removed in x.22 */) - default boolean readUTFΔ( ACS sb) - throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - return readUtf8(sb); - } - - default boolean read8bit( Bytes b) - throws BufferUnderflowException, IllegalStateException, BufferOverflowException { - b.clear(); - if (readRemaining() <= 0) - return false; - long len0 = this.readStopBit(); - if (len0 == -1) - return false; - int len = Maths.toUInt31(len0); - b.write((BytesStore) this, readPosition(), len); - readSkip(len); - return true; - } - - default boolean read8bit( ACS sb) - throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { - AppendableUtil.setLength(sb, 0); - long len0 = BytesInternal.readStopBit(this); - if (len0 == -1) - return false; - int len = Maths.toUInt31(len0); - try { - AppendableUtil.parse8bit(this, sb, len); - } catch (IOException e) { - throw new IORuntimeException(e); - } - return true; - } - - default boolean read8bit( StringBuilder sb) - throws IORuntimeException, BufferUnderflowException { - sb.setLength(0); - long len0 = BytesInternal.readStopBit(this); - if (len0 == -1) - return false; - int len = Maths.toUInt31(len0); - try { - AppendableUtil.parse8bit(this, sb, len); - } catch (IOException e) { - throw new IORuntimeException(e); - } - return true; - } - - default int read( byte[] bytes) { - return read(bytes, 0, bytes.length); - } - - default int read( byte[] bytes, int off, int len) { - long remaining = readRemaining(); - if (remaining <= 0) - return -1; - int len2 = (int) Math.min(len, remaining); - int i = 0; - for (; i < len2 - 7; i += 8) - UnsafeMemory.unsafePutLong(bytes, i + off, rawReadLong()); - for (; i < len2; i++) - bytes[off + i] = rawReadByte(); - return len2; - } - - default int read( char[] bytes, int off, int len) { - long remaining = readRemaining(); - if (remaining <= 0) - return -1; - int len2 = (int) Math.min(len, remaining); - for (int i = 0; i < len2; i++) - bytes[off + i] = (char) readUnsignedByte(); - return len2; - } - - default void read( ByteBuffer buffer) { - for (int i = (int) Math.min(readRemaining(), buffer.remaining()); i > 0; i--) - buffer.put(readByte()); - } - - default void read( Bytes bytes, int length) { - int len2 = (int) Math.min(length, readRemaining()); - int i = 0; - for (; i < len2 - 7; i += 8) - bytes.rawWriteLong(rawReadLong()); - for (; i < len2; i++) - bytes.rawWriteByte(rawReadByte()); - } - - default void unsafeReadObject(Object o, int length) { - unsafeReadObject(o, (o.getClass().isArray() ? 4 : 0) + Jvm.objectHeaderSize(), length); - } - - default void unsafeReadObject(Object o, int offset, int length) { - assert BytesUtil.isTriviallyCopyable(o.getClass(), offset, length); - if (readRemaining() < length) - throw new BufferUnderflowException(); - int i = 0; - for (; i < length - 7; i += 8) - UnsafeMemory.unsafePutLong(o, offset + i, rawReadLong()); - for (; i < length; i++) - UnsafeMemory.unsafePutByte(o, offset + i, rawReadByte()); - } - - int readVolatileInt() throws BufferUnderflowException; - - long readVolatileLong() throws BufferUnderflowException; - - int peekUnsignedByte(); - - - - default > E readEnum( Class eClass) - throws IORuntimeException, BufferUnderflowException { - return BytesInternal.readEnum(this, eClass); - } - - @Deprecated(/* to be removed in x.22 */) - default void parseUTF(Appendable sb, int length) - throws IllegalArgumentException, BufferUnderflowException, UTFDataFormatRuntimeException { - parseUtf8(sb, length); - } - - default void parseUtf8(Appendable sb, int encodedLength) - throws IllegalArgumentException, BufferUnderflowException, UTFDataFormatRuntimeException { - parseUtf8(sb, true, encodedLength); - } - - default void parseUtf8(Appendable sb, boolean utf, int length) - throws IllegalArgumentException, BufferUnderflowException, UTFDataFormatRuntimeException { - AppendableUtil.setLength(sb, 0); - BytesInternal.parseUtf8(this, sb, utf, length); - } - - default long parseHexLong() { - return BytesInternal.parseHexLong(this); - } - - void copyTo(OutputStream out) throws IOException; - - long copyTo(BytesStore to); - - default void readHistogram( Histogram histogram) { - BytesInternal.readHistogram(this, histogram); - } - - default void readWithLength(Bytes bytes) { - bytes.clear(); - int length = Maths.toUInt31(readStopBit()); - int i; - for (i = 0; i < length - 7; i++) - bytes.writeLong(readLong()); - for (; i < length; i++) - bytes.writeByte(readByte()); - } - - /** - * When there is no more data to read, return zero, false and empty string. - * - * @param lenient if true, return nothing rather than error. - */ - void lenient(boolean lenient); - - boolean lenient(); - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingDataOutput.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingDataOutput.java deleted file mode 100644 index ef924a8..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingDataOutput.java +++ /dev/null @@ -1,562 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.UnsafeMemory; -import net.openhft.chronicle.core.annotation.Java9; -import net.openhft.chronicle.core.util.Histogram; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * Position based access. Once data has been read, the position() moves. - *

The use of this instance is single threaded, though the use of the data - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public interface StreamingDataOutput> extends StreamingCommon { - int JAVA9_STRING_CODER_LATIN = 0; - int JAVA9_STRING_CODER_UTF16 = 1; - - S writePosition(long position) throws BufferOverflowException; - - S writeLimit(long limit) throws BufferOverflowException; - - /** - * Skip a number of bytes by moving the writePosition. Must be less than or equal to the writeLimit. - * - * @param bytesToSkip bytes to skip. - * @return this - * @throws BufferOverflowException if the offset is outside the limits of the Bytes - */ - S writeSkip(long bytesToSkip) throws BufferOverflowException; - - default S alignBy(int width) { - return writeSkip((-writePosition()) & (width - 1)); - } - - /** - * @return Bytes as an OutputStream - */ - - default OutputStream outputStream() { - return new StreamingOutputStream(this); - } - - /** - * Write a stop bit encoded long - * - * @param x long to write - * @return this. - */ - - default S writeStopBit(long x) throws BufferOverflowException { - BytesInternal.writeStopBit(this, x); - return (S) this; - } - - - default S writeStopBit(char x) throws BufferOverflowException { - BytesInternal.writeStopBit(this, x); - return (S) this; - } - - - default S writeStopBit(double d) throws BufferOverflowException { - BytesInternal.writeStopBit(this, d); - return (S) this; - } - - - default S writeStopBitDecimal(double d) throws BufferOverflowException { - boolean negative = d < 0; - double ad = Math.abs(d); - long value; - int scale = 0; - if ((long) ad == ad) { - value = (long) ad * 10; - - } else { - double factor = 1; - while (scale < 9) { - double v = ad * factor; - if (v >= 1e14 || (long) v == v) - break; - factor *= 10; - scale++; - } - value = Math.round(ad * factor); - while (scale > 0 && value % 10 == 0) { - value /= 10; - scale--; - } - value = value * 10 + scale; - } - if (negative) - value = -value; - BytesInternal.writeStopBit(this, value); - return (S) this; - } - - /** - * Write the same encoding as writeUTF with the following changes. 1) The length is stop bit encoded - * i.e. one byte longer for short strings, but is not limited in length. 2) The string can be null. - * - * @param cs the string value to be written. Can be null. - * @throws BufferOverflowException if there is not enough space left - */ - - default S writeUtf8(CharSequence cs) - throws BufferOverflowException { - BytesInternal.writeUtf8(this, cs); - return (S) this; - } - - - default S writeUtf8(String s) - throws BufferOverflowException { - BytesInternal.writeUtf8(this, s); - return (S) this; - } - - - @Deprecated(/* to be removed in x.22 */) - default S writeUTFΔ(CharSequence cs) throws BufferOverflowException { - return writeUtf8(cs); - } - - - default S write8bit(CharSequence cs) - throws BufferOverflowException { - if (cs == null) - return writeStopBit(-1); - - if (cs instanceof BytesStore) - return write8bit((BytesStore) cs); - - if (cs instanceof String) - return write8bit((String) cs); - - return write8bit(cs, 0, cs.length()); - } - - - default S write8bit( CharSequence s, int start, int length) - throws BufferOverflowException, IllegalArgumentException, IndexOutOfBoundsException { - writeStopBit(length); - for (int i = 0; i < length; i++) { - char c = s.charAt(i + start); - rawWriteByte((byte) Maths.toUInt8((int) c)); - } - return (S) this; - } - - - default S write(CharSequence cs) - throws BufferOverflowException, BufferUnderflowException, IllegalArgumentException { - if (cs instanceof BytesStore) { - return write((BytesStore) cs); - } - return write(cs, 0, cs.length()); - } - - - default S write( CharSequence s, int start, int length) - throws BufferOverflowException, IllegalArgumentException, IndexOutOfBoundsException { - for (int i = 0; i < length; i++) { - char c = s.charAt(i + start); - appendUtf8(c); - } - return (S) this; - } - - - default S write8bit(String s) - throws BufferOverflowException { - if (s == null) - writeStopBit(-1); - else - write8bit(s, 0, (int) Math.min(writeRemaining(), s.length())); - return (S) this; - } - - - default S write8bit(BytesStore bs) - throws BufferOverflowException { - if (bs == null) { - writeStopBit(-1); - } else { - long offset = bs.readPosition(); - long readRemaining = Math.min(writeRemaining(), bs.readLimit() - offset); - writeStopBit(readRemaining); - write(bs, offset, readRemaining); - } - return (S) this; - } - - - S writeByte(byte i8) throws BufferOverflowException; - - default S rawWriteByte(byte i8) throws BufferOverflowException { - return writeByte(i8); - } - - - default S writeUnsignedByte(int i) - throws BufferOverflowException, IllegalArgumentException { - return writeByte((byte) Maths.toUInt8(i)); - } - - - default S writeChar(char ch) { - return writeStopBit(ch); - } - - - S writeShort(short i16) throws BufferOverflowException; - - - default S writeUnsignedShort(int u16) - throws BufferOverflowException, IllegalArgumentException { - return writeShort((short) Maths.toUInt16(u16)); - } - - - default S writeInt24(int i) throws BufferOverflowException { - writeUnsignedShort((short) i); - return writeUnsignedByte((i >>> 16) & 0xFF); - } - - - default S writeUnsignedInt24(int i) throws BufferOverflowException { - writeUnsignedShort((short) i); - return writeUnsignedByte(i >>> 16); - } - - - S writeInt(int i) throws BufferOverflowException; - - default S rawWriteInt(int i) throws BufferOverflowException { - return writeInt(i); - } - - - S writeIntAdv(int i, int advance) throws BufferOverflowException; - - - default S writeUnsignedInt(long i) - throws BufferOverflowException, IllegalArgumentException { - return writeInt((int) Maths.toUInt32(i)); - } - - /** - * Write a long - */ - - S writeLong(long i64) throws BufferOverflowException; - - /** - * Write a long without a bounds check - */ - default S rawWriteLong(long i) throws BufferOverflowException { - return writeLong(i); - } - - - S writeLongAdv(long i64, int advance) throws BufferOverflowException; - - - S writeFloat(float f) throws BufferOverflowException; - - - S writeDouble(double d) throws BufferOverflowException; - - - S writeDoubleAndInt(double d, int i) throws BufferOverflowException; - - /** - * Write all data or fail. - */ - - default S write( RandomDataInput bytes) { - assert bytes != this : "you should not write to yourself !"; - - try { - return write(bytes, bytes.readPosition(), Math.min(writeRemaining(), bytes.readRemaining())); - } catch (BufferOverflowException | BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - /** - * Writes the passed BytesStore - * - * @param bytes to write - * @return this - */ - default S write( BytesStore bytes) { - assert bytes != this : "you should not write to yourself !"; - - try { - return write(bytes, bytes.readPosition(), Math.min(writeRemaining(), bytes.readRemaining())); - } catch (BufferOverflowException | BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - /** - * @return capacity without resize - */ - long realCapacity(); - - /** - * @return writeRemaining with resize - */ - long realWriteRemaining(); - - default boolean canWriteDirect(long count) { - return false; - } - - - default S writeSome( Bytes bytes) { - try { - long length = Math.min(bytes.readRemaining(), writeRemaining()); - if (length + writePosition() >= 1 << 20) - length = Math.min(bytes.readRemaining(), realCapacity() - writePosition()); - write(bytes, bytes.readPosition(), length); - if (length == bytes.readRemaining()) { - bytes.clear(); - } else { - bytes.readSkip(length); - if (bytes.writePosition() > bytes.realCapacity() / 2) - bytes.compact(); - } - return (S) this; - } catch (BufferOverflowException | BufferUnderflowException | IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - /** - * Write all data or fail. - */ - - default S write( RandomDataInput bytes, long offset, long length) - throws BufferOverflowException, BufferUnderflowException { - BytesInternal.writeFully(bytes, offset, length, this); - return (S) this; - } - - /** - * Write all data or fail. - */ - - default S write( BytesStore bytes, long offset, long - length) - throws BufferOverflowException, BufferUnderflowException { - if (length + writePosition() > capacity()) - throw new IllegalArgumentException("Cannot write " + length + " bytes as position is " + writePosition() + " and capacity is " + capacity()); - BytesInternal.writeFully(bytes, offset, length, this); - return (S) this; - } - - - default S write( byte[] bytes) throws BufferOverflowException { - write(bytes, 0, bytes.length); - return (S) this; - } - - /** - * Write all data or fail. - */ - - S write(byte[] bytes, int offset, int length) throws BufferOverflowException; - - default S unsafeWriteObject(Object o, int length) { - return unsafeWriteObject(o, (o.getClass().isArray() ? 4 : 0) + Jvm.objectHeaderSize(), length); - } - - default S unsafeWriteObject(Object o, int offset, int length) { - int i = 0; - for (; i < length - 7; i += 8) - writeLong(UnsafeMemory.unsafeGetLong(o, offset + i)); - for (; i < length; i++) - writeByte(UnsafeMemory.unsafeGetByte(o, offset + i)); - return (S) this; - } - - - S writeSome(ByteBuffer buffer) throws BufferOverflowException; - - - default S writeBoolean(boolean flag) throws BufferOverflowException { - return writeByte(flag ? (byte) 'Y' : (byte) 'N'); - } - - - S writeOrderedInt(int i) throws BufferOverflowException; - - - S writeOrderedLong(long i) throws BufferOverflowException; - - default > S writeEnum( E e) - throws BufferOverflowException { - return write8bit(e.name()); - } - - - default S appendUtf8( CharSequence cs) - throws BufferOverflowException { - return appendUtf8(cs, 0, cs.length()); - } - - - default S appendUtf8(int codepoint) throws BufferOverflowException { - BytesInternal.appendUtf8Char(this, codepoint); - return (S) this; - } - - - default S appendUtf8(char[] chars, int offset, int length) - throws BufferOverflowException, IllegalArgumentException { - int i; - ascii: - { - for (i = 0; i < length; i++) { - char c = chars[offset + i]; - if (c > 0x007F) - break ascii; - writeByte((byte) c); - } - return (S) this; - } - for (; i < length; i++) { - char c = chars[offset + i]; - BytesInternal.appendUtf8Char(this, c); - } - return (S) this; - } - - - default S appendUtf8( CharSequence cs, int offset, int length) - throws BufferOverflowException, IllegalArgumentException { - BytesInternal.appendUtf8(this, cs, offset, length); - return (S) this; - } - - // length is number of characters (not bytes) - @Java9 - - default S appendUtf8(byte[] bytes, int offset, int length, byte coder) - throws BufferOverflowException, IllegalArgumentException { - if (coder == JAVA9_STRING_CODER_LATIN) { - for (int i = 0; i < length; i++) { - byte b = bytes[offset + i]; - int b2 = (b & 0xFF); - BytesInternal.appendUtf8Char(this, b2); - } - } else { - assert coder == JAVA9_STRING_CODER_UTF16; - for (int i = 0; i < 2 * length; i += 2) { - byte b1 = bytes[2 * offset + i]; - byte b2 = bytes[2 * offset + i + 1]; - - int uBE = ((b2 & 0xFF) << 8) | b1 & 0xFF; - BytesInternal.appendUtf8Char(this, uBE); - } - } - return (S) this; - } - - @Java9 - - default S appendUtf8(byte[] bytes, int offset, int length) - throws BufferOverflowException, IllegalArgumentException { - for (int i = 0; i < length; i++) { - int b = bytes[offset + i] & 0xFF; // unsigned byte - - if (b >= 0xF0) { - int b2 = bytes[offset + i + 1] & 0xFF; // unsigned byte - int b3 = bytes[offset + i + 2] & 0xFF; // unsigned byte - int b4 = bytes[offset + i + 3] & 0xFF; // unsigned byte - this.writeByte((byte) b4); - this.writeByte((byte) b3); - this.writeByte((byte) b2); - this.writeByte((byte) b); - - i += 3; - } else if (b >= 0xE0) { - int b2 = bytes[offset + i + 1] & 0xFF; // unsigned byte - int b3 = bytes[offset + i + 2] & 0xFF; // unsigned byte - this.writeByte((byte) b3); - this.writeByte((byte) b2); - this.writeByte((byte) b); - - i += 2; - } else if (b >= 0xC0) { - int b2 = bytes[offset + i + 1] & 0xFF; // unsigned byte - this.writeByte((byte) b2); - this.writeByte((byte) b); - - i += 1; - } else { - this.writeByte((byte) b); - } - } - return (S) this; - } - - default void copyFrom( InputStream input) throws IOException, BufferOverflowException, IllegalArgumentException { - BytesInternal.copy(input, this); - } - - default void writePositionRemaining(long position, long length) { - writeLimit(position + length); - writePosition(position); - } - - default void writeHistogram( Histogram histogram) { - BytesInternal.writeHistogram(this, histogram); - } - - default void writeBigDecimal( BigDecimal bd) { - writeBigInteger(bd.unscaledValue()); - writeStopBit(bd.scale()); - } - - default void writeBigInteger( BigInteger bi) { - byte[] bytes = bi.toByteArray(); - writeStopBit(bytes.length); - write(bytes); - } - - default void writeWithLength(RandomDataInput bytes) { - writeStopBit(bytes.readRemaining()); - write(bytes); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingInputStream.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingInputStream.java deleted file mode 100644 index ffc4232..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingInputStream.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.io.InputStream; - -@SuppressWarnings("rawtypes") -public class StreamingInputStream extends InputStream { - - private StreamingDataInput in; - - public StreamingInputStream() { - this(NoBytesStore.NO_BYTES); - } - - public StreamingInputStream(StreamingDataInput in) { - this.in = in; - } - - - public StreamingInputStream init(StreamingDataInput in) { - this.in = in; - return this; - } - - @Override - public long skip(long n) throws IOException { - long len = Math.min(in.readRemaining(), n); - in.readSkip(len); - return len; - } - - @Override - public int available() throws IOException { - return (int) Math.min(Integer.MAX_VALUE, in.readRemaining()); - } - - @Override - public int read(byte[] b, int off, int len) throws IOException { - if (len == 0) { - return 0; - } - int len2 = in.read(b, off, len); - return len2 == 0 ? -1 : len2; - } - - @Override - public int read() throws IOException { - return in.readRemaining() > 0 ? in.readUnsignedByte() : -1; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingOutputStream.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingOutputStream.java deleted file mode 100644 index 5bd5e5e..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/StreamingOutputStream.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.BufferOverflowException; - -@SuppressWarnings("rawtypes") -public class StreamingOutputStream extends OutputStream { - private StreamingDataOutput sdo; - - public StreamingOutputStream() { - this(NoBytesStore.NO_BYTES); - } - - public StreamingOutputStream(StreamingDataOutput sdo) { - this.sdo = sdo; - } - - - public StreamingOutputStream init(StreamingDataOutput sdo) { - this.sdo = sdo; - return this; - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - try { - sdo.write(b, off, len); - - } catch ( BufferOverflowException | IllegalArgumentException e) { - throw new IOException(e); - } - } - - @Override - public void write(int b) throws IOException { - try { - sdo.writeUnsignedByte(0xff & b); - - } catch ( BufferOverflowException | IllegalArgumentException e) { - throw new IOException(e); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/SubBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/SubBytes.java deleted file mode 100644 index 5e31448..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/SubBytes.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public class SubBytes extends VanillaBytes { - private final long start; - private final long capacity; - - public SubBytes( BytesStore bytesStore, long start, long capacity) throws IllegalStateException { - super(bytesStore); - this.start = start; - this.capacity = capacity; - clear(); - readLimit(writeLimit()); - } - - public SubBytes( BytesStore bytesStore) throws IllegalStateException { - super(bytesStore); - this.start = 0; - this.capacity = bytesStore.capacity(); - clear(); - readLimit(writeLimit()); - } - - @Override - public long capacity() { - return capacity; - } - - @Override - public long start() { - return start; - } - - @Override - public long realCapacity() { - return capacity; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UTFDataFormatRuntimeException.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UTFDataFormatRuntimeException.java deleted file mode 100644 index 48c9708..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UTFDataFormatRuntimeException.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; - -public class UTFDataFormatRuntimeException extends IORuntimeException { - public UTFDataFormatRuntimeException(String message) { - super(message); - } - - public UTFDataFormatRuntimeException(String message, Exception cause) { - super(message, cause); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UncheckedBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UncheckedBytes.java deleted file mode 100644 index 1caf3f8..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UncheckedBytes.java +++ /dev/null @@ -1,294 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.util.StringUtils; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -import static net.openhft.chronicle.core.util.StringUtils.extractBytes; -import static net.openhft.chronicle.core.util.StringUtils.extractChars; - -/** - * Fast unchecked version of AbstractBytes - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public class UncheckedBytes - extends AbstractBytes { - Bytes underlyingBytes; - - public UncheckedBytes( Bytes underlyingBytes) throws IllegalStateException { - super(underlyingBytes.bytesStore(), underlyingBytes.writePosition(), underlyingBytes.writeLimit()); - this.underlyingBytes = underlyingBytes; - readPosition(underlyingBytes.readPosition()); - } - - public void setBytes( Bytes bytes) throws IllegalStateException { - BytesStore underlyingBytes = bytes.bytesStore(); - if (bytesStore != underlyingBytes) { - bytesStore.release(this); - this.bytesStore(underlyingBytes); - bytesStore.reserve(this); - } - readPosition(bytes.readPosition()); - this.uncheckedWritePosition(bytes.writePosition()); - this.writeLimit = bytes.writeLimit(); - - this.underlyingBytes = bytes; - } - - @Override - public void ensureCapacity(long size) throws IllegalArgumentException { - if (size > realCapacity()) { - underlyingBytes.ensureCapacity(size); - bytesStore(underlyingBytes.bytesStore()); - } - } - - @Override - - public Bytes unchecked(boolean unchecked) { - return this; - } - - @Override - public boolean unchecked() { - return true; - } - - @Override - void writeCheckOffset(long offset, long adding) { - } - - @Override - void readCheckOffset(long offset, long adding, boolean given) { - } - - @Override - void prewriteCheckOffset(long offset, long subtracting) { - } - - - @Override - public Bytes readPosition(long position) { - readPosition = position; - return this; - } - - - @Override - public Bytes readLimit(long limit) { - uncheckedWritePosition(limit); - return this; - } - - - @Override - public Bytes writePosition(long position) { - uncheckedWritePosition(position); - return this; - } - - - @Override - public Bytes readSkip(long bytesToSkip) { - readPosition += bytesToSkip; - return this; - } - - - @Override - public Bytes writeSkip(long bytesToSkip) { - uncheckedWritePosition(writePosition() + bytesToSkip); - return this; - } - - - @Override - public Bytes writeLimit(long limit) { - writeLimit = limit; - return this; - } - - - @Override - public BytesStore, Underlying> copy() { - throw new UnsupportedOperationException("todo"); - } - - @Override - public boolean isElastic() { - return false; - } - - @Override - protected long readOffsetPositionMoved(long adding) { - long offset = readPosition; - readPosition += adding; - return offset; - } - - @Override - protected long writeOffsetPositionMoved(long adding, long advance) { - long oldPosition = writePosition(); - uncheckedWritePosition(writePosition() + advance); - return oldPosition; - } - - @Override - protected long prewriteOffsetPositionMoved(long subtracting) throws BufferOverflowException { - return readPosition -= subtracting; - } - - - @Override - public Bytes write( RandomDataInput bytes, long offset, long length) - throws BufferOverflowException, IllegalArgumentException { - if (length == 8) { - writeLong(bytes.readLong(offset)); - - } else { - super.write(bytes, offset, length); - } - return this; - } - - - public Bytes write( BytesStore bytes, long offset, long length) - throws BufferOverflowException, IllegalArgumentException { - if (length == 8) { - writeLong(bytes.readLong(offset)); - } else if (bytes.underlyingObject() == null - && bytesStore - .isDirectMemory() && - length >= 32) { - rawCopy(bytes, offset, length); - - } else { - super.write(bytes, offset, length); - } - return this; - } - - @Override - - public Bytes append8bit( CharSequence cs) - throws BufferOverflowException, BufferUnderflowException { - if (cs instanceof RandomDataInput) { - return write((RandomDataInput) cs); - } - - int length = cs.length(); - long offset = writeOffsetPositionMoved(length); - for (int i = 0; i < length; i++) { - char c = cs.charAt(i); - if (c > 255) c = '?'; - writeByte(offset, (byte) c); - } - return this; - } - - long rawCopy( BytesStore bytes, long offset, long length) - throws BufferOverflowException, IllegalArgumentException { - long len = Math.min(writeRemaining(), Math.min(bytes.capacity() - offset, length)); - if (len > 0) { - writeCheckOffset(writePosition(), len); - this.throwExceptionIfReleased(); - OS.memory().copyMemory(bytes.addressForRead(offset), addressForWritePosition(), len); - writeSkip(len); - } - return len; - } - - - @Override - public Bytes writeByte(byte i8) throws BufferOverflowException { - long offset = writeOffsetPositionMoved(1, 1); - bytesStore.writeByte(offset, i8); - return this; - } - - - @Override - public Bytes writeUtf8(String s) throws BufferOverflowException { - if (s == null) { - writeStopBit(-1); - return this; - } - - try { - if (Jvm.isJava9Plus()) { - byte[] strBytes = extractBytes(s); - byte coder = StringUtils.getStringCoder(s); - long utfLength = AppendableUtil.findUtf8Length(strBytes, coder); - writeStopBit(utfLength); - appendUtf8(strBytes, 0, s.length(), coder); - } else { - char[] chars = extractChars(s); - long utfLength = AppendableUtil.findUtf8Length(chars); - writeStopBit(utfLength); - if (utfLength == chars.length) - append8bit(chars); - else - appendUtf8(chars, 0, chars.length); - } - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - return this; - } - - void append8bit(char[] chars) throws BufferOverflowException, IllegalArgumentException { - for (int i = 0; i < chars.length; i++) { - char c = chars[i]; - bytesStore.writeByte(writePosition++, (byte) c); - } - } - - - @Override - public Bytes appendUtf8(char[] chars, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - int i; - ascii: - { - for (i = 0; i < length; i++) { - char c = chars[offset + i]; - if (c > 0x007F) - break ascii; - bytesStore.writeByte(writePosition++, (byte) c); - } - return this; - } - for (; i < length; i++) { - char c = chars[offset + i]; - BytesInternal.appendUtf8Char(this, c); - } - return this; - } - - @Override - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) throws BufferOverflowException { - - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UncheckedNativeBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UncheckedNativeBytes.java deleted file mode 100644 index 62037f1..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UncheckedNativeBytes.java +++ /dev/null @@ -1,930 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.algo.BytesStoreHash; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Memory; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.annotation.ForceInline; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.BackgroundResourceReleaser; -import net.openhft.chronicle.core.io.IORuntimeException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; - -/** - * Fast unchecked version of AbstractBytes - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public class UncheckedNativeBytes - extends AbstractReferenceCounted - implements Bytes { - protected final long capacity; - - private final Bytes underlyingBytes; - - protected NativeBytesStore bytesStore; - protected long readPosition; - protected long writePosition; - protected long writeLimit; - private int lastDecimalPlaces = 0; - - public UncheckedNativeBytes( Bytes underlyingBytes) - throws IllegalStateException { - this.underlyingBytes = underlyingBytes; - underlyingBytes.reserve(this); - this.bytesStore = (NativeBytesStore) underlyingBytes.bytesStore(); - assert bytesStore.start() == 0; - writePosition = underlyingBytes.writePosition(); - writeLimit = underlyingBytes.writeLimit(); - readPosition = underlyingBytes.readPosition(); - capacity = bytesStore.capacity(); - } - - @Override - public void ensureCapacity(long size) throws IllegalArgumentException { - if (size > realCapacity()) { - underlyingBytes.ensureCapacity(size); - bytesStore = (NativeBytesStore) underlyingBytes.bytesStore(); - } - } - - @Override - public boolean unchecked() { - return true; - } - - @Override - public boolean isDirectMemory() { - return true; - } - - @Override - - public Bytes unchecked(boolean unchecked) { - return this; - } - - @Override - public void move(long from, long to, long length) { - bytesStore.move(from - start(), to - start(), length); - } - - - @Override - public Bytes compact() { - long start = start(); - long readRemaining = readRemaining(); - if (readRemaining > 0 && start < readPosition) { - bytesStore.move(readPosition, start, readRemaining); - readPosition = start; - writePosition = readPosition + readRemaining; - } - return this; - } - - - @Override - public Bytes readPosition(long position) { - readPosition = position; - return this; - } - - - @Override - public Bytes readLimit(long limit) { - writePosition = limit; - return this; - } - - - @Override - public Bytes writePosition(long position) { - writePosition = position; - return this; - } - - - @Override - public Bytes readSkip(long bytesToSkip) { - readPosition += bytesToSkip; - return this; - } - - @Override - public byte readVolatileByte(long offset) throws BufferUnderflowException { - return bytesStore.readVolatileByte(offset); - } - - @Override - public short readVolatileShort(long offset) throws BufferUnderflowException { - return bytesStore.readVolatileShort(offset); - } - - @Override - public int readVolatileInt(long offset) throws BufferUnderflowException { - return bytesStore.readVolatileInt(offset); - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - return bytesStore.readVolatileLong(offset); - } - - @Override - public void uncheckedReadSkipOne() { - readPosition++; - } - - @Override - public void uncheckedReadSkipBackOne() { - readPosition--; - } - - - @Override - public Bytes writeSkip(long bytesToSkip) { - writePosition += bytesToSkip; - return this; - } - - - @Override - public Bytes writeLimit(long limit) { - writeLimit = limit; - return this; - } - - - @Override - public BytesStore, Underlying> copy() { - throw new UnsupportedOperationException("todo"); - } - - @Override - public boolean isElastic() { - return false; - } - - protected long readOffsetPositionMoved(long adding) { - long offset = readPosition; - readPosition += adding; - return offset; - } - - protected long writeOffsetPositionMoved(long adding) { - return writeOffsetPositionMoved(adding, adding); - } - - protected long writeOffsetPositionMoved(long adding, long advance) { - long oldPosition = writePosition; - long writeEnd = oldPosition + adding; - assert writeEnd <= bytesStore.safeLimit(); - writePosition += advance; - return oldPosition; - } - - protected long prewriteOffsetPositionMoved(long substracting) { - return readPosition -= substracting; - } - - - @Override - public Bytes write( RandomDataInput bytes, long offset, long length) - throws BufferUnderflowException, BufferOverflowException { - if (length == 8) { - writeLong(bytes.readLong(offset)); - - } else if (length >= 16 && bytes.isDirectMemory()) { - rawCopy(bytes, offset, length); - - } else { - BytesInternal.writeFully(bytes, offset, length, this); - } - return this; - } - - public long rawCopy( RandomDataInput bytes, long offset, long length) - throws BufferOverflowException, BufferUnderflowException { - long len = Math.min(writeRemaining(), Math.min(bytes.capacity() - offset, length)); - if (len > 0) { - writeCheckOffset(writePosition(), len); - this.throwExceptionIfReleased(); - OS.memory().copyMemory(bytes.addressForRead(offset), addressForWritePosition(), len); - writeSkip(len); - } - return len; - } - - @Override - - public Bytes clear() { - readPosition = writePosition = start(); - writeLimit = capacity(); - return this; - } - - - @Override - public Bytes clearAndPad(long length) throws BufferOverflowException { - if (start() + length > capacity()) - throw new BufferOverflowException(); - readPosition = writePosition = start() + length; - writeLimit = capacity(); - return this; - } - - @Override - @ForceInline - public long readLimit() { - return writePosition; - } - - @Override - @ForceInline - public long writeLimit() { - return writeLimit; - } - - @Override - @ForceInline - public long realCapacity() { - return bytesStore.realCapacity(); - } - - @Override - public long realWriteRemaining() { - return writeRemaining(); - } - - @Override - @ForceInline - public long capacity() { - return capacity; - } - - - @Override - public Underlying underlyingObject() { - return bytesStore.underlyingObject(); - } - - @Override - @ForceInline - public long readPosition() { - return readPosition; - } - - @Override - @ForceInline - public long writePosition() { - return writePosition; - } - - @Override - @ForceInline - public boolean compareAndSwapInt(long offset, int expected, int value) - throws BufferOverflowException { - writeCheckOffset(offset, 4); - return bytesStore.compareAndSwapInt(offset, expected, value); - } - - @Override - public void testAndSetInt(long offset, int expected, int value) { - writeCheckOffset(offset, 4); - bytesStore.testAndSetInt(offset, expected, value); - } - - @Override - @ForceInline - public boolean compareAndSwapLong(long offset, long expected, long value) - throws BufferOverflowException { - writeCheckOffset(offset, 8); - return bytesStore.compareAndSwapLong(offset, expected, value); - } - - @Override - protected void performRelease() { - this.underlyingBytes.release(this); - // need to wait as checks rely on this completing. - BackgroundResourceReleaser.releasePendingResources(); - } - - @Override - public int readUnsignedByte() { - long offset = readOffsetPositionMoved(1); - return bytesStore.memory.readByte(bytesStore.address + offset) & 0xFF; - } - - @Override - public int uncheckedReadUnsignedByte() { - return readUnsignedByte(); - } - - @Override - @ForceInline - public byte readByte() { - long offset = readOffsetPositionMoved(1); - return bytesStore.memory.readByte(bytesStore.address + offset); - } - - @Override - @ForceInline - public int peekUnsignedByte() { - try { - return readRemaining() > 0 ? bytesStore.readUnsignedByte(readPosition) : -1; - - } catch (BufferUnderflowException e) { - return -1; - } - } - - @Override - @ForceInline - public short readShort() { - long offset = readOffsetPositionMoved(2); - return bytesStore.readShort(offset); - } - - @Override - @ForceInline - public int readInt() { - long offset = readOffsetPositionMoved(4); - return bytesStore.readInt(offset); - } - - @Override - @ForceInline - public long readLong() { - long offset = readOffsetPositionMoved(8); - return bytesStore.readLong(offset); - } - - @Override - @ForceInline - public float readFloat() { - long offset = readOffsetPositionMoved(4); - return bytesStore.readFloat(offset); - } - - @Override - @ForceInline - public double readDouble() { - long offset = readOffsetPositionMoved(8); - return bytesStore.readDouble(offset); - } - - @Override - @ForceInline - public int readVolatileInt() { - long offset = readOffsetPositionMoved(4); - return bytesStore.readVolatileInt(offset); - } - - @Override - @ForceInline - public long readVolatileLong() { - long offset = readOffsetPositionMoved(8); - return bytesStore.readVolatileLong(offset); - } - - - @Override - @ForceInline - public Bytes writeByte(long offset, byte i) throws BufferOverflowException { - writeCheckOffset(offset, 1); - bytesStore.writeByte(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeShort(long offset, short i) throws BufferOverflowException { - writeCheckOffset(offset, 2); - bytesStore.writeShort(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeInt(long offset, int i) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeOrderedInt(long offset, int i) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeOrderedInt(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeLong(long offset, long i) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeLong(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeOrderedLong(long offset, long i) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeOrderedLong(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeFloat(long offset, float d) throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeFloat(offset, d); - return this; - } - - - @Override - @ForceInline - public Bytes writeDouble(long offset, double d) throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeDouble(offset, d); - return this; - } - - - @Override - @ForceInline - public Bytes writeVolatileByte(long offset, byte i8) - throws BufferOverflowException { - writeCheckOffset(offset, 1); - bytesStore.writeVolatileByte(offset, i8); - return this; - } - - - @Override - @ForceInline - public Bytes writeVolatileShort(long offset, short i16) - throws BufferOverflowException { - writeCheckOffset(offset, 2); - bytesStore.writeVolatileShort(offset, i16); - return this; - } - - - @Override - @ForceInline - public Bytes writeVolatileInt(long offset, int i32) - throws BufferOverflowException { - writeCheckOffset(offset, 4); - bytesStore.writeVolatileInt(offset, i32); - return this; - } - - - @Override - @ForceInline - public Bytes writeVolatileLong(long offset, long i64) - throws BufferOverflowException { - writeCheckOffset(offset, 8); - bytesStore.writeVolatileLong(offset, i64); - return this; - } - - @Override - - @ForceInline - public Bytes write(long offsetInRDO, byte[] bytes, int offset, int length) - throws BufferOverflowException { - writeCheckOffset(offsetInRDO, length); - bytesStore.write(offsetInRDO, bytes, offset, length); - return this; - } - - @Override - @ForceInline - public void write(long offsetInRDO, ByteBuffer bytes, int offset, int length) throws BufferOverflowException { - writeCheckOffset(offsetInRDO, length); - bytesStore.write(offsetInRDO, bytes, offset, length); - } - - @Override - - @ForceInline - public Bytes write(long writeOffset, - RandomDataInput bytes, long readOffset, long length) - throws BufferUnderflowException, BufferOverflowException { - writeCheckOffset(writeOffset, length); - bytesStore.write(writeOffset, bytes, readOffset, length); - return this; - } - - @ForceInline - void writeCheckOffset(long offset, long adding) throws BufferOverflowException { -// assert writeCheckOffset0(offset, adding); - } - - @Override - @ForceInline - public byte readByte(long offset) { - return bytesStore.readByte(offset); - } - - @Override - public int readUnsignedByte(long offset) { - return bytesStore.memory.readByte(bytesStore.address + offset) & 0xFF; - } - - @Override - public int peekUnsignedByte(long offset) { - return offset >= writePosition ? -1 : readByte(offset); - } - - @Override - @ForceInline - public short readShort(long offset) { - return bytesStore.readShort(offset); - } - - @Override - @ForceInline - public int readInt(long offset) { - return bytesStore.readInt(offset); - } - - @Override - @ForceInline - public long readLong(long offset) { - return bytesStore.readLong(offset); - } - - @Override - @ForceInline - public float readFloat(long offset) { - return bytesStore.readFloat(offset); - } - - @Override - @ForceInline - public double readDouble(long offset) { - return bytesStore.readDouble(offset); - } - - - @Override - @ForceInline - public Bytes writeByte(byte i8) { - long offset = writeOffsetPositionMoved(1); - bytesStore.memory.writeByte(bytesStore.address + offset, i8); - return this; - } - - - @Override - @ForceInline - public Bytes prewriteByte(byte i8) { - long offset = prewriteOffsetPositionMoved(1); - bytesStore.memory.writeByte(bytesStore.address + offset, i8); - return this; - } - - - @Override - @ForceInline - public Bytes writeShort(short i16) { - long offset = writeOffsetPositionMoved(2); - bytesStore.writeShort(offset, i16); - return this; - } - - - @Override - @ForceInline - public Bytes prewriteShort(short i16) { - long offset = prewriteOffsetPositionMoved(2); - bytesStore.writeShort(offset, i16); - return this; - } - - - @Override - @ForceInline - public Bytes writeInt(int i) { - long offset = writeOffsetPositionMoved(4); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeIntAdv(int i, int advance) { - long offset = writeOffsetPositionMoved(4, advance); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes prewriteInt(int i) { - long offset = prewriteOffsetPositionMoved(4); - bytesStore.writeInt(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeLong(long i64) { - long offset = writeOffsetPositionMoved(8); - bytesStore.writeLong(offset, i64); - return this; - } - - - @Override - @ForceInline - public Bytes writeLongAdv(long i64, int advance) { - long offset = writeOffsetPositionMoved(8, advance); - bytesStore.writeLong(offset, i64); - return this; - } - - - @Override - @ForceInline - public Bytes prewriteLong(long i64) { - long offset = prewriteOffsetPositionMoved(8); - bytesStore.writeLong(offset, i64); - return this; - } - - - @Override - @ForceInline - public Bytes writeFloat(float f) { - long offset = writeOffsetPositionMoved(4); - bytesStore.writeFloat(offset, f); - return this; - } - - - @Override - @ForceInline - public Bytes writeDouble(double d) { - long offset = writeOffsetPositionMoved(8); - bytesStore.writeDouble(offset, d); - return this; - } - - - @Override - @ForceInline - public Bytes writeDoubleAndInt(double d, int i) { - long offset = writeOffsetPositionMoved(12); - bytesStore.writeDouble(offset, d); - bytesStore.writeInt(offset + 8, i); - return this; - } - - - @Override - @ForceInline - public Bytes write( byte[] bytes, int offset, int length) { - if (length + offset > bytes.length) - throw new ArrayIndexOutOfBoundsException("bytes.length=" + bytes.length + ", " + - "length=" + length + ", offset=" + offset); - if (length > writeRemaining()) - throw new BufferOverflowException(); - long offsetInRDO = writeOffsetPositionMoved(length); - bytesStore.write(offsetInRDO, bytes, offset, length); - return this; - } - - - @Override - @ForceInline - public Bytes prewrite( byte[] bytes) { - long offsetInRDO = prewriteOffsetPositionMoved(bytes.length); - bytesStore.write(offsetInRDO, bytes); - return this; - } - - - @Override - @ForceInline - public Bytes prewrite( BytesStore bytes) { - long offsetInRDO = prewriteOffsetPositionMoved(bytes.length()); - bytesStore.write(offsetInRDO, bytes); - return this; - } - - - @Override - @ForceInline - public Bytes writeSome( ByteBuffer buffer) { - bytesStore.write(writePosition, buffer, buffer.position(), buffer.limit()); - writePosition += buffer.remaining(); - assert writePosition <= writeLimit(); - return this; - } - - - @Override - @ForceInline - public Bytes writeOrderedInt(int i) { - long offset = writeOffsetPositionMoved(4); - bytesStore.writeOrderedInt(offset, i); - return this; - } - - - @Override - @ForceInline - public Bytes writeOrderedLong(long i) { - long offset = writeOffsetPositionMoved(8); - bytesStore.writeOrderedLong(offset, i); - return this; - } - - @Override - public long addressForRead(long offset) throws BufferUnderflowException { - return bytesStore.addressForRead(offset); - } - - @Override - public long addressForWrite(long offset) throws BufferOverflowException { - return bytesStore.addressForWrite(offset); - } - - @Override - public long addressForWritePosition() throws UnsupportedOperationException, BufferOverflowException { - return bytesStore.addressForWrite(0); - } - - @Override - public int hashCode() { - return BytesStoreHash.hash32(this); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof Bytes)) return false; - Bytes b2 = (Bytes) obj; - long remaining = readRemaining(); - return b2.readRemaining() == remaining && equalsBytes(b2, remaining); - } - - public boolean equalsBytes( Bytes b2, long remaining) { - long i = 0; - try { - for (; i < remaining - 7; i += 8) - if (readLong(readPosition() + i) != b2.readLong(b2.readPosition() + i)) - return false; - for (; i < remaining; i++) - if (readByte(readPosition() + i) != b2.readByte(b2.readPosition() + i)) - return false; - - } catch (BufferUnderflowException e) { - throw Jvm.rethrow(e); - } - return true; - } - - - @Override - public String toString() { - return BytesInternal.toString(this); - } - - - @Override - public void lenient(boolean lenient) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean lenient() { - return false; - } - - - - @Override - @ForceInline - public void nativeRead(long position, long address, long size) { - bytesStore.nativeRead(position, address, size); - } - - @Override - @ForceInline - public void nativeWrite(long address, long position, long size) { - bytesStore.nativeWrite(address, position, size); - } - - - @Override - public BytesStore bytesStore() { - return bytesStore; - } - - @Override - public int byteCheckSum() throws IORuntimeException { - NativeBytesStore bytesStore = (NativeBytesStore) bytesStore(); - return bytesStore.byteCheckSum(readPosition(), readLimit()); - } - - @Override - - public Bytes append8bit( CharSequence cs) - throws BufferOverflowException, BufferUnderflowException { - if (cs instanceof BytesStore) { - return write((BytesStore) cs); - } - int length = cs.length(); - long offset = writeOffsetPositionMoved(length); - long address = bytesStore.addressForWrite(offset); - Memory memory = bytesStore.memory; - assert memory != null; - try { - int i = 0; - for (; i < length - 1; i += 2) { - char c = cs.charAt(i); - char c2 = cs.charAt(i + 1); - memory.writeByte(address + i, (byte) c); - memory.writeByte(address + i + 1, (byte) c2); - } - for (; i < length; i++) { - char c = cs.charAt(i); - memory.writeByte(address + i, (byte) c); - } - - return this; - } catch (IndexOutOfBoundsException e) { - throw new AssertionError(e); - } - } - - - @Override - public Bytes appendUtf8(char[] chars, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - ensureCapacity(length); - NativeBytesStore nbs = this.bytesStore; - long position = nbs.appendUtf8(writePosition(), chars, offset, length); - writePosition(position); - return this; - } - - @Override - public int lastDecimalPlaces() { - return lastDecimalPlaces; - } - - @Override - public void lastDecimalPlaces(int lastDecimalPlaces) { - this.lastDecimalPlaces = Math.max(0, lastDecimalPlaces); - } - - - @Override - public Bytes write( RandomDataInput bytes) { - assert bytes != this : "you should not write to yourself !"; - - try { - return write(bytes, bytes.readPosition(), Math.min(writeRemaining(), bytes.readRemaining())); - } catch (BufferOverflowException | BufferUnderflowException e) { - throw new AssertionError(e); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UpdateInterceptor.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UpdateInterceptor.java deleted file mode 100644 index 4e9e948..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/UpdateInterceptor.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.openhft.chronicle.bytes; - -/** - * Represents an operation that accepts a single input argument then modifies that same instance. - */ -@FunctionalInterface -public interface UpdateInterceptor { - - /** - * modifies {@code t} with changed data - * - * @param methodName the name of the method - * @param t the input argument - for a method call with multiple arguments, the last one is passed - * @return whether to proceed. If false, don't write - */ - boolean update(String methodName, Object t); - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/VanillaBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/VanillaBytes.java deleted file mode 100644 index dcf92b8..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/VanillaBytes.java +++ /dev/null @@ -1,597 +0,0 @@ - /* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - - import net.openhft.chronicle.core.Jvm; - import net.openhft.chronicle.core.Maths; - import net.openhft.chronicle.core.Memory; - import net.openhft.chronicle.core.OS; - import net.openhft.chronicle.core.annotation.Java9; - import net.openhft.chronicle.core.io.IORuntimeException; - import net.openhft.chronicle.core.util.StringUtils; - import org.jetbrains.annotations.NotNull; - import org.jetbrains.annotations.Nullable; - - import java.nio.BufferOverflowException; - import java.nio.BufferUnderflowException; - import java.nio.ByteBuffer; - - import static net.openhft.chronicle.bytes.NoBytesStore.noBytesStore; - - /** - * Simple Bytes implementation which is not Elastic. - */ - @SuppressWarnings({"rawtypes", "unchecked"}) - public class VanillaBytes - extends AbstractBytes - implements Byteable, Underlying>, Comparable { - - public VanillaBytes( BytesStore bytesStore) throws IllegalStateException { - this(bytesStore, bytesStore.writePosition(), bytesStore.writeLimit()); - } - - public VanillaBytes( BytesStore bytesStore, long writePosition, long writeLimit) throws IllegalStateException { - super(bytesStore, writePosition, writeLimit); - } - - /** - * @return a non elastic bytes. - */ - - public static VanillaBytes vanillaBytes() { - try { - return new VanillaBytes<>(noBytesStore()); - - } catch (IllegalStateException e) { - throw new AssertionError(e); - } - } - - @Java9 - private static boolean isEqual0( byte[] bytes, byte coder, NativeBytesStore bs, long address) { - - Memory memory = bs.memory; - - if (coder == 0) { - int i = 0; - for (; i < bytes.length; i++) { - byte b = memory.readByte(address + i); - char c = (char) (bytes[i] & 0xFF); - if (b != c) { - return false; - } - } - } else { - int i = 0; - for (; i < bytes.length; i++) { - byte b = memory.readByte(address + i); - char c = (char) (((bytes[i + 1] & 0xFF) << 8) | (bytes[i] & 0xFF)); - - if (b != c) { - return false; - } - } - } - - return true; - } - - private static boolean isEqual0( char[] chars, NativeBytesStore bs, long address) { - - Memory memory = bs.memory; - int i = 0; - for (; i < chars.length - 3; i += 4) { - int b = memory.readInt(address + i); - int b0 = b & 0xFF; - int b1 = (b >> 8) & 0xFF; - int b2 = (b >> 16) & 0xFF; - int b3 = (b >> 24) & 0xFF; - if (b0 != chars[i] || b1 != chars[i + 1] || b2 != chars[i + 2] || b3 != chars[i + 3]) - return false; - } - for (; i < chars.length; i++) { - int b = memory.readByte(address + i) & 0xFF; - if (b != chars[i]) - return false; - } - - return true; - } - - private static boolean isEqual1( char[] chars, BytesStore bytesStore, long readPosition) throws BufferUnderflowException { - for (int i = 0; i < chars.length; i++) { - int b = bytesStore.readByte(readPosition + i) & 0xFF; - if (b != chars[i]) - return false; - } - return true; - } - - @Java9 - private static boolean isEqual1( byte[] bytes, byte coder, BytesStore bytesStore, long readPosition) throws BufferUnderflowException { - for (int i = 0; i < bytes.length; i++) { - int b = bytesStore.readByte(readPosition + i) & 0xFF; - char c; - - if (coder == 0) { - c = (char) (bytes[i] & 0xFF); - } else { - c = (char) (((bytes[i + 1] & 0xFF) << 8) | (bytes[i] & 0xFF)); - i++; - } - - if (b != c) - return false; - } - return true; - } - - @Override - public long readVolatileLong(long offset) throws BufferUnderflowException { - readCheckOffset(offset, 8, true); - return bytesStore.readVolatileLong(offset); - } - - @Override - public void bytesStore( BytesStore, Underlying> byteStore, long offset, long length) - throws IllegalStateException, BufferOverflowException, BufferUnderflowException { - setBytesStore(byteStore); - // assume its read-only - readLimit(offset + length); - writeLimit(offset + length); - readPosition(offset); - } - - private void setBytesStore( BytesStore, Underlying> bytesStore) - throws IllegalStateException { - if (this.bytesStore != bytesStore) { - BytesStore oldBS = this.bytesStore; - this.bytesStore(bytesStore); - bytesStore.reserve(this); - oldBS.release(this); - } - clear(); - } - - @Override - public long offset() { - return readPosition(); - } - - @Override - public long maxSize() { - return readRemaining(); - } - - @Override - public boolean isElastic() { - return false; - } - - - @Override - public Bytes bytesForRead() throws IllegalStateException { - return isClear() - ? new VanillaBytes<>(bytesStore, writePosition(), bytesStore.writeLimit()) - : new SubBytes<>(bytesStore, readPosition(), readLimit()); - } - - @Override - public boolean isEqual( String s) { - if (s == null || s.length() != readRemaining()) return false; - - try { - if (Jvm.isJava9Plus()) { - byte[] bytes = StringUtils.extractBytes(s); - byte coder = StringUtils.getStringCoder(s); - if (bytesStore instanceof NativeBytesStore) { - NativeBytesStore bs = (NativeBytesStore) this.bytesStore; - long address = bs.addressForRead(readPosition); - return isEqual0(bytes, coder, bs, address); - - } else { - return isEqual1(bytes, coder, bytesStore, readPosition); - } - } else { - char[] chars = StringUtils.extractChars(s); - if (bytesStore instanceof NativeBytesStore) { - NativeBytesStore bs = (NativeBytesStore) this.bytesStore; - long address = bs.addressForRead(readPosition); - return isEqual0(chars, bs, address); - - } else { - return isEqual1(chars, bytesStore, readPosition); - } - } - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - } - - @Override - public long realCapacity() { - return bytesStore.realCapacity(); - } - - - @Override - public BytesStore, Underlying> copy() { - if (bytesStore.underlyingObject() instanceof ByteBuffer) { - try { - ByteBuffer bb = ByteBuffer.allocateDirect(Maths.toInt32(readRemaining())); - ByteBuffer bbu = (ByteBuffer) bytesStore.underlyingObject(); - ByteBuffer slice = bbu.slice(); - slice.position((int) readPosition()); - slice.limit((int) readLimit()); - bb.put(slice); - bb.clear(); - return (BytesStore) BytesStore.wrap(bb); - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } else { - return (BytesStore) NativeBytes.copyOf(this); - } - } - - - @Override - public Bytes write( RandomDataInput bytes, long offset, long length) - throws BufferOverflowException, BufferUnderflowException, IllegalArgumentException { - optimisedWrite(bytes, offset, length); - return this; - } - - protected void optimisedWrite( RandomDataInput bytes, long offset, long length) { - if (length <= safeCopySize() && isDirectMemory() && bytes.isDirectMemory()) { - long len = Math.min(writeRemaining(), Math.min(bytes.capacity() - offset, length)); - if (len > 0) { - writeCheckOffset(writePosition(), len); - long address = bytes.addressForRead(offset); - long address2 = addressForWritePosition(); - assert address != 0; - assert address2 != 0; - OS.memory().copyMemory(address, address2, len); - writeSkip(len); - } - } else { - super.write(bytes, offset, length); - } - } - - public void write(long position, CharSequence str, int offset, int length) - throws BufferOverflowException, IllegalArgumentException { - - ensureCapacity(length); - if (offset + length > str.length()) - throw new IllegalArgumentException("offset=" + offset + " + length=" + length + " > str.length =" + str.length()); - - for (int i = 0; i < length; i++) { - bytesStore.writeByte(position + i, str.charAt(offset + i)); - } - } - - @Override - - public VanillaBytes append( CharSequence str, int start, int end) throws IndexOutOfBoundsException { - assert end > start : "end=" + end + ",start=" + start; - try { - if (isDirectMemory()) { - if (str instanceof BytesStore) { - - write((BytesStore) str, (long) start, end - start); - return this; - } - if (str instanceof String) { - if (Jvm.isJava9Plus()) { - byte coder = StringUtils.getStringCoder((String) str); - appendUtf8(StringUtils.extractBytes((String) str), start, end - start, coder); - } else { - appendUtf8(StringUtils.extractChars((String) str), start, end - start); - } - - return this; - } - } - super.append(str, start, end); - return this; - - } catch (Exception e) { - throw new IndexOutOfBoundsException(e.toString()); - } - } - - - @Override - public VanillaBytes appendUtf8( CharSequence str) throws BufferOverflowException { - try { - if (isDirectMemory()) { - if (str instanceof BytesStore) { - write((BytesStore) str, 0L, str.length()); - return this; - } - if (str instanceof String) { - if (Jvm.isJava9Plus()) { - String str1 = (String) str; - byte coder = StringUtils.getStringCoder(str1); - appendUtf8(StringUtils.extractBytes(str1), 0, str.length(), coder); - } else { - appendUtf8(StringUtils.extractChars((String) str), 0, str.length()); - } - return this; - } - } - super.append(str, 0, str.length()); - return this; - - } catch (Exception e) { - BufferOverflowException e2 = new BufferOverflowException(); - e2.initCause(e); - throw e2; - } - } - - @Override - - public Bytes append8bit( CharSequence cs) - throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - if (cs instanceof RandomDataInput) - return write((RandomDataInput) cs); - - if (isDirectMemory() && cs instanceof String) - return append8bitNBS_S((String) cs); - return append8bit0(cs); - } - - @Override - - public Bytes append8bit( BytesStore bs) - throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - long remaining = bs.readLimit() - bs.readPosition(); - return write(bs, 0L, remaining); - } - - - @Override - public Bytes write( BytesStore bytes, long offset, long length) throws BufferOverflowException, BufferUnderflowException { - if (bytes.canReadDirect(length) && canWriteDirect(length) && length == (int) length) { - long wAddr = addressForWritePosition(); - writeSkip(length); - long rAddr = bytes.addressForRead(offset); - BytesInternal.copyMemory(rAddr, wAddr, (int) length); - - } else { - BytesInternal.writeFully(bytes, offset, length, this); - } - return this; - } - - @Override - - public Bytes append8bit( String cs) - throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { - if (isDirectMemory()) - return append8bitNBS_S(cs); - return append8bit0(cs); - } - - - private Bytes append8bitNBS_S( String s) throws BufferOverflowException { - int length = s.length(); - long offset = writeOffsetPositionMoved(length); // can re-assign the byteStore if not large enough. - NativeBytesStore bytesStore = (NativeBytesStore) this.bytesStore; - final long address = bytesStore.address + bytesStore.translate(offset); - final Memory memory = bytesStore.memory; - - if (memory == null) - bytesStore.throwExceptionIfReleased(); - - if (Jvm.isJava9Plus()) { - final byte[] chars = StringUtils.extractBytes(s); - - int i; - for (i = 0; i < length; i++) { - memory.writeByte(address + i, chars[i]); - } - } else { - final char[] chars = StringUtils.extractChars(s); - int i; - for (i = 0; i < length - 3; i += 4) { - int c0 = chars[i] & 0xFF; - int c1 = chars[i + 1] & 0xFF; - int c2 = chars[i + 2] & 0xFF; - int c3 = chars[i + 3] & 0xFF; - memory.writeInt(address + i, c0 | (c1 << 8) | (c2 << 16) | (c3 << 24)); - } - for (; i < length; i++) { - int c0 = chars[i]; - memory.writeByte(address + i, (byte) c0); - } - } - return this; - } - - - public String toString() { - return bytesStore instanceof NativeBytesStore - ? toString2((NativeBytesStore) bytesStore) - : toString0(); - } - - private String toString2( NativeBytesStore bytesStore) { - int length = (int) - Math.min(Bytes.MAX_HEAP_CAPACITY, readRemaining()); - char[] chars = new char[length]; - final Memory memory = bytesStore.memory; - final long address = bytesStore.address + bytesStore.translate(readPosition()); - for (int i = 0; i < length && i < realCapacity(); i++) - chars[i] = (char) (memory.readByte(address + i) & 0xFF); - - return StringUtils.newString(chars); - } - - - protected String toString0() { - int length = (int) Math.min(Bytes.MAX_HEAP_CAPACITY, readRemaining()); - char[] chars = new char[length]; - try { - for (int i = 0; i < length; i++) { - chars[i] = (char) (bytesStore.readByte(readPosition() + i) & 0xFF); - } - } catch (BufferUnderflowException e) { - // ignored - } - return StringUtils.newString(chars); - } - - - protected Bytes append8bit0( CharSequence cs) throws BufferOverflowException, IndexOutOfBoundsException { - int length = cs.length(); - long offset = writeOffsetPositionMoved(length); - for (int i = 0; i < length; i++) { - char c = cs.charAt(i); - if (c > 255) c = '?'; - bytesStore.writeByte(offset + i, (byte) c); - } - return this; - } - - @Override - public boolean equalBytes( BytesStore bytesStore, long length) throws BufferUnderflowException { - if (isDirectMemory() && - bytesStore instanceof VanillaBytes && - bytesStore.isDirectMemory()) { - VanillaBytes b2 = (VanillaBytes) bytesStore; - NativeBytesStore nbs0 = (NativeBytesStore) this.bytesStore; - NativeBytesStore nbs2 = (NativeBytesStore) b2.bytesStore(); - long i = 0; - for (; i < length - 7; i += 8) { - long addr0 = nbs0.address + readPosition() - nbs0.start() + i; - long addr2 = nbs2.address + b2.readPosition() - nbs2.start() + i; - long l0 = nbs0.memory.readLong(addr0); - long l2 = nbs2.memory.readLong(addr2); - if (l0 != l2) - return false; - } - for (; i < length; i++) { - long offset2 = readPosition() + i - nbs0.start(); - long offset21 = b2.readPosition() + i - nbs2.start(); - byte b0 = nbs0.memory.readByte(nbs0.address + offset2); - byte b1 = nbs2.memory.readByte(nbs2.address + offset21); - if (b0 != b1) - return false; - } - return true; - } else { - return super.equalBytes(bytesStore, length); - } - } - - public void read8Bit(char[] chars, int length) { - if (isDirectMemory()) { - long position = readPosition(); - NativeBytesStore nbs = (NativeBytesStore) bytesStore(); - nbs.read8bit(position, chars, length); - } else { - long pos = this.readPosition(); - for (int i = 0; i < length; i++) - chars[i] = (char) this.readUnsignedByte(pos + i); - } - } - - // TODO: protected? - @Override - public int byteCheckSum(int start, int end) throws IORuntimeException { - byte b = 0; - // the below cast is safe as should only be called from net.openhft.chronicle.bytes.AbstractBytes.byteCheckSum(long, long) - NativeBytesStore bytesStore = (NativeBytesStore) bytesStore(); - Memory memory = bytesStore.memory; - assert memory != null; - long addr = bytesStore.addressForRead(start); - int i = 0, len = end - start; - for (; i < len - 3; i += 4) { - b += memory.readByte(addr + i) - + memory.readByte(addr + i + 1) - + memory.readByte(addr + i + 2) - + memory.readByte(addr + i + 3); - } - for (; i < len; i++) { - b += memory.readByte(addr + i); - } - return b & 0xFF; - } - - - @Override - public Bytes appendUtf8(char[] chars, int offset, int length) throws BufferOverflowException, IllegalArgumentException { - ensureCapacity(length); - if (bytesStore instanceof NativeBytesStore) { - NativeBytesStore nbs = (NativeBytesStore) this.bytesStore; - long position = nbs.appendUtf8(writePosition(), chars, offset, length); - writePosition(position); - } else { - super.appendUtf8(chars, offset, length); - } - return this; - } - - @Override - public ByteBuffer toTemporaryDirectByteBuffer() throws IllegalArgumentException { - if (isClear()) - return bytesStore.toTemporaryDirectByteBuffer(); - return super.toTemporaryDirectByteBuffer(); - } - - @Override - public int read( byte[] bytes) { - int len = (int) Math.min(bytes.length, readRemaining()); - if (bytesStore instanceof NativeBytesStore) { - NativeBytesStore nbs = (NativeBytesStore) this.bytesStore; - long len2 = nbs.read(readPosition(), bytes, 0, len); - try { - readSkip(len2); - } catch (BufferUnderflowException e) { - throw new AssertionError(e); - } - return (int) (len2); - } - return super.read(bytes); - } - - @Override - public int compareTo( CharSequence cs) { - long len1 = readRemaining(); - int len2 = cs.length(); - long lim = Math.min(len1, len2); - - int k = 0; - while (k < lim) { - char c1 = bytesStore.charAt(k); - char c2 = cs.charAt(k); - if (c1 != c2) { - return c1 - c2; - } - k++; - } - return (int) (len1 - len2); - } - - - - } diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/WriteBytesMarshallable.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/WriteBytesMarshallable.java deleted file mode 100644 index dc4f4fb..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/WriteBytesMarshallable.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.annotation.DontChain; - -/** - * Write data directly as Bytes. - */ -@SuppressWarnings("rawtypes") -@FunctionalInterface -@DontChain -public interface WriteBytesMarshallable extends CommonMarshallable { - /** - * Write to Bytes. This can be used as an interface to extend or a lambda - * - * @param bytes to write to. - */ - void writeMarshallable(BytesOut bytes); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/BytesStoreHash.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/BytesStoreHash.java deleted file mode 100644 index ae4be60..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/BytesStoreHash.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.algo; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.bytes.NativeBytesStore; - -import java.util.function.ToLongFunction; - -/** - * Simple function to derive a long hash from a BytesStore - */ -@SuppressWarnings("rawtypes") -public interface BytesStoreHash extends ToLongFunction { - static long hash( BytesStore b) { - return b.isDirectMemory() && b.bytesStore() instanceof NativeBytesStore - ? OptimisedBytesStoreHash.INSTANCE.applyAsLong(b) - : VanillaBytesStoreHash.INSTANCE.applyAsLong(b); - } - - static long hash( BytesStore b, long length) { - return b.isDirectMemory() && b.bytesStore() instanceof NativeBytesStore - ? OptimisedBytesStoreHash.INSTANCE.applyAsLong(b, length) - : VanillaBytesStoreHash.INSTANCE.applyAsLong(b, length); - } - - static int hash32(BytesStore b) { - long hash = hash(b); - return (int) (hash ^ (hash >>> 32)); - } - - static int hash32( BytesStore b, int length) { - long hash = hash(b, length); - return (int) (hash ^ (hash >>> 32)); - } - - long applyAsLong(BytesStore bytes, long length); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/OptimisedBytesStoreHash.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/OptimisedBytesStoreHash.java deleted file mode 100644 index cc937c1..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/OptimisedBytesStoreHash.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.algo; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.bytes.NativeBytesStore; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.Memory; -import net.openhft.chronicle.core.OS; - -import java.nio.ByteOrder; - -import static net.openhft.chronicle.bytes.algo.VanillaBytesStoreHash.*; - -@SuppressWarnings("rawtypes") -public enum OptimisedBytesStoreHash implements BytesStoreHash { - INSTANCE; - - - public static final Memory MEMORY = OS.memory(); - public static final boolean IS_LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; - private static final int TOP_BYTES = IS_LITTLE_ENDIAN ? 4 : 0; - - static long applyAsLong1to7( BytesStore store, int remaining) { - final long address = store.addressForRead(store.readPosition()); - - return hash(readIncompleteLong(address, remaining)); - } - - static long applyAsLong8( BytesStore store) { - final long address = store.addressForRead(store.readPosition()); - - return hash0(MEMORY.readLong(address), MEMORY.readInt(address + TOP_BYTES)); - } - - public static long hash(long l) { - return hash0(l, l >> 32); - } - - static long hash0(long l, long hi) { - return agitate(l * K0 + hi * K1); - } - - static long applyAsLong9to16( BytesStore store, int remaining) { - final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); - final long address = bytesStore.addressForRead(store.readPosition()); - long h0 = (long) remaining * K0; - - int left = remaining; - long addrI = address; - - long l0 = readIncompleteLong(addrI, left); - int l0a = (int) (l0 >> 32); - long l1 = readIncompleteLong(addrI + 8, left - 8); - int l1a = (int) (l1 >> 32); - final long l2 = 0; - final int l2a = 0; - final long l3 = 0; - final int l3a = 0; - - h0 += (l0 + l1a - l2a) * M0; - long h1 = (l1 + l2a - l3a) * M1; - long h2 = (l2 + l3a - l0a) * M2; - long h3 = (l3 + l0a - l1a) * M3; - - return agitate(h0) ^ agitate(h1) - ^ agitate(h2) ^ agitate(h3); - } - - static long applyAsLong17to32( BytesStore store, int remaining) { - final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); - final long address = bytesStore.addressForRead(store.readPosition()); - long h0 = (long) remaining * K0; - - int left = remaining; - long addrI = address; - - long l0 = MEMORY.readLong(addrI); - int l0a = MEMORY.readInt(addrI + TOP_BYTES); - long l1 = MEMORY.readLong(addrI + 8); - int l1a = MEMORY.readInt(addrI + 8 + TOP_BYTES); - long l2 = readIncompleteLong(addrI + 16, left - 16); - int l2a = (int) (l2 >> 32); - long l3 = readIncompleteLong(addrI + 24, left - 24); - int l3a = (int) (l3 >> 32); - - h0 += (l0 + l1a - l2a) * M0; - long h1 = (l1 + l2a - l3a) * M1; - long h2 = (l2 + l3a - l0a) * M2; - long h3 = (l3 + l0a - l1a) * M3; - - return agitate(h0) ^ agitate(h1) - ^ agitate(h2) ^ agitate(h3); - } - - public static long applyAsLong32bytesMultiple( BytesStore store, int remaining) { - final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); - final long address = bytesStore.addressForRead(store.readPosition()); - long h0 = remaining * K0, h1 = 0, h2 = 0, h3 = 0; - - int i; - for (i = 0; i < remaining - 31; i += 32) { - if (i > 0) { - h0 *= K0; - h1 *= K1; - h2 *= K2; - h3 *= K3; - } - long addrI = address + i; - long l0 = MEMORY.readLong(addrI); - int l0a = MEMORY.readInt(addrI + TOP_BYTES); - long l1 = MEMORY.readLong(addrI + 8); - int l1a = MEMORY.readInt(addrI + 8 + TOP_BYTES); - long l2 = MEMORY.readLong(addrI + 16); - int l2a = MEMORY.readInt(addrI + 16 + TOP_BYTES); - long l3 = MEMORY.readLong(addrI + 24); - int l3a = MEMORY.readInt(addrI + 24 + TOP_BYTES); - - h0 += (l0 + l1a - l2a) * M0; - h1 += (l1 + l2a - l3a) * M1; - h2 += (l2 + l3a - l0a) * M2; - h3 += (l3 + l0a - l1a) * M3; - } - - return agitate(h0) ^ agitate(h1) - ^ agitate(h2) ^ agitate(h3); - } - - public static long applyAsLongAny( BytesStore store, long remaining) { - final NativeBytesStore bytesStore = (NativeBytesStore) store.bytesStore(); - final long address = bytesStore.addressForRead(store.readPosition()); - long h0 = remaining * K0, h1 = 0, h2 = 0, h3 = 0; - - int i; - for (i = 0; i < remaining - 31; i += 32) { - if (i > 0) { - h0 *= K0; - h1 *= K1; - h2 *= K2; - h3 *= K3; - } - long addrI = address + i; - long l0 = MEMORY.readLong(addrI); - int l0a = MEMORY.readInt(addrI + TOP_BYTES); - long l1 = MEMORY.readLong(addrI + 8); - int l1a = MEMORY.readInt(addrI + 8 + TOP_BYTES); - long l2 = MEMORY.readLong(addrI + 16); - int l2a = MEMORY.readInt(addrI + 16 + TOP_BYTES); - long l3 = MEMORY.readLong(addrI + 24); - int l3a = MEMORY.readInt(addrI + 24 + TOP_BYTES); - - h0 += (l0 + l1a - l2a) * M0; - h1 += (l1 + l2a - l3a) * M1; - h2 += (l2 + l3a - l0a) * M2; - h3 += (l3 + l0a - l1a) * M3; - } - long left = remaining - i; - if (left > 0) { - if (i > 0) { - h0 *= K0; - h1 *= K1; - h2 *= K2; - h3 *= K3; - } - long addrI = address + i; - if (left <= 16) { - - long l0 = readIncompleteLong(addrI, (int) left); - int l0a = (int) (l0 >> 32); - long l1 = readIncompleteLong(addrI + 8, (int) (left - 8)); - int l1a = (int) (l1 >> 32); - final long l2 = 0; - final int l2a = 0; - final long l3 = 0; - final int l3a = 0; - - h0 += (l0 + l1a - l2a) * M0; - h1 += (l1 + l2a - l3a) * M1; - h2 += (l2 + l3a - l0a) * M2; - h3 += (l3 + l0a - l1a) * M3; - - } else { - long l0 = MEMORY.readLong(addrI); - int l0a = MEMORY.readInt(addrI + TOP_BYTES); - long l1 = MEMORY.readLong(addrI + 8); - int l1a = MEMORY.readInt(addrI + 8 + TOP_BYTES); - long l2 = readIncompleteLong(addrI + 16, (int) (left - 16)); - int l2a = (int) (l2 >> 32); - long l3 = readIncompleteLong(addrI + 24, (int) (left - 24)); - int l3a = (int) (l3 >> 32); - - h0 += (l0 + l1a - l2a) * M0; - h1 += (l1 + l2a - l3a) * M1; - h2 += (l2 + l3a - l0a) * M2; - h3 += (l3 + l0a - l1a) * M3; - } - } - - return agitate(h0) ^ agitate(h1) - ^ agitate(h2) ^ agitate(h3); - } - - static long readIncompleteLong(long address, int len) { - switch (len) { - case 1: - return MEMORY.readByte(address); - case 2: - return MEMORY.readShort(address); - case 3: - return IS_LITTLE_ENDIAN - ? (MEMORY.readShort(address) & 0xFFFF) + ((MEMORY.readByte(address + 2) & 0xFF) << 16) - : ((MEMORY.readShort(address) & 0xFFFF) << 8) + (MEMORY.readByte(address + 2) & 0xFF); - case 4: - return MEMORY.readInt(address); - case 5: - return IS_LITTLE_ENDIAN - ? (MEMORY.readInt(address) & 0xFFFFFFFFL) + ((long) (MEMORY.readByte(address + 4) & 0xFF) << 32) - : ((MEMORY.readInt(address) & 0xFFFFFFFFL) << 8) + (MEMORY.readByte(address + 4) & 0xFF); - case 6: - return IS_LITTLE_ENDIAN - ? (MEMORY.readInt(address) & 0xFFFFFFFFL) + ((long) (MEMORY.readShort(address + 4) & 0xFFFF) << 32) - : ((MEMORY.readInt(address) & 0xFFFFFFFFL) << 16) + (MEMORY.readShort(address + 4) & 0xFFFF); - case 7: - return IS_LITTLE_ENDIAN - ? (MEMORY.readInt(address) & 0xFFFFFFFFL) + ((long) (MEMORY.readShort(address + 4) & 0xFFFF) << 32) + ((long) (MEMORY.readByte(address + 6) & 0xFF) << 48) - : ((MEMORY.readInt(address) & 0xFFFFFFFFL) << 24) + ((MEMORY.readShort(address + 4) & 0xFFFF) << 8) + (MEMORY.readByte(address + 6) & 0xFF); - default: - return len >= 8 ? MEMORY.readLong(address) : 0; - } - } - - @Override - public long applyAsLong( BytesStore store) { - final int remaining = Maths.toInt32(store.readRemaining()); - return applyAsLong(store, remaining); - } - - @Override - public long applyAsLong( BytesStore store, long remaining) { - if (remaining <= 16) { - if (remaining == 0) { - return 0; - } else if (remaining < 8) { - return applyAsLong1to7(store, (int) remaining); - } else if (remaining == 8) { - return applyAsLong8(store); - } else { - return applyAsLong9to16(store, (int) remaining); - } - } else if (remaining <= 32) { - return applyAsLong17to32(store, (int) remaining); - } else if ((remaining & 31) == 0) { - return applyAsLong32bytesMultiple(store, (int) remaining); - } else { - return applyAsLongAny(store, remaining); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/VanillaBytesStoreHash.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/VanillaBytesStoreHash.java deleted file mode 100644 index dbc6340..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/VanillaBytesStoreHash.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.algo; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.Maths; -import org.jetbrains.annotations.NotNull; - -import java.nio.ByteOrder; - -@SuppressWarnings("rawtypes") -public enum VanillaBytesStoreHash implements BytesStoreHash { - INSTANCE; - - public static final int K0 = 0x6d0f27bd; - public static final int K1 = 0xc1f3bfc9; - public static final int K2 = 0x6b192397; - public static final int K3 = 0x6b915657; - public static final int M0 = 0x5bc80bad; - public static final int M1 = 0xea7585d7; - public static final int M2 = 0x7a646e19; - public static final int M3 = 0x855dd4db; - private static final int HI_BYTES = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN ? 4 : 0; - - public static long agitate(long l) { - l ^= Long.rotateLeft(l, 26); - l ^= Long.rotateRight(l, 17); - return l; - } - - @Override - public long applyAsLong( BytesStore store) { - int remaining = Maths.toInt32(store.readRemaining()); - return applyAsLong(store, remaining); - } - - @Override - public long applyAsLong(BytesStore bytes, long length) { - long start = bytes.readPosition(); - if (length <= 8) { - long l = bytes.readIncompleteLong(start); - return agitate(l * K0 + (l >> 32) * K1); - } - // use two hashes so that when they are combined the 64-bit hash is more random. - long h0 = length * K0; - long h1 = 0, h2 = 0, h3 = 0; - int i; - // optimise chunks of 32 bytes but this is the same as the next loop. - for (i = 0; i < length - 31; i += 32) { - if (i > 0) { - h0 *= K0; - h1 *= K1; - h2 *= K2; - h3 *= K3; - } - long addrI = start + i; - long l0 = bytes.readLong(addrI); - int l0a = bytes.readInt(addrI + HI_BYTES); - long l1 = bytes.readLong(addrI + 8); - int l1a = bytes.readInt(addrI + 8 + HI_BYTES); - long l2 = bytes.readLong(addrI + 16); - int l2a = bytes.readInt(addrI + 16 + HI_BYTES); - long l3 = bytes.readLong(addrI + 24); - int l3a = bytes.readInt(addrI + 24 + HI_BYTES); - - h0 += (l0 + l1a - l2a) * M0; - h1 += (l1 + l2a - l3a) * M1; - h2 += (l2 + l3a - l0a) * M2; - h3 += (l3 + l0a - l1a) * M3; - } - - // perform a hash of the end. - long left = length - i; - if (left > 0) { - if (i > 0) { - h0 *= K0; - h1 *= K1; - h2 *= K2; - h3 *= K3; - } - - long addrI = start + i; - long l0 = bytes.readIncompleteLong(addrI); - int l0a = (int) (l0 >> 32); - long l1 = bytes.readIncompleteLong(addrI + 8); - int l1a = (int) (l1 >> 32); - long l2 = bytes.readIncompleteLong(addrI + 16); - int l2a = (int) (l2 >> 32); - long l3 = bytes.readIncompleteLong(addrI + 24); - int l3a = (int) (l3 >> 32); - - h0 += (l0 + l1a - l2a) * M0; - h1 += (l1 + l2a - l3a) * M1; - h2 += (l2 + l3a - l0a) * M2; - h3 += (l3 + l0a - l1a) * M3; - } - return agitate(h0) ^ agitate(h1) - ^ agitate(h2) ^ agitate(h3); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/XxHash.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/XxHash.java deleted file mode 100644 index 6b19d51..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/algo/XxHash.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.algo; - -import net.openhft.chronicle.bytes.BytesStore; - -// Migration of XxHash from Zero-Allocation-Hashing -@SuppressWarnings("rawtypes") -public class XxHash implements BytesStoreHash { - // Primes if treated as unsigned - private static final long P1 = -7046029288634856825L; - private static final long P2 = -4417276706812531889L; - private static final long P3 = 1609587929392839161L; - private static final long P4 = -8796714831421723037L; - public static final XxHash INSTANCE = new XxHash(P4); - private static final long P5 = 2870177450012600261L; - private final long seed; - - public XxHash(long seed) { - this.seed = seed; - } - - private static long finalize(long hash) { - hash ^= hash >>> 33; - hash *= P2; - hash ^= hash >>> 29; - hash *= P3; - hash ^= hash >>> 32; - return hash; - } - - long fetch64(BytesStore bytes, long off) { - return bytes.readLong(off); - } - - // long because of unsigned nature of original algorithm - long fetch32(BytesStore bytes, long off) { - return bytes.readUnsignedInt(off); - } - - // int because of unsigned nature of original algorithm - long fetch8(BytesStore bytes, long off) { - return bytes.readUnsignedByte(off); - } - - @Override - public long applyAsLong(BytesStore bytes) { - return applyAsLong(bytes, bytes.readRemaining()); - } - - @Override - public long applyAsLong(BytesStore bytes, long length) { - long hash; - long remaining = length; - long off = bytes.readPosition(); - - if (remaining >= 32) { - long v1 = seed + P1 + P2; - long v2 = seed + P2; - long v3 = seed; - long v4 = seed - P1; - - do { - v1 += fetch64(bytes, off) * P2; - v1 = Long.rotateLeft(v1, 31); - v1 *= P1; - - v2 += fetch64(bytes, off + 8) * P2; - v2 = Long.rotateLeft(v2, 31); - v2 *= P1; - - v3 += fetch64(bytes, off + 16) * P2; - v3 = Long.rotateLeft(v3, 31); - v3 *= P1; - - v4 += fetch64(bytes, off + 24) * P2; - v4 = Long.rotateLeft(v4, 31); - v4 *= P1; - - off += 32; - remaining -= 32; - } while (remaining >= 32); - - hash = Long.rotateLeft(v1, 1) - + Long.rotateLeft(v2, 7) - + Long.rotateLeft(v3, 12) - + Long.rotateLeft(v4, 18); - - v1 *= P2; - v1 = Long.rotateLeft(v1, 31); - v1 *= P1; - hash ^= v1; - hash = hash * P1 + P4; - - v2 *= P2; - v2 = Long.rotateLeft(v2, 31); - v2 *= P1; - hash ^= v2; - hash = hash * P1 + P4; - - v3 *= P2; - v3 = Long.rotateLeft(v3, 31); - v3 *= P1; - hash ^= v3; - hash = hash * P1 + P4; - - v4 *= P2; - v4 = Long.rotateLeft(v4, 31); - v4 *= P1; - hash ^= v4; - hash = hash * P1 + P4; - } else { - hash = seed + P5; - } - - hash += length; - - while (remaining >= 8) { - long k1 = fetch64(bytes, off); - k1 *= P2; - k1 = Long.rotateLeft(k1, 31); - k1 *= P1; - hash ^= k1; - hash = Long.rotateLeft(hash, 27) * P1 + P4; - off += 8; - remaining -= 8; - } - - if (remaining >= 4) { - hash ^= fetch32(bytes, off) * P1; - hash = Long.rotateLeft(hash, 23) * P2 + P3; - off += 4; - remaining -= 4; - } - - while (remaining != 0) { - hash ^= fetch8(bytes, off) * P5; - hash = Long.rotateLeft(hash, 11) * P1; - --remaining; - ++off; - } - - return finalize(hash); - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/internal/package-info.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/internal/package-info.java deleted file mode 100644 index 71ed9d4..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/internal/package-info.java +++ /dev/null @@ -1,16 +0,0 @@ -/** - * This package and any and all sub-packages contains strictly internal classes for this Chronicle library. - * Internal classes shall never be used directly. - *

- * Specifically, the following actions (including, but not limited to) are not allowed - * on internal classes and packages: - *

    - *
  • Casting to
  • - *
  • Reflection of any kind
  • - *
  • Explicit Serialize/deserialize
  • - *
- *

- * The classes in this package and any sub-package are subject to - * changes at any time for any reason. - */ -package net.openhft.chronicle.bytes.internal; diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/pool/BytesPool.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/pool/BytesPool.java deleted file mode 100644 index 6884858..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/pool/BytesPool.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.pool; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.core.io.IOTools; -import org.jetbrains.annotations.NotNull; - -import java.nio.ByteBuffer; - -@SuppressWarnings("rawtypes") -public class BytesPool { - final ThreadLocal bytesTL = new ThreadLocal<>(); - - public Bytes acquireBytes() { - Bytes bytes = bytesTL.get(); - if (bytes == null) { - bytesTL.set(bytes = createBytes()); - } else { - bytes.clear(); - } - return bytes; - } - - - protected Bytes createBytes() { - // use heap buffer as we never know when a thread will die and not release this resource. - Bytes bbb = Bytes.elasticHeapByteBuffer(256); - IOTools.unmonitor(bbb); - return bbb; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/AbstractReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/AbstractReference.java deleted file mode 100644 index 69b3449..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/AbstractReference.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Byteable; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.io.AbstractCloseable; -import net.openhft.chronicle.core.io.Closeable; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -@SuppressWarnings("rawtypes") -public abstract class AbstractReference extends AbstractCloseable implements Byteable, Closeable { - - - protected BytesStore bytes; - protected long offset; - - @Override - public void bytesStore( final BytesStore bytes, final long offset, final long length) throws IllegalStateException, IllegalArgumentException, BufferOverflowException, BufferUnderflowException { - throwExceptionIfClosedInSetter(); - - acceptNewBytesStore(bytes); - this.offset = offset; - } - - - @Override - public BytesStore bytesStore() { - return bytes; - } - - @Override - public long offset() { - return offset; - } - - @Override - public abstract long maxSize(); - - protected void acceptNewBytesStore( final BytesStore bytes) { - if (this.bytes != null) { - this.bytes.release(this); - } - this.bytes = bytes.bytesStore(); - this.bytes.reserve(this); - } - - @Override - protected void performClose() { - if (this.bytes != null) { - this.bytes.release(this); - this.bytes = null; - } - } - - public long address() { - throwExceptionIfClosed(); - - return bytesStore().addressForRead(offset); - } - -/* TODO FIX - @Override - protected void finalize() throws Throwable { - warnIfNotClosed(); - super.finalize(); - }*/ - - @Override - protected boolean threadSafetyCheck(boolean isUsed) { - // References are thread safe - return true; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryBooleanReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryBooleanReference.java deleted file mode 100644 index 209914e..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryBooleanReference.java +++ /dev/null @@ -1,51 +0,0 @@ -package net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.values.BooleanValue; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -public class BinaryBooleanReference extends AbstractReference implements BooleanValue { - - private static final byte FALSE = (byte) 0xB0; - private static final byte TRUE = (byte) 0xB1; - - @SuppressWarnings("rawtypes") - @Override - public void bytesStore( final BytesStore bytes, final long offset, final long length) throws IllegalStateException, IllegalArgumentException, BufferOverflowException, BufferUnderflowException { - throwExceptionIfClosedInSetter(); - - if (length != maxSize()) - throw new IllegalArgumentException(); - - super.bytesStore(bytes, offset, length); - } - - @Override - public long maxSize() { - return 1; - } - - @Override - public boolean getValue() { - throwExceptionIfClosed(); - - byte b = bytes.readByte(offset); - if (b == FALSE) - return false; - if (b == TRUE) - return true; - - throw new IllegalStateException("unexpected code=" + b); - - } - - @Override - public void setValue(final boolean flag) { - throwExceptionIfClosed(); - - bytes.writeByte(offset, flag ? TRUE : FALSE); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryIntArrayReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryIntArrayReference.java deleted file mode 100644 index e9b4190..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryIntArrayReference.java +++ /dev/null @@ -1,282 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.*; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.values.IntValue; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.lang.ref.WeakReference; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.util.Collections; -import java.util.IdentityHashMap; -import java.util.Set; - -import static net.openhft.chronicle.bytes.ref.BinaryIntReference.INT_NOT_COMPLETE; - -/** - * This class acts a Binary array of 64-bit values. c.f. TextLongArrayReference - */ -@SuppressWarnings("rawtypes") -public class BinaryIntArrayReference extends AbstractReference implements ByteableIntArrayValues, BytesMarshallable { - public static final int SHIFT = 2; - private static final long CAPACITY = 0; - private static final long USED = CAPACITY + Long.BYTES; - private static final long VALUES = USED + Long.BYTES; - private static final int MAX_TO_STRING = 1024; - - private static Set> binaryIntArrayReferences = null; - private long length; - - public BinaryIntArrayReference() { - this(0); - } - - public BinaryIntArrayReference(long defaultCapacity) { - this.length = (defaultCapacity << SHIFT) + VALUES; - } - - public static void startCollecting() { - binaryIntArrayReferences = Collections.newSetFromMap(new IdentityHashMap<>()); - } - - public static void forceAllToNotCompleteState() { - binaryIntArrayReferences.forEach(x -> { - BinaryIntArrayReference binaryLongReference = x.get(); - if (binaryLongReference != null) { - binaryLongReference.setValueAt(0, INT_NOT_COMPLETE); - } - }); - binaryIntArrayReferences = null; - } - - public static void write( Bytes bytes, long capacity) throws BufferOverflowException, IllegalArgumentException { - assert (bytes.writePosition() & 0x7) == 0; - - bytes.writeLong(capacity); - bytes.writeLong(0L); // used - long start = bytes.writePosition(); - bytes.zeroOut(start, start + (capacity << SHIFT)); - bytes.writeSkip(capacity << SHIFT); - } - - public static void lazyWrite( Bytes bytes, long capacity) throws BufferOverflowException { - assert (bytes.writePosition() & 0x7) == 0; - - bytes.writeLong(capacity); - bytes.writeLong(0L); // used - bytes.writeSkip(capacity << SHIFT); - } - - public static long peakLength( BytesStore bytes, long offset) throws BufferUnderflowException { - final long capacity = bytes.readLong(offset + CAPACITY); - assert capacity > 0 : "capacity too small " + capacity; - return (capacity << SHIFT) + VALUES; - } - - @Override - public long getCapacity() { - throwExceptionIfClosed(); - - return (length - VALUES) >>> SHIFT; - } - - @Override - public long getUsed() { - throwExceptionIfClosed(); - - return bytes.readVolatileInt(offset + USED); - } - - @Override - public void setMaxUsed(long usedAtLeast) { - throwExceptionIfClosedInSetter(); - - bytes.writeMaxLong(offset + USED, usedAtLeast); - } - - @Override - public int getValueAt(long index) throws BufferUnderflowException { - throwExceptionIfClosed(); - - return bytes.readInt(VALUES + offset + (index << SHIFT)); - } - - @Override - public void setValueAt(long index, int value) throws IllegalArgumentException, BufferOverflowException { - throwExceptionIfClosedInSetter(); - - bytes.writeInt(VALUES + offset + (index << SHIFT), value); - } - - @Override - public int getVolatileValueAt(long index) throws BufferUnderflowException { - throwExceptionIfClosed(); - - return bytes.readVolatileInt(VALUES + offset + (index << SHIFT)); - } - - @Override - public void bindValueAt(long index, IntValue value) { - throwExceptionIfClosed(); - - ((BinaryIntReference) value).bytesStore(bytes, VALUES + offset + (index << SHIFT), 8); - } - - @Override - public void setOrderedValueAt(long index, int value) throws IllegalArgumentException, BufferOverflowException { - throwExceptionIfClosedInSetter(); - - bytes.writeOrderedInt(VALUES + offset + (index << SHIFT), value); - } - - @Override - public void bytesStore( BytesStore bytes, long offset, long length) throws BufferUnderflowException, IllegalArgumentException { - throwExceptionIfClosed(); - - if (length != peakLength(bytes, offset)) - throw new IllegalArgumentException(length + " != " + peakLength(bytes, offset)); - - assert (offset & 7) == 0 : "offset=" + offset; - super.bytesStore(bytes, (offset + 7) & ~7, length); - this.length = length; - } - - @Override - public void readMarshallable(BytesIn bytes) throws IORuntimeException { - throwExceptionIfClosedInSetter(); - - long position = bytes.readPosition(); - long capacity = bytes.readLong(); - long used = bytes.readLong(); - if (capacity < 0 || capacity > bytes.readRemaining() >> SHIFT) - throw new IORuntimeException("Corrupt used capacity"); - - if (used < 0 || used > capacity) - throw new IORuntimeException("Corrupt used value"); - - bytes.readSkip(capacity << SHIFT); - long length = bytes.readPosition() - position; - bytesStore(((Bytes) bytes).bytesStore(), position, length); - } - - @Override - public void writeMarshallable(BytesOut bytes) { - BytesStore bytesStore = bytesStore(); - if (bytesStore == null) { - long capacity = getCapacity(); - bytes.writeLong(capacity); - bytes.writeLong(0); - bytes.writeSkip(capacity << SHIFT); - } else { - bytes.write(bytesStore, offset, length); - } - } - - @Override - public boolean isNull() { - throwExceptionIfClosed(); - - return bytes == null; - } - - @Override - public void reset() { - throwExceptionIfClosedInSetter(); - - bytes = null; - offset = 0; - length = 0; - } - - - @Override - public BytesStore bytesStore() { - return bytes; - } - - @Override - public long offset() { - return offset; - } - - @Override - public long maxSize() { - return length; - } - - - public String toString() { - if (bytes == null) - return "not set"; - StringBuilder sb = new StringBuilder(); - sb.append("used: "); - long used = getUsed(); - sb.append(used); - sb.append(", value: "); - String sep = ""; - try { - int i, max = (int) Math.min(used, Math.min(getCapacity(), MAX_TO_STRING)); - for (i = 0; i < max; i++) { - long valueAt = getValueAt(i); - sb.append(sep).append(valueAt); - sep = ", "; - } - if (i < getCapacity()) - sb.append(" ..."); - - } catch (BufferUnderflowException e) { - sb.append(" ").append(e); - } - return sb.toString(); - } - - @Override - public long sizeInBytes(long capacity) { - throwExceptionIfClosed(); - - return (capacity << SHIFT) + VALUES; - } - - @Override - public ByteableIntArrayValues capacity(long arrayLength) { - throwExceptionIfClosedInSetter(); - - BytesStore bytesStore = bytesStore(); - long length = sizeInBytes(arrayLength); - if (bytesStore == null) { - this.length = length; - } else { - assert this.length == length; - } - return this; - } - - @Override - public boolean compareAndSet(long index, int expected, int value) throws IllegalArgumentException, BufferOverflowException { - throwExceptionIfClosed(); - - if (value == INT_NOT_COMPLETE && binaryIntArrayReferences != null) - binaryIntArrayReferences.add(new WeakReference<>(this)); - return bytes.compareAndSwapInt(VALUES + offset + (index << SHIFT), expected, value); - } -} - diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryIntReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryIntReference.java deleted file mode 100644 index 8e82d1c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryIntReference.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.values.IntValue; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -/** - * This class acts as a Binary 32-bit in values. c.f. TextIntReference - */ -public class BinaryIntReference extends AbstractReference implements IntValue { - public static final int INT_NOT_COMPLETE = Integer.MIN_VALUE; - - @SuppressWarnings("rawtypes") - @Override - public void bytesStore( final BytesStore bytes, final long offset, final long length) throws IllegalStateException, IllegalArgumentException, BufferOverflowException, BufferUnderflowException { - throwExceptionIfClosedInSetter(); - - if (length != maxSize()) - throw new IllegalArgumentException(); - super.bytesStore(bytes, offset, length); - } - - @Override - public long maxSize() { - return 4; - } - - - public String toString() { - return bytes == null ? "bytes is null" : "value: " + getValue(); - } - - @Override - public int getValue() { - throwExceptionIfClosed(); - - return bytes == null ? 0 : bytes.readInt(offset); - } - - @Override - public void setValue(int value) { - throwExceptionIfClosedInSetter(); - - bytes.writeInt(offset, value); - } - - @Override - public int getVolatileValue() { - throwExceptionIfClosed(); - - return bytes.readVolatileInt(offset); - } - - @Override - public void setOrderedValue(int value) { - throwExceptionIfClosedInSetter(); - - bytes.writeOrderedInt(offset, value); - } - - @Override - public int addValue(int delta) { - throwExceptionIfClosed(); - - return bytes.addAndGetInt(offset, delta); - } - - @Override - public int addAtomicValue(int delta) { - throwExceptionIfClosed(); - - return addValue(delta); - } - - @Override - public boolean compareAndSwapValue(int expected, int value) { - throwExceptionIfClosed(); - - return bytes.compareAndSwapInt(offset, expected, value); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryLongArrayReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryLongArrayReference.java deleted file mode 100644 index 4a421d8..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryLongArrayReference.java +++ /dev/null @@ -1,282 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.*; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.values.LongValue; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.lang.ref.WeakReference; -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.util.Collections; -import java.util.IdentityHashMap; -import java.util.Set; - -import static net.openhft.chronicle.bytes.ref.BinaryLongReference.LONG_NOT_COMPLETE; - -/** - * This class acts a Binary array of 64-bit values. c.f. TextLongArrayReference - */ -@SuppressWarnings("rawtypes") -public class BinaryLongArrayReference extends AbstractReference implements ByteableLongArrayValues, BytesMarshallable { - public static final int SHIFT = 3; - private static final long CAPACITY = 0; - private static final long USED = CAPACITY + Long.BYTES; - private static final long VALUES = USED + Long.BYTES; - private static final int MAX_TO_STRING = 1024; - - private static Set> binaryLongArrayReferences = null; - private long length; - - public BinaryLongArrayReference() { - this(0); - } - - public BinaryLongArrayReference(long defaultCapacity) { - this.length = (defaultCapacity << SHIFT) + VALUES; - } - - public static void startCollecting() { - binaryLongArrayReferences = Collections.newSetFromMap(new IdentityHashMap<>()); - } - - public static void forceAllToNotCompleteState() { - binaryLongArrayReferences.forEach(x -> { - BinaryLongArrayReference binaryLongReference = x.get(); - if (binaryLongReference != null) { - binaryLongReference.setValueAt(0, LONG_NOT_COMPLETE); - } - }); - binaryLongArrayReferences = null; - } - - public static void write( Bytes bytes, long capacity) throws BufferOverflowException, IllegalArgumentException { - assert (bytes.writePosition() & 0x7) == 0; - - bytes.writeLong(capacity); - bytes.writeLong(0L); // used - long start = bytes.writePosition(); - bytes.zeroOut(start, start + (capacity << SHIFT)); - bytes.writeSkip(capacity << SHIFT); - } - - public static void lazyWrite( Bytes bytes, long capacity) throws BufferOverflowException { - assert (bytes.writePosition() & 0x7) == 0; - - bytes.writeLong(capacity); - bytes.writeLong(0L); // used - bytes.writeSkip(capacity << SHIFT); - } - - public static long peakLength( BytesStore bytes, long offset) throws BufferUnderflowException { - final long capacity = bytes.readLong(offset + CAPACITY); - assert capacity > 0 : "capacity too small " + capacity; - return (capacity << SHIFT) + VALUES; - } - - @Override - public long getCapacity() { - throwExceptionIfClosed(); - - return (length - VALUES) >>> SHIFT; - } - - @Override - public long getUsed() { - throwExceptionIfClosed(); - - return bytes.readVolatileLong(offset + USED); - } - - @Override - public void setMaxUsed(long usedAtLeast) { - throwExceptionIfClosedInSetter(); - - bytes.writeMaxLong(offset + USED, usedAtLeast); - } - - @Override - public long getValueAt(long index) throws BufferUnderflowException { - throwExceptionIfClosed(); - - return bytes.readLong(VALUES + offset + (index << SHIFT)); - } - - @Override - public void setValueAt(long index, long value) throws IllegalArgumentException, BufferOverflowException { - throwExceptionIfClosedInSetter(); - - bytes.writeLong(VALUES + offset + (index << SHIFT), value); - } - - @Override - public long getVolatileValueAt(long index) throws BufferUnderflowException { - throwExceptionIfClosed(); - - return bytes.readVolatileLong(VALUES + offset + (index << SHIFT)); - } - - @Override - public void bindValueAt(long index, LongValue value) { - throwExceptionIfClosed(); - - ((BinaryLongReference) value).bytesStore(bytes, VALUES + offset + (index << SHIFT), 8); - } - - @Override - public void setOrderedValueAt(long index, long value) throws IllegalArgumentException, BufferOverflowException { - throwExceptionIfClosedInSetter(); - - bytes.writeOrderedLong(VALUES + offset + (index << SHIFT), value); - } - - @Override - public void bytesStore( BytesStore bytes, long offset, long length) throws BufferUnderflowException, IllegalArgumentException { - throwExceptionIfClosed(); - - if (length != peakLength(bytes, offset)) - throw new IllegalArgumentException(length + " != " + peakLength(bytes, offset)); - - assert (offset & 7) == 0 : "offset=" + offset; - super.bytesStore(bytes, (offset + 7) & ~7, length); - this.length = length; - } - - @Override - public void readMarshallable(BytesIn bytes) throws IORuntimeException { - throwExceptionIfClosedInSetter(); - - long position = bytes.readPosition(); - long capacity = bytes.readLong(); - long used = bytes.readLong(); - if (capacity < 0 || capacity > bytes.readRemaining() >> SHIFT) - throw new IORuntimeException("Corrupt used capacity"); - - if (used < 0 || used > capacity) - throw new IORuntimeException("Corrupt used value"); - - bytes.readSkip(capacity << SHIFT); - long length = bytes.readPosition() - position; - bytesStore(((Bytes) bytes).bytesStore(), position, length); - } - - @Override - public void writeMarshallable(BytesOut bytes) { - BytesStore bytesStore = bytesStore(); - if (bytesStore == null) { - long capacity = getCapacity(); - bytes.writeLong(capacity); - bytes.writeLong(0); - bytes.writeSkip(capacity << SHIFT); - } else { - bytes.write(bytesStore, offset, length); - } - } - - @Override - public boolean isNull() { - throwExceptionIfClosed(); - - return bytes == null; - } - - @Override - public void reset() { - throwExceptionIfClosedInSetter(); - - bytes = null; - offset = 0; - length = 0; - } - - - @Override - public BytesStore bytesStore() { - return bytes; - } - - @Override - public long offset() { - return offset; - } - - @Override - public long maxSize() { - return length; - } - - - public String toString() { - if (bytes == null) - return "not set"; - StringBuilder sb = new StringBuilder(); - sb.append("used: "); - long used = getUsed(); - sb.append(used); - sb.append(", value: "); - String sep = ""; - try { - int i, max = (int) Math.min(used, Math.min(getCapacity(), MAX_TO_STRING)); - for (i = 0; i < max; i++) { - long valueAt = getValueAt(i); - sb.append(sep).append(valueAt); - sep = ", "; - } - if (i < getCapacity()) - sb.append(" ..."); - - } catch (BufferUnderflowException e) { - sb.append(" ").append(e); - } - return sb.toString(); - } - - @Override - public long sizeInBytes(long capacity) { - throwExceptionIfClosed(); - - return (capacity << SHIFT) + VALUES; - } - - @Override - public ByteableLongArrayValues capacity(long arrayLength) { - throwExceptionIfClosedInSetter(); - - BytesStore bytesStore = bytesStore(); - long length = sizeInBytes(arrayLength); - if (bytesStore == null) { - this.length = length; - } else { - assert this.length == length; - } - return this; - } - - @Override - public boolean compareAndSet(long index, long expected, long value) throws IllegalArgumentException, BufferOverflowException { - throwExceptionIfClosed(); - - if (value == LONG_NOT_COMPLETE && binaryLongArrayReferences != null) - binaryLongArrayReferences.add(new WeakReference<>(this)); - return bytes.compareAndSwapLong(VALUES + offset + (index << SHIFT), expected, value); - } -} - diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryLongReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryLongReference.java deleted file mode 100644 index 7208b47..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryLongReference.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesStore; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; - -public class BinaryLongReference extends AbstractReference implements LongReference { - public static final long LONG_NOT_COMPLETE = -1; - - @SuppressWarnings("rawtypes") - @Override - public void bytesStore( final BytesStore bytes, final long offset, final long length) throws IllegalStateException, IllegalArgumentException, BufferOverflowException, BufferUnderflowException { - throwExceptionIfClosed(); - - if (length != maxSize()) - throw new IllegalArgumentException(); - - super.bytesStore(bytes, offset, length); - } - - @Override - public long maxSize() { - return 8; - } - - - public String toString() { - return bytes == null ? "bytes is null" : "value: " + getValue(); - } - - @Override - public long getValue() { - return bytes == null ? 0L : bytes.readLong(offset); - } - - @Override - public void setValue(long value) { - try { - bytes.writeLong(offset, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - - throw e; - } - } - - @Override - public long getVolatileValue() { - try { - return bytes.readVolatileLong(offset); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - - throw e; - } - } - - @Override - public void setVolatileValue(long value) { - try { - bytes.writeVolatileLong(offset, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - - throw e; - } - } - - @Override - public long getVolatileValue(long closedValue) { - if (isClosed()) - return closedValue; - try { - return getVolatileValue(); - } catch (Exception e) { - return closedValue; - } - } - - @Override - public void setOrderedValue(long value) { - try { - bytes.writeOrderedLong(offset, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - - throw e; - } - } - - @Override - public long addValue(long delta) { - try { - return bytes.addAndGetLong(offset, delta); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - - throw e; - } - } - - @Override - public long addAtomicValue(long delta) { - return addValue(delta); - } - - @Override - public boolean compareAndSwapValue(long expected, long value) { - try { - return bytes.compareAndSwapLong(offset, expected, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - - throw e; - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryTwoLongReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryTwoLongReference.java deleted file mode 100644 index 74f2311..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/BinaryTwoLongReference.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import org.jetbrains.annotations.NotNull; - -public class BinaryTwoLongReference extends BinaryLongReference implements TwoLongReference { - @Override - public long maxSize() { - return 2 * 8; - } - - - public String toString() { - return bytes == null ? "bytes is null" : "value: " + getValue() + ", value2: " + getValue2(); - } - - @Override - public long getValue2() { - try { - return bytes.readLong(offset + 8); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public void setValue2(long value) { - try { - bytes.writeLong(offset + 8, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public long getVolatileValue2() { - try { - return bytes.readVolatileLong(offset + 8); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - - } - - @Override - public void setVolatileValue2(long value) { - try { - bytes.writeVolatileLong(offset + 8, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public void setOrderedValue2(long value) { - try { - bytes.writeOrderedLong(offset + 8, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public long addValue2(long delta) { - try { - return bytes.addAndGetLong(offset + 8, delta); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public long addAtomicValue2(long delta) { - try { - return addValue2(delta); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public boolean compareAndSwapValue2(long expected, long value) { - try { - return bytes.compareAndSwapLong(offset + 8, expected, value); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/ByteableIntArrayValues.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/ByteableIntArrayValues.java deleted file mode 100644 index c51ba0a..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/ByteableIntArrayValues.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Byteable; -import net.openhft.chronicle.bytes.DynamicallySized; -import net.openhft.chronicle.core.values.IntArrayValues; - -@SuppressWarnings("rawtypes") -public interface ByteableIntArrayValues extends IntArrayValues, Byteable, DynamicallySized { - @Override - long sizeInBytes(long sizeInBytes); - - ByteableIntArrayValues capacity(long arrayLength); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/ByteableLongArrayValues.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/ByteableLongArrayValues.java deleted file mode 100644 index 1a9a6c2..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/ByteableLongArrayValues.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Byteable; -import net.openhft.chronicle.bytes.DynamicallySized; -import net.openhft.chronicle.core.values.LongArrayValues; - -@SuppressWarnings("rawtypes") -public interface ByteableLongArrayValues extends LongArrayValues, Byteable, DynamicallySized { - @Override - long sizeInBytes(long sizeInBytes); - - ByteableLongArrayValues capacity(long arrayLength); -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/LongReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/LongReference.java deleted file mode 100644 index 2bc9d93..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/LongReference.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Byteable; -import net.openhft.chronicle.core.values.LongValue; - -@SuppressWarnings("rawtypes") -public interface LongReference extends LongValue, Byteable { -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextBooleanReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextBooleanReference.java deleted file mode 100644 index f2806e2..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextBooleanReference.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.values.BooleanValue; -import org.jetbrains.annotations.NotNull; - -/** - * Implementation of a reference to a 32-bit in in text wire format. - */ -public class TextBooleanReference extends AbstractReference implements BooleanValue { - - private static final int FALSE = 'f' | ('a' << 8) | ('l' << 16) | ('s' << 24); - private static final int TRUE = ' ' | ('t' << 8) | ('r' << 16) | ('u' << 24); - - @SuppressWarnings("rawtypes") - public static void write(final boolean value, final BytesStore bytes, long offset) { - bytes.writeVolatileInt(offset, value ? TRUE : FALSE); - bytes.writeByte(offset + 4, (byte) 'e'); - } - - @Override - public long maxSize() { - return 5; - } - - - public String toString() { - return "value: " + getValue(); - } - - @Override - public boolean getValue() { - throwExceptionIfClosed(); - - return bytes.readVolatileInt(offset) == TRUE; - } - - @Override - public void setValue(final boolean value) { - throwExceptionIfClosedInSetter(); - - write(value, bytes, offset); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextIntArrayReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextIntArrayReference.java deleted file mode 100644 index 69f7f51..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextIntArrayReference.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.values.IntValue; -import org.jetbrains.annotations.NotNull; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; - -/* -The format for a long array in text is -{ capacity: 12345678901234567890, values: [ 12345678901234567890, ... ] } - */ -@SuppressWarnings("rawtypes") -public class TextIntArrayReference extends AbstractReference implements ByteableIntArrayValues { - private static final byte[] SECTION1 = "{ locked: false, capacity: ".getBytes(ISO_8859_1); - private static final byte[] SECTION2 = ", used: ".getBytes(ISO_8859_1); - private static final byte[] SECTION3 = ", values: [ ".getBytes(ISO_8859_1); - private static final byte[] SECTION4 = " ] }\n".getBytes(ISO_8859_1); - private static final byte[] ZERO = "0000000000".getBytes(ISO_8859_1); - private static final byte[] SEP = ", ".getBytes(ISO_8859_1); - - private static final int DIGITS = ZERO.length; - private static final int CAPACITY = SECTION1.length; - private static final int USED = CAPACITY + DIGITS + SECTION2.length; - private static final int VALUES = USED + DIGITS + SECTION3.length; - private static final int VALUE_SIZE = DIGITS + SEP.length; - private static final int LOCK_OFFSET = 10; - private static final int FALS = 'f' | ('a' << 8) | ('l' << 16) | ('s' << 24); - private static final int TRU = ' ' | ('t' << 8) | ('r' << 16) | ('u' << 24); - - private long length = VALUES; - - public static void write( Bytes bytes, long capacity) { - long start = bytes.writePosition(); - bytes.write(SECTION1); - bytes.append(capacity); - while (bytes.writePosition() - start < CAPACITY + DIGITS) { - bytes.writeUnsignedByte(' '); - } - bytes.write(SECTION2); - bytes.write(ZERO); - bytes.write(SECTION3); - for (long i = 0; i < capacity; i++) { - if (i > 0) - bytes.appendUtf8(", "); - bytes.write(ZERO); - } - bytes.write(SECTION4); - } - - public static long peakLength( BytesStore bytes, long offset) { - //todo check this, I think there could be a bug here - return (bytes.parseLong(offset + CAPACITY) * VALUE_SIZE) - SEP.length - + VALUES + SECTION4.length; - } - - @Override - public long getUsed() { - try { - return bytes.parseLong(USED + offset); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - private void setUsed(long used) { - try { - bytes.append(VALUES + offset, used, DIGITS); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public void setMaxUsed(long usedAtLeast) { - try { - while (true) { - if (!bytes.compareAndSwapInt(LOCK_OFFSET + offset, FALS, TRU)) - continue; - try { - if (getUsed() < usedAtLeast) { - setUsed(usedAtLeast); - } - return; - } finally { - bytes.writeInt(LOCK_OFFSET + offset, FALS); - } - } - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public long getCapacity() { - return (length - VALUES) / VALUE_SIZE; - } - - @Override - public ByteableIntArrayValues capacity(long arrayLength) { - BytesStore bytesStore = bytesStore(); - long length = sizeInBytes(arrayLength); - if (bytesStore == null) { - this.length = length; - } else { - assert this.length == length; - } - return this; - } - - @Override - public int getValueAt(long index) { - try { - return (int) bytes.parseLong(VALUES + offset + index * VALUE_SIZE); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - - } - - @Override - public void setValueAt(long index, int value) { - try { - bytes.append(VALUES + offset + index * VALUE_SIZE, value, DIGITS); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public void bindValueAt(long index, IntValue value) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public int getVolatileValueAt(long index) { - OS.memory().loadFence(); - return getValueAt(index); - } - - @Override - public void setOrderedValueAt(long index, int value) { - setValueAt(index, value); - OS.memory().storeFence(); - } - - @Override - public boolean compareAndSet(long index, int expected, int value) { - try { - if (!bytes.compareAndSwapInt(LOCK_OFFSET + offset, FALS, TRU)) - return false; - boolean ret = false; - try { - if (getVolatileValueAt(index) == expected) { - setOrderedValueAt(index, value); - ret = true; - } - return ret; - } finally { - bytes.writeInt(LOCK_OFFSET + offset, FALS); - } - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - - } - - @Override - public void bytesStore( final BytesStore bytes, long offset, long length) { - throwExceptionIfClosedInSetter(); - - if (length != peakLength(bytes, offset)) - throw new IllegalArgumentException(length + " != " + peakLength(bytes, offset)); - super.bytesStore(bytes, offset, length); - this.length = length; - } - - @Override - public boolean isNull() { - return bytes == null; - } - - @Override - public void reset() { - throwExceptionIfClosedInSetter(); - - bytes = null; - offset = 0; - length = 0; - } - - @Override - public long maxSize() { - return length; - } - - - public String toString() { - if (bytes == null) { - return "LongArrayTextReference{" + - "bytes=null" + - ", offset=" + offset + - ", length=" + length + - '}'; - } - - return "value: " + getValueAt(0) + " ..."; - } - - @Override - public long sizeInBytes(long capacity) { - return (capacity * VALUE_SIZE) + VALUES + SECTION3.length - SEP.length; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextIntReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextIntReference.java deleted file mode 100644 index 54891f0..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextIntReference.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.values.IntValue; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.util.function.IntSupplier; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.BytesUtil.roundUpTo8ByteAlign; - -/** - * Implementation of a reference to a 32-bit in in text wire format. - */ -public class TextIntReference extends AbstractReference implements IntValue { - private static final byte[] template = "!!atomic { locked: false, value: 0000000000 }".getBytes(ISO_8859_1); - private static final int FALSE = 'f' | ('a' << 8) | ('l' << 16) | ('s' << 24); - private static final int TRUE = ' ' | ('t' << 8) | ('r' << 16) | ('u' << 24); - private static final int UNINITIALIZED = 0; - private static final int INT_TRUE = 1; - private static final int INT_FALSE = 0; - private static final int LOCKED = 20; - private static final int VALUE = 34; - private static final int DIGITS = 10; - - @SuppressWarnings("rawtypes") - public static void write( Bytes bytes, int value) throws BufferOverflowException { - long position = bytes.writePosition(); - bytes.write(template); - try { - bytes.append(position + VALUE, value, DIGITS); - } catch (IllegalArgumentException e) { - throw new AssertionError(e); - } - } - - private int withLock( IntSupplier call) throws BufferUnderflowException { - long alignedOffset = roundUpTo8ByteAlign(offset); - long lockValueOffset = alignedOffset + LOCKED; - int lockValue = bytes.readVolatileInt(lockValueOffset); - if (lockValue != FALSE && lockValue != TRUE) - throw new IllegalStateException(); - try { - while (true) { - if (bytes.compareAndSwapInt(lockValueOffset, FALSE, TRUE)) { - int t = call.getAsInt(); - bytes.writeOrderedInt(lockValueOffset, FALSE); - return t; - } - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } - - @Override - public int getValue() { - throwExceptionIfClosed(); - - return withLock(() -> (int) bytes.parseLong(offset + VALUE)); - } - - @Override - public void setValue(int value) { - throwExceptionIfClosedInSetter(); - - withLock(() -> { - bytes.append(offset + VALUE, value, DIGITS); - return INT_TRUE; - }); - } - - @Override - public int getVolatileValue() { - throwExceptionIfClosed(); - - return getValue(); - } - - @Override - public void setOrderedValue(int value) { - throwExceptionIfClosedInSetter(); - - setValue(value); - } - - @Override - public int addValue(int delta) { - throwExceptionIfClosed(); - - return withLock(() -> { - long value = bytes.parseLong(offset + VALUE) + delta; - bytes.append(offset + VALUE, value, DIGITS); - return (int) value; - }); - } - - @Override - public int addAtomicValue(int delta) { - throwExceptionIfClosed(); - - return addValue(delta); - } - - @Override - public boolean compareAndSwapValue(int expected, int value) { - throwExceptionIfClosed(); - - return withLock(() -> { - if (bytes.parseLong(offset + VALUE) == expected) { - bytes.append(offset + VALUE, value, DIGITS); - return INT_TRUE; - } - return INT_FALSE; - }) == INT_TRUE; - } - - @SuppressWarnings("rawtypes") - @Override - public void bytesStore( final BytesStore bytes, long offset, long length) { - throwExceptionIfClosedInSetter(); - - if (length != template.length) - throw new IllegalArgumentException(length + " != " + template.length); - - // align for ARM - long newOffset = roundUpTo8ByteAlign(offset); - for (long i = offset; i < newOffset; i++) { - bytes.writeByte(i, (byte) ' '); - } - - super.bytesStore(bytes, newOffset, length); - - if (bytes.readInt(newOffset) == UNINITIALIZED) - bytes.write(newOffset, template); - } - - @Override - public long maxSize() { - return template.length; - } - - - public String toString() { - return "value: " + getValue(); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextLongArrayReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextLongArrayReference.java deleted file mode 100644 index 13bae46..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextLongArrayReference.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.values.LongValue; -import org.jetbrains.annotations.NotNull; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; - -/* -The format for a long array in text is -{ capacity: 12345678901234567890, values: [ 12345678901234567890, ... ] } - */ -@SuppressWarnings("rawtypes") -public class TextLongArrayReference extends AbstractReference implements ByteableLongArrayValues { - private static final byte[] SECTION1 = "{ locked: false, capacity: ".getBytes(ISO_8859_1); - private static final byte[] SECTION2 = ", used: ".getBytes(ISO_8859_1); - private static final byte[] SECTION3 = ", values: [ ".getBytes(ISO_8859_1); - private static final byte[] SECTION4 = " ] }\n".getBytes(ISO_8859_1); - private static final byte[] ZERO = "00000000000000000000".getBytes(ISO_8859_1); - private static final byte[] SEP = ", ".getBytes(ISO_8859_1); - - private static final int DIGITS = ZERO.length; - private static final int CAPACITY = SECTION1.length; - private static final int USED = CAPACITY + DIGITS + SECTION2.length; - private static final int VALUES = USED + DIGITS + SECTION3.length; - private static final int VALUE_SIZE = DIGITS + SEP.length; - private static final int LOCK_OFFSET = 10; - private static final int FALS = 'f' | ('a' << 8) | ('l' << 16) | ('s' << 24); - private static final int TRU = ' ' | ('t' << 8) | ('r' << 16) | ('u' << 24); - - private long length = VALUES; - - public static void write( Bytes bytes, long capacity) { - long start = bytes.writePosition(); - bytes.write(SECTION1); - bytes.append(capacity); - while (bytes.writePosition() - start < CAPACITY + DIGITS) { - bytes.writeUnsignedByte(' '); - } - bytes.write(SECTION2); - bytes.write(ZERO); - bytes.write(SECTION3); - for (long i = 0; i < capacity; i++) { - if (i > 0) - bytes.appendUtf8(", "); - bytes.write(ZERO); - } - bytes.write(SECTION4); - } - - public static long peakLength( BytesStore bytes, long offset) { - //todo check this, I think there could be a bug here - return (bytes.parseLong(offset + CAPACITY) * VALUE_SIZE) - SEP.length - + VALUES + SECTION4.length; - } - - @Override - public long getUsed() { - try { - return bytes.parseLong(USED + offset); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - private void setUsed(long used) { - try { - bytes.append(VALUES + offset, used, DIGITS); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public void setMaxUsed(long usedAtLeast) { - try { - while (true) { - if (!bytes.compareAndSwapInt(LOCK_OFFSET + offset, FALS, TRU)) - continue; - try { - if (getUsed() < usedAtLeast) { - setUsed(usedAtLeast); - } - return; - } finally { - bytes.writeInt(LOCK_OFFSET + offset, FALS); - } - } - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public long getCapacity() { - return (length - VALUES) / VALUE_SIZE; - } - - @Override - public ByteableLongArrayValues capacity(long arrayLength) { - BytesStore bytesStore = bytesStore(); - long length = sizeInBytes(arrayLength); - if (bytesStore == null) { - this.length = length; - } else { - assert this.length == length; - } - return this; - } - - @Override - public long getValueAt(long index) { - try { - return bytes.parseLong(VALUES + offset + index * VALUE_SIZE); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - - } - - @Override - public void setValueAt(long index, long value) { - try { - bytes.append(VALUES + offset + index * VALUE_SIZE, value, DIGITS); - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @Override - public void bindValueAt(long index, LongValue value) { - throw new UnsupportedOperationException("todo"); - } - - @Override - public long getVolatileValueAt(long index) { - OS.memory().loadFence(); - return getValueAt(index); - } - - @Override - public void setOrderedValueAt(long index, long value) { - setValueAt(index, value); - OS.memory().storeFence(); - } - - @Override - public boolean compareAndSet(long index, long expected, long value) { - try { - if (!bytes.compareAndSwapInt(LOCK_OFFSET + offset, FALS, TRU)) - return false; - boolean ret = false; - try { - if (getVolatileValueAt(index) == expected) { - setOrderedValueAt(index, value); - ret = true; - } - return ret; - } finally { - bytes.writeInt(LOCK_OFFSET + offset, FALS); - } - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - - } - - @Override - public void bytesStore( final BytesStore bytes, long offset, long length) { - throwExceptionIfClosedInSetter(); - - if (length != peakLength(bytes, offset)) - throw new IllegalArgumentException(length + " != " + peakLength(bytes, offset)); - super.bytesStore(bytes, offset, length); - this.length = length; - } - - @Override - public boolean isNull() { - return bytes == null; - } - - @Override - public void reset() { - throwExceptionIfClosedInSetter(); - - bytes = null; - offset = 0; - length = 0; - } - - @Override - public long maxSize() { - return length; - } - - - public String toString() { - if (bytes == null) { - return "LongArrayTextReference{" + - "bytes=null" + - ", offset=" + offset + - ", length=" + length + - '}'; - } - - return "value: " + getValueAt(0) + " ..."; - } - - @Override - public long sizeInBytes(long capacity) { - return (capacity * VALUE_SIZE) + VALUES + SECTION3.length - SEP.length; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextLongReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextLongReference.java deleted file mode 100644 index c185c06..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TextLongReference.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.bytes.BytesUtil; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.util.function.LongSupplier; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.BytesUtil.roundUpTo8ByteAlign; - -/** - * reference to an array fo 32-bit in values in Text wire format. - */ -public class TextLongReference extends AbstractReference implements LongReference { - static final int VALUE = 34; - private static final byte[] template = "!!atomic { locked: false, value: 00000000000000000000 }".getBytes(ISO_8859_1); - private static final int FALSE = BytesUtil.asInt("fals"); - private static final int TRUE = BytesUtil.asInt(" tru"); - private static final long UNINITIALIZED = 0x0L; - private static final long LONG_TRUE = 1L; - private static final long LONG_FALSE = 0L; - private static final int LOCKED = 20; - private static final int DIGITS = 20; - - @SuppressWarnings("rawtypes") - public static void write( Bytes bytes, long value) throws BufferOverflowException, IllegalArgumentException { - long position = bytes.writePosition(); - bytes.write(template); - bytes.append(position + VALUE, value, DIGITS); - } - - private long withLock( LongSupplier call) throws IllegalStateException, BufferUnderflowException { - try { - long valueOffset = offset + LOCKED; - int value = bytes.readVolatileInt(valueOffset); - if (value != FALSE && value != TRUE) - throw new IllegalStateException("Not a lock value"); - try { - while (true) { - if (bytes.compareAndSwapInt(valueOffset, FALSE, TRUE)) { - long t = call.getAsLong(); - bytes.writeOrderedInt(valueOffset, FALSE); - return t; - } - } - } catch (BufferOverflowException e) { - throw new AssertionError(e); - } - } catch (NullPointerException e) { - throwExceptionIfClosed(); - throw e; - } - } - - @SuppressWarnings("rawtypes") - @Override - public void bytesStore( final BytesStore bytes, long offset, long length) { - if (length != template.length) - throw new IllegalArgumentException(); - - // align for ARM - long newOffset = roundUpTo8ByteAlign(offset); - for (long i = offset; i < newOffset; i++) { - bytes.writeByte(i, (byte) ' '); - } - - super.bytesStore(bytes, newOffset, length); - - if (bytes.readLong(newOffset) == UNINITIALIZED) - bytes.write(newOffset, template); - } - - @Override - public long getValue() { - return withLock(() -> bytes.parseLong(offset + VALUE)); - } - - @Override - public void setValue(long value) { - withLock(() -> { - bytes.append(offset + VALUE, value, DIGITS); - return LONG_TRUE; - }); - } - - @Override - public long getVolatileValue() { - return getValue(); - } - - @Override - public void setVolatileValue(long value) { - setValue(value); - } - - @Override - public long getVolatileValue(long closedValue) { - if (isClosed()) - return closedValue; - try { - return getVolatileValue(); - } catch (Exception e) { - return closedValue; - } - } - - @Override - public long maxSize() { - return template.length; - } - - @Override - public void setOrderedValue(long value) { - setValue(value); - } - - - public String toString() { - return "value: " + getValue(); - } - - @Override - public long addValue(long delta) { - return withLock(() -> { - long value = bytes.parseLong(offset + VALUE) + delta; - bytes.append(offset + VALUE, value, DIGITS); - return value; - }); - } - - @Override - public long addAtomicValue(long delta) { - return addValue(delta); - } - - @Override - public boolean compareAndSwapValue(long expected, long value) { - return withLock(() -> { - if (bytes.parseLong(offset + VALUE) == expected) { - bytes.append(offset + VALUE, value, DIGITS); - return LONG_TRUE; - } - return LONG_FALSE; - }) == LONG_TRUE; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TwoLongReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TwoLongReference.java deleted file mode 100644 index 7c3063d..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/TwoLongReference.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Byteable; -import net.openhft.chronicle.core.values.TwoLongValue; - -@SuppressWarnings("rawtypes") -public interface TwoLongReference extends TwoLongValue, Byteable { -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/UncheckedLongReference.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/UncheckedLongReference.java deleted file mode 100644 index af259bc..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/ref/UncheckedLongReference.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.io.UnsafeCloseable; -import org.jetbrains.annotations.NotNull; - -@SuppressWarnings({"rawtypes", "unchecked"}) -public class UncheckedLongReference extends UnsafeCloseable implements LongReference, ReferenceOwner { - private BytesStore bytes; - - - public static LongReference create(BytesStore bytesStore, long offset, int size) { - LongReference ref = Jvm.isDebug() ? new BinaryLongReference() : new UncheckedLongReference(); - ref.bytesStore(bytesStore, offset, size); - return ref; - } - - @Override - public void bytesStore( BytesStore bytes, long offset, long length) { - throwExceptionIfClosedInSetter(); - - if (length != maxSize()) throw new IllegalArgumentException(); - if (this.bytes != bytes) { - if (this.bytes != null) - this.bytes.release(this); - this.bytes = bytes; - bytes.reserve(this); - } - address(bytes.addressForRead(offset)); - } - - - @Override - public BytesStore bytesStore() { - return bytes; - } - - @Override - public long offset() { - return address; - } - - @Override - public long maxSize() { - return 8; - } - - - public String toString() { - return address == 0 ? "addressForRead is 0" : "value: " + getValue(); - } - - @Override - public long getValue() { - return getLong(); - } - - @Override - public void setValue(long value) { - setLong(value); - } - - @Override - public long getVolatileValue() { - return getVolatileLong(); - } - - @Override - public void setVolatileValue(long value) { - setVolatileLong(value); - } - - @Override - public long getVolatileValue(long closedValue) { - return getVolatileLong(closedValue); - } - - @Override - public void setOrderedValue(long value) { - setOrderedLong(value); - } - - @Override - public long addValue(long delta) { - return addLong(delta); - } - - @Override - public long addAtomicValue(long delta) { - return addAtomicLong(delta); - } - - @Override - public boolean compareAndSwapValue(long expected, long value) { - return compareAndSwapLong(expected, value); - } - - @Override - protected void performClose() { - if (this.bytes != null) - this.bytes.release(this); - this.bytes = null; - super.performClose(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/AbstractInterner.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/AbstractInterner.java deleted file mode 100644 index 8ad010e..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/AbstractInterner.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.IOTools; -import org.jetbrains.annotations.NotNull; - -import java.nio.BufferUnderflowException; -import java.util.Objects; -import java.util.stream.Stream; - -/** - * This cache only gaurentees it will provide a String which matches the decoded bytes. - *

- * It doesn't guantee it will always return the same object, - * nor that different threads will return the same object, - * though the contents should always be the same. - *

- * While not technically thread safe, it should still behave correctly. - */ -@SuppressWarnings({"rawtypes", "unchecked"}) -public abstract class AbstractInterner { - - protected final InternerEntry[] entries; - protected final int mask, shift; - protected boolean toggle = false; - - public AbstractInterner(int capacity) throws IllegalArgumentException { - int n = Maths.nextPower2(capacity, 128); - shift = Maths.intLog2(n); - entries = new InternerEntry[n]; - mask = n - 1; - } - - private static int hash32( BytesStore bs, int length) { - return bs.fastHash(bs.readPosition(), length); - } - - public T intern( Bytes cs) - throws IllegalArgumentException, IORuntimeException, BufferUnderflowException { - return intern((BytesStore) cs, (int) cs.readRemaining()); - } - - public T intern( BytesStore cs) - throws IllegalArgumentException, IORuntimeException, BufferUnderflowException { - return intern(cs, (int) cs.readRemaining()); - } - - public T intern( Bytes cs, int length) - throws IllegalArgumentException, IORuntimeException, BufferUnderflowException { - return intern((BytesStore) cs, length); - } - - public T intern( BytesStore cs, int length) - throws IllegalArgumentException, IORuntimeException, BufferUnderflowException { - if (length > entries.length) - return getValue(cs, length); - // TODO This needs to be reviewd. -// UnsafeMemory.UNSAFE.loadFence(); - int hash = hash32(cs, length); - int h = hash & mask; - InternerEntry s = entries[h]; - if (s != null && s.bytes.length() == length && s.bytes.equalBytes(cs, length)) - return s.t; - int h2 = (hash >> shift) & mask; - InternerEntry s2 = entries[h2]; - if (s2 != null && s2.bytes.length() == length && s2.bytes.equalBytes(cs, length)) - return s2.t; - T t = getValue(cs, length); - final byte[] bytes = new byte[length]; - BytesStore bs = BytesStore.wrap(bytes); - IOTools.unmonitor(bs); - cs.read(cs.readPosition(), bytes, 0, length); - entries[s == null || (s2 != null && toggle()) ? h : h2] = new InternerEntry<>(bs, t); -// UnsafeMemory.UNSAFE.storeFence(); - return t; - } - - - protected abstract T getValue(BytesStore bs, int length) throws IORuntimeException; - - protected boolean toggle() { - return toggle = !toggle; - } - - public int valueCount() { - return (int) Stream.of(entries).filter(Objects::nonNull).count(); - } - - static class InternerEntry { - final BytesStore bytes; - final T t; - - InternerEntry(BytesStore bytes, T t) { - this.bytes = bytes; - this.t = t; - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/BigCopyMain.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/BigCopyMain.java deleted file mode 100644 index 8e598a2..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/BigCopyMain.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; -import java.util.Random; - -public class BigCopyMain { - - public static void main(String[] args) throws IOException { - int initialCapacity = 10 * 1024 * 1024; - long fileSize = 5368709120l; - byte[] buffer = new byte[initialCapacity]; - new Random().nextBytes(buffer); - - Bytes bytes = Bytes.allocateElasticDirect(initialCapacity); - while (bytes.writePosition() < fileSize) { - bytes.write(buffer); - } - System.out.println("Writing file 1"); - Path path = Paths.get("./textFile1.bin"); - try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW)) { - while (bytes.read(buffer) > 0) { - outputStream.write(buffer); - } - } - long result = path.toFile().length(); - if (fileSize != result) { - throw new RuntimeException(String.format("Expecting %s but file size is %s", fileSize, result)); - } - - bytes = Bytes.allocateElasticDirect(initialCapacity); - new Random().nextBytes(buffer); - while (bytes.writePosition() < fileSize) { - bytes.write(buffer); - } - path = Paths.get("./textFile2.bin"); - System.out.println("Writing file 2"); - // crashing... - try (OutputStream outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW)) { - bytes.copyTo(outputStream); - } - result = path.toFile().length(); - if (fileSize != result) { - throw new RuntimeException(String.format("Expecting %s but file size is %s", fileSize, result)); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Bit8StringInterner.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Bit8StringInterner.java deleted file mode 100644 index 0986584..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Bit8StringInterner.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.pool.StringBuilderPool; -import org.jetbrains.annotations.NotNull; - -public class Bit8StringInterner extends AbstractInterner { - - private static final StringBuilderPool SBP = new StringBuilderPool(); - - public Bit8StringInterner(int capacity) throws IllegalArgumentException { - super(capacity); - } - - @SuppressWarnings("rawtypes") - @Override - - protected String getValue( BytesStore cs, int length) { - StringBuilder sb = SBP.acquireStringBuilder(); - for (int i = 0; i < length; i++) - sb.append((char) cs.readUnsignedByte(cs.readPosition() + i)); - return sb.toString(); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Compression.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Compression.java deleted file mode 100644 index b2d1bae..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Compression.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesIn; -import net.openhft.chronicle.bytes.BytesOut; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.util.StringUtils; -import net.openhft.chronicle.core.util.ThrowingFunction; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.io.*; - -@SuppressWarnings("rawtypes") -public interface Compression { - - static void compress( CharSequence cs, Bytes uncompressed, Bytes compressed) { - switch (cs.charAt(0)) { - case 'l': - if (StringUtils.isEqual("lzw", cs)) { - Compressions.LZW.compress(uncompressed, compressed); - return; - } - break; - case 'g': - if (StringUtils.isEqual("gzip", cs)) { - Compressions.GZIP.compress(uncompressed, compressed); - return; - } - break; - default: - break; - } - Compressions.Binary.compress(uncompressed, compressed); - } - - static void uncompress( CharSequence cs, BytesIn from, BytesOut to) throws IORuntimeException { - switch (cs.charAt(0)) { - case 'b': - case '!': - if (StringUtils.isEqual("binary", cs) || StringUtils.isEqual("!binary", cs)) { - Compressions.Binary.uncompress(from, to); - return; - } - - break; - case 'l': - if (StringUtils.isEqual("lzw", cs)) { - Compressions.LZW.uncompress(from, to); - return; - } - break; - case 'g': - if (StringUtils.isEqual("gzip", cs)) { - Compressions.GZIP.uncompress(from, to); - return; - } - break; - default: - throw new IllegalArgumentException("Unsupported compression " + cs); - } - } - - - static byte[] uncompress( CharSequence cs, T t, ThrowingFunction bytes) throws IORuntimeException { - switch (cs.charAt(0)) { - case 'b': - case '!': - if (StringUtils.isEqual("binary", cs) || StringUtils.isEqual("!binary", cs)) - return Compressions.Binary.uncompress(bytes.apply(t)); - break; - case 'l': - if (StringUtils.isEqual("lzw", cs)) - return Compressions.LZW.uncompress(bytes.apply(t)); - break; - case 'g': - if (StringUtils.isEqual("gzip", cs)) - return Compressions.GZIP.uncompress(bytes.apply(t)); - break; - default: - return null; - } - return null; - } - - default byte[] compress( byte[] bytes) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (OutputStream output = compressingStream(baos)) { - output.write(bytes); - - } catch (IOException e) { - throw new AssertionError(e); // compressing in memory - } - return baos.toByteArray(); - } - - default void compress( BytesIn from, BytesOut to) { - try (OutputStream output = compressingStream(to.outputStream())) { - from.copyTo(output); - - } catch (IOException e) { - throw new AssertionError(e); // compressing in memory - } - } - - default byte[] uncompress( byte[] bytes) throws IORuntimeException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (InputStream input = decompressingStream(new ByteArrayInputStream(bytes))) { - byte[] buf = new byte[512]; - for (int len; (len = input.read(buf)) > 0; ) - baos.write(buf, 0, len); - - } catch (IOException e) { - throw new IORuntimeException(e); - } - return baos.toByteArray(); - } - - default void uncompress( BytesIn from, BytesOut to) throws IORuntimeException { - try (InputStream input = decompressingStream(from.inputStream())) { - to.copyFrom(input); - - } catch (IOException e) { - throw new IORuntimeException(e); - } - } - - InputStream decompressingStream(InputStream input) throws IORuntimeException; - - OutputStream compressingStream(OutputStream output); - - default boolean available() { - return true; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Compressions.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Compressions.java deleted file mode 100644 index 6de70a6..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/Compressions.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.BytesIn; -import net.openhft.chronicle.bytes.BytesOut; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.core.io.IORuntimeException; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.zip.*; - -@SuppressWarnings("rawtypes") -public enum Compressions implements Compression { - Binary { - - @Override - public byte[] compress(byte[] bytes) { - return bytes; - } - - @Override - public byte[] uncompress(byte[] bytes) throws IORuntimeException { - return bytes; - } - - @Override - public void compress( BytesIn from, BytesOut to) { - copy(from, to); - } - - @Override - public void uncompress( BytesIn from, BytesOut to) { - copy(from, to); - } - - private void copy( BytesIn from, BytesOut to) { - long copied = from.copyTo((BytesStore) to); - to.writeSkip(copied); - } - - @Override - public InputStream decompressingStream(InputStream input) { - return input; - } - - @Override - public OutputStream compressingStream(OutputStream output) { - return output; - } - }, - LZW { - - @Override - public InputStream decompressingStream( InputStream input) { - return new InflaterInputStream(input); - } - - - @Override - public OutputStream compressingStream( OutputStream output) { - return new DeflaterOutputStream(output, new Deflater(Deflater.DEFAULT_COMPRESSION)); - } - }, - GZIP { - - @Override - public InputStream decompressingStream( InputStream input) throws IORuntimeException { - try { - return new GZIPInputStream(input); - - } catch (IOException e) { - throw new AssertionError(e); - } - } - - - @Override - public OutputStream compressingStream( OutputStream output) { - try { - return new GZIPOutputStream(output); - - } catch (IOException e) { - throw new AssertionError(e); // in memory. - } - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/DecoratedBufferOverflowException.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/DecoratedBufferOverflowException.java deleted file mode 100644 index 70f9263..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/DecoratedBufferOverflowException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import java.nio.BufferOverflowException; - -public final class DecoratedBufferOverflowException extends BufferOverflowException { - private final String message; - - public DecoratedBufferOverflowException(final String message) { - this.message = message; - } - - @Override - public String getMessage() { - return message; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/DecoratedBufferUnderflowException.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/DecoratedBufferUnderflowException.java deleted file mode 100644 index bb740af..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/DecoratedBufferUnderflowException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import java.nio.BufferUnderflowException; - -public final class DecoratedBufferUnderflowException extends BufferUnderflowException { - private final String message; - - public DecoratedBufferUnderflowException(final String message) { - this.message = message; - } - - @Override - public String getMessage() { - return message; - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/EscapingStopCharTester.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/EscapingStopCharTester.java deleted file mode 100644 index 8cf5de1..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/EscapingStopCharTester.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.StopCharTester; -import net.openhft.chronicle.core.annotation.ForceInline; - -public class EscapingStopCharTester implements StopCharTester { - private final StopCharTester sct; - private boolean escaped = false; - - public EscapingStopCharTester(StopCharTester sct) { - this.sct = sct; - } - - @Override - @ForceInline - public boolean isStopChar(int ch) { - if (escaped) { - escaped = false; - return false; - } - if (ch == '\\') { - escaped = true; - return false; - } - return sct.isStopChar(ch); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/EscapingStopCharsTester.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/EscapingStopCharsTester.java deleted file mode 100644 index cea6139..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/EscapingStopCharsTester.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.StopCharsTester; - -public class EscapingStopCharsTester implements StopCharsTester { - private final StopCharsTester sct; - private boolean escaped = false; - - public EscapingStopCharsTester(StopCharsTester sct) { - this.sct = sct; - } - - @Override - public boolean isStopChar(int ch, int ch2) { - if (escaped) { - escaped = false; - return false; - } - if (ch == '\\') { - escaped = true; - return false; - } - return sct.isStopChar(ch, ch2); - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/PropertyReplacer.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/PropertyReplacer.java deleted file mode 100644 index 765d19c..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/PropertyReplacer.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.BytesUtil; -import org.jetbrains.annotations.NotNull; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Properties; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public enum PropertyReplacer { - ; - - private static final Pattern EXPRESSION_PATTERN = Pattern.compile("\\$\\{\\s*([^}]*?)\\s*\\}"); - - public static String replaceTokensWithProperties(String expression) { - - StringBuilder result = new StringBuilder(expression.length()); - int i = 0; - Matcher matcher = EXPRESSION_PATTERN.matcher(expression); - while (matcher.find()) { - result.append(expression, i, matcher.start()); - String property = matcher.group(1); - - //look up property and replace - String p = System.getProperty(property); - - if (p == null) { - throw new IllegalArgumentException(String.format("System property is missing: " + - "[property=%s, expression=%s]", property, expression)); - } - - result.append(p); - - i = matcher.end(); - } - result.append(expression.substring(i)); - return result.toString(); - } - - public static String replaceTokensWithProperties(String expression, Properties properties) { - - StringBuilder result = new StringBuilder(expression.length()); - int i = 0; - Matcher matcher = EXPRESSION_PATTERN.matcher(expression); - while (matcher.find()) { - result.append(expression, i, matcher.start()); - String property = matcher.group(1); - - //look up property and replace - String p = properties.getProperty(property); - - if (p == null) { - throw new IllegalArgumentException(String.format("Property is missing: " + - "[property=%s, expression=%s, properties=%s]", property, expression, properties)); - } - - result.append(p); - - i = matcher.end(); - } - result.append(expression.substring(i)); - return result.toString(); - } - - - public static String fileAsString(String fileName) throws IOException { - try { - Class propertyReplacerClass = PropertyReplacer.class; - InputStream is = propertyReplacerClass.getResourceAsStream(fileName); - if (is != null) - return convertStreamToString(is); - } catch (Exception ignored) { - } - return BytesUtil.readFile(fileName).toString(); - } - - - private static String convertStreamToString( java.io.InputStream is) { - java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } - -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/StringInternerBytes.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/StringInternerBytes.java deleted file mode 100644 index f1cd942..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/StringInternerBytes.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesUtil; -import net.openhft.chronicle.bytes.algo.BytesStoreHash; -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.pool.StringInterner; -import net.openhft.chronicle.core.util.StringUtils; -import org.jetbrains.annotations.NotNull; - -import static net.openhft.chronicle.bytes.BytesUtil.toCharArray; - -public class StringInternerBytes extends StringInterner { - - public StringInternerBytes(int capacity) { - super(capacity); - } - - @SuppressWarnings("rawtypes") - public String intern( final Bytes bytes) { - return intern(bytes, Maths.toUInt31(bytes.readRemaining())); - } - - /** - * converts the bytes to a ISO-8859-1 String, the end of the string is either the bytes .limit - * () or a byte containing the stopByte ( which ever comes first ). If the string can be - * obtained from the pool, this string is used instead. otherwise, the string is added to the - * pool. - * - * @param bytes the bytes to convert to a string - * @param length parse the string up to the length - * @return the string made from bytes only ( rather than chars ) - */ - @SuppressWarnings("rawtypes") - public String intern( final Bytes bytes, int length) { - try { - int hash32 = BytesStoreHash.hash32(bytes, length); - int h = hash32 & mask; - String s = interner[h]; - long position = bytes.readPosition(); - if (BytesUtil.bytesEqual(s, bytes, position, length)) - return s; - int h2 = (hash32 >> shift) & mask; - String s2 = interner[h2]; - if (BytesUtil.bytesEqual(s2, bytes, position, length)) - return s2; - - char[] chars = toCharArray(bytes, position, length); - return interner[s == null || (s2 != null && toggle()) ? h : h2] = StringUtils.newString(chars); - } finally { - bytes.readSkip(length); - } - } -} diff --git a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/UTF8StringInterner.java b/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/UTF8StringInterner.java deleted file mode 100644 index fdefa58..0000000 --- a/datastructures-bytes/src/main/java/net/openhft/chronicle/bytes/util/UTF8StringInterner.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.AppendableUtil; -import net.openhft.chronicle.bytes.BytesStore; -import net.openhft.chronicle.bytes.UTFDataFormatRuntimeException; -import net.openhft.chronicle.core.pool.StringBuilderPool; -import org.jetbrains.annotations.NotNull; - -public class UTF8StringInterner extends AbstractInterner { - - private static final StringBuilderPool SBP = new StringBuilderPool(); - - public UTF8StringInterner(int capacity) throws IllegalArgumentException { - super(capacity); - } - - @SuppressWarnings("rawtypes") - @Override - - protected String getValue( BytesStore cs, int length) throws UTFDataFormatRuntimeException { - StringBuilder sb = SBP.acquireStringBuilder(); - AppendableUtil.parseUtf8(cs, sb, true, length); - return sb.toString(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/AllocationRatesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/AllocationRatesTest.java deleted file mode 100644 index 27c3bcd..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/AllocationRatesTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; - -/** - * User: peter.lawrey Date: 24/12/13 Time: 19:43 - */ -/* -buffers 128 KB took an average of 18,441 ns for heap ByteBuffer, 33,683 ns for direct ByteBuffer and 1,761 for DirectStore -buffers 128 KB took an average of 13,062 ns for heap ByteBuffer, 17,855 ns for direct ByteBuffer and 903 for DirectStore -buffers 128 KB took an average of 12,809 ns for heap ByteBuffer, 21,602 ns for direct ByteBuffer and 922 for DirectStore -buffers 128 KB took an average of 10,768 ns for heap ByteBuffer, 21,444 ns for direct ByteBuffer and 894 for DirectStore -buffers 128 KB took an average of 8,739 ns for heap ByteBuffer, 22,684 ns for direct ByteBuffer and 890 for DirectStore - */ -public class AllocationRatesTest extends BytesTestCommon { - public static final int BATCH = 10; - static final int BUFFER_SIZE = 128 * 1024; - static final int ALLOCATIONS = 10000; - - @Test - public void compareAllocationRates() { - for (int i = 4; i >= 0; i--) { - long timeHBB = timeHeapByteBufferAllocations(); - long timeDBB = timeDirectByteBufferAllocations(); - long timeDS = timeDirectStoreAllocations(); - if (i == 0) - System.out.printf("buffers %d KB took an average of %,d ns for heap ByteBuffer, %,d ns for direct ByteBuffer and %,d for DirectStore%n", - BUFFER_SIZE / 1024, timeHBB / ALLOCATIONS, timeDBB / ALLOCATIONS, timeDS / ALLOCATIONS); - } - } - - private long timeHeapByteBufferAllocations() { - long start = System.nanoTime(); - for (int i = 0; i < ALLOCATIONS; i += BATCH) { - @NotNull ByteBuffer[] bb = new ByteBuffer[BATCH]; - for (int j = 0; j < BATCH; j++) - bb[j] = ByteBuffer.allocate(BUFFER_SIZE); - } - return System.nanoTime() - start; - } - - private long timeDirectByteBufferAllocations() { - long start = System.nanoTime(); - for (int i = 0; i < ALLOCATIONS; i += BATCH) { - @NotNull ByteBuffer[] bb = new ByteBuffer[BATCH]; - for (int j = 0; j < BATCH; j++) - bb[j] = ByteBuffer.allocateDirect(BUFFER_SIZE); - } - return System.nanoTime() - start; - } - - @SuppressWarnings("rawtypes") - private long timeDirectStoreAllocations() { - long start = System.nanoTime(); - for (int i = 0; i < ALLOCATIONS; i += BATCH) { - @NotNull NativeBytesStore[] ds = new NativeBytesStore[BATCH]; - for (int j = 0; j < BATCH; j++) - ds[j] = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(BUFFER_SIZE); - for (int j = 0; j < BATCH; j++) { - ds[j].releaseLast(); - assertEquals(0, ds[j].refCount()); - } - } - return System.nanoTime() - start; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Allocator.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Allocator.java deleted file mode 100644 index 847b83b..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Allocator.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.nio.ByteBuffer; - -public enum Allocator { - - NATIVE { - @NotNull - @Override - Bytes elasticBytes(int capacity) { - return Bytes.elasticByteBuffer(capacity); - } - - @NotNull - @Override - ByteBuffer byteBuffer(int capacity) { - return ByteBuffer.allocateDirect(capacity); - } - }, - HEAP { - @NotNull - @Override - Bytes elasticBytes(int capacity) { - return Bytes.allocateElasticOnHeap(capacity); - } - - @NotNull - @Override - ByteBuffer byteBuffer(int capacity) { - return ByteBuffer.allocate(capacity); - } - }, - BYTE_BUFFER { - @NotNull - @Override - Bytes elasticBytes(int capacity) { - return Bytes.elasticHeapByteBuffer(capacity); - } - - @NotNull - @Override - ByteBuffer byteBuffer(int capacity) { - return ByteBuffer.allocate(capacity); - } - }, - NATIVE_UNCHECKED { - @NotNull - @Override - Bytes elasticBytes(int capacity) { - return Bytes.elasticByteBuffer(Math.max(128, capacity)).unchecked(true); - } - - @NotNull - @Override - ByteBuffer byteBuffer(int capacity) { - return ByteBuffer.allocateDirect(capacity); - } - }, - HEAP_UNCHECKED { - @NotNull - @Override - Bytes elasticBytes(int capacity) { - return Bytes.elasticHeapByteBuffer(Math.max(32, capacity)).unchecked(true); - } - - @NotNull - @Override - ByteBuffer byteBuffer(int capacity) { - return ByteBuffer.allocate(capacity); - } - }; - - @NotNull - abstract Bytes elasticBytes(int capacity); - - @NotNull - abstract ByteBuffer byteBuffer(int capacity); - - Bytes fixedBytes(int capacity) { - return Bytes.wrapForWrite(byteBuffer(capacity)); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/AppendableUtilTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/AppendableUtilTest.java deleted file mode 100644 index d412c7a..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/AppendableUtilTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import org.junit.After; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class AppendableUtilTest extends BytesTestCommon { - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @SuppressWarnings("rawtypes") - @Test - public void setLength() { - StringBuilder sb = new StringBuilder("hello world"); - AppendableUtil.setLength(sb, 5); - assertEquals("hello", sb.toString()); - - Bytes b = Bytes.from("Hello World"); - AppendableUtil.setLength(b, 5); - assertEquals("Hello", b.toString()); - - StringBuffer sb2 = new StringBuffer(); - try { - AppendableUtil.setLength(sb2, 0); - fail(); - } catch (IllegalArgumentException iae) { - // expected. - } - b.releaseLast(); - } - - @SuppressWarnings("rawtypes") - @Test - public void setCharAt() { - StringBuilder sb = new StringBuilder("hello world"); - Bytes b = Bytes.allocateElasticOnHeap(16).append("Hello World"); - AppendableUtil.setCharAt(sb, 5, 'X'); - AppendableUtil.setCharAt(b, 5, 'X'); - assertEquals("helloXworld", sb.toString()); - assertEquals("HelloXWorld", b.toString()); - b.releaseLast(); - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteCheckSumTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteCheckSumTest.java deleted file mode 100644 index 63ac398..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteCheckSumTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -@SuppressWarnings("rawtypes") -public class ByteCheckSumTest extends BytesTestCommon { - @Test - public void test() { - Bytes bytes = Bytes.allocateDirect(32); - doTest(bytes); - bytes.releaseLast(); - } - - @Test - public void testHeap() { - Bytes bytes = Bytes.allocateElasticOnHeap(32); - doTest(bytes); - bytes.releaseLast(); - } - - private void doTest(Bytes bytes) { - bytes.append("abcdef"); - assertEquals(('a' + 'b' + 'c' + 'd' + 'e' + 'f') & 0xff, bytes.byteCheckSum()); - assertEquals(('b' + 'c' + 'd' + 'e' + 'f') & 0xff, bytes.byteCheckSum(1, 6)); - assertEquals(('b' + 'c' + 'd') & 0xff, bytes.byteCheckSum(1, 4)); - assertEquals(('c' + 'd') & 0xff, bytes.byteCheckSum(2, 4)); - assertEquals(('c') & 0xff, bytes.byteCheckSum(2, 3)); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStoreTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStoreTest.java deleted file mode 100644 index fcaa974..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStoreTest.java +++ /dev/null @@ -1,668 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.util.Arrays; -import java.util.concurrent.ThreadLocalRandom; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.core.io.ReferenceOwner.INIT; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -public class ByteStoreTest extends BytesTestCommon { - - private static final int SIZE = 128; - private Bytes bytes; - private BytesStore bytesStore; - private ThreadDump threadDump; - - @After - public void checkRegisteredBytes() { - bytes.releaseLast(); - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Before - public void beforeTest() { - bytesStore = BytesStore.wrap(ByteBuffer.allocate(SIZE).order(ByteOrder.nativeOrder())); - bytes = bytesStore.bytesForWrite(); - bytesStore.release(INIT); - bytes.clear(); - } - - @Test - public void testReadIncompleteLong() { - bytes.writeLong(0x0102030405060708L); - assertEquals(0x0102030405060708L, bytes.readIncompleteLong(0)); - bytes.clear(); - - long l = 0; - for (int i = 1; i <= 8; i++) { - bytes.writeUnsignedByte(i); - l |= (long) i << (i * 8 - 8); - assertEquals(l, bytes.readIncompleteLong(0)); - } - } - - @Test - public void testCAS() { - if (Jvm.isArm()) return; // TODO FIX - final BytesStore bytes = BytesStore.wrap(ByteBuffer.allocate(100)); - bytes.compareAndSwapLong(0, 0L, 1L); - assertEquals(1L, bytes.readLong(0)); - bytes.releaseLast(); - } - - @Test - public void testRead() { - for (int i = 0; i < bytes.capacity(); i++) - bytes.writeByte(i, i); - bytes.writePosition(bytes.capacity()); - for (int i = 0; i < bytes.capacity(); i++) - assertEquals((byte) i, bytes.readByte()); - for (int i = (int) (bytes.capacity() - 1); i >= 0; i--) { - assertEquals((byte) i, bytes.readByte(i)); - } - } - - @Test - public void testReadFully() { - for (int i = 0; i < bytes.capacity(); i++) - bytes.writeByte((byte) i); - - @NotNull byte[] bytes = new byte[(int) this.bytes.capacity()]; - this.bytes.read(bytes); - for (int i = 0; i < this.bytes.capacity(); i++) - Assert.assertEquals((byte) i, bytes[i]); - } - - @Test - public void testCompareAndSetInt() { - Assert.assertTrue(bytes.compareAndSwapInt(0, 0, 1)); - Assert.assertFalse(bytes.compareAndSwapInt(0, 0, 1)); - Assert.assertTrue(bytes.compareAndSwapInt(8, 0, 1)); - Assert.assertTrue(bytes.compareAndSwapInt(0, 1, 2)); - } - - @Test - public void testCompareAndSetLong() { - if (Jvm.isArm()) return; // TODO FIX - - Assert.assertTrue(bytes.compareAndSwapLong(0L, 0L, 1L)); - Assert.assertFalse(bytes.compareAndSwapLong(0L, 0L, 1L)); - Assert.assertTrue(bytes.compareAndSwapLong(8L, 0L, 1L)); - Assert.assertTrue(bytes.compareAndSwapLong(0L, 1L, 2L)); - } - - @Test - public void testPosition() { - for (int i = 0; i < bytes.capacity(); i++) - bytes.writeByte((byte) i); - for (int i = (int) (bytes.capacity() - 1); i >= 0; i--) { - bytes.readPosition(i); - assertEquals((byte) i, bytes.readByte()); - } - } - - @Test - public void testCapacity() { - assertEquals(SIZE, bytes.capacity()); - final VanillaBytes bytes = Bytes.allocateDirect(10); - try { - assertEquals(10, bytes.capacity()); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testRemaining() { - assertEquals(0, bytes.readRemaining()); - assertEquals(SIZE, bytes.writeRemaining()); - bytes.writePosition(10); - assertEquals(10, bytes.readRemaining()); - assertEquals(SIZE - 10, bytes.writeRemaining()); - } - - @Test - public void testByteOrder() { - assertEquals(ByteOrder.nativeOrder(), bytes.byteOrder()); - } - - /* @Test - public void testWriteReadBytes() { - byte[] bytes = "Hello World!".getBytes(ISO_8859_1); - this.bytes.write(bytes); - byte[] bytes2 = new byte[bytes.length]; - this.bytes.position(0); - this.bytes.read(bytes2); - assertTrue(Arrays.equals(bytes, bytes2)); - - this.bytes.write(22, bytes); - byte[] bytes3 = new byte[bytes.length]; - this.bytes.skipBytes((int) (22 - this.bytes.position())); - assertEquals(bytes3.length, this.bytes.read(bytes3)); - assertTrue(Arrays.equals(bytes, bytes3)); - this.bytes.position(this.bytes.capacity()); - assertEquals(-1, this.bytes.read(bytes3)); - }*/ - @Test - public void testWriteReadUtf8() throws IORuntimeException { - bytes.writeUtf8(null); - final String[] words = new String[]{"Hello", "World!", "Bye£€!", ""}; - for (String word : words) { - bytes.writeUtf8(word); - } - - assertNull(bytes.readUtf8()); - for (String word : words) { - assertEquals(word, bytes.readUtf8()); - } - assertNull(bytes.readUtf8()); - assertEquals(26, bytes.readPosition()); // check the size - - bytes.readPosition(0); - final StringBuilder sb = new StringBuilder(); - Assert.assertFalse(bytes.readUtf8(sb)); - for (String word : words) { - Assert.assertTrue(bytes.readUtf8(sb)); - Assert.assertEquals(word, sb.toString()); - } - assertFalse(bytes.readUtf8(sb)); - Assert.assertEquals("", sb.toString()); - } - - @Test - public void testWriteReadUTF() { - final String[] words = "Hello,World!,Bye£€!".split(","); - for (String word : words) { - bytes.writeUtf8(word); - } - bytes.writeUtf8(""); - bytes.writeUtf8(null); - assertEquals(26, bytes.writePosition()); // check the size, more bytes for less strings than writeUtf8 - - for (String word : words) { - assertEquals(word, bytes.readUtf8()); - } - assertEquals("", bytes.readUtf8()); - assertNull(bytes.readUtf8()); - } - - @Test - public void testWriteReadByteBuffer() { - final byte[] bytes = "Hello\nWorld!\r\nBye".getBytes(ISO_8859_1); - this.bytes.writeSome(ByteBuffer.wrap(bytes)); - - final byte[] bytes2 = new byte[bytes.length + 1]; - final ByteBuffer bb2 = ByteBuffer.wrap(bytes2); - this.bytes.read(bb2); - - Assert.assertEquals(bytes.length, bb2.position()); - final byte[] bytes2b = Arrays.copyOf(bytes2, bytes.length); - Assert.assertArrayEquals(bytes, bytes2b); - } - - @Test - public void testReadWriteBoolean() { - for (int i = 0; i < 32; i++) - bytes.writeBoolean(i, (i & 3) == 0); - bytes.writePosition(32); - for (int i = 32; i < 64; i++) { - final boolean flag = (i & 5) == 0; - bytes.writeBoolean(flag); - } - - for (int i = 0; i < 32; i++) - assertEquals((i & 3) == 0, bytes.readBoolean()); - for (int i = 32; i < 64; i++) { - final boolean actual = bytes.readBoolean(i); - final boolean expected = (i & 5) == 0; - assertEquals(expected, actual); - } - } - - @Test - public void testReadWriteShort() { - for (int i = 0; i < 32; i += 2) - bytes.writeShort(i, (short) i); - bytes.writePosition(32); - for (int i = 32; i < 64; i += 2) - bytes.writeShort((short) i); - - for (int i = 0; i < 32; i += 2) - assertEquals(i, bytes.readShort()); - for (int i = 32; i < 64; i += 2) - assertEquals(i, bytes.readShort(i)); - } - - @Test - public void testReadWriteStop() throws IORuntimeException { - final long[] longs = {Long.MIN_VALUE, Long.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE}; - for (long i : longs) { - bytes.writeStopBit(i); -// LOG.info(i + " " + bytes.position()); - } - assertEquals(9 + 10, +5 + 6, bytes.writePosition()); - - for (long i : longs) - assertEquals(i, bytes.readStopBit()); - } - - @Test - public void testReadWriteUnsignedShort() { - for (int i = 0; i < 32; i += 2) - bytes.writeUnsignedShort(i, (~i) & 0xFFFF); - bytes.writePosition(32); - for (int i = 32; i < 64; i += 2) - bytes.writeUnsignedShort(~i & 0xFFFF); - - for (int i = 0; i < 32; i += 2) - assertEquals(~i & 0xFFFF, bytes.readUnsignedShort()); - for (int i = 32; i < 64; i += 2) - assertEquals(~i & 0xFFFF, bytes.readUnsignedShort(i)); - } - - @Test - public void testReadWriteInt() { - for (int i = 0; i < 32; i += 4) - bytes.writeInt(i, i); - bytes.writePosition(32); - for (int i = 32; i < 64; i += 4) - bytes.writeInt(i); - - for (int i = 0; i < 32; i += 4) - assertEquals(i, bytes.readInt()); - for (int i = 32; i < 64; i += 4) - assertEquals(i, bytes.readInt(i)); - } - - @Test - public void testReadWriteThreadSafeInt() { - for (int i = 0; i < 32; i += 4) - bytes.writeOrderedInt(i, i); - bytes.writePosition(32); - for (int i = 32; i < 64; i += 4) - bytes.writeOrderedInt(i); - - for (int i = 0; i < 32; i += 4) - assertEquals(i, bytes.readVolatileInt()); - for (int i = 32; i < 64; i += 4) - assertEquals(i, bytes.readVolatileInt(i)); - } - - @Test - public void testReadWriteFloat() { - for (int i = 0; i < 32; i += 4) - bytes.writeFloat(i, i); - bytes.writePosition(32); - for (int i = 32; i < 64; i += 4) - bytes.writeFloat(i); - - for (int i = 0; i < 32; i += 4) - assertEquals(i, bytes.readFloat(), 0); - for (int i = 32; i < 64; i += 4) - assertEquals(i, bytes.readFloat(i), 0); - } - - @Test - public void testReadWriteUnsignedInt() { - for (int i = 0; i < 32; i += 4) - bytes.writeUnsignedInt(i, ~i & 0xFFFF); - bytes.writePosition(32); - for (int i = 32; i < 64; i += 4) - bytes.writeUnsignedInt(~i & 0xFFFF); - - for (int i = 0; i < 32; i += 4) - assertEquals(~i & 0xFFFFL, bytes.readUnsignedInt()); - for (int i = 32; i < 64; i += 4) - assertEquals(~i & 0xFFFFL, bytes.readUnsignedInt(i)); - } - - @Test - public void testReadWriteLong() { - for (long i = 0; i < 32; i += 8) - bytes.writeLong(i, i); - bytes.writePosition(32); - for (long i = 32; i < 64; i += 8) - bytes.writeLong(i); - - for (long i = 0; i < 32; i += 8) - assertEquals(i, bytes.readLong()); - for (long i = 32; i < 64; i += 8) - assertEquals(i, bytes.readLong(i)); - } - - @Test - public void testReadWriteThreadSafeLong() { - if (Jvm.isArm()) - return; // TODO FIX - for (long i = 0; i < 32; i += 8) - bytes.writeOrderedLong(i, i); - bytes.writePosition(32); - for (long i = 32; i < 64; i += 8) - bytes.writeOrderedLong(i); -// LOG.info(bytes.bytes().toDebugString()); - - for (long i = 0; i < 32; i += 8) - assertEquals(i, bytes.readVolatileLong()); - for (long i = 32; i < 64; i += 8) - assertEquals(i, bytes.readVolatileLong(i)); - } - - @Test - public void testReadWriteDouble() { - for (long i = 0; i < 32; i += 8) - bytes.writeDouble(i, i); - bytes.writePosition(32); - for (long i = 32; i < 64; i += 8) - bytes.writeDouble(i); - - for (long i = 0; i < 32; i += 8) - assertEquals(i, bytes.readDouble(), 0); - for (long i = 32; i < 64; i += 8) - assertEquals(i, bytes.readDouble(i), 0); - } - - @Test - public void testReadWriteDoubleAndInt() { - for (long i = 0; i < 48; i += 12) - bytes.writeDoubleAndInt(i, (int) i); - - for (long i = 0; i < 48; i += 12) { - assertEquals(i, bytes.readDouble(), 0); - assertEquals(i, bytes.readInt()); - } - } - - @Test - public void testReadWriteStopBitDouble() { - final double[] doubles = { - -Double.MAX_VALUE, Double.NEGATIVE_INFINITY, - Byte.MIN_VALUE, Byte.MAX_VALUE, - Short.MIN_VALUE, Short.MAX_VALUE, - Long.MIN_VALUE, Long.MAX_VALUE, - Integer.MIN_VALUE, Integer.MAX_VALUE}; - for (double i : doubles) { - bytes.writeStopBit(i); - //System.out.println(i + " " + bytes.writePosition()); - } - - for (double i : doubles) - assertEquals(i, bytes.readStopBitDouble(), 0.0); - } - - @Test - public void testStream() throws IOException { - final BytesStore bytes0 = BytesStore.wrap(ByteBuffer.allocate(1000)); - final Bytes bytes2 = bytes0.bytesForWrite(); - bytes0.release(INIT); - try { - final GZIPOutputStream out = new GZIPOutputStream(bytes2.outputStream()); - out.write("Hello world\n".getBytes(ISO_8859_1)); - out.close(); - - try (GZIPInputStream in = new GZIPInputStream(bytes2.inputStream())) { - final byte[] bytes = new byte[12]; - for (int i = 0; i < 12; i++) - bytes[i] = (byte) in.read(); - Assert.assertEquals(-1, in.read()); - Assert.assertEquals("Hello world\n", new String(bytes)); - } - } finally { - bytes2.releaseLast(); - } - } - - @Test - public void testStream2() throws IOException { - try (OutputStream out = bytes.outputStream()) { - out.write(11); - out.write(22); - out.write(33); - out.write(44); - out.write(55); - - try (InputStream in = bytes.inputStream()) { - assertFalse(in.markSupported()); - assertEquals(11, in.read()); - assertEquals(1, bytes.readPosition()); - assertEquals(22, in.read()); - assertEquals(2, bytes.readPosition()); - assertEquals(33, in.read()); - - assertEquals(1, in.skip(1)); - assertEquals(4, bytes.readPosition()); - assertEquals(1, bytes.readRemaining()); - assertEquals(55, in.read()); - - assertEquals(-1, in.read()); - } - } - } - - @Test - public void testAddAndGet() { - final BytesStore bytesStore2 = NativeBytesStore.nativeStore(128); - try { - for (int i = 0; i < 10; i++) - bytesStore.addAndGetInt(0L, 10); - assertEquals(100, bytesStore.readInt(0L)); - assertEquals(0, bytesStore.readInt(4L)); - - for (int i = 0; i < 11; i++) - bytesStore.addAndGetInt(4L, 11); - assertEquals(100, bytesStore.readInt(0L)); - assertEquals(11 * 11, bytesStore.readInt(4L)); - } finally { - bytesStore2.releaseLast(); - } - } - - @Test - public void testAddAndGetLongNative() { - if (Jvm.isArm()) - return; // TODO FIX - final BytesStore bytesStore2 = NativeBytesStore.nativeStore(128); - try { - checkAddAndGetLong(); - } finally { - bytesStore2.releaseLast(); - } - } - - @Test - public void testAddAndGetLong() { - if (Jvm.isArm()) - return; // TODO FIX - final BytesStore bytesStore2 = BytesStore.wrap(new byte[128]); - try { - checkAddAndGetLong(); - } finally { - bytesStore2.releaseLast(); - } - } - - private void checkAddAndGetLong() { - for (int i = 0; i < 10; i++) - assertEquals((i + 1) * 10, bytesStore.addAndGetLong(0L, 10)); - assertEquals(100, bytesStore.readLong(0L)); - assertEquals(0, bytesStore.readLong(8L)); - - for (int i = 0; i < 11; i++) - bytesStore.addAndGetLong(8L, 11); - assertEquals(100, bytesStore.readLong(0L)); - assertEquals(11 * 11, bytesStore.readLong(8L)); - } - - @Test - public void testAddAndGetFloat() { - final BytesStore bytesStore2 = NativeBytesStore.nativeStore(128); - try { - - for (int i = 0; i < 10; i++) - bytesStore2.addAndGetFloat(0L, 10); - assertEquals(100, bytesStore2.readFloat(0L), 0f); - assertEquals(0, bytesStore2.readVolatileFloat(4L), 0f); - - for (int i = 0; i < 11; i++) - bytesStore2.addAndGetFloat(4L, 11); - assertEquals(100, bytesStore2.readVolatileFloat(0L), 0f); - assertEquals(11 * 11, bytesStore2.readFloat(4L), 0f); - } finally { - bytesStore2.releaseLast(); - } - } - - @Test - public void testAddAndGetDouble() { - final BytesStore bytesStore2 = NativeBytesStore.nativeStore(128); - try { - - for (int i = 0; i < 10; i++) - bytesStore2.addAndGetDouble(0L, 10); - assertEquals(100, bytesStore2.readDouble(0L), 0.0); - assertEquals(0, bytesStore2.readVolatileDouble(8L), 0.0); - - for (int i = 0; i < 11; i++) - bytesStore2.addAndGetDouble(8L, 11); - assertEquals(100, bytesStore2.readVolatileDouble(0L), 0.0); - assertEquals(11 * 11, bytesStore2.readDouble(8L), 0.0); - } finally { - bytesStore2.releaseLast(); - } - } - - @Test - public void testToString() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - NativeBytesStore bytes0 = NativeBytesStore.nativeStore(32); - final Bytes bytes = bytes0.bytesForWrite(); - bytes0.release(INIT); - try { - assertEquals("[pos: 0, rlim: 0, wlim: 8EiB, cap: 8EiB ] ǁ‡٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(1); - System.gc(); - assertEquals(1, bytes.refCount()); - assertEquals("[pos: 0, rlim: 1, wlim: 8EiB, cap: 8EiB ] ǁ⒈‡٠٠٠٠٠٠٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(2); - bytes.readByte(); - assertEquals("[pos: 1, rlim: 2, wlim: 8EiB, cap: 8EiB ] ⒈ǁ⒉‡٠٠٠٠٠٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(3); - assertEquals("[pos: 1, rlim: 3, wlim: 8EiB, cap: 8EiB ] ⒈ǁ⒉⒊‡٠٠٠٠٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(4); - bytes.readByte(); - assertEquals("[pos: 2, rlim: 4, wlim: 8EiB, cap: 8EiB ] ⒈⒉ǁ⒊⒋‡٠٠٠٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(5); - assertEquals("[pos: 2, rlim: 5, wlim: 8EiB, cap: 8EiB ] ⒈⒉ǁ⒊⒋⒌‡٠٠٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(6); - bytes.readByte(); - System.gc(); - assertEquals(1, bytes.refCount()); - assertEquals("[pos: 3, rlim: 6, wlim: 8EiB, cap: 8EiB ] ⒈⒉⒊ǁ⒋⒌⒍‡٠٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(7); - assertEquals("[pos: 3, rlim: 7, wlim: 8EiB, cap: 8EiB ] ⒈⒉⒊ǁ⒋⒌⒍⒎‡٠٠٠٠٠٠٠٠٠", bytes.toDebugString()); - bytes.writeUnsignedByte(8); - assertEquals("[pos: 3, rlim: 8, wlim: 8EiB, cap: 8EiB ] ⒈⒉⒊ǁ⒋⒌⒍⒎⒏‡٠٠٠٠٠٠٠٠", bytes.toDebugString()); - } finally { - bytes.releaseLast(); - assertEquals(0, bytes.refCount()); - } - } - - @Test - public void testOverflowReadUtf8() throws IORuntimeException { - final NativeBytesStore bs = NativeBytesStore.nativeStore(32); - BytesInternal.writeStopBit(bs, 10, 30); - try { - bs.readUtf8(10, new StringBuilder()); - throw new AssertionError("should throw BufferUnderflowException"); - } catch (BufferUnderflowException e) { - // expected - } finally { - bs.releaseLast(); - } - } - - @Test - public void testCopyTo() { - final BytesStore bytesStoreOriginal = BytesStore.wrap(new byte[SIZE]); - try { - for (int i = 0; i < SIZE; i++) { - final byte randomByte = (byte) ThreadLocalRandom.current().nextInt(Byte.MAX_VALUE); - bytesStoreOriginal.writeByte(i, randomByte); - } - final BytesStore bytesStoreCopy = BytesStore.wrap(new byte[SIZE]); - try { - bytesStoreOriginal.copyTo(bytesStoreCopy); - for (int i = 0; i < SIZE; i++) - assertEquals(bytesStoreOriginal.readByte(i), bytesStoreCopy.readByte(i)); - } finally { - bytesStoreCopy.releaseLast(); - } - } finally { - bytesStoreOriginal.releaseLast(); - } - } - - @Test - public void testEmpty() { - assertEquals(0, BytesStore.empty().realCapacity()); - } - - @Test(expected = DecoratedBufferOverflowException.class) - public void testClearAndPadTooMuch() { - final Bytes b = bytesStore.bytesForWrite(); - try { - b.clearAndPad(SIZE + 1); - } finally { - b.releaseLast(); - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStringAppenderTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStringAppenderTest.java deleted file mode 100644 index 40b1180..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStringAppenderTest.java +++ /dev/null @@ -1,256 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import net.openhft.chronicle.core.util.ObjectUtils; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Arrays; -import java.util.Collection; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.assumeFalse; - -@RunWith(Parameterized.class) -public class ByteStringAppenderTest extends BytesTestCommon { - - @NotNull - private ThreadDump threadDump; - @SuppressWarnings("rawtypes") - private final Bytes bytes; - - public ByteStringAppenderTest(String name, boolean direct) { - bytes = direct ? Bytes.allocateElasticDirect() : Bytes.elasticByteBuffer(); - } - - @Parameterized.Parameters(name = "{0}") - public static Collection data() { - return Arrays.asList(new Object[][]{ -// {"heap", false}, - {"native", true} - }); - } - - @After - public void checkRegisteredBytes() { - bytes.releaseLast(); - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testConvertTo() { - Bytes hello = Bytes.from("hello"); - Bytes hello1 = ObjectUtils.convertTo(Bytes.class, "hello"); - assertEquals(hello, hello1); - VanillaBytes bytes = Bytes.allocateDirect(2); - Bytes one = ObjectUtils.convertTo(Bytes.class, 1); - assertEquals(bytes.append(1), one); - one.releaseLast(); - hello1.releaseLast(); - hello.releaseLast(); - bytes.releaseLast(); - } - - @Test - public void testAppendInt() throws IORuntimeException { - for (int expected = 1; expected != 0; expected *= 2) { - bytes.append(expected); - bytes.append(","); - bytes.append(-expected); - bytes.append(","); - - assertEquals(expected, (int) bytes.parseLong()); - assertEquals(-expected, (int) bytes.parseLong()); - } - } - - @Test - public void testAppend() throws IORuntimeException { - for (long expected = 1; expected != 0; expected *= 2) { - bytes.clear(); - bytes.append(expected); - bytes.append(","); - bytes.append(-expected); - bytes.append(","); -// System.out.println(bytes); - assertEquals(expected, bytes.parseLong()); - assertEquals(-expected, bytes.parseLong()); - } - } - - @Test - public void testAppendWithOffset() { - bytes.readLimit(20); - bytes.writeLimit(20); - for (long expected : new long[]{123456, 12345, 1234, 123, 12, 1, 0}) { - bytes.append(10, expected, 6); - assertEquals(expected, bytes.parseLong(10)); - } - } - - @Test - public void testAppendWithOffsetNeg() { - bytes.readLimit(20); - bytes.writeLimit(20); - for (long expected : new long[]{-123456, 12345, -1234, 123, -12, 1, 0}) { - bytes.append(10, expected, 7); - assertEquals(expected, bytes.parseLong(10)); - } - } - - @Test - public void testAppendDouble() throws IORuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - testAppendDouble0(-1.42278619425894E11); -/* - @NotNull Random random = new Random(1); - for (int i = 0; i < 100000; i++) { - double d = Math.pow(1e32, random.nextDouble()) / 1e6; - if (i % 3 == 0) d = -d; - testAppendDouble0(d); - } -*/ - } - - private void testAppendDouble0(double d) throws IORuntimeException { - bytes.clear(); - bytes.append(d).append(' '); - - double d2 = bytes.parseDouble(); - assertEquals(d, d2, 0); - -/* assumes self terminating. - bytes.clear(); - bytes.appendDouble(d); - bytes.flip(); - double d3 = bytes.parseDouble(); - Assert.assertEquals(d, d3, 0); -*/ - } - - @Test - public void testAppendLongDecimal() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - bytes.appendDecimal(128, 0).append('\n'); - bytes.appendDecimal(128, 1).append('\n'); - bytes.appendDecimal(128, 2).append('\n'); - bytes.appendDecimal(128, 3).append('\n'); - bytes.appendDecimal(128, 4).append('\n'); - - bytes.appendDecimal(0, 0).append('\n'); - bytes.appendDecimal(0, 1).append('\n'); - bytes.appendDecimal(0, 4).append('\n'); - bytes.appendDecimal(1, 0).append('\n'); - bytes.appendDecimal(1, 1).append('\n'); - bytes.appendDecimal(1, 2).append('\n'); - bytes.appendDecimal(1, 3).append('\n'); - bytes.appendDecimal(1, 4).append('\n'); - - assertEquals("128\n" + - "12.8\n" + - "1.28\n" + - "0.128\n" + - "0.0128\n" + - "0\n" + - "0.0\n" + - "0.0000\n" + - "1\n" + - "0.1\n" + - "0.01\n" + - "0.001\n" + - "0.0001\n", bytes.toString()); - } - - @Test - public void testAppendDoublePrecision() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - - bytes.append(1.28, 0).append('\n'); - bytes.append(-1.28, 1).append('\n'); - bytes.append(1.28, 2).append('\n'); - bytes.append(-1.28, 3).append('\n'); - bytes.append(1.28, 4).append('\n'); - - bytes.append(0, 0).append('\n'); - bytes.append(-0, 1).append('\n'); - bytes.append(0, 4).append('\n'); - bytes.append(1, 0).append('\n'); - bytes.append(-1.11, 1).append('\n'); - bytes.append(0.111, 2).append('\n'); - bytes.append(1.1, 3).append('\n'); - bytes.append(-0.01111, 4).append('\n'); - - assertEquals("1\n" + - "-1.3\n" + - "1.28\n" + - "-1.280\n" + - "1.2800\n" + - "0\n" + - "0.0\n" + - "0.0000\n" + - "1\n" + - "-1.1\n" + - "0.11\n" + - "1.100\n" + - "-0.0111\n", bytes.toString()); - } - - @Test - public void tens() { - double d = 1; - for (int i = 0; i <= (int) Math.log10(Double.MAX_VALUE); i++) { - bytes.clear(); - { - double expected = d; - bytes.append(expected).append(' '); - String s = bytes.toString(); - double d2 = bytes.parseDouble(); - double ulp = i < 90 ? 0 : i < 235 ? Math.ulp(d) : Math.ulp(d) * 2; - assertEquals(s, expected, d2, ulp); - } - { - double expected = 1 / d; - bytes.append(expected).append(' '); - String s = bytes.toString(); - double d2 = bytes.parseDouble(); - assertEquals(s, expected, d2, Jvm.isArm() ? 2e-39 : 2e-40); - } - d *= 10; - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStringParserTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStringParserTest.java deleted file mode 100644 index 2b2377e..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ByteStringParserTest.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.StopCharTesters.CONTROL_STOP; -import static net.openhft.chronicle.bytes.StopCharTesters.SPACE_STOP; -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.assumeFalse; - -public class ByteStringParserTest extends BytesTestCommon { - @SuppressWarnings("rawtypes") - @NotNull - Bytes bytes = Bytes.elasticByteBuffer(); - private ThreadDump threadDump; - - @After - public void checkRegisteredBytes() { - bytes.releaseLast(); - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @SuppressWarnings("rawtypes") - @Test - public void testParseLong() { - long expected = 123456789012345678L; - bytes.append(expected); - Bytes bytes2 = Bytes.allocateElasticOnHeap((int) bytes.readRemaining()); - - Assert.assertEquals(expected, bytes.parseLong(0)); - Assert.assertEquals(expected, BytesInternal.parseLong(bytes)); - - bytes2.append(expected); - Assert.assertEquals(expected, bytes2.parseLong(0)); - Assert.assertEquals(expected, BytesInternal.parseLong(bytes2)); - bytes2.releaseLast(); - - } - - @Test - public void testParseInt() { - int expected = 123; - bytes.append(expected); - - Assert.assertEquals(expected, BytesInternal.parseLong(bytes)); - } - - @Test - public void testParseDouble() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - double expected = 123.1234; - bytes.append(expected); - - Assert.assertEquals(expected, BytesInternal.parseDouble(bytes), 0); - } - - @Test - public void testParseFloat() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - float expected = 123; - bytes.append(expected); - - Assert.assertEquals(expected, BytesInternal.parseDouble(bytes), 0); - } - - @Test - public void testParseShort() { - short expected = 123; - bytes.append(expected); - - Assert.assertEquals(expected, BytesInternal.parseLong(bytes)); - } - - @Test - public void testAppendParse() throws IOException, IORuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - bytes.write("word£€) ".getBytes(StandardCharsets.UTF_8)); - bytes.append("word£€)").append(' '); - bytes.append(1234).append(' '); - bytes.append(123456L).append(' '); - bytes.append(1.2345).append(' '); - bytes.append(0.0012345).append(' '); - - assertEquals("word£€)", bytes.parseUtf8(SPACE_STOP)); - assertEquals("word£€)", bytes.parseUtf8(SPACE_STOP)); - assertEquals(1234, bytes.parseLong()); - assertEquals(123456L, bytes.parseLong()); - assertEquals(1.2345, bytes.parseDouble(), 0); - assertEquals(0.0012345, bytes.parseDouble(), 0); - } - - @Test - public void testLastDecimalPlaces() throws IOException, IORuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - bytes.append("1").append(' '); - bytes.append("1.").append(' '); - bytes.append("0.0").append(' '); - bytes.append("0.1").append(' '); - bytes.append("1.1").append(' '); - bytes.append("1.28").append(' '); - bytes.append("1.10").append(' '); - bytes.append("1.10000").append(' '); - - // test long first - bytes.readPosition(0); - assertEquals(1, bytes.parseLongDecimal()); - assertEquals(0, bytes.lastDecimalPlaces()); - - assertEquals(1, bytes.parseLongDecimal()); - assertEquals(0, bytes.lastDecimalPlaces()); - - assertEquals(0, bytes.parseLongDecimal()); - assertEquals(1, bytes.lastDecimalPlaces()); - - assertEquals(1, bytes.parseLongDecimal()); - assertEquals(1, bytes.lastDecimalPlaces()); - - assertEquals(11, bytes.parseLongDecimal()); - assertEquals(1, bytes.lastDecimalPlaces()); - - assertEquals(128, bytes.parseLongDecimal()); - assertEquals(2, bytes.lastDecimalPlaces()); - - assertEquals(110, bytes.parseLongDecimal()); - assertEquals(2, bytes.lastDecimalPlaces()); - - assertEquals(110000, bytes.parseLongDecimal()); - assertEquals(5, bytes.lastDecimalPlaces()); - - // test double - bytes.readPosition(0); - assertEquals(1, bytes.parseDouble(), 0); - assertEquals(0, bytes.lastDecimalPlaces()); - - assertEquals(1., bytes.parseDouble(), 0); - assertEquals(0, bytes.lastDecimalPlaces()); - - assertEquals(0.0, bytes.parseDouble(), 0); - assertEquals(1, bytes.lastDecimalPlaces()); - - assertEquals(0.1, bytes.parseDouble(), 0); - assertEquals(1, bytes.lastDecimalPlaces()); - - assertEquals(1.1, bytes.parseDouble(), 0); - assertEquals(1, bytes.lastDecimalPlaces()); - - assertEquals(1.28, bytes.parseDouble(), 0); - assertEquals(2, bytes.lastDecimalPlaces()); - - assertEquals(1.10, bytes.parseDouble(), 0); - assertEquals(2, bytes.lastDecimalPlaces()); - - assertEquals(1.10000, bytes.parseDouble(), 0); - assertEquals(5, bytes.lastDecimalPlaces()); - } - - @Test - public void testAppendParseUTF() throws IOException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull String[] words = "Hello,World!,Bye£€!".split(","); - for (@NotNull String word : words) { - bytes.append(word).append('\t'); - } - bytes.append('\t'); - - for (String word : words) { - assertEquals(word, bytes.parseUtf8(CONTROL_STOP)); - } - assertEquals("", bytes.parseUtf8(CONTROL_STOP)); - - bytes.readPosition(0); - @NotNull StringBuilder sb = new StringBuilder(); - for (String word : words) { - bytes.parseUtf8(sb, CONTROL_STOP); - Assert.assertEquals(word, sb.toString()); - } - bytes.parseUtf8(sb, CONTROL_STOP); - Assert.assertEquals("", sb.toString()); - - bytes.readPosition(0); - bytes.skipTo(CONTROL_STOP); - assertEquals(6, bytes.readPosition()); - bytes.skipTo(CONTROL_STOP); - assertEquals(13, bytes.readPosition()); - Assert.assertTrue(bytes.skipTo(CONTROL_STOP)); - assertEquals(23, bytes.readPosition()); - Assert.assertTrue(bytes.skipTo(CONTROL_STOP)); - assertEquals(24, bytes.readPosition()); - Assert.assertFalse(bytes.skipTo(CONTROL_STOP)); - } - - @Test - public void testAppendSubstring() throws IOException { - bytes.append("Hello World", 2, 7).append("\n"); - - assertEquals("Hello World".substring(2, 7), bytes.parseUtf8(CONTROL_STOP)); - } - - @Test - public void testWriteBytes() { - bytes.write("Hello World\n".getBytes(ISO_8859_1), 0, 10); - bytes.write("good bye\n".getBytes(ISO_8859_1), 4, 4); - bytes.write(4, "0 w".getBytes(ISO_8859_1)); - - assertEquals("Hell0 worl bye", bytes.parseUtf8(CONTROL_STOP)); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Bytes2Test.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Bytes2Test.java deleted file mode 100644 index f11a24f..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Bytes2Test.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Arrays; -import java.util.Collection; - -import static net.openhft.chronicle.bytes.Allocator.HEAP; -import static net.openhft.chronicle.bytes.Allocator.NATIVE; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -@SuppressWarnings("rawtypes") -@RunWith(Parameterized.class) -public class Bytes2Test extends BytesTestCommon { - - private final Allocator alloc1; - private final Allocator alloc2; - private ThreadDump threadDump; - - public Bytes2Test(Allocator alloc1, Allocator alloc2) { - this.alloc1 = alloc1; - this.alloc2 = alloc2; - } - - @Parameterized.Parameters - public static Collection data() { - return Arrays.asList(new Object[][]{ - {NATIVE, NATIVE}, {HEAP, NATIVE}, {NATIVE, HEAP}, {HEAP, HEAP} - }); - } - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testPartialWrite() { - Bytes from = alloc1.elasticBytes(1); - Bytes to = alloc2.fixedBytes(6); - - try { - from.write("Hello World"); - - to.writeSome(from); - assertEquals("World", from.toString()); - } finally { - from.releaseLast(); - to.releaseLast(); - } - } - - @Test - public void testPartialWrite64plus() { - Bytes from = alloc1.elasticBytes(1); - Bytes to = alloc2.fixedBytes(6); - - from.write("Hello World 0123456789012345678901234567890123456789012345678901234567890123456789"); - - try { - to.writeSome(from); - assertTrue("from: " + from, from.toString().startsWith("World ")); - } finally { - from.releaseLast(); - to.releaseLast(); - } - } - - @Test - public void testWrite64plus() { - Bytes from = alloc1.fixedBytes(128); - Bytes to = alloc2.fixedBytes(128); - - from.write("Hello World 0123456789012345678901234567890123456789012345678901234567890123456789"); - - try { - to.write(from); - assertEquals(from.toString(), to.toString()); - } finally { - from.releaseLast(); - to.releaseLast(); - } - } - - @Test - public void testParseToBytes() throws IORuntimeException { - Bytes from = alloc1.fixedBytes(64); - Bytes to = alloc2.fixedBytes(32); - try { - from.append8bit("0123456789 aaaaaaaaaa 0123456789 0123456789"); - - for (int i = 0; i < 4; i++) { - from.parse8bit(to, StopCharTesters.SPACE_STOP); - assertEquals(10, to.readRemaining()); - } - assertEquals(0, from.readRemaining()); - } finally { - from.releaseLast(); - to.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesInternalTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesInternalTest.java deleted file mode 100644 index 29d0ab2..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesInternalTest.java +++ /dev/null @@ -1,455 +0,0 @@ -/* - * Copyright 2016-2018-2020 chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.*; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.io.IOException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.Collection; -import java.util.Locale; -import java.util.Random; - -import static java.nio.charset.StandardCharsets.US_ASCII; -import static net.openhft.chronicle.bytes.BytesInternalTest.Nested.LENGTH; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -@SuppressWarnings({"rawtypes"}) -@RunWith(Parameterized.class) -public class BytesInternalTest extends BytesTestCommon { - - private final boolean guarded; - private ThreadDump threadDump; - - public BytesInternalTest(String name, boolean guarded) { - this.guarded = guarded; - } - - @Parameterized.Parameters(name = "{0}") - public static Collection data() { - return Arrays.asList(new Object[][]{ - {"Unguarded", false}, - {"Guarded", true} - }); - } - - @AfterClass - public static void resetGuarded() { - NativeBytes.resetNewGuarded(); - } - - @Before - public void setGuarded() { - NativeBytes.setNewGuarded(guarded); - } - - @Test - public void testParse8bitAndStringBuilderWithUtf16Coder() throws BufferUnderflowException, IOException { - @NotNull NativeBytesStore bs = NativeBytesStore.nativeStore(32); - bs.write(0, new byte[]{0x76, 0x61, 0x6c, 0x75, 0x65}); // "value" string - - StringBuilder sb = new StringBuilder(); - sb.append("你好"); - - BytesInternal.parse8bit(0, bs, sb, 5); - String actual = sb.toString(); - - assertEquals("value", actual); - assertEquals(5, actual.length()); - bs.releaseLast(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - /* - @After - public void checkRegisteredBytes() { - BytesUtil.checkRegisteredBytes(); - } - */ - - @Test - public void testParseUTF_SB1() throws UTFDataFormatRuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - @NotNull byte[] bytes2 = new byte[128]; - Arrays.fill(bytes2, (byte) '?'); - bytes.write(bytes2); - - @NotNull StringBuilder sb = new StringBuilder(); - - BytesInternal.parseUtf8(bytes, sb, true, 128); - assertEquals(128, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); - bytes.readPosition(0); - sb.setLength(0); - BytesInternal.parseUtf8(bytes, sb, false, 128); - assertEquals(128, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); - bytes.releaseLast(); - } - - @Test - public void testParseUTF8_LongString() throws UTFDataFormatRuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - @NotNull byte[] bytes2 = new byte[length]; - Arrays.fill(bytes2, (byte) '!'); - bytes.write(bytes2); - - @NotNull StringBuilder sb = new StringBuilder(); - - BytesInternal.parseUtf8(bytes, sb, true, length); - assertEquals(length, sb.length()); - String actual = sb.toString(); - sb = null; // free some memory. - assertEquals(new String(bytes2, US_ASCII), actual); - - bytes.releaseLast(); - } - - @Test - public void parseDoubleScientificNegative() { - String strDouble = "6.1E-4"; - double expected = 6.1E-4; - int expectedDp = 5; //0.00061 needs dp 5 - Bytes from = Bytes.from(strDouble); - assertEquals(expected, from.parseDouble(), 0.0); - assertEquals(expectedDp, from.lastDecimalPlaces()); - from.releaseLast(); - } - - @Test - public void parseDoubleScientificNegative1() { - String strDouble = "6.123E-4"; - double expected = 6.123E-4; - int expectedDp = 7; //0.0006123 needs dp 7 - Bytes from = Bytes.from(strDouble); - assertEquals(expected, from.parseDouble(), 0.0); - assertEquals(expectedDp, from.lastDecimalPlaces()); //Last dp should be 7. - from.releaseLast(); - } - - @Test - public void parseDoubleScientificPositive1() { - String strDouble = "6.12345E4"; - double expected = 6.12345E4; - int expectedDp = 1; //6.12345 x 10^4 = 61234.5 needs 1 - Bytes from = Bytes.from(strDouble); - assertEquals(expected, from.parseDouble(), 0.0); - assertEquals(expectedDp, from.lastDecimalPlaces()); - from.releaseLast(); - } - - @Test - public void testParseUTF81_LongString() throws UTFDataFormatRuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - @NotNull byte[] bytes2 = new byte[length]; - Arrays.fill(bytes2, (byte) '!'); - bytes.write(bytes2); - - @NotNull StringBuilder sb = new StringBuilder(); - - BytesInternal.parseUtf81(bytes, sb, true, length); - assertEquals(length, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); - - bytes.readPosition(0); - sb.setLength(0); - - BytesInternal.parseUtf81(bytes, sb, false, length); - assertEquals(length, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); - - bytes.releaseLast(); - } - - @Test - public void testParseUTF_SB1_LongString() throws UTFDataFormatRuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - @NotNull byte[] bytes2 = new byte[length]; - Arrays.fill(bytes2, (byte) '!'); - bytes.write(bytes2); - - @NotNull StringBuilder sb = new StringBuilder(); - - BytesInternal.parseUtf8_SB1(bytes, sb, true, length); - assertEquals(length, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); - - bytes.readPosition(0); - sb.setLength(0); - -/* - BytesInternal.parseUtf8_SB1(bytes, sb, false, length); - assertEquals(length, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); -*/ - - bytes.releaseLast(); - } - - @Test - public void testParse8bit_LongString() throws Exception { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - @NotNull byte[] bytes2 = new byte[length]; - Arrays.fill(bytes2, (byte) '!'); - bytes.write(bytes2); - - @NotNull StringBuilder sb = new StringBuilder(); - - BytesInternal.parse8bit(0, bytes, sb, length); - assertEquals(length, sb.length()); - assertEquals(new String(bytes2, US_ASCII), sb.toString()); - - bytes.releaseLast(); - } - - @Test - public void testAllParseDouble() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - for (String s : "0.,1.,9.".split(",")) { - // todo FIX for i == 7 && d == 8 - for (int d = 0; d < 8; d++) { - s += '0'; - for (int i = 1; i < 10; i += 2) { - String si = s + i; - Bytes from = Bytes.from(si); - assertEquals(si, - Double.parseDouble(si), - from.parseDouble(), 0.0); - from.releaseLast(); - } - } - } - } - - @Test - public void testWriteUtf8LongString() throws IORuntimeException { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - StringBuilder sb = new StringBuilder(length); - - for (int i = 0; i < length; i++) - sb.append('!'); - - String test = sb.toString(); - BytesInternal.writeUtf8(bytes, test); - - sb.setLength(0); - assertTrue(BytesInternal.compareUtf8(bytes, 0, test)); - - bytes.releaseLast(); - } - - @Test - public void testAppendUtf8LongString() throws Exception { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - StringBuilder sb = new StringBuilder(length); - - for (int i = 0; i < length; i++) - sb.append('!'); - - String test = sb.toString(); - BytesInternal.appendUtf8(bytes, test, 0, length); - - sb.setLength(0); - BytesInternal.parse8bit(0, bytes, sb, length); - - assertEquals(test, sb.toString()); - bytes.releaseLast(); - } - - @Test - public void testAppend8bitLongString() throws Exception { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull VanillaBytes bytes = Bytes.allocateElasticDirect(); - int length = LENGTH; - StringBuilder sb = new StringBuilder(length); - - for (int i = 0; i < length; i++) - sb.append('!'); - - String test = sb.toString(); - BytesInternal.append8bit(0, bytes, test, 0, length); - - sb.setLength(0); - BytesInternal.parse8bit(0, bytes, sb, length); - - assertEquals(test, sb.toString()); - bytes.releaseLast(); - } - - @Test - public void testCompareUTF() throws IORuntimeException { - @NotNull NativeBytesStore bs = NativeBytesStore.nativeStore(32); - bs.writeUtf8(0, "test"); - assertTrue(BytesInternal.compareUtf8(bs, 0, "test")); - assertFalse(BytesInternal.compareUtf8(bs, 0, null)); - - bs.writeUtf8(0, null); - assertTrue(BytesInternal.compareUtf8(bs, 0, null)); - assertFalse(BytesInternal.compareUtf8(bs, 0, "test")); - - bs.writeUtf8(1, "£€"); - @NotNull StringBuilder sb = new StringBuilder(); - bs.readUtf8(1, sb); - assertEquals("£€", sb.toString()); - assertTrue(BytesInternal.compareUtf8(bs, 1, "£€")); - assertFalse(BytesInternal.compareUtf8(bs, 1, "£")); - assertFalse(BytesInternal.compareUtf8(bs, 1, "£€$")); - bs.releaseLast(); - } - - @Test - public void shouldHandleDifferentSizedStores() { - Bytes bytes = Bytes.elasticHeapByteBuffer(32); - final BytesStore storeOfThirtyTwoBytes = bytes.bytesStore(); - storeOfThirtyTwoBytes.writeUtf8(0, "thirty_two_bytes_of_utf8_chars_"); - - Bytes bytes2 = Bytes.elasticHeapByteBuffer(512); - final BytesStore longerBuffer = bytes2.bytesStore(); - longerBuffer.writeUtf8(0, "thirty_two_bytes_of_utf8_chars_"); - - assertTrue(BytesInternal.equalBytesAny(storeOfThirtyTwoBytes, longerBuffer, 32)); - bytes2.releaseLast(); - bytes.releaseLast(); - } - - @Test - public void testParseDouble() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - @NotNull Object[][] tests = { - {"-1E-3 ", -1E-3}, - {"12E3 ", 12E3}, - {"-1.1E-3 ", -1.1E-3}, - {"-1.1E3 ", -1.1E3}, - {"-1.16823E70 ", -1.16823E70}, - {"1.17045E70 ", 1.17045E70}, - {"6.85202", 6.85202} - }; - for (Object[] objects : tests) { - @NotNull String text = (String) objects[0]; - double expected = (Double) objects[1]; - - Bytes from = Bytes.from(text); - assertEquals(expected, from.parseDouble(), 0.0); - from.releaseLast(); - } - } - - private int checkParse(int different, String s) { - double d = Double.parseDouble(s); - Bytes from = Bytes.from(s); - double d2 = from.parseDouble(); - from.releaseLast(); - if (d != d2) { -// System.out.println(d + " != " + d2); - ++different; - } - return different; - } - - @Test - public void testWritingDecimalVsJava() { - Bytes bytes = Bytes.allocateElasticOnHeap(32); - bytes.clear(); - double d = 0.04595828484241039; //Math.pow(1e9, rand.nextDouble()) / 1e3; - bytes.append(d); - String s = Double.toString(d); - if (s.length() != bytes.readRemaining()) { - assertEquals(d, Double.parseDouble(s), 0.0); - String s2 = bytes.toString(); -// System.out.println(s + " != " + s2); - } - bytes.releaseLast(); - } - - @Test - public void bytesParseDouble_Issue85_SeededRandom() { - assumeFalse(GuardedNativeBytes.areNewGuarded()); - Random random = new Random(1); - int different = 0; - int max = 10_000; - for (int i = 0; i < max; i++) { - double num = random.nextDouble(); - String s = String.format(Locale.US, "%.9f", num); - different = checkParse(different, s); - } - Assert.assertEquals("Different " + (100.0 * different) / max + "%", 0, different); - } - - @Test - public void contentsEqual() { - Bytes a = Bytes.elasticByteBuffer(9, 20) - .append("Hello") - .readLimit(16); - Bytes b = Bytes.elasticByteBuffer(5, 20) - .append("Hello") - .readLimit(16); - Bytes c = Bytes.elasticByteBuffer(15, 20) - .append("Hello") - .readLimit(16); - assertEquals("Hello\0\0\0\0\0\0\0\0\0\0\0", a.toString()); - assertEquals("Hello\0\0\0\0\0\0\0\0\0\0\0", b.toString()); - assertEquals("Hello\0\0\0\0\0\0\0\0\0\0\0", c.toString()); - assertEquals(a, b); - assertEquals(b, c); - assertEquals(c, a); - a.releaseLast(); - b.releaseLast(); - c.releaseLast(); - } - - static class Nested { - public static final int LENGTH; - - static { - long maxMemory = Runtime.getRuntime().maxMemory(); - int maxLength = OS.isLinux() ? 1 << 30 : 1 << 28; - LENGTH = (int) Math.min(maxMemory / 16, maxLength); - if (LENGTH < maxLength) - System.out.println("Not enough memory to run big test, was " + (LENGTH >> 20) + " MB."); - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesMarshallableTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesMarshallableTest.java deleted file mode 100644 index 64667df..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesMarshallableTest.java +++ /dev/null @@ -1,567 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.io.IOException; -import java.lang.annotation.RetentionPolicy; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZonedDateTime; -import java.util.*; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assume.assumeFalse; - -@RunWith(Parameterized.class) -public class BytesMarshallableTest extends BytesTestCommon { - - private final boolean guarded; - - public BytesMarshallableTest(String name, boolean guarded) { - this.guarded = guarded; - } - - @Parameterized.Parameters(name = "{0}") - public static Collection data() { - return Arrays.asList(new Object[][]{ - {"Unguarded", false}, - {"Guarded", true} - }); - } - - @AfterClass - public static void resetGuarded() { - NativeBytes.resetNewGuarded(); - } - - @Before - public void setGuarded() { - NativeBytes.setNewGuarded(guarded); - } - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Test - public void serializePrimitives() { - assumeFalse(NativeBytes.areNewGuarded()); - final Bytes bytes = new HexDumpBytes(); - try { - final MyByteable mb1 = new MyByteable(false, (byte) 1, (short) 2, '3', 4, 5.5f, 6, 7.7); - final MyByteable mb2 = new MyByteable(true, (byte) 11, (short) 22, 'T', 44, 5.555f, 66, 77.77); - bytes.comment("mb1").writeUnsignedByte(1); - mb1.writeMarshallable(bytes); - bytes.comment("mb2").writeUnsignedByte(2); - mb2.writeMarshallable(bytes); - - assertEquals( - "01 # mb1\n" + - " 4e # flag\n" + - " 01 # b\n" + - " 02 00 # s\n" + - " 33 # c\n" + - " 04 00 00 00 # i\n" + - " 00 00 b0 40 # f\n" + - " 06 00 00 00 00 00 00 00 # l\n" + - " cd cc cc cc cc cc 1e 40 # d\n" + - "02 # mb2\n" + - " 59 # flag\n" + - " 0b # b\n" + - " 16 00 # s\n" + - " 54 # c\n" + - " 2c 00 00 00 # i\n" + - " 8f c2 b1 40 # f\n" + - " 42 00 00 00 00 00 00 00 # l\n" + - " e1 7a 14 ae 47 71 53 40 # d\n", bytes.toHexString()); - - final MyByteable mb3 = new MyByteable(); - final MyByteable mb4 = new MyByteable(); - assertEquals(1, bytes.readUnsignedByte()); - mb3.readMarshallable(bytes); - assertEquals(2, bytes.readUnsignedByte()); - mb4.readMarshallable(bytes); - - assertEquals(mb1.toString(), mb3.toString()); - assertEquals(mb2.toString(), mb4.toString()); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void serializeScalars() { - final Bytes bytes = new HexDumpBytes(); - try { - final MyScalars mb1 = new MyScalars("Hello", BigInteger.ONE, BigDecimal.TEN, LocalDate.now(), LocalTime.now(), LocalDateTime.now(), ZonedDateTime.now(), UUID.randomUUID()); - final MyScalars mb2 = new MyScalars("World", BigInteger.ZERO, BigDecimal.ZERO, LocalDate.now(), LocalTime.now(), LocalDateTime.now(), ZonedDateTime.now(), UUID.randomUUID()); - bytes.comment("mb1").writeUnsignedByte(1); - mb1.writeMarshallable(bytes); - bytes.comment("mb2").writeUnsignedByte(2); - mb2.writeMarshallable(bytes); - -/* - assertEquals( - "01 # mb1\n" + - " 05 48 65 6c 6c 6f # s\n" + - " 01 31 # bi\n" + - " 02 31 30 # bd\n" + - " 0a 32 30 31 37 2d 31 31 2d 30 36 # date\n" + - " 0c 31 32 3a 32 37 3a 34 34 2e 33 33 30 # time\n" + - " 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a 32 # dateTime\n" + - " 37 3a 34 34 2e 33 33 30 27 32 30 31 37 2d 31 31 # zonedDateTime\n" + - " 2d 30 36 54 31 32 3a 32 37 3a 34 34 2e 33 33 31\n" + - " 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f 6e 5d # uuid\n" + - " 24 63 35 66 33 34 62 39 63 2d 36 34 35 34 2d 34\n" + - " 62 61 63 2d 61 32 66 37 2d 66 37 31 36 35 32 33\n" + - " 62 62 32 64 33\n" + - "02 # mb2\n" + - " 05 57 6f 72 6c 64 # s\n" + - " 01 30 # bi\n" + - " 01 30 # bd\n" + - " 0a 32 30 31 37 2d 31 31 2d 30 36 # date\n" + - " 0c 31 32 3a 32 37 3a 34 34 2e 33 33 36 # time\n" + - " 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a 32 # dateTime\n" + - " 37 3a 34 34 2e 33 33 36 27 32 30 31 37 2d 31 31 # zonedDateTime\n" + - " 2d 30 36 54 31 32 3a 32 37 3a 34 34 2e 33 33 36\n" + - " 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f 6e 5d # uuid\n" + - " 24 32 65 61 35 66 33 34 35 2d 36 65 38 30 2d 34\n" + - " 35 66 30 2d 62 66 62 64 2d 63 33 30 37 34 34 33\n" + - " 65 32 38 61 34\n", bytes.toHexString()); -*/ - final Bytes bytes2 = HexDumpBytes.fromText(bytes.toHexString()); - try { - doSerializeScalars(bytes, mb1, mb2); - doSerializeScalars(bytes2, mb1, mb2); - } finally { - bytes2.releaseLast(); - } - } finally { - bytes.releaseLast(); - } - } - - public void doSerializeScalars(@NotNull final Bytes bytes, - @NotNull final MyScalars mb1, - @NotNull final MyScalars mb2) { - final MyScalars mb3 = new MyScalars(); - final MyScalars mb4 = new MyScalars(); - assertEquals(1, bytes.readUnsignedByte()); - mb3.readMarshallable(bytes); - assertEquals(2, bytes.readUnsignedByte()); - mb4.readMarshallable(bytes); - - assertEquals(mb1.toString(), mb3.toString()); - assertEquals(mb2.toString(), mb4.toString()); - } - - @Test - public void serializeNested() { - final Bytes bytes = new HexDumpBytes(); - try { - - final MyByteable mb1 = new MyByteable(false, (byte) 1, (short) 2, '3', 4, 5.5f, 6, 7.7); - final MyByteable mb2 = new MyByteable(true, (byte) 11, (short) 22, 'T', 44, 5.555f, 66, 77.77); - final ZonedDateTime zdt1 = ZonedDateTime.parse("2017-11-06T12:35:56.775Z[Europe/London]"); - final ZonedDateTime zdt2 = ZonedDateTime.parse("2016-10-05T01:34:56.775Z[Europe/London]"); - final UUID uuid1 = new UUID(0x123456789L, 0xABCDEF); - final UUID uuid2 = new UUID(0x1111111111111111L, 0x2222222222222222L); - final MyScalars ms1 = new MyScalars("Hello", BigInteger.ONE, BigDecimal.TEN, zdt1.toLocalDate(), zdt1.toLocalTime(), zdt1.toLocalDateTime(), zdt1, uuid1); - final MyScalars ms2 = new MyScalars("World", BigInteger.ZERO, BigDecimal.ZERO, zdt2.toLocalDate(), zdt2.toLocalTime(), zdt2.toLocalDateTime(), zdt2, uuid2); - final MyNested mn1 = new MyNested(mb1, ms1); - final MyNested mn2 = new MyNested(mb2, ms2); - bytes.comment("mn1").writeUnsignedByte(1); - mn1.writeMarshallable(bytes); - bytes.comment("mn2").writeUnsignedByte(2); - mn2.writeMarshallable(bytes); - - String expected = "01 # mn1\n" + - " # byteable\n" + - " 4e # flag\n" + - " 01 # b\n" + - " 02 00 # s\n" + - " 33 # c\n" + - " 04 00 00 00 # i\n" + - " 00 00 b0 40 # f\n" + - " 06 00 00 00 00 00 00 00 # l\n" + - " cd cc cc cc cc cc 1e 40 # d\n" + - " # scalars\n" + - " 05 48 65 6c 6c 6f # s\n" + - " 01 31 # bi\n" + - " 02 31 30 # bd\n" + - " 0a 32 30 31 37 2d 31 31 2d 30 36 # date\n" + - " 0c 31 32 3a 33 35 3a 35 36 2e 37 37 35 # time\n" + - " 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a 33 # dateTime\n" + - " 35 3a 35 36 2e 37 37 35 27 32 30 31 37 2d 31 31 # zonedDateTime\n" + - " 2d 30 36 54 31 32 3a 33 35 3a 35 36 2e 37 37 35\n" + - " 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f 6e 5d # uuid\n" + - " 24 30 30 30 30 30 30 30 31 2d 32 33 34 35 2d 36\n" + - " 37 38 39 2d 30 30 30 30 2d 30 30 30 30 30 30 61\n" + - " 62 63 64 65 66\n" + - "02 # mn2\n" + - " # byteable\n" + - " 59 # flag\n" + - " 0b # b\n" + - " 16 00 # s\n" + - " 54 # c\n" + - " 2c 00 00 00 # i\n" + - " 8f c2 b1 40 # f\n" + - " 42 00 00 00 00 00 00 00 # l\n" + - " e1 7a 14 ae 47 71 53 40 # d\n" + - " # scalars\n" + - " 05 57 6f 72 6c 64 # s\n" + - " 01 30 # bi\n" + - " 01 30 # bd\n" + - " 0a 32 30 31 36 2d 31 30 2d 30 35 # date\n" + - (Jvm.isJava9Plus() ? - " 0c 30 32 3a 33 34 3a 35 36 2e 37 37 35 # time\n" : - " 0c 30 31 3a 33 34 3a 35 36 2e 37 37 35 # time\n") + - (Jvm.isJava9Plus() ? - " 17 32 30 31 36 2d 31 30 2d 30 35 54 30 32 3a 33 # dateTime\n" : - " 17 32 30 31 36 2d 31 30 2d 30 35 54 30 31 3a 33 # dateTime\n") + - " 34 3a 35 36 2e 37 37 35 2c 32 30 31 36 2d 31 30 # zonedDateTime\n" + - (Jvm.isJava9Plus() ? - " 2d 30 35 54 30 32 3a 33 34 3a 35 36 2e 37 37 35\n" : - " 2d 30 35 54 30 31 3a 33 34 3a 35 36 2e 37 37 35\n") + - " 2b 30 31 3a 30 30 5b 45 75 72 6f 70 65 2f 4c 6f\n" + - " 6e 64 6f 6e 5d 24 31 31 31 31 31 31 31 31 2d 31 # uuid\n" + - " 31 31 31 2d 31 31 31 31 2d 32 32 32 32 2d 32 32\n" + - " 32 32 32 32 32 32 32 32 32 32\n"; - - if (GuardedNativeBytes.areNewGuarded()) { - expected = "" + - "a4 01 # mn1\n" + - " # byteable\n" + - " a4 4e # flag\n" + - " a4 01 # b\n" + - " a5 02 00 # s\n" + - " ae 33 # c\n" + - " a6 04 00 00 00 # i\n" + - " 90 00 00 b0 40 # f\n" + - " a7 06 00 00 00 00 00 00 00 # l\n" + - " 91 cd cc cc cc cc cc 1e 40 # d\n" + - " # scalars\n" + - " ae 05 48 65 6c 6c 6f # s\n" + - " ae 01 31 # bi\n" + - " ae 02 31 30 # bd\n" + - " ae 0a 32 30 31 37 2d 31 31 2d 30 36 # date\n" + - " ae 0c 31 32 3a 33 35 3a 35 36 2e 37 37 35 # time\n" + - " ae 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a # dateTime\n" + - " 33 35 3a 35 36 2e 37 37 35 ae 27 32 30 31 37 2d # zonedDateTime\n" + - " 31 31 2d 30 36 54 31 32 3a 33 35 3a 35 36 2e 37\n" + - " 37 35 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f\n" + - " 6e 5d ae 24 30 30 30 30 30 30 30 31 2d 32 33 34 # uuid\n" + - " 35 2d 36 37 38 39 2d 30 30 30 30 2d 30 30 30 30\n" + - " 30 30 61 62 63 64 65 66\n" + - "a4 02 # mn2\n" + - " # byteable\n" + - " a4 59 # flag\n" + - " a4 0b # b\n" + - " a5 16 00 # s\n" + - " ae 54 # c\n" + - " a6 2c 00 00 00 # i\n" + - " 90 8f c2 b1 40 # f\n" + - " a7 42 00 00 00 00 00 00 00 # l\n" + - " 91 e1 7a 14 ae 47 71 53 40 # d\n" + - " # scalars\n" + - " ae 05 57 6f 72 6c 64 # s\n" + - " ae 01 30 # bi\n" + - " ae 01 30 # bd\n" + - " ae 0a 32 30 31 36 2d 31 30 2d 30 35 # date\n" + - (Jvm.isJava9Plus() ? "" + - " ae 0c 30 32 3a 33 34 3a 35 36 2e 37 37 35 # time\n" + - " ae 17 32 30 31 36 2d 31 30 2d 30 35 54 30 32 3a # dateTime\n" + - " 33 34 3a 35 36 2e 37 37 35 ae 2c 32 30 31 36 2d # zonedDateTime\n" + - " 31 30 2d 30 35 54 30 32 3a 33 34 3a 35 36 2e 37\n" + - " 37 35 2b 30 31 3a 30 30 5b 45 75 72 6f 70 65 2f\n" - : "" + - " ae 0c 30 31 3a 33 34 3a 35 36 2e 37 37 35 # time\n" + - " ae 17 32 30 31 36 2d 31 30 2d 30 35 54 30 31 3a # dateTime\n" + - " 33 34 3a 35 36 2e 37 37 35 ae 2c 32 30 31 36 2d # zonedDateTime\n" + - " 31 30 2d 30 35 54 30 31 3a 33 34 3a 35 36 2e 37\n" + - " 37 35 2b 30 31 3a 30 30 5b 45 75 72 6f 70 65 2f\n") + - " 4c 6f 6e 64 6f 6e 5d ae 24 31 31 31 31 31 31 31 # uuid\n" + - " 31 2d 31 31 31 31 2d 31 31 31 31 2d 32 32 32 32\n" + - " 2d 32 32 32 32 32 32 32 32 32 32 32 32\n"; - } - - //System.out.println(bytes.toHexString()); - - assertEquals( - expected, bytes.toHexString()); - - final MyNested mn3 = new MyNested(); - final MyNested mn4 = new MyNested(); - assertEquals(1, bytes.readUnsignedByte()); - mn3.readMarshallable(bytes); - assertEquals(2, bytes.readUnsignedByte()); - mn4.readMarshallable(bytes); - - assertEquals(mn1.toString(), mn3.toString()); - assertEquals(mn2.toString(), mn4.toString()); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void serializeBytes() throws IOException { - Bytes bytes = new HexDumpBytes(); - final Bytes hello = Bytes.from("hello"); - final Bytes byeee = Bytes.from("byeee"); - try (MyBytes mb1 = new MyBytes(hello, Bytes.allocateElasticDirect().append("2")); - MyBytes mb2 = new MyBytes(byeee, null)) { - bytes.comment("mb1").writeUnsignedByte(1); - mb1.writeMarshallable(bytes); - bytes.comment("mb2").writeUnsignedByte(2); - mb2.writeMarshallable(bytes); - - final Bytes bytes2 = HexDumpBytes.fromText(bytes.toHexString()); - for (int i = 0; i < 2; i++) { - try (MyBytes mb3 = new MyBytes(); - MyBytes mb4 = new MyBytes(Bytes.from("already"), Bytes.allocateElasticDirect().append("value"))) { - assertEquals(1, bytes.readUnsignedByte()); - mb3.readMarshallable(bytes); - assertEquals(2, bytes.readUnsignedByte()); - mb4.readMarshallable(bytes); - - assertEquals(mb1.toString(), mb3.toString()); - assertEquals(mb2.toString(), mb4.toString()); - - bytes.releaseLast(); - - bytes = bytes2; - } - } - } - } - - @Test - public void serializeCollections() { - final Bytes bytes = new HexDumpBytes(); - try { - final MyCollections mc = new MyCollections(); - mc.words.add("Hello"); - mc.words.add("World"); - mc.scoreCountMap.put(1.3, 11L); - mc.scoreCountMap.put(2.2, 22L); - mc.policies.add(RetentionPolicy.RUNTIME); - mc.policies.add(RetentionPolicy.CLASS); - mc.numbers.add(1); - mc.numbers.add(12); - mc.numbers.add(123); - mc.writeMarshallable(bytes); - - final MyCollections mc2 = new MyCollections(); - mc2.readMarshallable(bytes); - assertEquals(mc.words, mc2.words); - assertEquals(mc.scoreCountMap, mc2.scoreCountMap); - assertEquals(mc.policies, mc2.policies); - assertEquals(mc.numbers, mc2.numbers); - - final String expected = "" + - " 02 05 48 65 6c 6c 6f 05 57 6f 72 6c 64 # words\n" + - " 02 cd cc cc cc cc cc f4 3f 0b 00 00 00 00 00 00 # scoreCountMap\n" + - " 00 9a 99 99 99 99 99 01 40 16 00 00 00 00 00 00\n" + - " 00 02 07 52 55 4e 54 49 4d 45 05 43 4c 41 53 53 # policies\n" + - " 03 01 00 00 00 0c 00 00 00 7b 00 00 00 # numbers\n"; - final String expectedG = "" + - " ae 02 ae 05 48 65 6c 6c 6f ae 05 57 6f 72 6c 64 # words\n" + - " ae 02 91 cd cc cc cc cc cc f4 3f a7 0b 00 00 00 # scoreCountMap\n" + - " 00 00 00 00 91 9a 99 99 99 99 99 01 40 a7 16 00\n" + - " 00 00 00 00 00 00 ae 02 ae 07 52 55 4e 54 49 4d # policies\n" + - " 45 ae 05 43 4c 41 53 53 ae 03 a6 01 00 00 00 a6 # numbers\n" + - " 0c 00 00 00 a6 7b 00 00 00\n"; - assertEquals(NativeBytes.areNewGuarded() ? expectedG : expected, bytes.toHexString()); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void nested() { - final Bytes bytes = new HexDumpBytes(); - try { - final BM1 bm1 = new BM1(); - bm1.num = 5; - bm1.bm2.text = "hello"; - bm1.bm3.value = 1234567890L; - - bm1.writeMarshallable(bytes); - - final BM1 bm1b = new BM1(); - bm1b.readMarshallable(bytes); - assertEquals(bm1b.bm2.text, bm1.bm2.text); - assertEquals(bm1b.bm3.value, bm1.bm3.value); - - final String expected = "" + - " 05 00 00 00 # num\n" + - " # bm2\n" + - " 05 68 65 6c 6c 6f # text\n" + - " # bm3\n" + - " d2 02 96 49 00 00 00 00 # value\n"; - final String expected2 = "" + - " a6 05 00 00 00 # num\n" + - " # bm2\n" + - " ae 05 68 65 6c 6c 6f # text\n" + - " # bm3\n" + - " a7 d2 02 96 49 00 00 00 00 # value\n"; - assertEquals(NativeBytes.areNewGuarded() ? expected2 : expected, bytes.toHexString()); - final String expectedB = "" + - "# net.openhft.chronicle.bytes.BytesMarshallableTest$BM1\n" + - " 05 00 00 00 # num\n" + - " # bm2\n" + - " 05 68 65 6c 6c 6f # text\n" + - " # bm3\n" + - " d2 02 96 49 00 00 00 00 # value\n"; - final String expectedBG = "" + - "# net.openhft.chronicle.bytes.BytesMarshallableTest$BM1\n" + - " a6 05 00 00 00 # num\n" + - " # bm2\n" + - " ae 05 68 65 6c 6c 6f # text\n" + - " # bm3\n" + - " a7 d2 02 96 49 00 00 00 00 # value\n"; - assertEquals(NativeBytes.areNewGuarded() ? expectedBG : expectedB, bm1.toString()); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void nullArrays() { - final Bytes bytes = new HexDumpBytes(); - try { - final BMA bma = new BMA(); - bma.writeMarshallable(bytes); - final String expected = NativeBytes.areNewGuarded() ? - " a6 ff ff ff ff # bytes\n" + - " a6 ff ff ff ff # ints\n" + - " a6 ff ff ff ff # floats\n" + - " a6 ff ff ff ff # longs\n" + - " a6 ff ff ff ff # doubles\n" : - " ff ff ff ff # bytes\n" + - " ff ff ff ff # ints\n" + - " ff ff ff ff # floats\n" + - " ff ff ff ff # longs\n" + - " ff ff ff ff # doubles\n"; - assertEquals(expected, bytes.toHexString()); - final BMA bma2 = new BMA(); - bma2.bytes = new byte[0]; - bma2.ints = new int[0]; - bma2.floats = new float[0]; - bma2.longs = new long[0]; - bma2.doubles = new double[0]; - bma2.readMarshallable(bytes); - assertNull(bma2.longs); - assertNull(bma2.doubles); - } finally { - bytes.releaseLast(); - } - - } - - @Test - public void arrays() { - final Bytes bytes = new HexDumpBytes(); - try { - final BMA bma = new BMA(); - bma.bytes = "Hello".getBytes(); - bma.ints = new int[]{0x12345678}; - bma.floats = new float[]{0x1.234567p0f}; - bma.longs = new long[]{0x123456789ABCDEFL}; - bma.doubles = new double[]{0x1.23456789ABCDEp0}; - bma.writeMarshallable(bytes); - final String expected = NativeBytes.areNewGuarded() ? - " a6 05 00 00 00 a4 48 a4 65 a4 6c a4 6c a4 6f # bytes\n" + - " a6 01 00 00 00 a6 78 56 34 12 # ints\n" + - " a6 01 00 00 00 90 b4 a2 91 3f # floats\n" + - " a6 01 00 00 00 a7 ef cd ab 89 67 45 23 01 # longs\n" + - " a6 01 00 00 00 91 de bc 9a 78 56 34 f2 3f # doubles\n" : - " 05 00 00 00 48 65 6c 6c 6f # bytes\n" + - " 01 00 00 00 78 56 34 12 # ints\n" + - " 01 00 00 00 b4 a2 91 3f # floats\n" + - " 01 00 00 00 ef cd ab 89 67 45 23 01 # longs\n" + - " 01 00 00 00 de bc 9a 78 56 34 f2 3f # doubles\n"; - assertEquals(expected, bytes.toHexString()); - final BMA bma2 = new BMA(); - bma2.longs = new long[0]; - bma2.doubles = new double[0]; - bma2.readMarshallable(bytes); - assertEquals("[72, 101, 108, 108, 111]", Arrays.toString(bma2.bytes)); - assertEquals("[305419896]", Arrays.toString(bma2.ints)); - assertEquals("[1.1377778]", Arrays.toString(bma2.floats)); - assertEquals("[81985529216486895]", Arrays.toString(bma2.longs)); - assertEquals("[1.1377777777777776]", Arrays.toString(bma2.doubles)); - assertEquals(0x123456789ABCDEFL, bma2.longs[0]); - assertEquals(0x1.23456789ABCDEp0, bma2.doubles[0], 0); - } finally { - bytes.releaseLast(); - } - } - - private static final class MyCollections implements BytesMarshallable { - List words = new ArrayList<>(); - Map scoreCountMap = new LinkedHashMap<>(); - List policies = new ArrayList<>(); - List numbers = new ArrayList<>(); - } - - private static final class BM1 implements BytesMarshallable { - int num; - BM2 bm2 = new BM2(); - BM3 bm3 = new BM3(); - - @Override - public String toString() { - return $toString(); - } - } - - private static final class BM2 implements BytesMarshallable { - String text; - } - - private static final class BM3 implements BytesMarshallable { - long value; - } - - private static final class BMA implements BytesMarshallable { - byte[] bytes; - int[] ints; - float[] floats; - long[] longs; - double[] doubles; - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesMethodWriterBuilderTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesMethodWriterBuilderTest.java deleted file mode 100644 index ab25e54..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesMethodWriterBuilderTest.java +++ /dev/null @@ -1,137 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.Mocker; -import org.junit.Test; - -import java.io.StringWriter; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.time.ZonedDateTime; -import java.util.UUID; - -import static junit.framework.TestCase.assertEquals; -import static junit.framework.TestCase.assertTrue; -import static org.junit.Assert.assertFalse; -import static org.junit.Assume.assumeFalse; - -public class BytesMethodWriterBuilderTest extends BytesTestCommon { - - @Test - public void testPrimitives() { - assumeFalse(NativeBytes.areNewGuarded()); - final Bytes bytes = new HexDumpBytes(); - try { - final IBytesMethod m = bytes.bytesMethodWriter(IBytesMethod.class); - - final MyByteable mb1 = new MyByteable(false, (byte) 1, (short) 2, '3', 4, 5.5f, 6, 7.7); - final MyByteable mb2 = new MyByteable(true, (byte) 11, (short) 22, 'T', 44, 5.555f, 66, 77.77); - - m.myByteable(mb1); - m.myByteable(mb2); - - final ZonedDateTime zdt1 = ZonedDateTime.parse("2017-11-06T12:35:56.775Z[Europe/London]"); - final ZonedDateTime zdt2 = ZonedDateTime.parse("2016-10-05T01:34:56.775Z[Europe/London]"); - final UUID uuid1 = new UUID(0x123456789L, 0xABCDEF); - final UUID uuid2 = new UUID(0x1111111111111111L, 0x2222222222222222L); - final MyScalars ms1 = new MyScalars("Hello", BigInteger.ONE, BigDecimal.TEN, zdt1.toLocalDate(), zdt1.toLocalTime(), zdt1.toLocalDateTime(), zdt1, uuid1); - final MyScalars ms2 = new MyScalars("World", BigInteger.ZERO, BigDecimal.ZERO, zdt2.toLocalDate(), zdt2.toLocalTime(), zdt2.toLocalDateTime(), zdt2, uuid2); - final MyNested mn2 = new MyNested(mb2, ms2); - - m.myScalars(ms1); - - m.myNested(mn2); - - assertEquals("" + - "81 01 # myByteable\n" + - " 4e # flag\n" + - " 01 # b\n" + - " 02 00 # s\n" + - " 33 # c\n" + - " 04 00 00 00 # i\n" + - " 00 00 b0 40 # f\n" + - " 06 00 00 00 00 00 00 00 # l\n" + - " cd cc cc cc cc cc 1e 40 # d\n" + - "81 01 # myByteable\n" + - " 59 # flag\n" + - " 0b # b\n" + - " 16 00 # s\n" + - " 54 # c\n" + - " 2c 00 00 00 # i\n" + - " 8f c2 b1 40 # f\n" + - " 42 00 00 00 00 00 00 00 # l\n" + - " e1 7a 14 ae 47 71 53 40 # d\n" + - "82 01 # myScalars\n" + - " 05 48 65 6c 6c 6f # s\n" + - " 01 31 # bi\n" + - " 02 31 30 # bd\n" + - " 0a 32 30 31 37 2d 31 31 2d 30 36 # date\n" + - " 0c 31 32 3a 33 35 3a 35 36 2e 37 37 35 # time\n" + - " 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a 33 # dateTime\n" + - " 35 3a 35 36 2e 37 37 35 27 32 30 31 37 2d 31 31 # zonedDateTime\n" + - " 2d 30 36 54 31 32 3a 33 35 3a 35 36 2e 37 37 35\n" + - " 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f 6e 5d # uuid\n" + - " 24 30 30 30 30 30 30 30 31 2d 32 33 34 35 2d 36\n" + - " 37 38 39 2d 30 30 30 30 2d 30 30 30 30 30 30 61\n" + - " 62 63 64 65 66\n" + - "83 01 # myNested\n" + - " # byteable\n" + - " 59 # flag\n" + - " 0b # b\n" + - " 16 00 # s\n" + - " 54 # c\n" + - " 2c 00 00 00 # i\n" + - " 8f c2 b1 40 # f\n" + - " 42 00 00 00 00 00 00 00 # l\n" + - " e1 7a 14 ae 47 71 53 40 # d\n" + - " # scalars\n" + - " 05 57 6f 72 6c 64 # s\n" + - " 01 30 # bi\n" + - " 01 30 # bd\n" + - " 0a 32 30 31 36 2d 31 30 2d 30 35 # date\n" + - (Jvm.isJava9Plus() ? - " 0c 30 32 3a 33 34 3a 35 36 2e 37 37 35 # time\n" : - " 0c 30 31 3a 33 34 3a 35 36 2e 37 37 35 # time\n") + - (Jvm.isJava9Plus() ? - " 17 32 30 31 36 2d 31 30 2d 30 35 54 30 32 3a 33 # dateTime\n" : - " 17 32 30 31 36 2d 31 30 2d 30 35 54 30 31 3a 33 # dateTime\n") + - " 34 3a 35 36 2e 37 37 35 2c 32 30 31 36 2d 31 30 # zonedDateTime\n" + - (Jvm.isJava9Plus() ? - " 2d 30 35 54 30 32 3a 33 34 3a 35 36 2e 37 37 35\n" : - " 2d 30 35 54 30 31 3a 33 34 3a 35 36 2e 37 37 35\n") + - " 2b 30 31 3a 30 30 5b 45 75 72 6f 70 65 2f 4c 6f\n" + - " 6e 64 6f 6e 5d 24 31 31 31 31 31 31 31 31 2d 31 # uuid\n" + - " 31 31 31 2d 31 31 31 31 2d 32 32 32 32 2d 32 32\n" + - " 32 32 32 32 32 32 32 32 32 32\n", bytes.toHexString()); - - final StringWriter out = new StringWriter(); - final MethodReader reader = bytes.bytesMethodReader(Mocker.logging(IBytesMethod.class, "* ", out)); - - for (int i = 0; i < 4; i++) { - assertTrue(reader.readOne()); - } - assertFalse(reader.readOne()); - - final String expected = - Jvm.isJava9Plus() ? - "* myByteable[MyByteable{flag=false, b=1, s=2, c=3, i=4, f=5.5, l=6, d=7.7}]\n" + - "* myByteable[MyByteable{flag=true, b=11, s=22, c=T, i=44, f=5.555, l=66, d=77.77}]\n" + - "* myScalars[MyScalars{s='Hello', bi=1, bd=10, date=2017-11-06, time=12:35:56.775, dateTime=2017-11-06T12:35:56.775, zonedDateTime=2017-11-06T12:35:56.775Z[Europe/London], uuid=00000001-2345-6789-0000-000000abcdef}]\n" + - "* myNested[MyNested{byteable=MyByteable{flag=true, b=11, s=22, c=T, i=44, f=5.555, l=66, d=77.77}, scalars=MyScalars{s='World', bi=0, bd=0, date=2016-10-05, time=02:34:56.775, dateTime=2016-10-05T02:34:56.775, zonedDateTime=2016-10-05T02:34:56.775+01:00[Europe/London], uuid=11111111-1111-1111-2222-222222222222}}]\n" : - "* myByteable[MyByteable{flag=false, b=1, s=2, c=3, i=4, f=5.5, l=6, d=7.7}]\n" + - "* myByteable[MyByteable{flag=true, b=11, s=22, c=T, i=44, f=5.555, l=66, d=77.77}]\n" + - "* myScalars[MyScalars{s='Hello', bi=1, bd=10, date=2017-11-06, time=12:35:56.775, dateTime=2017-11-06T12:35:56.775, zonedDateTime=2017-11-06T12:35:56.775Z[Europe/London], uuid=00000001-2345-6789-0000-000000abcdef}]\n" + - "* myNested[MyNested{byteable=MyByteable{flag=true, b=11, s=22, c=T, i=44, f=5.555, l=66, d=77.77}, scalars=MyScalars{s='World', bi=0, bd=0, date=2016-10-05, time=01:34:56.775, dateTime=2016-10-05T01:34:56.775, zonedDateTime=2016-10-05T01:34:56.775+01:00[Europe/London], uuid=11111111-1111-1111-2222-222222222222}}]\n"; - -/* System.out.println(expected); - System.out.println(out.toString().replaceAll("\n", ""));*/ - - assertEquals(expected, - out.toString().replaceAll("\r", "")); - } finally { - bytes.releaseLast(); - } - - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesStoreTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesStoreTest.java deleted file mode 100644 index c44a1f8..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesStoreTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class BytesStoreTest { - @Test - public void from() { - BytesStore from = BytesStore.from(", "); - assertEquals(2, from.capacity()); - } - - @Test - public void from2() { - assertEquals("Hello", Bytes.from("Hello").subBytes(0, 5) - .bytesForRead() - .toString()); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTest.java deleted file mode 100644 index c982385..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTest.java +++ /dev/null @@ -1,802 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferUnderflowException; -import net.openhft.chronicle.bytes.util.UTF8StringInterner; -import net.openhft.chronicle.core.FlakyTestRunner; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.BackgroundResourceReleaser; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import net.openhft.chronicle.core.util.Histogram; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.io.PrintWriter; -import java.math.BigDecimal; -import java.nio.BufferOverflowException; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.Collection; -import java.util.Locale; -import java.util.Scanner; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.Allocator.*; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -@SuppressWarnings({"rawtypes"}) -@RunWith(Parameterized.class) -public class BytesTest extends BytesTestCommon { - - private final Allocator alloc1; - private ThreadDump threadDump; - - public BytesTest(String ignored, Allocator alloc1) { - this.alloc1 = alloc1; - } - - @Parameterized.Parameters(name = "{0}") - public static Collection data() { - return Arrays.asList(new Object[][]{ - {"Native", NATIVE}, - {"Heap", HEAP}, - {"Heap ByteBuffer", BYTE_BUFFER}, - {"Native Unchecked", NATIVE_UNCHECKED}, - {"Heap Unchecked", HEAP_UNCHECKED} - }); - } - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void throwExceptionIfReleased() { - Bytes bytes = alloc1.elasticBytes(16); - ((AbstractReferenceCounted) bytes).throwExceptionIfReleased(); - bytes.releaseLast(); - try { - ((AbstractReferenceCounted) bytes).throwExceptionIfReleased(); - fail(); - } catch (IllegalStateException ise) { - // expected. - } - } - - @Test - public void writeAdv() { - Bytes bytes = alloc1.fixedBytes(32); - for (int i = 0; i < 4; i++) - bytes.writeIntAdv('1', 1); - assertEquals("1111", bytes.toString()); - bytes.releaseLast(); - } - - @Test - public void writeLongAdv() { - Bytes bytes = alloc1.fixedBytes(32); - for (int i = 0; i < 4; i++) - bytes.writeLongAdv('1', 1); - assertEquals("1111", bytes.toString()); - bytes.releaseLast(); - } - - @Test - public void testName() throws IORuntimeException { - Bytes bytes = alloc1.fixedBytes(30); - try { - long expected = 12345L; - int offset = 5; - - bytes.writeLong(offset, expected); - bytes.writePosition(offset + 8); - assertEquals(expected, bytes.readLong(offset)); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void readUnsignedByte() { - Bytes bytes = alloc1.fixedBytes(30); - try { - bytes.writeInt(0x11111111); - bytes.readLimit(1); - - assertEquals(0x11, bytes.readUnsignedByte(0)); - assertEquals(-1, bytes.peekUnsignedByte(1)); - - // as the offset is given it only needs to be under the writeLimit. - assertEquals(0x11, bytes.readUnsignedByte(1)); - - } finally { - bytes.releaseLast(); - } - } - - @Test - public void writeHistogram() { - @NotNull Bytes bytes = alloc1.elasticBytes(0xFFFFF); - @NotNull Histogram hist = new Histogram(); - hist.sample(10); - @NotNull Histogram hist2 = new Histogram(); - for (int i = 0; i < 10000; i++) - hist2.sample(i); - - bytes.writeHistogram(hist); - bytes.writeHistogram(hist2); - - @NotNull Histogram histB = new Histogram(); - @NotNull Histogram histC = new Histogram(); - bytes.readHistogram(histB); - bytes.readHistogram(histC); - - assertEquals(hist, histB); - assertEquals(hist2, histC); - bytes.releaseLast(); - } - - @Test - public void testCopy() { - Bytes bbb = alloc1.fixedBytes(1024); - try { - for (int i = 'a'; i <= 'z'; i++) - bbb.writeUnsignedByte(i); - bbb.readPositionRemaining(4, 12); - BytesStore, ByteBuffer> copy = bbb.copy(); - bbb.writeUnsignedByte(10, '0'); - assertEquals("[pos: 0, rlim: 12, wlim: 12, cap: 12 ] efghijklmnop", copy.toDebugString()); - copy.releaseLast(); - } finally { - bbb.releaseLast(); - } - } - - @Test - public void toHexString() { - Bytes bytes = alloc1.elasticBytes(1020); - try { - bytes.append("Hello World"); - assertEquals("00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 Hello Wo rld \n", bytes.toHexString()); - bytes.readLimit(bytes.realCapacity()); - assertEquals("00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 00 00 00 00 00 Hello Wo rld·····\n" + - "00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "........\n" + - "000003f0 00 00 00 00 00 00 00 00 00 00 00 00 ········ ···· \n", bytes.toHexString()); - - assertEquals("00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 00 00 00 00 00 Hello Wo rld·····\n" + - "00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "........\n" + - "000000f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "... truncated", bytes.toHexString(256)); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void fromHexString() { - assumeFalse(NativeBytes.areNewGuarded()); - Bytes bytes = alloc1.elasticBytes(260); - try { - for (int i = 0; i < 259; i++) - bytes.writeByte((byte) i); - @NotNull String s = bytes.toHexString(); - Bytes bytes2 = Bytes.fromHexString(s); - assertEquals(s, bytes2.toHexString()); - bytes2.releaseLast(); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void internRegressionTest() throws IORuntimeException { - UTF8StringInterner utf8StringInterner = new UTF8StringInterner(4096); - - Bytes bytes1 = alloc1.elasticBytes(64).append("TW-TRSY-20181217-NY572677_3256N1"); - Bytes bytes2 = alloc1.elasticBytes(64).append("TW-TRSY-20181217-NY572677_3256N15"); - utf8StringInterner.intern(bytes1); - String intern = utf8StringInterner.intern(bytes2); - assertEquals(bytes2.toString(), intern); - String intern2 = utf8StringInterner.intern(bytes1); - assertEquals(bytes1.toString(), intern2); - bytes1.releaseLast(); - bytes2.releaseLast(); - } - - @Test - public void testEqualBytesWithSecondStoreBeingLonger() throws IORuntimeException { - - BytesStore store1 = null, store2 = null; - try { - store1 = alloc1.elasticBytes(64).append("TW-TRSY-20181217-NY572677_3256N1"); - store2 = alloc1.elasticBytes(64).append("TW-TRSY-20181217-NY572677_3256N15"); - assertFalse(store1.equalBytes(store2, store2.length())); - } finally { - store1.releaseLast(); - store2.releaseLast(); - } - } - - @Test - public void testStopBitDouble() throws IORuntimeException { - Bytes b = alloc1.elasticBytes(1); - try { - testSBD(b, -0.0, "00000000 40 @ " + - " \n"); - testSBD(b, -1.0, "00000000 DF 7C ·| \n"); - testSBD(b, -12345678, "00000000 E0 D9 F1 C2 4E ····N \n"); - testSBD(b, 0.0, "00000000 00 · \n"); - testSBD(b, 1.0, "00000000 9F 7C ·| \n"); - testSBD(b, 1024, "00000000 A0 24 ·$ \n"); - testSBD(b, 1000000, "00000000 A0 CB D0 48 ···H \n"); - testSBD(b, 0.1, "00000000 9F EE B3 99 CC E6 B3 99 4D ········ M \n"); - testSBD(b, Double.NaN, "00000000 BF 7E ·~ \n"); - } finally { - b.releaseLast(); - } - } - - private void testSBD(@NotNull Bytes b, double v, String s) throws IORuntimeException { - b.clear(); - b.writeStopBit(v); - assertEquals(s, b.toHexString().toUpperCase()); - } - - @Test - public void testParseUtf8() { - Bytes bytes = alloc1.elasticBytes(1); - try { - assertEquals(1, bytes.refCount()); - bytes.appendUtf8("starting Hello World"); - @NotNull String s0 = bytes.parseUtf8(StopCharTesters.SPACE_STOP); - assertEquals("starting", s0); - @NotNull String s = bytes.parseUtf8(StopCharTesters.ALL); - assertEquals("Hello World", s); - assertEquals(1, bytes.refCount()); - } finally { - bytes.releaseLast(); - assertEquals(0, bytes.refCount()); - } - } - - @Test(expected = BufferOverflowException.class) - public void testPartialWriteArray() { - @NotNull byte[] array = "Hello World".getBytes(ISO_8859_1); - Bytes to = alloc1.fixedBytes(6); - try { - to.write(array); - } finally { - to.releaseLast(); - } - } - - @Test - public void testPartialWriteBB() { - ByteBuffer bb = ByteBuffer.wrap("Hello World".getBytes(ISO_8859_1)); - Bytes to = alloc1.fixedBytes(6); - - to.writeSome(bb); - assertEquals("World", Bytes.wrapForRead(bb).toString()); - to.releaseLast(); - } - - @Test - public void testCompact() { - assumeFalse(NativeBytes.areNewGuarded()); - Bytes from = alloc1.elasticBytes(1); - try { - from.write("Hello World"); - from.readLong(); - from.compact(); - assertEquals("rld", from.toString()); - assertEquals(0, from.readPosition()); - } finally { - from.releaseLast(); - } - } - - @Test - public void testReadIncompleteLong() { - assumeFalse(NativeBytes.areNewGuarded()); - Bytes bytes = alloc1.elasticBytes(16); - bytes.writeLong(0x0706050403020100L); - bytes.writeLong(0x0F0E0D0C0B0A0908L); - try { - assertEquals(0x0706050403020100L, bytes.readIncompleteLong()); - assertEquals(0x0F0E0D0C0B0A0908L, bytes.readIncompleteLong()); - for (int i = 0; i <= 7; i++) { - assertEquals("i: " + i, Long.toHexString(0x0B0A090807060504L >>> (i * 8)), - Long.toHexString(bytes.readPositionRemaining(4 + i, 8 - i) - .readIncompleteLong())); - } - assertEquals(0, bytes.readPositionRemaining(4, 0).readIncompleteLong()); - - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testUnwrite() { - assumeFalse(NativeBytes.areNewGuarded()); - Bytes bytes = alloc1.elasticBytes(1); - try { - for (int i = 0; i < 26; i++) { - bytes.writeUnsignedByte('A' + i); - } - assertEquals(26, bytes.writePosition()); - assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZ", bytes.toString()); - bytes.unwrite(1, 1); - assertEquals(25, bytes.writePosition()); - assertEquals("ACDEFGHIJKLMNOPQRSTUVWXYZ", bytes.toString()); - } finally { - bytes.releaseLast(); - } - } - - @Test(expected = BufferOverflowException.class) - public void testExpectNegativeOffsetAbsoluteWriteOnElasticBytesThrowsBufferOverflowException() { - Bytes bytes = alloc1.elasticBytes(4); - try { - if (bytes.unchecked()) - throw new BufferOverflowException(); - bytes.writeInt(-1, 1); - } finally { - bytes.releaseLast(); - } - } - - @Test(expected = BufferOverflowException.class) - public void testExpectNegativeOffsetAbsoluteWriteOnElasticBytesOfInsufficientCapacityThrowsBufferOverflowException() { - Bytes bytes = alloc1.elasticBytes(1); - - try { - if (bytes.unchecked()) - throw new BufferOverflowException(); - bytes.writeInt(-1, 1); - } finally { - bytes.releaseLast(); - } - } - - @Test(expected = BufferOverflowException.class) - public void testExpectNegativeOffsetAbsoluteWriteOnFixedBytesThrowsBufferOverflowException() { - Bytes bytes = alloc1.fixedBytes(4); - try { - bytes.writeInt(-1, 1); - } finally { - bytes.releaseLast(); - } - } - - @Test(expected = BufferOverflowException.class) - public void testExpectNegativeOffsetAbsoluteWriteOnFixedBytesOfInsufficientCapacityThrowsBufferOverflowException() { - Bytes bytes = alloc1.fixedBytes(1); - try { - bytes.writeInt(-1, 1); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testWriter() { - assumeFalse(NativeBytes.areNewGuarded()); - Bytes bytes = alloc1.elasticBytes(1); - @NotNull PrintWriter writer = new PrintWriter(bytes.writer()); - writer.println(1); - writer.println("Hello"); - writer.println(12.34); - writer.append('a').append('\n'); - writer.append("bye\n"); - writer.append("for now\nxxxx", 0, 8); - assertEquals("1\n" + - "Hello\n" + - "12.34\n" + - "a\n" + - "bye\n" + - "for now\n", bytes.toString().replaceAll("\r\n", "\n")); - try (@NotNull Scanner scan = new Scanner(bytes.reader())) { - scan.useLocale(Locale.ENGLISH); - assertEquals(1, scan.nextInt()); - assertEquals("", scan.nextLine()); - assertEquals("Hello", scan.nextLine()); - assertEquals(12.34, scan.nextDouble(), 0.0); - assertEquals("", scan.nextLine()); - assertEquals("a", scan.nextLine()); - assertEquals("bye", scan.nextLine()); - assertEquals("for now", scan.nextLine()); - assertFalse(scan.hasNext()); - bytes.releaseLast(); - } - } - - @Test - public void testParseUtf8High() { - assumeFalse(NativeBytes.areNewGuarded()); - @NotNull Bytes b = alloc1.elasticBytes(0xFFFFF); - for (int i = ' '; i < Character.MAX_VALUE; i++) - if (Character.isValidCodePoint(i)) - b.appendUtf8(i); - b.appendUtf8(0); - @NotNull StringBuilder sb = new StringBuilder(); - b.parseUtf8(sb, StopCharTesters.CONTROL_STOP); - sb.setLength(0); - b.readPosition(0); - b.parseUtf8(sb, (c1, c2) -> c2 <= 0); - b.releaseLast(); - } - - @Test - public void testBigDecimalBinary() { - for (double d : new double[]{1.0, 1000.0, 0.1}) { - @NotNull Bytes b = alloc1.elasticBytes(16); - b.writeBigDecimal(new BigDecimal(d)); - - @NotNull BigDecimal bd = b.readBigDecimal(); - assertEquals(new BigDecimal(d), bd); - b.releaseLast(); - } - } - - @Test - public void testBigDecimalText() { - for (double d : new double[]{1.0, 1000.0, 0.1}) { - @NotNull Bytes b = alloc1.elasticBytes(0xFFFF); - b.append(new BigDecimal(d)); - - @NotNull BigDecimal bd = b.parseBigDecimal(); - assertEquals(new BigDecimal(d), bd); - b.releaseLast(); - } - } - - @Test - public void testWithLength() { - assumeFalse(NativeBytes.areNewGuarded()); - Bytes hello = Bytes.from("hello"); - Bytes world = Bytes.from("world"); - @NotNull Bytes b = alloc1.elasticBytes(16); - b.writeWithLength(hello); - b.writeWithLength(world); - assertEquals("hello", hello.toString()); - - @NotNull Bytes b2 = alloc1.elasticBytes(16); - b.readWithLength(b2); - assertEquals("hello", b2.toString()); - b.readWithLength(b2); - assertEquals("world", b2.toString()); - - b.releaseLast(); - b2.releaseLast(); - hello.releaseLast(); - world.releaseLast(); - } - - @Test - public void testAppendBase() { - @NotNull Bytes b = alloc1.elasticBytes(16); - for (long value : new long[]{Long.MIN_VALUE, Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE, Long.MAX_VALUE}) { - for (int base : new int[]{10, 16}) { - String s = Long.toString(value, base); - b.clear().appendBase(value, base); - assertEquals(s, b.toString()); - } - } - b.releaseLast(); - } - - @Test - public void testAppendBase16() { - @NotNull Bytes b = alloc1.elasticBytes(16); - for (long value : new long[]{Long.MIN_VALUE, Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE, Long.MAX_VALUE}) { - String s = Long.toHexString(value).toLowerCase(); - b.clear().appendBase16(value); - assertEquals(s, b.toString()); - } - b.releaseLast(); - } - - @Test - public void testMove() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.append("Hello World"); - b.move(3, 1, 3); - assertEquals("Hlo o World", b.toString()); - b.move(3, 5, 3); - assertEquals("Hlo o o rld", b.toString()); - } finally { - b.releaseLast(); - } - } - - - @Test(expected = IllegalStateException.class) - public void testMove2() { - FlakyTestRunner.run(OS.isWindows(), this::doTestMove2); - } - - public void doTestMove2() { - @NotNull Bytes b = alloc1.elasticBytes(16); - - b.append("Hello World"); - b.move(3, 1, 3); - assertEquals("Hlo o World", b.toString()); - b.releaseLast(); - b.move(3, 5, 3); - } - - @Test(expected = IllegalStateException.class) - public void testMove2B() { - @NotNull Bytes b = alloc1.elasticBytes(16); - - b.append("Hello World"); - b.bytesStore().move(3, 1, 3); - assertEquals("Hlo o World", b.toString()); - b.releaseLast(); - BackgroundResourceReleaser.releasePendingResources(); - b.bytesStore().move(3, 5, 3); - } - - @Test - public void testReadPosition() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.readPosition(17); - assertTrue(b.unchecked()); - } catch (DecoratedBufferUnderflowException ex) { - assertFalse(b.unchecked()); - } finally { - b.releaseLast(); - } - } - - @Test - public void testReadPositionTooSmall() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.readPosition(-1); - assertTrue(b.unchecked()); - } catch (DecoratedBufferUnderflowException ex) { - assertFalse(b.unchecked()); - } finally { - b.releaseLast(); - } - } - - @Test - public void testReadLimit() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.readPosition(b.writeLimit() + 1); - assertTrue(b.unchecked()); - } catch (DecoratedBufferUnderflowException ex) { - assertFalse(b.unchecked()); - } finally { - b.releaseLast(); - } - } - - @Test - public void testReadLimitTooSmall() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.readPosition(b.start() - 1); - assertTrue(b.unchecked()); - } catch (DecoratedBufferUnderflowException ex) { - assertFalse(b.unchecked()); - } finally { - b.releaseLast(); - } - } - - @Test - public void uncheckedSkip() { - assumeFalse(NativeBytes.areNewGuarded()); - - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.uncheckedReadSkipOne(); - assertEquals(1, b.readPosition()); - b.uncheckedReadSkipBackOne(); - assertEquals(0, b.readPosition()); - b.writeUnsignedByte('H'); - b.writeUnsignedByte(0xFF); - assertEquals('H', b.uncheckedReadUnsignedByte()); - assertEquals(0xFF, b.uncheckedReadUnsignedByte()); - } finally { - b.releaseLast(); - } - } - - @Test - public void readVolatile() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.writeVolatileByte(0, (byte) 1); - b.writeVolatileShort(1, (short) 2); - b.writeVolatileInt(3, 3); - b.writeVolatileLong(7, 4); - assertEquals(1, b.readVolatileByte(0)); - assertEquals(2, b.readVolatileShort(1)); - assertEquals(3, b.readVolatileInt(3)); - assertEquals(4, b.readVolatileLong(7)); - - } finally { - b.releaseLast(); - } - } - - @Test - public void testHashCode() { - assumeFalse(NativeBytes.areNewGuarded()); - - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.writeLong(0); - assertEquals(0, b.hashCode()); - b.clear(); - b.writeLong(1); - assertEquals(0x152ad77e, b.hashCode()); - b.clear(); - b.writeLong(2); - assertEquals(0x2a55aefc, b.hashCode()); - b.clear(); - b.writeLong(3); - assertEquals(0x7f448df2, b.hashCode()); - b.clear(); - b.writeLong(4); - assertEquals(0x54ab5df8, b.hashCode()); - - } finally { - b.releaseLast(); - } - } - - @Test - public void testEnum() { - - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.writeEnum(HEAP); - b.writeEnum(NATIVE); - assertEquals(HEAP, b.readEnum(Allocator.class)); - assertEquals(NATIVE, b.readEnum(Allocator.class)); - - } finally { - b.releaseLast(); - } - } - - @Test - public void testTimeMillis() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.appendTimeMillis(12345678L); - assertEquals("03:25:45.678", b.toString()); - - } finally { - b.releaseLast(); - } - } - - @Test - public void testDateTimeMillis() { - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - b.appendDateMillis(12345 * 86400_000L); - assertEquals("20031020", b.toString()); - - } finally { - b.releaseLast(); - } - } - - @Test - public void testWriteOffset() { - int length = 128; - Bytes from = NativeBytes.nativeBytes(length).unchecked(true); - Bytes to = alloc1.elasticBytes(length); - - Bytes a = Bytes.from("a"); - for (int i = 0; i < length; i++) { - from.write(i, a, 0L, 1); - } - a.releaseLast(); - - try { - to.write(from, 0L, length); - assertEquals(from.readLong(0), to.readLong(0)); - } finally { - from.releaseLast(); - to.releaseLast(); - } - } - - @Test - public void testToStringDoesNotChange() { - @NotNull Bytes a = alloc1.elasticBytes(16); - @NotNull Bytes b = alloc1.elasticBytes(16); - try { - String hello = "hello"; - a.append(hello); - b.append(hello); - - assertEquals(a, b); - assertEquals(a.bytesStore(), b.bytesStore()); - - assertEquals(hello, b.toString()); - - assertEquals(a, b); - assertEquals(a.bytesStore(), b.bytesStore()); - } finally { - a.releaseLast(); - b.releaseLast(); - } - } - - @Test - public void to8BitString() { - @NotNull Bytes a = alloc1.elasticBytes(16); - try { - assertEquals(a.toString(), a.to8bitString()); - String hello = "hello"; - a.append(hello); - assertEquals(a.toString(), a.to8bitString()); - } finally { - a.releaseLast(); - } - } - - @Test - public void testParseDoubleReadLimit() { - Bytes bytes = alloc1.fixedBytes(32); - try { - final String spaces = " "; - bytes.append(spaces).append(1.23); - bytes.readLimit(spaces.length()); - // only fails when assertions are off - assertEquals(0, BytesInternal.parseDouble(bytes), 0); - } finally { - bytes.releaseLast(); - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTestCommon.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTestCommon.java deleted file mode 100644 index ddbad3e..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTestCommon.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import org.junit.After; -import org.junit.Before; - -public class BytesTestCommon { - - @Before - public void enableReferenceTracing() { - AbstractReferenceCounted.enableReferenceTracing(); - } - - @After - public void assertReferencesReleased() { - AbstractReferenceCounted.assertReferencesReleased(); - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTextMethodTesterTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTextMethodTesterTest.java deleted file mode 100644 index a0ffd5a..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesTextMethodTesterTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import java.io.IOException; - -import static junit.framework.TestCase.assertEquals; -import static org.junit.Assume.assumeFalse; - -public class BytesTextMethodTesterTest extends BytesTestCommon { - @Test - public void run() throws IOException { - assumeFalse(NativeBytes.areNewGuarded()); - btmttTest("btmtt/prim-input.txt", "btmtt/prim-output.txt"); - } - - @SuppressWarnings("rawtypes") - protected void btmttTest(String input, String output) throws IOException { - BytesTextMethodTester tester = new BytesTextMethodTester<>( - input, - IBMImpl::new, - IBytesMethod.class, - output); - tester.run(); - assertEquals(tester.expected(), tester.actual()); - } - - static class IBMImpl implements IBytesMethod { - final IBytesMethod out; - - IBMImpl(IBytesMethod out) { - this.out = out; - } - - @Override - public void myByteable(MyByteable byteable) { - out.myByteable(byteable); - } - - @Override - public void myScalars(MyScalars scalars) { - out.myScalars(scalars); - } - - @Override - public void myNested(MyNested nested) { - out.myNested(nested); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java deleted file mode 100644 index 895dcd2..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/BytesUtilTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import org.junit.Test; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.Arrays; - -import static org.junit.Assert.*; -import static org.junit.Assume.assumeTrue; - -@SuppressWarnings("rawtypes") -public class BytesUtilTest extends BytesTestCommon { - @Test - public void fromFileInJar() throws IOException { - Bytes bytes = BytesUtil.readFile("/net/openhft/chronicle/core/onoes/Google.properties"); - Bytes apache_license = Bytes.from("Apache License"); - long n = bytes.indexOf(apache_license); - assertTrue(n > 0); - apache_license.releaseLast(); - } - - @Test - public void findFile() throws FileNotFoundException { - String file = BytesUtil.findFile("file-to-find.txt"); - assertTrue(new File(file).exists()); - assertTrue(new File(file).canWrite()); - } - - @Test - public void triviallyCopyable() { - assumeTrue(Jvm.is64bit()); - - assertFalse(BytesUtil.isTriviallyCopyable(Nested.class)); - // TODO allow a portion of B to be trivially copyable - assertFalse(BytesUtil.isTriviallyCopyable(B.class)); - assertFalse(BytesUtil.isTriviallyCopyable(C.class)); - - assertTrue(BytesUtil.isTriviallyCopyable(A.class)); - int start = Jvm.objectHeaderSize(); - assertEquals("[" + start + ", " + (start + 20) + "]", Arrays.toString(BytesUtil.triviallyCopyableRange(A.class))); - assertTrue(BytesUtil.isTriviallyCopyable(A.class, start, 4 + 2 * 8)); - assertTrue(BytesUtil.isTriviallyCopyable(A.class, start + 4, 8)); - assertFalse(BytesUtil.isTriviallyCopyable(A.class, start - 4, 4 + 2 * 8)); - assertFalse(BytesUtil.isTriviallyCopyable(A.class, start + 4, 4 + 2 * 8)); - - assertTrue(BytesUtil.isTriviallyCopyable(A2.class)); - int size = Jvm.isAzulZing() ? 28 : 24; - assertEquals("[" + start + ", " + (start + size) + "]", Arrays.toString(BytesUtil.triviallyCopyableRange(A2.class))); - assertTrue(BytesUtil.isTriviallyCopyable(A2.class, start, 4 + 2 * 8 + 2 * 2)); - assertTrue(BytesUtil.isTriviallyCopyable(A2.class, start + 4, 8)); - assertFalse(BytesUtil.isTriviallyCopyable(A2.class, start - 4, 4 + 2 * 8)); - assertEquals(Jvm.isAzulZing(), BytesUtil.isTriviallyCopyable(A2.class, start + 8, 4 + 2 * 8)); - assertFalse(BytesUtil.isTriviallyCopyable(A2.class, start + 12, 4 + 2 * 8)); - } - - static class A { - int i; - long l; - double d; - } - - static class A2 extends A { - short s; - char ch; - } - - static class B { - int i; - long l; - double d; - String s; - } - - static class C { - int i; - transient long l; - double d; - } - - class Nested { - // implicit this$0 - int i; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CipherPerfMain.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CipherPerfMain.java deleted file mode 100644 index 6f8883f..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CipherPerfMain.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.openhft.chronicle.bytes; - -import java.security.Provider; -import java.security.Security; -import java.util.Map; - -public class CipherPerfMain { - public static void main(String[] args) { - for (Provider providers : Security.getProviders()) { - for (Map.Entry entry : providers.entrySet()) { - if (entry.getKey().toString().startsWith("Cipher.")) - System.out.println(entry); - } - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CommonMarshallableTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CommonMarshallableTest.java deleted file mode 100644 index 177484a..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CommonMarshallableTest.java +++ /dev/null @@ -1,14 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertFalse; - -public class CommonMarshallableTest extends BytesTestCommon { - - @Test - public void usesSelfDescribingMessage() { - assertFalse(new CommonMarshallable() { - }.usesSelfDescribingMessage()); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ConcurrentRafAccessTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ConcurrentRafAccessTest.java deleted file mode 100644 index 8d1eff6..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ConcurrentRafAccessTest.java +++ /dev/null @@ -1,168 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IOTools; -import org.junit.*; -import org.junit.rules.TemporaryFolder; - -import java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.channels.FileChannel; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; -import java.util.LongSummaryStatistics; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ForkJoinPool; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static org.junit.Assert.fail; - -/* - Averages from TeamCity logs: - - Type. Linux [us] Windows [us] ARM [us] - =========================================================== - Sequential 15 49 120 - Parallel 3.5 15 51 -*/ - -@Ignore("This is a performance test and should not be run as a part of the normal build") -public class ConcurrentRafAccessTest extends BytesTestCommon { - - private static final String MODE = "rw"; - private static final String BASE_DIR = "rafs"; - private static final long INITIAL_LENGTH = 64L; - private static final int RUNS = 64; - private static final int NO_FILES = ForkJoinPool.commonPool().getPoolSize(); - - private List workers; - - @Rule - public TemporaryFolder tmpDir = new TemporaryFolder(); - - private static void bumpSize(File file, final RandomAccessFile raf, final FileChannel fc) throws IOException { - final long currentSize = fc.size(); - raf.setLength(currentSize * 2); - } - - @Before - public void setup() throws IOException { - Files.createDirectories(Paths.get(BASE_DIR)); - - workers = IntStream.range(0, NO_FILES) - .mapToObj(i -> { - try { - final File file = fileFromInt(i); - final RandomAccessFile raf = new RandomAccessFile(file, MODE); - raf.setLength(INITIAL_LENGTH); - final FileChannel fc = raf.getChannel(); - return new Worker(file, raf, fc); - } catch (IOException e) { - e.printStackTrace(); - fail("unable to create file for " + i); - return null; - } - }) - .collect(Collectors.toList()); - } - - @After - public void cleanup() { - IOTools.deleteDirWithFiles(BASE_DIR); - } - - @Test - public void testParallel2() { - final LongSummaryStatistics summaryStatistics = IntStream.range(0, RUNS) - .mapToLong(i -> test("testParallel2 " + i, ForkJoinPool.commonPool())) - .skip(4) - .summaryStatistics(); - -// System.out.println("testParallel2: " + summaryStatistics); - } - - @Test - public void testSequential() { - final LongSummaryStatistics summaryStatistics = IntStream.range(0, RUNS) - .mapToLong(i -> test("testSequential " + i, Executors.newSingleThreadExecutor())) - .skip(4) - .summaryStatistics(); - -// System.out.println("testSequential: " + summaryStatistics); - - } - - @Test - public void testParallel() { - final LongSummaryStatistics summaryStatistics = IntStream.range(0, RUNS) - .mapToLong(i -> test("testParallel " + i, ForkJoinPool.commonPool())) - .skip(4) - .summaryStatistics(); - -// System.out.println("testParallel: " + summaryStatistics); - } - - @Test - public void testSequential2() { - final LongSummaryStatistics summaryStatistics = IntStream.range(0, RUNS) - .mapToLong(i -> test("testSequential2 " + i, Executors.newSingleThreadExecutor())) - .skip(4) - .summaryStatistics(); - -// System.out.println("testSequential2: " + summaryStatistics); - - } - - private long test(final String name, final ExecutorService executor) { - final long beginNs = System.nanoTime(); - - workers.forEach(executor::submit); - executor.shutdown(); - try { - executor.awaitTermination(2, TimeUnit.SECONDS); - } catch (InterruptedException ie) { - ie.printStackTrace(); - } - - final long elapsedNs = System.nanoTime() - beginNs; -// System.out.format("%s: elapsedNs = %,d%n", name, elapsedNs); - return elapsedNs; - } - - private File fileFromInt(int i) throws IOException { - return tmpDir.newFile(Integer.toString(i)); - } - - private static final class Worker implements Runnable { - - private final File f; - private final RandomAccessFile raf; - private final FileChannel fc; - - public Worker(final File f, final RandomAccessFile raf, final FileChannel fc) { - this.f = f; - this.raf = raf; - this.fc = fc; - } - - @Override - public void run() { - final long beginNs = System.nanoTime(); - for (int i = 0; i < 24; i++) { - try { - bumpSize(f, raf, fc); - } catch (IOException e) { - e.printStackTrace(); - return; - } - } - final long elapsedNs = System.nanoTime() - beginNs; -// System.out.format("%s: elapsedNs = %,d%n", Thread.currentThread().getName(), elapsedNs); - } - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CopyBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CopyBytesTest.java deleted file mode 100644 index 3622e1f..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/CopyBytesTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2014-2020 chronicle.software - * - * http://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.Closeable; -import org.junit.Test; - -import java.io.File; - -import static org.junit.Assert.assertEquals; - -public class CopyBytesTest extends BytesTestCommon { - - static void doTest(Bytes toTest, int from) { - Bytes toCopy = Bytes.allocateDirect(32); - Bytes toValidate = Bytes.allocateDirect(32); - try { - toCopy.writeLong(0, (long) 'W' << 56L | 100L); - toCopy.writeLong(8, (long) 'W' << 56L | 200L); - - toTest.writePosition(from); - toTest.write(toCopy, 0, 2 * 8L); - toTest.write(toCopy, 0, 8L); - - toTest.readPosition(from); - toTest.read(toValidate, 3 * 8); - - assertEquals((long) 'W' << 56L | 100L, toValidate.readLong(0)); - assertEquals((long) 'W' << 56L | 200L, toValidate.readLong(8)); - assertEquals((long) 'W' << 56L | 100L, toValidate.readLong(16)); - - } finally { - toTest.releaseLast(); - toCopy.releaseLast(); - toValidate.releaseLast(); - // close if closeable. - Closeable.closeQuietly(toTest); - } - } - - @Test - public void testCanCopyBytesFromBytes() { - doTest(Bytes.allocateElasticDirect(), 0); - } - - @Test - public void testCanCopyBytesFromMappedBytes1() throws Exception { - File bytes = File.createTempFile("mapped-test", "bytes"); - bytes.deleteOnExit(); - doTest(MappedBytes.mappedBytes(bytes, 64 << 10, 0), 0); - } - - @Test - public void testCanCopyBytesFromMappedBytes2() throws Exception { - File bytes = File.createTempFile("mapped-test", "bytes"); - bytes.deleteOnExit(); - doTest(MappedBytes.mappedBytes(bytes, 64 << 10, 0), (64 << 10) - 8); - } - - @Test - public void testCanCopyBytesFromMappedBytes3() throws Exception { - File bytes = File.createTempFile("mapped-test", "bytes"); - bytes.deleteOnExit(); - doTest(MappedBytes.mappedBytes(bytes, 16 << 10, 16 << 10), (64 << 10) - 8); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/HeapByteStoreTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/HeapByteStoreTest.java deleted file mode 100644 index ef9054a..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/HeapByteStoreTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; - -public class HeapByteStoreTest extends BytesTestCommon { - @SuppressWarnings("rawtypes") - @Test - public void testEquals() { - @NotNull HeapBytesStore hbs = HeapBytesStore.wrap("Hello".getBytes()); - @NotNull HeapBytesStore hbs2 = HeapBytesStore.wrap("Hello".getBytes()); - @NotNull HeapBytesStore hbs3 = HeapBytesStore.wrap("He!!o".getBytes()); - @NotNull HeapBytesStore hbs4 = HeapBytesStore.wrap("Hi".getBytes()); - assertEquals(hbs, hbs2); - assertEquals(hbs2, hbs); - assertNotEquals(hbs, hbs3); - assertNotEquals(hbs3, hbs); - assertNotEquals(hbs, hbs4); - assertNotEquals(hbs4, hbs); - } - - @Test - public void testElasticBytesEnsuringCapacity() { - Bytes bytes = Bytes.elasticHeapByteBuffer(); - bytes.clearAndPad(bytes.realCapacity() + 128); - // ensure this succeeds even though we are above the real capacity - this should trigger resize - bytes.prewriteInt(1); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/HexDumpBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/HexDumpBytesTest.java deleted file mode 100644 index 849fa4b..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/HexDumpBytesTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class HexDumpBytesTest extends BytesTestCommon { - - @Test - public void offsetFormat() { - HexDumpBytes bytes = new HexDumpBytes() - .numberWrap(8) - .offsetFormat((o, b) -> b.appendBase16(o, 4)); - bytes.comment("hi").write(new byte[18]); - bytes.indent(1); - bytes.comment("nest").write(new byte[18]); - assertEquals("0000 00 00 00 00 00 00 00 00 # hi\n" + - "0008 00 00 00 00 00 00 00 00\n" + - "0010 00 00\n" + - "0012 00 00 00 00 00 00 00 00 # nest\n" + - "001a 00 00 00 00 00 00 00 00\n" + - "0022 00 00\n", bytes.toHexString()); - bytes.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/IBytesMethod.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/IBytesMethod.java deleted file mode 100644 index bf3005f..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/IBytesMethod.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.openhft.chronicle.bytes; - -interface IBytesMethod { - @MethodId(0x81L) - void myByteable(MyByteable byteable); - - @MethodId(0x82L) - void myScalars(MyScalars scalars); - - @MethodId(0x83L) - void myNested(MyNested nested); -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Issue128Test.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Issue128Test.java deleted file mode 100644 index eb46dfe..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Issue128Test.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import java.text.DecimalFormat; - -import static net.openhft.chronicle.bytes.UnsafeTextBytesTest.testAppendDouble; -import static org.junit.Assert.assertEquals; - -public class Issue128Test { - private static final DecimalFormat DF; - - static { - DecimalFormat df = new DecimalFormat(); - df.setMinimumFractionDigits(1); - df.setMaximumFractionDigits(30); - DF = df; - } - - static String toDecimal(double d) { - return DF.format(d); - } - - @Test -// @Ignore("https://github.com/OpenHFT/Chronicle-Bytes/issues/128") - public void testCorrect() { - Bytes bytes = Bytes.allocateDirect(32); - try { - // odd ones are trouble. - for (int i = 1; i < 1_000_000; i += 2) { - double v6 = (double) i / 1_000_000; - doTest(bytes, v6); - doTest(bytes, 999 + v6); - double v7 = (double) i / 10_000_000; - doTest(bytes, v7); - double v8 = (double) i / 100_000_000; - doTest(bytes, v8); - double v9 = (double) i / 100_000_000; - doTest(bytes, v9); - } - } finally { - bytes.releaseLast(); - } - } - - public void doTest(Bytes bytes, double v) { - String format = DF.format(v); - String output = testAppendDouble(bytes, v); - if (Double.parseDouble(output) != v || format.length() != output.length()) - assertEquals(DF.format(v), output); -// System.out.println(DF.format(v)+" != " + output); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Issue85Test.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Issue85Test.java deleted file mode 100644 index df31bdd..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/Issue85Test.java +++ /dev/null @@ -1,117 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import org.junit.Assert; -import org.junit.Test; - -import java.math.BigDecimal; -import java.nio.ByteBuffer; -import java.security.SecureRandom; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.util.Locale; - -import static org.junit.Assume.assumeFalse; - -public class Issue85Test extends BytesTestCommon { - int different = 0; - int different2 = 0; - DecimalFormat df = new DecimalFormat(); - - { - df.setMaximumIntegerDigits(99); - df.setMaximumFractionDigits(99); - df.setMinimumFractionDigits(1); - df.setGroupingUsed(false); - df.setDecimalFormatSymbols( - DecimalFormatSymbols.getInstance(Locale.ENGLISH)); - } - - @SuppressWarnings("rawtypes") - static double parseDouble(Bytes bytes) { - long value = 0; - int deci = Integer.MIN_VALUE; - while (bytes.readRemaining() > 0) { - byte ch = bytes.readByte(); - if (ch == '.') { - deci = 0; - } else if (ch >= '0' && ch <= '9') { - value *= 10; - value += ch - '0'; - deci++; - } else { - break; - } - } - if (deci <= 0) { - return value; - } - return asDouble(value, deci); - } - - private static double asDouble(long value, int deci) { - int scale2 = 0; - int leading = Long.numberOfLeadingZeros(value); - if (leading > 1) { - scale2 = leading - 1; - value <<= scale2; - } - long fives = Maths.fives(deci); - long whole = value / fives; - long rem = value % fives; - double d = whole + (double) rem / fives; - double scalb = Math.scalb(d, -deci - scale2); - return scalb; - } - - @Test - public void bytesParseDouble_Issue85_Many0() { - assumeFalse(NativeBytes.areNewGuarded()); - int max = 100, count = 0; - Bytes bytes = Bytes.elasticHeapByteBuffer(64); - for (double d0 = 1e21; d0 >= 1e-8; d0 /= 10) { - long val = Double.doubleToRawLongBits(d0); - for (int i = -max / 2; i <= max / 2; i++) { - double d = Double.longBitsToDouble(val + i); - doTest(bytes, i, d); - } - count += max + 1; - } - SecureRandom rand = new SecureRandom(); - for (int i = 0; i < max * 100; i++) { - double d = Math.pow(1e12, rand.nextDouble()) / 1e3; - doTest(bytes, 0, d); - count++; - } - if (different + different2 > 0) - Assert.fail("Different toString: " + 100.0 * different / count + "%," + - " parsing: " + 100.0 * different2 / count + "%"); - } - - protected void doTest(Bytes bytes, int i, double d) { - String s = df.format(d); - bytes.clear().append(s); - double d2 = bytes.parseDouble(); - if (d != d2) { -// System.out.println(i + ": Parsing " + s + " != " + d2); - ++different2; - } - - String s2 = bytes.append(d).toString(); - double d3 = Double.parseDouble(s2); - if (d != d3) { -// System.out.println(i + ": ToString " + s + " != " + s2 + " should be " + new BigDecimal(d)); - ++different; - } - } - - @Test - public void loseTrainingZeros() { - double d = -541098.2421; - Assert.assertEquals("" + d, - ((Bytes) Bytes.allocateElasticOnHeap(64)) - .append(d) - .toString()); - - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedBytesTest.java deleted file mode 100644 index 1ed62fb..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedBytesTest.java +++ /dev/null @@ -1,455 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.IOTools; -import org.junit.After; -import org.junit.Assert; -import org.junit.Ignore; -import org.junit.Test; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.util.Arrays; -import java.util.stream.IntStream; - -import static org.junit.Assert.*; - -@SuppressWarnings("rawtypes") -public class MappedBytesTest extends BytesTestCommon { - - final private String - smallText = "It's ten years since the iPhone was first unveiled and Apple has marked " + - "the occas" + - "ion with a new iPhone that doesn't just jump one generation, it jumps several. " + - "Apple has leapt straight from iPhone 7 (via the iPhone 8, reviewed here) all the way " + - "to iPhone 10 (yes, that's how you are supposed to say it).\n" + - "\n" + - "Read on to find out how the new flagship iPhone X shapes up. Is it going to revolutionise " + - "the mobile phone again like the original iPhone did, or is Apple now just playing catch-up " + - "with the rest of the industry? (For a comparison with one rival device, see iPhone X vs LG G7.)\n"; - - private final StringBuilder largeTextBuilder = new StringBuilder(); - - private final String text; - - { - for (int i = 0; i < 200; i++) { - largeTextBuilder.append(smallText); - } - - text = largeTextBuilder.toString(); - } - - @Test - public void testMappedFileSafeLimitTooSmall() throws IOException { - - final int arraySize = 40_000; - - byte[] data = new byte[arraySize]; - Arrays.fill(data, (byte) 'x'); - - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 50_000, 40_000); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 50_000, 40_000)) { - - for (int i = 0; i < 5; i++) { - bytesW.write(data); - } - - for (int i = 0; i < 5; i++) { - bytesR.write(data); - } - } - } - - @Test - public void testMappedFileSafeLimitTooSmall2() throws IOException { - - final int arraySize = 40_000; - - byte[] data = new byte[arraySize]; - Arrays.fill(data, (byte) 'x'); - - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 50_000, 30_000); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 50_000, 30_000)) { - - for (int i = 0; i < 5; i++) { - bytesW.write(data); - } - - for (int i = 0; i < 5; i++) { - bytesR.write(data); - } - } - } - - @Test - public void testWriteBytes() throws IOException { - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 4, 4); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 200 << 10, 200 << 10)) { - - // write - Bytes from = Bytes.from(text); - bytesW.write(from); - long wp = bytesW.writePosition; - Assert.assertEquals(text.length(), bytesW.writePosition); - - // read - bytesR.readLimit(wp); - - Assert.assertEquals(text, bytesR.toString()); - from.releaseLast(); - } - } - - @Test - public void testWriteReadBytes() throws IOException { - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 64 << 10, 16 << 10); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 64 << 10, 16 << 10)) { - - // write - Bytes from = Bytes.from(text); - bytesW.write(from); - long wp = bytesW.writePosition; - Assert.assertEquals(text.length(), bytesW.writePosition); - - // read - bytesR.readLimit(wp); - - Assert.assertEquals(text, bytesR.toString()); - from.releaseLast(); - } - } - - @Test - public void testWriteBytesWithOffset() throws IOException { - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 4, 4); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 200 << 10, 200 << 10)) { - - int offset = 10; - - // write - Bytes from = Bytes.from(text); - bytesW.write(offset, from); - long wp = text.length() + offset; - Assert.assertEquals(0, bytesW.writePosition); - - // read - bytesR.readLimit(wp); - bytesR.readPosition(offset); - Assert.assertEquals(text, bytesR.toString()); - from.releaseLast(); - } - } - - @Test - public void testWriteReadBytesWithOffset() throws IOException { - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 64 << 10, 16 << 10); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 64 << 10, 16 << 10)) { - - int offset = 10; - - // write - Bytes from = Bytes.from(text); - bytesW.write(offset, from); - long wp = text.length() + offset; - Assert.assertEquals(0, bytesW.writePosition); - - // read - bytesR.readLimit(wp); - bytesR.readPosition(offset); - Assert.assertEquals(text, bytesR.toString()); - from.releaseLast(); - } - } - - @Test - public void testWriteBytesWithOffsetAndTextShift() throws IOException { - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 4, 4); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 200 << 10, 200 << 10)) { - int offset = 10; - int shift = 128; - - //write - Bytes from = Bytes.from(text); - bytesW.write(offset, from, shift, text.length() - shift); - Assert.assertEquals(0, bytesW.writePosition); - - // read - bytesR.readLimit(offset + (text.length() - shift)); - bytesR.readPosition(offset); - String actual = bytesR.toString(); - Assert.assertEquals(text.substring(shift), actual); - from.releaseLast(); - } - } - - @Test - public void testWriteReadBytesWithOffsetAndTextShift() throws IOException { - File tempFile1 = File.createTempFile("mapped", "bytes"); - try (MappedBytes bytesW = MappedBytes.mappedBytes(tempFile1, 64 << 10, 16 << 10); - MappedBytes bytesR = MappedBytes.mappedBytes(tempFile1, 64 << 10, 16 << 10)) { - int offset = 10; - int shift = 128; - - //write - Bytes from = Bytes.from(text); - bytesW.write(offset, from, shift, text.length() - shift); - Assert.assertEquals(0, bytesW.writePosition); - - // read - bytesR.readLimit(offset + (text.length() - shift)); - bytesR.readPosition(offset); - String actual = bytesR.toString(); - Assert.assertEquals(text.substring(shift), actual); - from.releaseLast(); - } - } - - @Test - public void testLargeWrites() throws IOException { - MappedBytes bytes = MappedBytes.mappedBytes(File.createTempFile("mapped", "bytes"), 128 << - 10, 64 << 10); - - byte[] largeBytes = new byte[500 << 10]; - bytes.writePosition(0); - bytes.write(largeBytes); - bytes.writePosition(0); - bytes.write(64, largeBytes); - bytes.writePosition(0); - bytes.write(largeBytes, 64, largeBytes.length - 64); - bytes.writePosition(0); - bytes.write(64, largeBytes, 64, largeBytes.length - 64); - - bytes.writePosition(0); - bytes.write(Bytes.wrapForRead(largeBytes)); - bytes.writePosition(0); - Bytes bytes1 = Bytes.wrapForRead(largeBytes); - bytes.write(64, bytes1); - bytes.writePosition(0); - bytes.write(Bytes.wrapForRead(largeBytes), 64L, largeBytes.length - 64L); - bytes.writePosition(0); - bytes.write(64, Bytes.wrapForRead(largeBytes), 64L, largeBytes.length - 64L); - - Bytes bytes2 = Bytes.allocateDirect(largeBytes); - bytes.writePosition(0); - bytes.write(bytes2); - bytes.writePosition(0); - bytes.write(64, bytes2); - bytes.writePosition(0); - bytes.write(bytes2, 64L, largeBytes.length - 64L); - bytes.writePosition(0); - bytes.write(64, bytes2, 64L, largeBytes.length - 64L); - - bytes2.releaseLast(); - bytes.releaseLast(); - - } - - @Test - public void testLargeWrites3() throws IOException { - MappedBytes bytes = MappedBytes.mappedBytes(File.createTempFile("mapped", "bytes"), 47 << - 10, 21 << 10); - - byte[] largeBytes = new byte[513 << 10]; - bytes.writePosition(0); - bytes.write(largeBytes); - bytes.writePosition(0); - bytes.write(64, largeBytes); - bytes.writePosition(0); - bytes.write(largeBytes, 64, largeBytes.length - 64); - bytes.writePosition(0); - bytes.write(64, largeBytes, 64, largeBytes.length - 64); - - bytes.writePosition(0); - bytes.write(Bytes.wrapForRead(largeBytes)); - bytes.writePosition(0); - Bytes bytes1 = Bytes.wrapForRead(largeBytes); - bytes.write(64, bytes1); - bytes.writePosition(0); - bytes.write(Bytes.wrapForRead(largeBytes), 64L, largeBytes.length - 64L); - bytes.writePosition(0); - bytes.write(64, Bytes.wrapForRead(largeBytes), 64L, largeBytes.length - 64L); - - Bytes bytes2 = Bytes.allocateDirect(largeBytes); - bytes.writePosition(0); - bytes.write(bytes2); - bytes.writePosition(0); - bytes.write(64, bytes2); - bytes.writePosition(0); - bytes.write(bytes2, 64L, largeBytes.length - 64L); - bytes.writePosition(0); - bytes.write(64, bytes2, 64L, largeBytes.length - 64L); - - bytes2.releaseLast(); - bytes.releaseLast(); - - } - - @Test - public void testLargeWrites2() throws IOException { - MappedBytes bytes = MappedBytes.mappedBytes(File.createTempFile("mapped", "bytes"), 128 << - 10, 128 << 10); - - byte[] largeBytes = new byte[500 << 10]; - bytes.writePosition(0); - bytes.write(largeBytes); - bytes.writePosition(0); - bytes.write(64, largeBytes); - bytes.writePosition(0); - bytes.write(largeBytes, 64, largeBytes.length - 64); - bytes.writePosition(0); - bytes.write(64, largeBytes, 64, largeBytes.length - 64); - - bytes.writePosition(0); - bytes.write(Bytes.wrapForRead(largeBytes)); - bytes.writePosition(0); - Bytes bytes1 = Bytes.wrapForRead(largeBytes); - bytes.write(64, bytes1); - bytes.writePosition(0); - bytes.write(Bytes.wrapForRead(largeBytes), 64L, largeBytes.length - 64L); - bytes.writePosition(0); - bytes.write(64, Bytes.wrapForRead(largeBytes), 64L, largeBytes.length - 64L); - - Bytes bytes2 = Bytes.allocateDirect(largeBytes); - bytes.writePosition(0); - bytes.write(bytes2); - bytes.writePosition(0); - bytes.write(64, bytes2); - bytes.writePosition(0); - bytes.write(bytes2, 64L, largeBytes.length - 64L); - bytes.writePosition(0); - bytes.write(64, bytes2, 64L, largeBytes.length - 64L); - - bytes2.releaseLast(); - bytes.releaseLast(); - - } - - @Test - public void shouldNotBeReadOnly() throws Exception { - MappedBytes bytes = MappedBytes.mappedBytes(File.createTempFile("mapped", "bytes"), 64 << 10); - assertFalse(bytes.isBackingFileReadOnly()); - bytes.writeUtf8(null); // used to blow up. - assertNull(bytes.readUtf8()); - bytes.releaseLast(); - } - - @Ignore("concourse ci") - @Test - public void shouldBeReadOnly() throws Exception { - final File tempFile = File.createTempFile("mapped", "bytes"); - try (final RandomAccessFile raf = new RandomAccessFile(tempFile, "rw")) { - raf.setLength(4096); - assertTrue(tempFile.setWritable(false)); - final MappedBytes mappedBytes = MappedBytes.readOnly(tempFile); - assertTrue(mappedBytes.isBackingFileReadOnly()); - mappedBytes.releaseLast(); - assertEquals(0, mappedBytes.refCount()); - } - } - - @Test - public void interrupted() throws FileNotFoundException { - Thread.currentThread().interrupt(); - File file = IOTools.createTempFile("interrupted"); - file.deleteOnExit(); - MappedBytes mb = MappedBytes.mappedBytes(file, 64 << 10); - try { - mb.realCapacity(); - assertTrue(Thread.currentThread().isInterrupted()); - } finally { - mb.releaseLast(); - } - } - - @After - public void clearInterrupt() { - Thread.interrupted(); - } - - @Test - public void multiBytes() throws FileNotFoundException { - File tmpfile = IOTools.createTempFile("data.dat"); - try (MappedFile mappedFile = MappedFile.mappedFile(tmpfile, 64 << 10); - MappedBytes original = MappedBytes.mappedBytes(mappedFile)) { - original.zeroOut(0, 1000); - - original.writeInt(0, 1234); - - PointerBytesStore pbs = new PointerBytesStore(); - pbs.set(original.addressForRead(50), 100); - - // Print out the int in the two BytesStores. - // This shows that the copy has the same contents of the original. -// System.out.println("Original(0): " + original.readInt(0)); -// System.out.println("PBS(0): " + pbs.readInt(0)); - - // Now modify the copy and print out the new int in the two BytesStores again. - pbs.writeInt(0, 4321); -// System.out.println("Original(50): " + original.readInt(50)); -// System.out.println("PBS(0): " + pbs.readInt(0)); - original.writeInt(54, 12345678); -// System.out.println("Original(54): " + original.readInt(54)); -// System.out.println("PBS(4): " + pbs.readInt(4)); - } - } - - @Test - public void memoryOverlapRegions() throws FileNotFoundException { - String tmpfile = IOTools.createTempFile("memoryOverlapRegions").getAbsolutePath(); - int chunkSize = 256 << 16; - int overlapSize = 64 << 16; - String longString = new String(new char[overlapSize * 2]); - Bytes csb = Bytes.from(longString); - try (MappedBytes mb = MappedBytes.mappedBytes(new File(tmpfile), chunkSize, overlapSize)) { - StringBuilder sb = new StringBuilder(); - for (int offset : new int[]{chunkSize - OS.pageSize(), chunkSize + overlapSize - OS.pageSize()}) { - mb.writePosition(offset); - mb.appendUtf8(longString); - mb.readPosition(offset); - assertEquals(offset < chunkSize ? 0 : chunkSize, mb.bytesStore().start()); - - mb.equalBytes(csb, csb.length()); - assertEquals(chunkSize, mb.bytesStore().start()); - - mb.equalBytes(csb, csb.length()); - assertEquals(chunkSize, mb.bytesStore().start()); - - mb.parseUtf8(sb, csb.length()); - assertEquals(chunkSize, mb.bytesStore().start()); - } - } finally { - csb.releaseLast(); - } - IOTools.deleteDirWithFiles(tmpfile, 2); - } - - @Test - public void threadSafeMappedBytes() throws FileNotFoundException { - String tmpfile = IOTools.createTempFile("threadSafeMappedBytes").getAbsolutePath(); - int count = 4000; - IntStream.range(0, count) - .parallel() - .forEach(i -> { - try (MappedBytes mb = MappedBytes.mappedBytes(tmpfile, 256 << 10)) { - mb.addAndGetLong(0, 1); - } catch (FileNotFoundException e) { - throw Jvm.rethrow(e); - } - }); - try (MappedBytes mb = MappedBytes.mappedBytes(tmpfile, 256 << 10)) { - assertEquals(count, mb.readVolatileLong(0)); - } - IOTools.deleteDirWithFiles(tmpfile, 2); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedFileMultiThreadTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedFileMultiThreadTest.java deleted file mode 100644 index 1fd8f3c..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedFileMultiThreadTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IOTools; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class MappedFileMultiThreadTest extends BytesTestCommon { - private static final int CORES = Integer.getInteger("cores", Runtime.getRuntime().availableProcessors()); - private static final int RUNTIME_MS = Integer.getInteger("runtimems", 2_000); - private static final String TMP_FILE = System.getProperty("file", IOTools.createTempFile("testMultiThreadLock").getAbsolutePath()); - - private ThreadDump threadDump; - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testMultiThreadLock() throws Exception { - final List garbage = Collections.synchronizedList(new ArrayList<>()); - final long chunkSize = OS.isWindows() ? 64 << 10 : 4 << 10; - try (MappedFile mf = MappedFile.mappedFile(TMP_FILE, chunkSize, 0)) { - assertEquals("refCount: 1", mf.referenceCounts()); - - final List> futures = new ArrayList<>(); - final ExecutorService es = Executors.newFixedThreadPool(CORES); - final AtomicBoolean running = new AtomicBoolean(true); - for (int i = 0; i < CORES; i++) { - int finalI = i; - futures.add(es.submit(() -> { - long offset = 1; - final ReferenceOwner test = ReferenceOwner.temporary("test" + finalI); - while (running.get()) { - garbage.add(test.referenceName() + offset); - MappedBytesStore bs = null; - Bytes bytes = null; - try { - bs = mf.acquireByteStore(test, chunkSize * offset); - bytes = bs.bytesForRead(); - assertNotNull(bytes.toString()); // show it doesn't blow up. - assertNotNull(bs.toString()); // show it doesn't blow up. - ++offset; - } catch (IOException e) { - throw Jvm.rethrow(e); - } finally { - if (bytes != null) bytes.releaseLast(); - if (bs != null) bs.release(test); - } - if (finalI == 0 && offset % 1_000 == 0) { - garbage.clear(); - System.gc(); - } - } - })); - } - - Jvm.pause(RUNTIME_MS); - running.set(false); - for (Future f : futures) - f.get(5, TimeUnit.SECONDS); - es.shutdownNow(); - es.awaitTermination(1, TimeUnit.SECONDS); - } - IOTools.deleteDirWithFiles(TMP_FILE); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedFileTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedFileTest.java deleted file mode 100644 index 42cb377..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedFileTest.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IOTools; -import net.openhft.chronicle.core.io.ReferenceOwner; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.*; -import org.junit.rules.TemporaryFolder; - -import java.io.*; -import java.nio.BufferUnderflowException; - -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -public class MappedFileTest extends BytesTestCommon { - - @Rule - public final TemporaryFolder tmpDir = new TemporaryFolder(); - private ThreadDump threadDump; - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testWarmup() { - MappedFile.warmup(); - } - - @Test - public void shouldReleaseReferenceWhenNewStoreIsAcquired() throws IOException { - final File file = tmpDir.newFile(); - // this is what it will end up as - final long chunkSize = OS.mapAlign(64); - final ReferenceOwner test = ReferenceOwner.temporary("test"); - try (final MappedFile mappedFile = MappedFile.mappedFile(file, 64)) { - final MappedBytesStore first = mappedFile.acquireByteStore(test, 1); - - final int expected = MappedFile.RETAIN ? 2 : 1; - assertEquals(expected, first.refCount()); - - final MappedBytesStore second = mappedFile.acquireByteStore(test, 1 + chunkSize); - - assertEquals(expected, first.refCount()); - assertEquals(expected, second.refCount()); - - final MappedBytesStore third = mappedFile.acquireByteStore(test, 1 + chunkSize + chunkSize); - - assertEquals(expected, first.refCount()); - assertEquals(expected, second.refCount()); - assertEquals(expected, third.refCount()); - - third.release(test); - second.release(test); - first.release(test); - } - } - - @Test - public void testReferenceCounts() throws IOException { - final File tmp = IOTools.createTempFile("testReferenceCounts"); - final int chunkSize = OS.isWindows() ? 64 << 10 : 4 << 10; - try (MappedFile mf = MappedFile.mappedFile(tmp, chunkSize, 0)) { - assertEquals("refCount: 1", mf.referenceCounts()); - - final ReferenceOwner test = ReferenceOwner.temporary("test"); - final MappedBytesStore bs = mf.acquireByteStore(test, chunkSize + (1 << 10)); - try { - assertEquals(chunkSize, bs.start()); - assertEquals(chunkSize * 2, bs.capacity()); - final Bytes bytes = bs.bytesForRead(); - - assertNotNull(bytes.toString()); // show it doesn't blow up. - assertNotNull(bs.toString()); // show it doesn't blow up. - assertEquals(chunkSize, bytes.start()); - assertEquals(0L, bs.readLong(chunkSize + (1 << 10))); - assertEquals(0L, bytes.readLong(chunkSize + (1 << 10))); - Assert.assertFalse(bs.inside(chunkSize - (1 << 10))); - Assert.assertFalse(bs.inside(chunkSize - 1)); - Assert.assertTrue(bs.inside(chunkSize)); - Assert.assertTrue(bs.inside(chunkSize * 2 - 1)); - Assert.assertFalse(bs.inside(chunkSize * 2)); - try { - bytes.readLong(chunkSize - (1 << 10)); - Assert.fail(); - } catch (BufferUnderflowException e) { - // expected - } - try { - bytes.readLong(chunkSize * 2 + (1 << 10)); - Assert.fail(); - } catch (BufferUnderflowException e) { - // expected - } - assertEquals(1, mf.refCount()); - final int expected = MappedFile.RETAIN ? 2 : 1; - assertEquals(expected + 1, bs.refCount()); - assertEquals("refCount: 1, 0, " + (expected + 1), mf.referenceCounts()); - - final BytesStore bs2 = mf.acquireByteStore(test, chunkSize + (1 << 10), bs); - assertSame(bs, bs2); - assertEquals(expected + 1, bs2.refCount()); - assertEquals("refCount: 1, 0, " + (expected + 1), mf.referenceCounts()); - bytes.releaseLast(); - assertEquals(expected, bs2.refCount()); - assertEquals("refCount: 1, 0, " + expected, mf.referenceCounts()); - } finally { - bs.release(test); - } - } - } - - @Test - public void largeReadOnlyFile() throws IOException { - if (Runtime.getRuntime().maxMemory() < Integer.MAX_VALUE || OS.isWindows()) - return; - - final File file = File.createTempFile("largeReadOnlyFile", "deleteme"); - file.deleteOnExit(); - try (MappedBytes bytes = MappedBytes.mappedBytes(file, 1 << 30, OS.pageSize())) { - bytes.writeLong(3L << 30, 0x12345678); // make the file 3 GB. - } - - try (MappedBytes bytes = MappedBytes.readOnly(file)) { - Assert.assertEquals(0x12345678L, bytes.readLong(3L << 30)); - } - } - - @Test - public void interrupted() throws FileNotFoundException { - Thread.currentThread().interrupt(); - final String filename = IOTools.createTempFile("interrupted").getAbsolutePath(); - try (MappedFile mf = MappedFile.mappedFile(filename, 64 << 10, 0)) { - mf.actualSize(); - assertTrue(Thread.currentThread().isInterrupted()); - } - } - - @Test - public void testCreateMappedFile() throws IOException { - final File file = IOTools.createTempFile("mappedFile"); - - final MappedFile mappedFile = MappedFile.mappedFile(file, 1024, 256, 256, false); - try { - final MappedFile mappedFile2 = MappedFile.mappedFile(file, 1024, 256, 256, false); - mappedFile2.releaseLast(); - } finally { - mappedFile.releaseLast(); - } - } - - @Test - public void testReadOnlyOpen() throws IOException { - assumeFalse(OS.isWindows()); - - String text = "Some text to put in this file. yay!\n"; - - @NotNull File file = File.createTempFile("readOnlyOpenFile", "deleteme"); - - // write some stuff to a file so it exits using stock java APIs - @NotNull OutputStreamWriter outWrite = new OutputStreamWriter(new FileOutputStream(file)); - outWrite.append(text); - outWrite.flush(); - outWrite.close(); - - byte[] tmp = new byte[1024 * 16]; - - // Open and read the file w/ a MappedBytes to show it's readable - try (@NotNull MappedBytes mapBuf = MappedBytes.readOnly(file)) { - mapBuf.readLimit(file.length()); - int readLen = mapBuf.read(tmp, 0, tmp.length); - assertEquals(text, new String(tmp, 0, readLen)); - } - - // open up the same file via a mapped file - try (@NotNull MappedFile mapFile = MappedFile.mappedFile(file, OS.pageSize() * 16, OS.pageSize(), true)) { - // this throws a exception as of v2.20.9. it shouldn't - ReferenceOwner temp = ReferenceOwner.temporary("TEMP"); - @NotNull Bytes buf = mapFile.acquireBytesForRead(temp, 0); - buf.readLimit(file.length()); - int readLen = buf.read(tmp, 0, tmp.length); - assertEquals(text, new String(tmp, 0, readLen)); - buf.releaseLast(temp); - } - - file.deleteOnExit(); - } - - @After - public void clearInterrupt() { - Thread.interrupted(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedMemoryTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedMemoryTest.java deleted file mode 100644 index b4c4155..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedMemoryTest.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.io.ReferenceOwner; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; - -import static net.openhft.chronicle.bytes.MappedBytes.mappedBytes; -import static net.openhft.chronicle.bytes.MappedFile.mappedFile; -import static org.junit.Assert.assertEquals; - -public class MappedMemoryTest extends BytesTestCommon { - - private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(MappedMemoryTest.class); - private static final long SHIFT = 27L; - private static final long BLOCK_SIZE = 1L << SHIFT; - - private static void deleteIfPossible(@NotNull final File file) { - if (!file.delete()) { - Jvm.warn().on(MappedMemoryTest.class, "Unable to delete " + file.getAbsolutePath()); - } - } - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - // on i7-3970X ~ 3.3 ns - @Test - public void testRawMemoryMapped() throws IOException { - final ReferenceOwner test = ReferenceOwner.temporary("test"); - for (int t = 0; t < 5; t++) { - final File tempFile = File.createTempFile("chronicle", "q"); - try { - - final long startTime = System.nanoTime(); - MappedFile file0; - try (MappedFile mappedFile = mappedFile(tempFile, BLOCK_SIZE / 2, OS.pageSize())) { - file0 = mappedFile; - final MappedBytesStore bytesStore = mappedFile.acquireByteStore(test, 1); - final long address = bytesStore.address; - - for (long i = 0; i < BLOCK_SIZE / 2; i += 8L) { - OS.memory().writeLong(address + i, i); - } - for (long i = 0; i < BLOCK_SIZE / 2; i += 8L) { - OS.memory().writeLong(address + i, i); - } - bytesStore.release(test); - } - assertEquals(file0.referenceCounts(), 0, file0.refCount()); - LOG.info("With RawMemory,\t\t time= " + 80 * (System.nanoTime() - startTime) / BLOCK_SIZE / 10.0 + " ns, number of longs written=" + BLOCK_SIZE / 8); - } finally { - deleteIfPossible(tempFile); - } - } - } - - // on i7-3970X ~ 6.9 ns - @Test - public void withMappedNativeBytesTest() throws IOException { - - for (int t = 0; t < 3; t++) { - final File tempFile = File.createTempFile("chronicle", "q"); - try { - - final long startTime = System.nanoTime(); - final Bytes bytes = mappedBytes(tempFile, BLOCK_SIZE / 2); -// bytes.writeLong(1, 1); - for (long i = 0; i < BLOCK_SIZE; i += 8) { - bytes.writeLong(i); - } - bytes.releaseLast(); - assertEquals(0, bytes.refCount()); - LOG.info("With MappedNativeBytes,\t avg time= " + 80 * (System.nanoTime() - startTime) / BLOCK_SIZE / 10.0 + " ns, number of longs written=" + BLOCK_SIZE / 8); - } finally { - deleteIfPossible(tempFile); - } - } - } - - // on i7-3970X ~ 6.0 ns - @Test - public void withRawNativeBytesTess() throws IOException { - final ReferenceOwner test = ReferenceOwner.temporary("test"); - - for (int t = 0; t < 3; t++) { - final File tempFile = File.createTempFile("chronicle", "q"); - try { - - final long startTime = System.nanoTime(); - try (MappedFile mappedFile = mappedFile(tempFile, BLOCK_SIZE / 2, OS.pageSize())) { - Bytes bytes = mappedFile.acquireBytesForWrite(test, 1); - for (long i = 0; i < BLOCK_SIZE / 2; i += 8L) { - bytes.writeLong(i); - } - bytes.releaseLast(test); - - bytes = mappedFile.acquireBytesForWrite(test, BLOCK_SIZE / 2 + 1); - for (long i = 0; i < BLOCK_SIZE / 2; i += 8L) { - bytes.writeLong(i); - } - bytes.releaseLast(test); - - } - LOG.info("With NativeBytes,\t\t time= " + 80 * (System.nanoTime() - startTime) / BLOCK_SIZE / 10.0 + " ns, number of longs written=" + BLOCK_SIZE / 8); - } finally { - deleteIfPossible(tempFile); - } - } - } - - @Test - public void mappedMemoryTest() throws IOException, IORuntimeException { - - final File tempFile = File.createTempFile("chronicle", "q"); - Bytes bytes0; - try { - try (MappedBytes bytes = mappedBytes(tempFile, OS.pageSize())) { - bytes0 = bytes; - final ReferenceOwner test = ReferenceOwner.temporary("test"); - try { - assertEquals(1, bytes.refCount()); - bytes.reserve(test); - assertEquals(2, bytes.refCount()); - final char[] chars = new char[OS.pageSize() * 11]; - Arrays.fill(chars, '.'); - chars[chars.length - 1] = '*'; - bytes.writeUtf8(new String(chars)); - final String text = "hello this is some very long text"; - bytes.writeUtf8(text); - final String textValue = bytes.toString(); - assertEquals(text, textValue.substring(chars.length + 4)); - assertEquals(2, bytes.refCount()); - } finally { - bytes.release(test); - assertEquals(1, bytes.refCount()); - } - } - } finally { - deleteIfPossible(tempFile); - } - assertEquals(0, bytes0.refCount()); - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedUniqueMicroTimeProviderTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedUniqueMicroTimeProviderTest.java deleted file mode 100644 index 01471ad..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedUniqueMicroTimeProviderTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.time.LongTime; -import net.openhft.chronicle.core.time.TimeProvider; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class MappedUniqueMicroTimeProviderTest extends BytesTestCommon { - - @Test - public void currentTimeMicros() { - TimeProvider tp = MappedUniqueMicroTimeProvider.INSTANCE; - long last = 0; - for (int i = 0; i < 100_000; i++) { - long time = tp.currentTimeMicros(); - assertTrue(time > last); - assertEquals(LongTime.toMicros(time), time); - last = time; - } - } - - @Test - public void currentTimeNanos() { - TimeProvider tp = MappedUniqueMicroTimeProvider.INSTANCE; - long last = 0; - for (int i = 0; i < 100_000; i++) { - long time = tp.currentTimeNanos(); - assertTrue(time > last); - assertEquals(LongTime.toNanos(time), time); - last = time; - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedUniqueTimeProviderTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedUniqueTimeProviderTest.java deleted file mode 100644 index f2a5873..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MappedUniqueTimeProviderTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.time.LongTime; -import net.openhft.chronicle.core.time.TimeProvider; -import org.junit.Test; - -import java.util.stream.IntStream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class MappedUniqueTimeProviderTest extends BytesTestCommon { - - @Test - public void currentTimeMicros() { - TimeProvider tp = MappedUniqueTimeProvider.INSTANCE; - long last = 0; - for (int i = 0; i < 100_000; i++) { - long time = tp.currentTimeMicros(); - assertTrue(time > last); - assertEquals(LongTime.toMicros(time), time); - last = time; - } - } - - @Test - public void currentTimeNanos() { - TimeProvider tp = MappedUniqueTimeProvider.INSTANCE; - long start = tp.currentTimeNanos(); - long last = start; - int count = 0; - long runTime = Jvm.isArm() ? 3_000_000_000L : 500_000_000L; - for (; ; ) { - long now = tp.currentTimeNanos(); - if (now > start + runTime) - break; - // check the times are different after shifting by 5 bits. - assertTrue((now >>> 5) > (last >>> 5)); - last = now; - count++; - } - System.out.printf("count: %,d%n", count); - assertTrue(count > 1_000_000); - } - - @Test - public void concurrentTimeNanos() { - long start0 = System.nanoTime(); - final int runTimeUS = 1_000_000; - final int threads = Jvm.isArm() ? 4 : 16; - final int stride = Jvm.isArm() ? 1 : threads; - IntStream.range(0, threads) - .parallel() - .forEach(i -> { - TimeProvider tp = MappedUniqueTimeProvider.INSTANCE; - long start = tp.currentTimeNanos(); - long last = start; - for (int j = 0; j < runTimeUS; j += stride) { - long now = tp.currentTimeNanos(); - if (!Jvm.isArm()) - assertTrue(now < start + runTimeUS * 1000); - // check the times are different after shifting by 5 bits. - assertTrue((now >>> 5) > (last >>> 5)); - last = now; - } - }); - long time0 = System.nanoTime() - start0; - System.out.printf("Time: %,d ms%n", time0 / 1_000_000); - assertTrue(Jvm.isArm() || time0 < runTimeUS * 1000); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MoreBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MoreBytesTest.java deleted file mode 100644 index e6dcfad..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MoreBytesTest.java +++ /dev/null @@ -1,325 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.UTF8StringInterner; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.pool.StringInterner; -import net.openhft.chronicle.core.util.StringUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.junit.Assert; -import org.junit.Test; - -import java.nio.BufferOverflowException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static org.junit.Assert.*; - -public class MoreBytesTest { - - private static void testIndexOf(@NotNull final String sourceStr, @NotNull final String subStr) { - final Bytes source = Bytes.wrapForRead(sourceStr.getBytes(StandardCharsets.ISO_8859_1)); - final Bytes subBytes = Bytes.wrapForRead(subStr.getBytes(StandardCharsets.ISO_8859_1)); - Assert.assertEquals(sourceStr.indexOf(subStr), source.indexOf(subBytes)); - } - - @Test - public void testOneRelease() { - int count = 0; - for (@NotNull Bytes b : new Bytes[]{ - Bytes.allocateDirect(10), - Bytes.allocateDirect(new byte[5]), - Bytes.allocateElasticDirect(100), - Bytes.elasticByteBuffer(), - Bytes.wrapForRead(new byte[1]), - Bytes.wrapForRead(ByteBuffer.allocateDirect(128)), - Bytes.wrapForWrite(new byte[1]), - Bytes.wrapForWrite(ByteBuffer.allocateDirect(128)), - Bytes.elasticHeapByteBuffer(), - Bytes.elasticHeapByteBuffer(1), - Bytes.allocateElasticOnHeap(), - Bytes.allocateElasticOnHeap(1) - }) { - try { - assertEquals(count + ": " + b.getClass().getSimpleName(), 1, b.refCount()); - assertEquals(count + ": " + b.getClass().getSimpleName(), 1, b.bytesStore().refCount()); - } finally { - b.releaseLast(); - assertEquals(count + ": " + b.getClass().getSimpleName(), 0, b.refCount()); - assertEquals(count++ + ": " + b.getClass().getSimpleName(), 0, b.bytesStore().refCount()); - } - } - } - - @Test - public void testAppendLongRandomPosition() { - final @NotNull byte[] bytes = "00000".getBytes(ISO_8859_1); - final ByteBuffer bb = ByteBuffer.wrap(bytes); - final Bytes to = Bytes.wrapForWrite(bb); - try { - to.append(0, 1, 5); - assertEquals("00001", Bytes.wrapForRead(bb).toString()); - } finally { - to.releaseLast(); - } - } - - @Test - public void testAppendLongRandomPosition2() { - final @NotNull byte[] bytes = "WWWWW00000".getBytes(ISO_8859_1); - final ByteBuffer bb = ByteBuffer.wrap(bytes); - final Bytes to = Bytes.wrapForWrite(bb); - try { - to.append(5, 10, 5); - final Bytes bbb = Bytes.wrapForRead(bb); - assertEquals("WWWWW00010", bbb.toString()); - bbb.releaseLast(); - } finally { - to.releaseLast(); - } - } - - @Test - public void testAppendLongRandomPositionShouldThrowBufferOverflowException() { - try { - final @NotNull byte[] bytes = "000".getBytes(ISO_8859_1); - final ByteBuffer bb = ByteBuffer.wrap(bytes); - final Bytes to = Bytes.wrapForWrite(bb); - try { - to.append(0, 1000, 5); - fail("Should throw Exception"); - } finally { - to.releaseLast(); - } - } catch (Exception ignore) { - } - } - - @Test(expected = IllegalArgumentException.class) - public void testAppendLongRandomPositionShouldThrowIllegalArgumentException() { - try { - final @NotNull byte[] bytes = "000".getBytes(ISO_8859_1); - final ByteBuffer bb = ByteBuffer.wrap(bytes); - final Bytes to = Bytes.wrapForWrite(bb); - try { - to.append(0, 1000, 3); - } finally { - to.releaseLast(); - } - fail("Should throw Exception"); - } catch (BufferOverflowException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Test - public void testAppendDoubleRandomPosition() { - final @NotNull byte[] bytes = "000000".getBytes(ISO_8859_1); - final Bytes to = Bytes.wrapForWrite(bytes); - try { - to.append(0, 3.14, 2, 6); - } finally { - to.releaseLast(); - } - assertEquals("003.14", Bytes.wrapForRead(bytes).toString()); - } - - @Test - public void testAppendDoubleRandomPositionShouldThrowBufferOverflowException() { - try { - final @NotNull byte[] bytes = "000000".getBytes(ISO_8859_1); - final Bytes to = Bytes.wrapForWrite(bytes); - try { - to.append(0, 3.14, 2, 8); - } finally { - to.releaseLast(); - } - fail("Should throw Exception"); - } catch (BufferOverflowException ignore) { - // Ignore - } - } - - @Test(expected = IllegalArgumentException.class) - public void testAppendDoubleRandomPositionShouldThrowIllegalArgumentException() { - - final @NotNull byte[] bytes = "000000".getBytes(ISO_8859_1); - final Bytes to = Bytes.wrapForWrite(bytes); - try { - to.append(0, 33333.14, 2, 6); - } finally { - to.releaseLast(); - } - - } - - @Test - public void testInvalidUTF8Scan() { - int expected = 0; - for (int i = 0x80; i <= 0xFF; i++) - for (int j = 0x80; j <= 0xFF; j++) { - final @NotNull byte[] b = {(byte) i, (byte) j}; - final @NotNull String s = new String(b, StandardCharsets.UTF_8); - if (s.charAt(0) == 65533) { - final Bytes bytes = Bytes.wrapForRead(b); - try { - bytes.parseUtf8(StopCharTesters.ALL); - fail(Arrays.toString(b)); - } catch (UTFDataFormatRuntimeException e) { - expected++; - } - } - } - assertEquals(14464, expected); - } - - @Test - public void internBytes() throws IORuntimeException { - final Bytes b = Bytes.from("Hello World"); - try { - b.readSkip(6); - { - final @NotNull StringInterner si = new StringInterner(128); - final @Nullable String s = si.intern(b); - final @Nullable String s2 = si.intern(b); - assertEquals("World", s); - assertSame(s, s2); - } - { - final @NotNull UTF8StringInterner si = new UTF8StringInterner(128); - final String s = si.intern(b); - final String s2 = si.intern(b); - assertEquals("World", s); - assertSame(s, s2); - } - } finally { - b.releaseLast(); - } - } - - @Test - public void testIndexOfExactMatchAfterReadSkip() { - final String sourceStr = " some"; - final String subStr = "some"; - final Bytes source = Bytes.wrapForRead(sourceStr.getBytes(StandardCharsets.ISO_8859_1)); - source.readSkip(1); - final Bytes subBytes = Bytes.wrapForRead(subStr.getBytes(StandardCharsets.ISO_8859_1)); - Assert.assertEquals(0, source.indexOf(subBytes)); - } - - @Test - public void testIndexOfExactMatchAfterReadSkipOnSubStr() { - final String sourceStr = "some"; - final String subStr = " some"; - final Bytes source = Bytes.wrapForRead(sourceStr.getBytes(StandardCharsets.ISO_8859_1)); - final Bytes subBytes = Bytes.wrapForRead(subStr.getBytes(StandardCharsets.ISO_8859_1)); - subBytes.readSkip(1); - - Assert.assertEquals(0, source.indexOf(subBytes)); - assertEquals(1, subBytes.readPosition()); - assertEquals(0, source.readPosition()); - } - - @Test - public void testIndexOfAtEnd() { - final String sourceStr = "A string of some data"; - final String subStr = "ta"; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfEmptySubStr() { - final String sourceStr = "A string of some data"; - final String subStr = ""; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfEmptySubStrAndSource() { - final String sourceStr = ""; - final String subStr = ""; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfEmptySource() { - final String sourceStr = ""; - final String subStr = "some"; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfExactMatch() { - final String sourceStr = "some"; - final String subStr = "some"; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfIncorrectExactMatch() { - final String sourceStr = "some"; - final String subStr = " some"; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfExactMatchAtChar1() { - final String sourceStr = " some"; - final String subStr = "some"; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testIndexOfLastChar() { - final String sourceStr = " some"; - final String subStr = "e"; - testIndexOf(sourceStr, subStr); - } - - @Test - public void testCharAt() { - final Bytes b = Bytes.from("Hello World"); - try { - b.readSkip(6); - assertTrue(StringUtils.isEqual("World", b)); - } finally { - b.releaseLast(); - } - } - - @Test - public void testReadWithLength() { - final Bytes b = Bytes.from("Hello World"); - final Bytes bytesOut = Bytes.elasticByteBuffer(); - try { - // Todo: Why is this cast needed? - b.readWithLength(2, (Bytes) bytesOut); - assertEquals("He", bytesOut.toString()); - } finally { - b.releaseLast(); - bytesOut.releaseLast(); - } - } - - @Test - public void testStartsWith() { - final Bytes aaa = Bytes.from("aaa"); - final Bytes a = Bytes.from("a"); - assertTrue(aaa.startsWith(a)); - final Bytes aa = Bytes.from("aa"); - assertTrue(aaa.startsWith(aa)); - assertTrue(aaa.startsWith(aaa)); - final Bytes aaaa = Bytes.from("aaaa"); - assertFalse(aaa.startsWith(aaaa)); - final Bytes b = Bytes.from("b"); - assertFalse(aaa.startsWith(b)); - a.releaseLast(); - aa.releaseLast(); - aaa.releaseLast(); - aaaa.releaseLast(); - b.releaseLast(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyByteable.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyByteable.java deleted file mode 100644 index 826197b..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyByteable.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -class MyByteable implements BytesMarshallable { - boolean flag; - byte b; - short s; - char c; - int i; - float f; - long l; - double d; - - public MyByteable() { - } - - public MyByteable(boolean flag, byte b, short s, char c, int i, float f, long l, double d) { - this.flag = flag; - this.b = b; - this.s = s; - this.c = c; - this.i = i; - this.f = f; - this.l = l; - this.d = d; - } - - @NotNull - @Override - public String toString() { - return "MyByteable{" + - "flag=" + flag + - ", b=" + b + - ", s=" + s + - ", c=" + c + - ", i=" + i + - ", f=" + f + - ", l=" + l + - ", d=" + d + - '}'; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyBytes.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyBytes.java deleted file mode 100644 index 613e1d8..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyBytes.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.openhft.chronicle.bytes; - -import java.io.Closeable; -import java.io.IOException; - -@SuppressWarnings("rawtypes") -class MyBytes implements BytesMarshallable, Closeable { - Bytes bytes1; - Bytes bytes2; - - public MyBytes() { - } - - public MyBytes(Bytes bytes1, Bytes bytes2) { - this.bytes1 = bytes1; - this.bytes2 = bytes2; - } - - @Override - public void close() throws IOException { - if (bytes1 != null) bytes1.releaseLast(); - if (bytes2 != null) bytes2.releaseLast(); - } - - @Override - public String toString() { - return "MyBytes{" + - "bytes1=" + bytes1 + - ", bytes2=" + bytes2 + - '}'; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyNested.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyNested.java deleted file mode 100644 index baecb0e..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyNested.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -class MyNested implements BytesMarshallable { - MyByteable byteable; - MyScalars scalars; - - public MyNested() { - } - - public MyNested(MyByteable byteable, MyScalars scalars) { - this.byteable = byteable; - this.scalars = scalars; - } - - @NotNull - @Override - public String toString() { - return "MyNested{" + - "byteable=" + byteable + - ", scalars=" + scalars + - '}'; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyScalars.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyScalars.java deleted file mode 100644 index 9645c15..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/MyScalars.java +++ /dev/null @@ -1,51 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZonedDateTime; -import java.util.UUID; - -class MyScalars implements BytesMarshallable { - String s; - BigInteger bi; - BigDecimal bd; - LocalDate date; - LocalTime time; - LocalDateTime dateTime; - ZonedDateTime zonedDateTime; - UUID uuid; - - public MyScalars() { - } - - public MyScalars(String s, BigInteger bi, BigDecimal bd, LocalDate date, LocalTime time, LocalDateTime dateTime, ZonedDateTime zonedDateTime, UUID uuid) { - this.s = s; - this.bi = bi; - this.bd = bd; - this.date = date; - this.time = time; - this.dateTime = dateTime; - this.zonedDateTime = zonedDateTime; - this.uuid = uuid; - } - - @NotNull - @Override - public String toString() { - return "MyScalars{" + - "s='" + s + '\'' + - ", bi=" + bi + - ", bd=" + bd + - ", date=" + date + - ", time=" + time + - ", dateTime=" + dateTime + - ", zonedDateTime=" + zonedDateTime + - ", uuid=" + uuid + - '}'; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesOverflowTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesOverflowTest.java deleted file mode 100644 index 2b75562..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesOverflowTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import java.nio.BufferOverflowException; -import java.nio.ByteBuffer; - -import static net.openhft.chronicle.bytes.BytesStore.wrap; -import static org.junit.Assert.assertTrue; - -public class NativeBytesOverflowTest { - - @Test(expected = BufferOverflowException.class) - public void testExceedWriteLimitNativeWriteBytes() { - BytesStore store = wrap(ByteBuffer.allocate(128)); - Bytes nb = new NativeBytes(store); - try { - nb.writeLimit(2).writePosition(0); - nb.writeLong(10L); - } finally { - nb.releaseLast(); - } - } - - @Test(expected = BufferOverflowException.class) - public void testExceedWriteLimitGuardedBytes() { - Bytes guardedNativeBytes = new GuardedNativeBytes(wrap(ByteBuffer.allocate(128)), 128); - try { - guardedNativeBytes.writeLimit(2).writePosition(0); - guardedNativeBytes.writeLong(10L); - } finally { - guardedNativeBytes.releaseLast(); - } - } - - @Test(expected = BufferOverflowException.class) - public void testElastic() { - Bytes bytes = Bytes.elasticByteBuffer(); - try { - bytes.writeLimit(2).writePosition(0); - bytes.writeLong(10L); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testNativeWriteBytes2() { - Bytes nb = new NativeBytes(wrap(ByteBuffer.allocate(128))).unchecked(true); - - nb.writeLimit(2).writePosition(0); - nb.writeLong(10L); - - // this is OK as we are unchecked ! - assertTrue(nb.writePosition() > nb.writeLimit()); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesStoreTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesStoreTest.java deleted file mode 100644 index 94f3cc9..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesStoreTest.java +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import net.openhft.chronicle.core.util.Histogram; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import javax.crypto.Cipher; -import javax.crypto.NoSuchPaddingException; -import javax.crypto.spec.SecretKeySpec; -import java.nio.BufferOverflowException; -import java.nio.ByteBuffer; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Random; -import java.util.stream.Stream; - -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -public class NativeBytesStoreTest extends BytesTestCommon { - - volatile int bcs; - private ThreadDump threadDump; - - private static void generate(final @NotNull Bytes bytes, final int t) { - bytes.clear(); - bytes.append("hello world "); - for (int i = 0; i <= t; i++) - bytes.append(t); - } - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testCipherPerf() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { - final byte[] keyBytes = new SecureRandom().generateSeed(16); - final SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); - final Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - final Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - encCipher.init(Cipher.ENCRYPT_MODE, key); - decCipher.init(Cipher.DECRYPT_MODE, key); - - final StringBuilder sb = new StringBuilder("Hello World!!"); - while (sb.length() < 100) - sb.append(" 123456789"); - final String expected = sb.toString(); - - final Bytes bytes = Bytes.allocateDirect(expected.getBytes()); - final Bytes enc = Bytes.allocateElasticDirect(); - final Bytes dec = Bytes.allocateElasticDirect(); - try { - final Histogram hist = new Histogram(); - for (int t = 1; t <= 4; t++) { - for (int i = 0; i < t * 100000; i++) { - enc.clear(); - dec.clear(); - final long start = System.nanoTime(); - bytes.cipher(encCipher, enc); - enc.cipher(decCipher, dec); - final long time = System.nanoTime() - start; - hist.sampleNanos(time); - } - assertEquals(expected, dec.toString()); -// System.out.println("Encrypt/Decrypt took " + hist.toMicrosFormat()); - } - } finally { - bytes.releaseLast(); - enc.releaseLast(); - dec.releaseLast(); - } - } - - @Test - public void testCipher() throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException { - assumeFalse(NativeBytes.areNewGuarded()); - final byte[] keyBytes = new SecureRandom().generateSeed(16); - final SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); - final Cipher encCipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - final Cipher decCipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - encCipher.init(Cipher.ENCRYPT_MODE, key); - decCipher.init(Cipher.DECRYPT_MODE, key); - - final Bytes bytes = Bytes.allocateElasticDirect(); - final Bytes enc = Bytes.allocateElasticDirect(); - final Bytes dec = Bytes.allocateElasticDirect(); - try { - - for (int t = 0; t < 9; t++) { - final long pos = enc.writePosition(); - enc.writeByte((byte) 0); - generate(bytes, t); - bytes.cipher(encCipher, enc); - final long len = enc.writePosition() - pos - 1; - assertEquals(0, len % 16); - enc.writeUnsignedByte(pos, Maths.toUInt8(len)); -// System.out.println(len); - } -// System.out.println("reading"); - for (int t = 0; t < 9; t++) { - final int len = enc.readUnsignedByte(); -// System.out.println(len); - assertEquals(0, len % 16); - final long pos = enc.readPosition(); - enc.readPositionRemaining(pos, len); - dec.clear(); - enc.cipher(decCipher, dec); - generate(bytes, t); - assertEquals(bytes.toString(), dec.toString()); - enc.readPositionRemaining(pos + len, 1); - } - } finally { - bytes.releaseLast(); - enc.releaseLast(); - dec.releaseLast(); - } - - } - - @Test - public void testElasticByteBuffer() throws IORuntimeException, BufferOverflowException { - final Bytes bbb = Bytes.elasticByteBuffer(); - try { - assertEquals(Bytes.MAX_HEAP_CAPACITY, bbb.capacity()); - assertEquals(Bytes.DEFAULT_BYTE_BUFFER_CAPACITY, bbb.realCapacity()); - final @Nullable ByteBuffer bb = bbb.underlyingObject(); - assertNotNull(bb); - - for (int i = 0; i < 20; i++) { - bbb.writeSkip(1000); - bbb.writeLong(12345); - } - assertEquals(28672, bbb.realCapacity()); - final @Nullable ByteBuffer bb2 = bbb.underlyingObject(); - assertNotNull(bb2); - assertNotSame(bb, bb2); - } finally { - bbb.releaseLast(); - } - } - - @Test - public void testAppendUtf8() { - final String hi = "Hello World"; - final char[] chars = hi.toCharArray(); - final NativeBytesStore nbs = NativeBytesStore.nativeStore(chars.length); - try { - nbs.appendUtf8(0, chars, 0, chars.length); - assertEquals(hi, nbs.toString()); - } finally { - nbs.releaseLast(); - } - } - - @Test - public void testToTempByteBuf() { - final String hi = "Hello World"; - final char[] chars = hi.toCharArray(); - final NativeBytesStore bs = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(128); - try { - bs.appendUtf8(0, chars, 0, chars.length); - final ByteBuffer bb = bs.toTemporaryDirectByteBuffer(); - for (int i = 0; i < chars.length; i++) { - assertEquals(bb.get(i), (byte) chars[i]); - } - } finally { - bs.releaseLast(); - } - } - - @Test - public void perfCheckSum() throws IORuntimeException { - final NativeBytesStore[] nbs = { - NativeBytesStore.nativeStoreWithFixedCapacity(140), - NativeBytesStore.nativeStoreWithFixedCapacity(149), - NativeBytesStore.nativeStoreWithFixedCapacity(159), - NativeBytesStore.nativeStoreWithFixedCapacity(194) - }; - try { - final Random rand = new Random(); - for (NativeBytesStore nb : nbs) { - final byte[] bytes = new byte[(int) nb.capacity()]; - rand.nextBytes(bytes); - nb.write(0, bytes); - assertEquals(Bytes.wrapForRead(bytes).byteCheckSum(), nb.byteCheckSum()); - } - for (int t = 2; t >= 0; t--) { - int runs = 10000000; - final long start = System.nanoTime(); - for (int i = 0; i < runs; i += 4) { - for (NativeBytesStore nb : nbs) { - bcs = nb.byteCheckSum(); - if (bcs < 0 || bcs > 255) - throw new AssertionError(); - } - } - long time = System.nanoTime() - start; - if (t == 0) - System.out.printf("Average time was %,d ns%n", time / runs); - } - } finally { - Stream.of(nbs) - .forEach(NativeBytesStore::releaseLast); - } - } - - @Test - public void testCopyTo() { - final Bytes src = Bytes.elasticByteBuffer().writeUtf8("hello"); - final Bytes dst = Bytes.elasticByteBuffer(); - try { - dst.writePosition(src.copyTo(dst)); - assertEquals(src, dst); - } finally { - src.releaseLast(); - dst.releaseLast(); - } - } - - @SuppressWarnings("rawtypes") - @Test - public void testEquals() { - @NotNull NativeBytesStore hbs = NativeBytesStore.from("Hello".getBytes()); - @NotNull NativeBytesStore hbs2 = NativeBytesStore.from("Hello".getBytes()); - @NotNull NativeBytesStore hbs3 = NativeBytesStore.from("He!!o".getBytes()); - @NotNull NativeBytesStore hbs4 = NativeBytesStore.from("Hi".getBytes()); - assertEquals(hbs, hbs2); - assertEquals(hbs2, hbs); - assertNotEquals(hbs, hbs3); - assertNotEquals(hbs3, hbs); - assertNotEquals(hbs, hbs4); - assertNotEquals(hbs4, hbs); - hbs.releaseLast(); - hbs2.releaseLast(); - hbs3.releaseLast(); - hbs4.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesTest.java deleted file mode 100644 index 5cc1868..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/NativeBytesTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.bytes.util.DecoratedBufferOverflowException; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.junit.*; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.nio.BufferOverflowException; -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.Collection; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.Allocator.*; -import static org.junit.Assert.*; - -@RunWith(Parameterized.class) -public class NativeBytesTest extends BytesTestCommon { - - private final Allocator alloc; - private ThreadDump threadDump; - - public NativeBytesTest(Allocator alloc) { - this.alloc = alloc; - } - - @Parameterized.Parameters - public static Collection data() { - return Arrays.asList(new Object[][]{ - {NATIVE}, {HEAP}, {BYTE_BUFFER} - }); - } - - @After - public void checkRegisteredBytes() { - BytesUtil.checkRegisteredBytes(); - } - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @SuppressWarnings("rawtypes") - @Test - public void testWriteBytesWhereResizeNeeded0() throws IORuntimeException, BufferUnderflowException, BufferOverflowException { - Bytes b = alloc.elasticBytes(1); - assertEquals(b.start(), b.readLimit()); - assertEquals(b.capacity(), b.writeLimit()); - assertEquals(1, b.realCapacity()); - assertTrue(b.readLimit() < b.writeLimit()); - - Bytes wrap0 = Bytes.wrapForRead("Hello World, Have a great day!".getBytes(ISO_8859_1)); - b.writeSome(wrap0); - assertEquals("Hello World, Have a great day!", b.toString()); - b.releaseLast(); - } - - @SuppressWarnings("rawtypes") - @Test - public void testWriteBytesWhereResizeNeeded() throws IllegalArgumentException, IORuntimeException, BufferUnderflowException, BufferOverflowException { - Bytes b = alloc.elasticBytes(1); - assertEquals(b.start(), b.readLimit()); - assertEquals(b.capacity(), b.writeLimit()); - assertEquals(1, b.realCapacity()); - assertTrue(b.readLimit() < b.writeLimit()); - - Bytes wrap1 = Bytes.wrapForRead("Hello World, Have a great day!".getBytes(ISO_8859_1)); - b.writeSome(wrap1); - assertEquals("Hello World, Have a great day!", b.toString()); - b.releaseLast(); - } - - @SuppressWarnings("rawtypes") - @Test - public void testAppendCharArrayNonAscii() { - Bytes b = alloc.elasticBytes(1); - b.appendUtf8(new char[]{'Δ'}, 0, 1); - b.releaseLast(); - } - - @Test - public void testResizeTwoPagesToThreePages() { - Assume.assumeFalse(alloc == HEAP); - - long pageSize = OS.pageSize(); - @NotNull NativeBytes nativeBytes = NativeBytes.nativeBytes(2 * pageSize); - assertEquals(2 * pageSize, nativeBytes.realCapacity()); - nativeBytes.writePosition(nativeBytes.realCapacity() - 3); - nativeBytes.writeInt(0); - assertEquals(4 * pageSize, nativeBytes.realCapacity()); - - nativeBytes.releaseLast(); - } - - @Test(expected = DecoratedBufferOverflowException.class) - public void tryGrowBeyondByteBufferCapacity() { - Assume.assumeFalse(alloc == HEAP); - long maxMemory = Runtime.getRuntime().maxMemory(); - Assume.assumeTrue(maxMemory >= Bytes.MAX_HEAP_CAPACITY * 3L / 2); - - @NotNull Bytes bytes = Bytes.elasticHeapByteBuffer(Bytes.MAX_HEAP_CAPACITY); - @Nullable ByteBuffer byteBuffer = bytes.underlyingObject(); - assertFalse(byteBuffer.isDirect()); - - // Trigger growing beyond ByteBuffer - bytes.writePosition(bytes.realCapacity() - 1); - bytes.writeInt(0); - } - - @Test(expected = BufferOverflowException.class) - public void tryGrowBeyondCapacity() { - final int maxCapacity = 1024; - @NotNull Bytes bytes = Bytes.elasticByteBuffer(128, maxCapacity); - assertEquals(128, bytes.realCapacity()); - assertEquals(maxCapacity, bytes.capacity()); - @Nullable ByteBuffer byteBuffer = bytes.underlyingObject(); - assertTrue(byteBuffer.isDirect()); - - // trigger resize - bytes.write(new byte[256]); - try { - // Trigger growing beyond maxCapacity - bytes.write(new byte[maxCapacity]); - Assert.fail("should not get here"); - } finally { - bytes.releaseLast(); - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PointerBytesStoreTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PointerBytesStoreTest.java deleted file mode 100644 index 0fd6459..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PointerBytesStoreTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import org.junit.After; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class PointerBytesStoreTest extends BytesTestCommon { - - @After - public void checkRegisteredBytes() { - AbstractReferenceCounted.assertReferencesReleased(); - } - - @Test - public void testWrap() { - final NativeBytesStore nbs = NativeBytesStore.nativeStore(10000); - final PointerBytesStore pbs = BytesStore.nativePointer(); - try { - pbs.set(nbs.addressForRead(nbs.start()), nbs.realCapacity()); - final long nanoTime = System.nanoTime(); - pbs.writeLong(0L, nanoTime); - - assertEquals(nanoTime, nbs.readLong(0L)); - } finally { - nbs.releaseLast(); - pbs.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PrewriteTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PrewriteTest.java deleted file mode 100644 index bc67612..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PrewriteTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class PrewriteTest extends BytesTestCommon { - @SuppressWarnings("rawtypes") - @Test - public void test() { - Bytes bytes = Bytes.allocateDirect(64); - bytes.clearAndPad(64); - bytes.prepend(1234); - bytes.prewrite(",hi,".getBytes()); - Bytes words = Bytes.from("words"); - bytes.prewrite(words); - bytes.prewriteByte((byte) ','); - bytes.prewriteInt(0x34333231); - bytes.prewriteLong(0x3837363534333231L); - bytes.prewriteShort((short) 0x3130); - assertEquals("01123456781234,words,hi,1234", bytes.toString()); - - bytes.releaseLast(); - words.releaseLast(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PrintVdsoMain.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PrintVdsoMain.java deleted file mode 100644 index 6e36c7f..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/PrintVdsoMain.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; - -import java.io.*; - -public class PrintVdsoMain { - @SuppressWarnings("rawtypes") - public static void main(String[] args) throws IOException, IllegalStateException { - long start = 0; - long end = 0; - @NotNull String maps = "/proc/self/maps"; - if (!new File(maps).exists()) return; - try (@NotNull BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(maps)))) { - for (String line; (line = br.readLine()) != null; ) { - if (line.endsWith("[vdso]")) { - @NotNull String[] parts = line.split("[- ]"); - start = Long.parseLong(parts[0], 16); - end = Long.parseLong(parts[1], 16); - } - -// System.out.println(line); - } - } catch (IOException ioe) { - throw ioe; - } - System.out.printf("vdso %x to %x %n", start, end); - @NotNull PointerBytesStore nb = new PointerBytesStore(); - nb.set(start, end - start); - @NotNull FileOutputStream fos = new FileOutputStream("vdso.elf"); - for (Bytes b = nb.bytesForRead(); b.readRemaining() > 0; ) - fos.write(b.readByte()); - fos.close(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReadLenientTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReadLenientTest.java deleted file mode 100644 index 9683c56..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReadLenientTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.ByteBuffer; - -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -public class ReadLenientTest extends BytesTestCommon { - @Test - public void testLenient() { - assumeFalse(NativeBytes.areNewGuarded()); - doTest(Bytes.allocateDirect(64)); - doTest(Bytes.allocateElasticOnHeap(64)); - doTest(Bytes.from("")); - } - - @SuppressWarnings("rawtypes") - private void doTest(Bytes bytes) { - bytes.lenient(true); - ByteBuffer bb = ByteBuffer.allocateDirect(32); - bytes.read(bb); - assertEquals(0, bb.position()); - - assertEquals(BigDecimal.ZERO, bytes.readBigDecimal()); - assertEquals(BigInteger.ZERO, bytes.readBigInteger()); - assertFalse(bytes.readBoolean()); - assertNull(bytes.read8bit()); - assertNull(bytes.readUtf8()); - assertEquals(0, bytes.readByte()); - assertEquals(-1, bytes.readUnsignedByte()); // note this behaviour is need to find the end of a stream. - assertEquals(0, bytes.readShort()); - assertEquals(0, bytes.readUnsignedShort()); - assertEquals(0, bytes.readInt()); - assertEquals(0, bytes.readUnsignedInt()); - assertEquals(0.0, bytes.readFloat(), 0.0); - assertEquals(0.0, bytes.readDouble(), 0.0); - bytes.readSkip(8); - assertEquals(0, bytes.readPosition()); - - bytes.releaseLast(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReadWriteMarshallableTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReadWriteMarshallableTest.java deleted file mode 100644 index 2e86218..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReadWriteMarshallableTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.io.IORuntimeException; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.assumeFalse; - -@SuppressWarnings("rawtypes") -public class ReadWriteMarshallableTest extends BytesTestCommon { - @Test - public void test() { - // TODO Make guarded safe - assumeFalse(NativeBytes.areNewGuarded()); - - Bytes bytes = Bytes.allocateElasticOnHeap(128); - Bytes hello_world = Bytes.from("Hello World"); - Bytes bye = Bytes.from("Bye"); - RWOuter o = new RWOuter( - new RWInner(hello_world), - new RWInner(bye)); - - bytes.writeMarshallableLength16(o); - - RWOuter o2 = bytes.readMarshallableLength16(RWOuter.class, null); - assertEquals("Hello World", o2.i1.data.toString()); - assertEquals("Bye", o2.i2.data.toString()); - hello_world.releaseLast(); - bye.releaseLast(); - } - - static class RWOuter implements BytesMarshallable { - RWInner i1, i2; - - public RWOuter(RWInner i1, RWInner i2) { - this.i1 = i1; - this.i2 = i2; - } - - @Override - public void readMarshallable(BytesIn bytes) throws IORuntimeException { - BytesIn in = (BytesIn) bytes; - i1 = in.readMarshallableLength16(RWInner.class, i1); - i2 = in.readMarshallableLength16(RWInner.class, i2); - } - - @Override - public void writeMarshallable(BytesOut bytes) { - bytes.writeMarshallableLength16(i1); - bytes.writeMarshallableLength16(i2); - } - } - - static class RWInner implements BytesMarshallable { - Bytes data; - - public RWInner(Bytes data) { - this.data = data; - } - - @Override - public void readMarshallable(BytesIn bytes) throws IORuntimeException { - if (data == null) data = Bytes.allocateElasticOnHeap(64); - data.clear().write((BytesStore) bytes); - } - - @Override - public void writeMarshallable(BytesOut bytes) { - bytes.write(data); - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReleasedBytesStoreTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReleasedBytesStoreTest.java deleted file mode 100644 index a9a4559..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ReleasedBytesStoreTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class ReleasedBytesStoreTest extends BytesTestCommon { - - @Test - public void release() { - Bytes bytes = Bytes.allocateElasticDirect(); - assertEquals(NoBytesStore.class, bytes.bytesStore().getClass()); - bytes.writeLong(0, 0); - assertEquals(NativeBytesStore.class, bytes.bytesStore().getClass()); - bytes.releaseLast(); - assertEquals(ReleasedBytesStore.class, bytes.bytesStore().getClass()); - try { - bytes.writeLong(0, 0); - fail(); - } catch (IllegalStateException e) { - // expected. - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StopBitDecimalTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StopBitDecimalTest.java deleted file mode 100644 index f13ea31..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StopBitDecimalTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import org.junit.Test; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.util.Random; - -import static junit.framework.TestCase.assertEquals; -import static org.junit.Assume.assumeFalse; - -public class StopBitDecimalTest extends BytesTestCommon { - @Test - public void testDecimals() { - assumeFalse(NativeBytes.areNewGuarded()); - - Bytes bytes = Bytes.elasticHeapByteBuffer(16); - Random rand = new Random(); - for (int i = 0; i < 10_000; i++) { - rand.setSeed(i); - bytes.clear(); - int scale = rand.nextInt(10); - double d = (rand.nextLong() % 1e14) / Maths.tens(scale); - bytes.writeStopBitDecimal(d); - BigDecimal bd = BigDecimal.valueOf(d); - long v = bytes.readStopBit(); - BigDecimal ebd = new BigDecimal(BigInteger.valueOf(v / 10), (int) (Math.abs(v) % 10)); - assertEquals("i: " + i + ", d: " + d + ", v: " + v, ebd.doubleValue(), bd.doubleValue(), 0.0); - bytes.readPosition(0); - double d2 = bytes.readStopBitDecimal(); - assertEquals("i: " + i + ", d: " + d + ", v: " + v, d, d2, 0.0); - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StreamingDataInputTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StreamingDataInputTest.java deleted file mode 100644 index 11153ee..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StreamingDataInputTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class StreamingDataInputTest extends BytesTestCommon { - @Test - public void read() { - Bytes b = Bytes.allocateDirect(128); - b.append("0123456789"); - byte[] byteArr = "ABCDEFGHIJKLMNOP".getBytes(); - b.read(byteArr, 2, 6); - assertEquals("AB012345IJKLMNOP", new String(byteArr, 0)); - assertEquals('6', b.readByte()); - b.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StreamingInputStreamTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StreamingInputStreamTest.java deleted file mode 100644 index eaf046d..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/StreamingInputStreamTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; - -@SuppressWarnings("rawtypes") -public class StreamingInputStreamTest extends BytesTestCommon { - private ThreadDump threadDump; - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - // https://github.com/OpenHFT/Chronicle-Bytes/issues/48 - @Test - public void readOfZeroShouldReturnZero() throws IOException { - @NotNull Bytes b = Bytes.allocateElasticDirect(); - prepareBytes(b); - - @NotNull InputStream is = b.inputStream(); - assertEquals(0, is.read(new byte[5], 0, 0)); - b.releaseLast(); - } - - @Test(timeout = 1000) - public void testReadBlock() throws IOException { - - @NotNull Bytes b = Bytes.allocateElasticDirect(); - @NotNull byte[] test = prepareBytes(b); - - @NotNull InputStream is = b.inputStream(); - try (@NotNull ByteArrayOutputStream os = new ByteArrayOutputStream()) { - @NotNull byte[] buffer = new byte[8]; - for (int len; (len = is.read(buffer)) != -1; ) - os.write(buffer, 0, len); - os.flush(); - assertArrayEquals(test, os.toByteArray()); - } - - b.releaseLast(); - } - - private byte[] prepareBytes(final Bytes b) { - @NotNull byte[] test = "Hello World, Have a great day!".getBytes(ISO_8859_1); - b.write(test); - return test; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UTF8BytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UTF8BytesTest.java deleted file mode 100644 index 1f64ed9..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UTF8BytesTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; - -import java.io.File; -import java.io.IOException; - -import static org.junit.Assert.assertEquals; - -public class UTF8BytesTest { - - private static final String MESSAGE = "awésome-message-1"; - - @Test - public void testUtfEncoding() throws IOException { - File f = File.createTempFile("testUtfEncoding", "data"); - f.deleteOnExit(); - final MappedBytes bytes = MappedBytes.mappedBytes(f, 256); - int len = (int) AppendableUtil.findUtf8Length(MESSAGE); - bytes.appendUtf8(MESSAGE); - - StringBuilder sb = new StringBuilder(); - bytes.parseUtf8(sb, true, len); - assertEquals(MESSAGE, sb.toString()); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UnsafeRWObjectTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UnsafeRWObjectTest.java deleted file mode 100644 index 836800e..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UnsafeRWObjectTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Jvm; -import org.junit.Test; - -import java.util.Arrays; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.assumeTrue; - -public class UnsafeRWObjectTest { - @Test - public void shortObject() { - assumeTrue(Jvm.is64bit() && !Jvm.isAzulZing()); - assertEquals("[12, 32]", - Arrays.toString( - BytesUtil.triviallyCopyableRange(AA.class))); - Bytes bytes = Bytes.allocateDirect(32); - AA aa = new AA(1, 2, 3); - bytes.unsafeWriteObject(aa, 4 + 2 * 8); - assertEquals("" + - "00000000 01 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000010 00 00 08 40 ···@ \n", - bytes.toHexString()); - AA a2 = new AA(0, 0, 0); - bytes.unsafeReadObject(a2, 4 + 2 * 8); - assertEquals(aa.i, a2.i); - assertEquals(aa.l, a2.l); - assertEquals(aa.d, a2.d, 0.0); - bytes.releaseLast(); - } - - @Test - public void longObject() { - assumeTrue(Jvm.is64bit() && !Jvm.isAzulZing()); - assertEquals("[16, 48]", - Arrays.toString( - BytesUtil.triviallyCopyableRange(BB.class))); - Bytes bytes = Bytes.allocateDirect(4 * 8); - BB bb = new BB(1, 2, 3, 4); - bytes.unsafeWriteObject(bb, 16, 4 * 8); - String expected = "" + - "00000000 01 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 ········ ········\n" + - "00000010 03 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ········ ········\n"; - assertEquals(expected, - bytes.toHexString()); - BB b2 = new BB(0, 0, 0, 0); - bytes.unsafeReadObject(b2, 16, 4 * 8); - Bytes bytes2 = Bytes.allocateElasticOnHeap(4 * 8); - bytes2.unsafeWriteObject(b2, 16, 4 * 8); - assertEquals(expected, - bytes2.toHexString()); - - bytes.releaseLast(); - } - - @Test - public void array() { - assumeTrue(Jvm.is64bit() && !Jvm.isAzulZing()); - assertEquals("[16]", - Arrays.toString( - BytesUtil.triviallyCopyableRange(byte[].class))); - Bytes bytes = Bytes.allocateDirect(32); - byte[] byteArray = "Hello World.".getBytes(); - bytes.unsafeWriteObject(byteArray, byteArray.length); - assertEquals("" + - "00000000 48 65 6c 6c 6f 20 57 6f 72 6c 64 2e Hello Wo rld. \n", - bytes.toHexString()); - byte[] byteArray2 = new byte[byteArray.length]; - bytes.unsafeReadObject(byteArray2, byteArray.length); - assertEquals("Hello World.", new String(byteArray2)); - bytes.releaseLast(); - - } - - static class AA { - int i; - long l; - double d; - - public AA(int i, long l, double d) { - this.i = i; - this.l = l; - this.d = d; - } - } - - static class BB { - long l0; - long l1; - long l2; - long l3; - - public BB(long l0, long l1, long l2, long l3) { - this.l0 = l0; - this.l1 = l1; - this.l2 = l2; - this.l3 = l3; - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UnsafeTextBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UnsafeTextBytesTest.java deleted file mode 100644 index 42ae60a..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/UnsafeTextBytesTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package net.openhft.chronicle.bytes; - -import net.openhft.chronicle.core.Maths; -import net.openhft.chronicle.core.io.UnsafeText; -import org.junit.Test; - -import java.util.Random; - -import static org.junit.Assert.assertEquals; - -public class UnsafeTextBytesTest extends BytesTestCommon { - - static void testAppendBase10(final Bytes bytes, final long l) { - final long address = bytes.clear().addressForRead(0); - final long end = UnsafeText.appendFixed(address, l); - bytes.readLimit(end - address); - String message = bytes.toString(); - assertEquals(message, l, bytes.parseLong()); - } - - static String testAppendDouble(final Bytes bytes, final double l) { - final long address = bytes.clear().addressForRead(0); - final long end = UnsafeText.appendDouble(address, l); - bytes.readLimit(end - address); - final String message = bytes.toString(); - assertEquals(message, l, bytes.parseDouble(), Math.ulp(l)); - return message; - } - - static void testAppendFixed(final Bytes bytes, - final double l, - final int digits) { - final long address = bytes.clear().addressForRead(0); - final long end = UnsafeText.appendFixed(address, l, digits); - bytes.readLimit(end - address); - final String message = bytes.toString(); - final double expected = Maths.round4(l); - final double actual = bytes.parseDouble(); - assertEquals(message, expected, actual, 0.0); - } - - @Test - public void appendBase10() { - final Bytes bytes = Bytes.allocateDirect(32); - try { - for (long l = Long.MAX_VALUE; l > 0; l /= 2) { - testAppendBase10(bytes, l); - testAppendBase10(bytes, 1 - l); - } - } finally { - bytes.releaseLast(); - } - } - - @Test - public void appendDouble() { - final Random rand = new Random(1); - final Bytes bytes = Bytes.allocateDirect(32); - try { - testAppendFixed(bytes, 0.0003, 4); - for (int i = 0; i < 1000000; i++) { - double d = Math.pow(1e16, rand.nextDouble()) / 1e4; - testAppendDouble(bytes, d); - testAppendFixed(bytes, d, 4); - } - } finally { - bytes.releaseLast(); - } - } - - @Test - public void appendDouble2() { - final Bytes bytes = Bytes.allocateDirect(32); - try { - for (double d : new double[]{ - 741138311171.555, - 0.0, -0.0, 0.1, 0.012, 0.00123, 1.0, Double.NaN, 1 / 0.0, -1 / 0.0}) - testAppendDouble(bytes, d); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void extraZeros() { - final Bytes bytes = Bytes.allocateDirect(32); - try { - final double d = -0.00002; - final String output = testAppendDouble(bytes, d); - assertEquals("-0.00002", output); - } finally { - bytes.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/VanillaBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/VanillaBytesTest.java deleted file mode 100644 index 81b9448..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/VanillaBytesTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes; - -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -@SuppressWarnings("rawtypes") -public class VanillaBytesTest extends BytesTestCommon { - @Test - public void testBytesForRead() { - @NotNull byte[] byteArr = new byte[128]; - for (int i = 0; i < byteArr.length; i++) - byteArr[i] = (byte) i; - Bytes bytes = Bytes.wrapForRead(byteArr); - bytes.readSkip(8); - @NotNull Bytes bytes2 = bytes.bytesForRead(); - assertEquals(128 - 8, bytes2.readRemaining()); - assertEquals(8, bytes2.readPosition()); - assertEquals(8, bytes2.readByte(bytes2.start())); - assertEquals(9, bytes2.readByte(bytes2.start() + 1)); - assertEquals(9, bytes.readByte(9)); - bytes2.writeByte(bytes2.start() + 1, 99); - assertEquals(99, bytes.readByte(99)); - - bytes.releaseLast(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/WriteLimitTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/WriteLimitTest.java deleted file mode 100644 index 539b89c..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/WriteLimitTest.java +++ /dev/null @@ -1,78 +0,0 @@ -package net.openhft.chronicle.bytes; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.nio.BufferOverflowException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Random; -import java.util.function.Consumer; - -import static org.junit.Assert.fail; - -@RunWith(Parameterized.class) -public class WriteLimitTest { - static final Allocator[] ALLOCATORS = {Allocator.NATIVE, Allocator.HEAP, Allocator.BYTE_BUFFER}; - static List tests; - static Random random = new Random(); - final String name; - final Allocator allocator; - final Consumer action; - final int length; - - public WriteLimitTest(String name, Allocator allocator, Consumer action, int length) { - this.name = name; - this.allocator = allocator; - this.action = action; - this.length = length; - } - - @Parameterized.Parameters(name = "{0}") - public static Collection data() { - tests = new ArrayList<>(); - addTest("boolean", b -> b.writeBoolean(true), 1); - addTest("byte", b -> b.writeByte((byte) 1), 1); - addTest("unsigned-byte", b -> b.writeUnsignedByte(1), 1); - addTest("short", b -> b.writeShort((short) 1), 2); - addTest("unsigned-short", b -> b.writeUnsignedShort(1), 2); - addTest("char $", b -> b.writeChar('$'), 1); - addTest("char £", b -> b.writeChar('£'), 2); - addTest("char " + (char) (1 << 14), b -> b.writeChar((char) (1 << 14)), 3); - addTest("int", b -> b.writeInt(1), 4); - addTest("unsigned-int", b -> b.writeUnsignedInt(1), 4); - addTest("float", b -> b.writeFloat(1), 4); - addTest("long", b -> b.writeLong(1), 8); - addTest("double", b -> b.writeDouble(1), 8); - return tests; - } - - static void addTest(String name, Consumer action, int length) { - for (Allocator a : ALLOCATORS) - tests.add(new Object[]{a + " " + name, a, action, length}); - } - - @Test - public void writeLimit() { - Bytes bytes = allocator.elasticBytes(64); - for (int i = 0; i < 16; i++) { - int position = (int) (bytes.realCapacity() - length - i); - bytes.clear().writePosition(position).writeLimit(position + length); - action.accept(bytes); - - try { - bytes.clear().writePosition(position).writeLimit(position + length - 1); - action.accept(bytes); - - bytes.clear().writePosition(position).writeLimit(position + length - 1); - action.accept(bytes); - fail("position: " + position); - } catch (BufferOverflowException expected) { - - } - } - bytes.releaseLast(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/algo/OptimisedBytesStoreHashTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/algo/OptimisedBytesStoreHashTest.java deleted file mode 100644 index a47c23c..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/algo/OptimisedBytesStoreHashTest.java +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.algo; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytes; -import net.openhft.chronicle.bytes.NativeBytesStore; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.security.SecureRandom; -import java.util.Random; - -import static net.openhft.chronicle.bytes.algo.OptimisedBytesStoreHash.*; -import static org.junit.Assert.assertEquals; - -@SuppressWarnings("rawtypes") -public class OptimisedBytesStoreHashTest extends BytesTestCommon { - - private ThreadDump threadDump; - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testApplyAsLong() { - @NotNull NativeBytes b = Bytes.allocateElasticDirect(128); - b.writeLong(0x0102030405060708L); - b.writeLong(0x1112131415161718L); - assertEquals(VanillaBytesStoreHash.INSTANCE.applyAsLong(b), - OptimisedBytesStoreHash.INSTANCE.applyAsLong(b)); - - while (b.readSkip(1).readRemaining() > 0) { - long expected = VanillaBytesStoreHash.INSTANCE.applyAsLong(b); - long actual = OptimisedBytesStoreHash.INSTANCE.applyAsLong(b); - assertEquals("Rem: " + b.readRemaining(), expected, actual); - } - assertEquals(VanillaBytesStoreHash.INSTANCE.applyAsLong(b), - OptimisedBytesStoreHash.INSTANCE.applyAsLong(b)); - b.releaseLast(); - } - - @Test - public void sizeMatch() { - @NotNull NativeBytesStore nb = NativeBytesStore.nativeStore(64); - for (int i = 1; i <= 64; i++) - nb.writeUnsignedByte(i - 1, i); -/* - assertEquals(0L, applyAsLong1to7(nb, 0)); - for (int i = 1; i <= 7; i++) - assertEquals(applyAsLong1to7(nb, i), applyAsLong9to16(nb, i)); - assertEquals(applyAsLong8(nb), applyAsLong1to7(nb, 8)); - assertEquals(applyAsLong8(nb), applyAsLong9to16(nb, 8)); -*/ - for (int i = 1; i <= 16; i++) - assertEquals("i: " + i, applyAsLong9to16(nb, i), applyAsLongAny(nb, i)); - for (int i = 17; i <= 32; i++) - assertEquals("i: " + i, applyAsLong17to32(nb, i), applyAsLongAny(nb, i)); - nb.releaseLast(); - } - - //@Test - //@Ignore("Long running, har mean score = 5436") - public void testRandomness() { - @NotNull SecureRandom rand = new SecureRandom(); - - long time = 0, timeCount = 0; - double scoreSum = 0; - int runs = 500; - for (int t = 0; t < runs; t++) { - @NotNull long[] hashs = new long[8192]; - @NotNull byte[] init = new byte[hashs.length / 8]; - Bytes b = Bytes.allocateDirect(init.length); - rand.nextBytes(init); - for (int i = 0; i < hashs.length; i++) { - b.clear(); - b.write(init); - - long prev = b.readLong(i >> 6 << 3); - b.writeLong(i >> 6 << 3, prev ^ (1L << i)); - - b.readLimit(init.length); - long start = System.nanoTime(); - hashs[i] = VanillaBytesStoreHash.INSTANCE.applyAsLong(b); - - time += System.nanoTime() - start; - timeCount++; - } - long score = 0; - for (int i = 0; i < hashs.length - 1; i++) - for (int j = i + 1; j < hashs.length; j++) { - long diff = hashs[j] ^ hashs[i]; - int diffBC = Long.bitCount(diff); - if (diffBC <= 17) { - long d = 1L << (17 - diffBC); - score += d; - } - } - scoreSum += 1.0 / score; -// if (t % 50 == 0) -// System.out.println(t + " - Score: " + score); - } - System.out.println("Average score: " + (long) (runs / scoreSum)); - System.out.printf("Average time %.3f us%n", time / timeCount / 1e3); - } - - //@Test - //@Ignore("Long running, avg score = 5414, avg time 0.043 us") - public void testSmallRandomness() throws IOException { - long time = 0, timeCount = 0; - long scoreSum = 0; -// StringBuilder sb = new StringBuilder(); - - for (int t = 0; t < 500; t++) { - @NotNull long[] hashs = new long[8192]; - @NotNull NativeBytes b = Bytes.allocateElasticDirect(8); - for (int i = 0; i < hashs.length; i++) { - b.clear(); - b.append(t); - b.append('-'); - b.append(i); - long start = System.nanoTime(); - hashs[i] = OptimisedBytesStoreHash.INSTANCE.applyAsLong(b); - time += System.nanoTime() - start; - timeCount++; - -/* if (true) { - sb.setLength(0); - sb.append(b); - assertEquals(hashs[i], Maths.longHash(sb)); - }*/ - } - long score = 0; - for (int i = 0; i < hashs.length - 1; i++) - for (int j = i + 1; j < hashs.length; j++) { - long diff = hashs[j] ^ hashs[i]; - int diffBC = Long.bitCount(diff); - if (diffBC < 18) { - long d = 1L << (17 - diffBC); - score += d; - } - } - scoreSum += score; - if (t % 50 == 0) - System.out.println(t + " - Score: " + score); - } - System.out.println("Average score: " + scoreSum / 500); - System.out.printf("Average time %.3f us%n", time / timeCount / 1e3); - } - - //@Test - //@Ignore("Only run for comparison, avg score = 6843") - public void testSecureRandomness() { - long scoreSum = 0; - for (int t = 0; t < 500; t++) { - @NotNull Random rand = new SecureRandom(); - @NotNull long[] hashs = new long[8192]; - for (int i = 0; i < hashs.length; i++) { - hashs[i] = rand.nextLong(); - } - int score = 0; - for (int i = 0; i < hashs.length - 1; i++) - for (int j = i + 1; j < hashs.length; j++) { - long diff = hashs[j] ^ hashs[i]; - int diffBC = Long.bitCount(diff); - if (diffBC < 18) { - int d = 1 << (17 - diffBC); - score += d; - } - } - scoreSum += score; - if (t % 50 == 0) - System.out.println(t + " - Score: " + score); - } - System.out.println("Average score: " + scoreSum / 500); - } - - @Test - public void testReadIncompleteLong() { - Bytes bs = Bytes.allocateDirect(8); - for (int i = 1; i <= 8; i++) - bs.writeUnsignedByte(i); - @NotNull Bytes bs2 = Bytes.allocateDirect(9).unchecked(true); - - for (int i = 0; i <= 8; i++) { - assertEquals("i: " + i, Long.toHexString(bs2.readLong(0)), - Long.toHexString(OptimisedBytesStoreHash.readIncompleteLong(bs.addressForRead(0), i))); - bs2.writeUnsignedByte(i + 1); - } - bs.releaseLast(); - bs2.releaseLast(); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryMessager.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryMessager.java deleted file mode 100644 index 67d1c4e..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryMessager.java +++ /dev/null @@ -1,85 +0,0 @@ -package net.openhft.chronicle.bytes.jitter; - -import net.openhft.chronicle.bytes.MappedBytes; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.UnsafeMemory; - -public class MemoryMessager { - - public static final int NOT_READY = Integer.MIN_VALUE; - public static final int HEADER_LENGTH = 20; - private final MappedBytes bytes; - private final int padTo; - private final int padMask; - private long address; - private long firstLong; - - public MemoryMessager(MappedBytes bytes, int padTo) { - this.bytes = bytes; - address = bytes.addressForRead(bytes.readPosition()); - this.padTo = padTo; - this.padMask = padTo - 1; - } - - /** - * Writes length bytes. First writes a 4 byte header then a 8 byte index (count) - * and then the remaining number of bytes so that total message is of required length - */ - public void writeMessage(int length, long count, long firstLong) { - long pos = bytes.writePosition(); - boolean works = bytes.compareAndSwapInt(pos, 0x0, NOT_READY); - - if (!works) throw new AssertionError(); - Jvm.safepoint(); - bytes.writeSkip(4); - bytes.writeLong(count); - bytes.writeLong(firstLong); - if (padTo != 0) { - int masked = length & padMask; - if (masked != 0) - length += (padTo - masked); - } - length -= HEADER_LENGTH; - int i = 0; - Jvm.safepoint(); - for (; i < length - 7; i += 8) - bytes.writeLong(i); - for (; i < length; i++) - bytes.writeByte((byte) 0); - Jvm.safepoint(); - boolean works2 = bytes.compareAndSwapInt(pos, NOT_READY, (int) (bytes.writePosition() - pos)); - if (!works2) throw new AssertionError(); - } - - @SuppressWarnings("restriction") - public int length() { - UnsafeMemory.unsafeLoadFence(); - int length = UnsafeMemory.unsafeGetInt(address); - return length; - } - - public long consumeBytes() { - int length = length(); - if (length == 0x0 || length == NOT_READY) - throw new AssertionError("length: " + length); - - Jvm.safepoint(); - bytes.readSkip(4); - long ret = bytes.readLong(); - this.firstLong = bytes.readLong(); - length -= HEADER_LENGTH; - int i = 0; - Jvm.safepoint(); - for (; i < length - 7; i += 8) - bytes.readLong(); - for (; i < length; i++) - bytes.readByte(); - Jvm.safepoint(); - address = bytes.addressForRead(bytes.readPosition(), 4); - return ret; - } - - public long firstLong() { - return firstLong; - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryReadJitterMain.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryReadJitterMain.java deleted file mode 100644 index 8a471ed..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryReadJitterMain.java +++ /dev/null @@ -1,126 +0,0 @@ -package net.openhft.chronicle.bytes.jitter; - -import net.openhft.chronicle.bytes.MappedBytes; -import net.openhft.chronicle.bytes.MappedFile; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.util.Histogram; -import net.openhft.chronicle.core.util.Time; - -import java.io.File; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicLong; - -public class MemoryReadJitterMain { - public static final String PROFILE_OF_THE_THREAD = "profile of the thread"; - - static int runTime = Integer.getInteger("runTime", 600); // seconds - static int size = Integer.getInteger("size", 128); // bytes - static int padTo = Integer.getInteger("pad", 0); // bytes - static int sampleTime = Integer.getInteger("sampleTime", 2); // micro-seconds - static int throughput = Integer.getInteger("throughput", 20_000); // per second - static volatile boolean running = true; - - static { - System.setProperty("jvm.safepoint.enabled", "true"); - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info"); - } - - public static void main(String[] args) throws IOException { - MappedFile.warmup(); - - String path = "test-mem-" + Time.uniqueId(); - System.out.println("Writing to " + path); - AtomicLong lastRead = new AtomicLong(); - File file = new File(path); - file.deleteOnExit(); - - final Histogram histoRead = new Histogram(); - final Histogram histoWrite = new Histogram(); - final Histogram histoReadWrite = new Histogram(); - - Thread reader = new Thread(() -> { - try { - MappedBytes mf = MappedBytes.mappedBytes(file, 64 << 10); - mf.readLimit(mf.writeLimit()); - MemoryMessager mm = new MemoryMessager(mf, padTo); - boolean found = false; - while (running) { - if (found) - Jvm.safepoint(); - else - Jvm.safepoint(); - int length = mm.length(); - if (length == 0x0 || length == MemoryMessager.NOT_READY) { - found = false; - Jvm.safepoint(); - length = mm.length(); - if (length == 0x0 || length == MemoryMessager.NOT_READY) { - Jvm.safepoint(); - continue; - } - } - long startTimeNs = System.nanoTime(); - Jvm.safepoint(); - long last = mm.consumeBytes(); - if (found) - Jvm.safepoint(); - else - Jvm.safepoint(); - long now = System.nanoTime(); - histoRead.sampleNanos(now - startTimeNs); - histoReadWrite.sampleNanos(now - mm.firstLong()); - lastRead.lazySet(last); - if (found) - Jvm.safepoint(); - else - Jvm.safepoint(); - found = true; - } - mf.releaseLast(); - } catch (Throwable t) { - t.printStackTrace(); - } - }); - reader.setDaemon(true); - reader.start(); - Jvm.pause(100); // give it time to start - - long count = 0; - MappedBytes mf = MappedBytes.mappedBytes(file, 64 << 10); - MemoryMessager mm = new MemoryMessager(mf, padTo); - long start0 = System.currentTimeMillis(); - int sampleNS = sampleTime * 1000; - int intervalNS = (int) (1e9 / throughput); - int subSampler = 0; - do { - long startTimeNs = System.nanoTime(); - mm.writeMessage(size, ++count, startTimeNs); - histoWrite.sampleNanos(System.nanoTime() - startTimeNs); - long start1 = System.nanoTime(); - while (System.nanoTime() < start1 + sampleNS) { - // wait one micro-second. - } - if (lastRead.get() != count) { - StackTraceElement[] stes = reader.getStackTrace(); - if (lastRead.get() != count || ++subSampler > 100) { // 1% of race condition samples arbitrarily chosen. - StringBuilder sb = new StringBuilder(); - sb.append(PROFILE_OF_THE_THREAD); - Jvm.trimStackTrace(sb, stes); - System.out.println(sb); - subSampler = 0; - } - } - while (System.nanoTime() < start1 + intervalNS) { - Thread.yield(); - } - } while (System.currentTimeMillis() < start0 + runTime * 1_000); - running = false; - mf.releaseLast(); - System.gc();// give it time to release the file so the delete on exit will work on windows. - - System.out.println("size=" + size + " padTo=" + padTo); - System.out.println("histoRead =" + histoRead.toMicrosFormat()); - System.out.println("histoWrite =" + histoWrite.toMicrosFormat()); - System.out.println("histoReadWrite=" + histoReadWrite.toMicrosFormat()); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryWriteJitterMain.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryWriteJitterMain.java deleted file mode 100644 index 11c4674..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/jitter/MemoryWriteJitterMain.java +++ /dev/null @@ -1,113 +0,0 @@ -package net.openhft.chronicle.bytes.jitter; - -import net.openhft.chronicle.bytes.MappedBytes; -import net.openhft.chronicle.bytes.MappedFile; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.util.Histogram; -import net.openhft.chronicle.core.util.Time; - -import java.io.File; -import java.io.IOException; - -public class MemoryWriteJitterMain { - public static final String PROFILE_OF_THE_THREAD = "profile of the thread"; - - static int runTime = Integer.getInteger("runTime", 600); // seconds - static int size = Integer.getInteger("size", 128); // bytes - static int padTo = Integer.getInteger("pad", 0); // bytes - static int sampleTime = Integer.getInteger("sampleTime", 2); // micro-seconds - static int throughput = Integer.getInteger("throughput", 20_000); // per second - static volatile boolean running = true; - static volatile boolean writing = false; - static volatile int count = 0; - - static { - System.setProperty("jvm.safepoint.enabled", "true"); - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info"); - } - - public static void main(String[] args) throws IOException { - MappedFile.warmup(); - - String path = "test-mem-" + Time.uniqueId(); - System.out.println("Writing to " + path); - - File file = new File(path); - file.deleteOnExit(); - - final Histogram histoRead = new Histogram(); - final Histogram histoWrite = new Histogram(); - final Histogram histoReadWrite = new Histogram(); - - Thread writer = new Thread(() -> { - try { - MappedBytes mf = MappedBytes.mappedBytes(file, 1 << 20); - MemoryMessager mm = new MemoryMessager(mf, padTo); - int intervalNS = (int) (1e9 / throughput); - while (running) { - writing = true; - Jvm.safepoint(); - long startTimeNs = System.nanoTime(); - mm.writeMessage(size, ++count, startTimeNs); - long now = System.nanoTime(); - Jvm.safepoint(); - histoWrite.sampleNanos(now - startTimeNs); - writing = false; - long start = System.nanoTime(); - Thread.yield(); - //noinspection StatementWithEmptyBody - while (System.nanoTime() < start + intervalNS) ; - } - mf.releaseLast(); - } catch (Throwable t) { - t.printStackTrace(); - System.exit(-1); - } - }); - writer.setDaemon(true); - writer.start(); - - MappedBytes mf = MappedBytes.mappedBytes(file, 1 << 20); - mf.readLimit(mf.writeLimit()); - MemoryMessager mm = new MemoryMessager(mf, padTo); - - long start0 = System.currentTimeMillis(); - int sampleNS = sampleTime * 1000; - do { - if (writing) { - long start1 = System.nanoTime(); - while (System.nanoTime() < start1 + sampleNS) { - // wait one micro-second. - } - if (writing) { - StackTraceElement[] stes = writer.getStackTrace(); - if (writing) { - StringBuilder sb = new StringBuilder(); - sb.append(PROFILE_OF_THE_THREAD); - Jvm.trimStackTrace(sb, stes); - if (sb.indexOf("MemoryWriteJitterMain.java:58") < 0 - && sb.indexOf("MemoryWriteJitterMain.java:59") < 0 - && sb.indexOf("MemoryWriteJitterMain.java:60") < 0) - System.out.println(sb); - } - } - } - int length = mm.length(); - if (length > 0x0) { - long startTimeNs = System.nanoTime(); - mm.consumeBytes(); - long now = System.nanoTime(); - histoRead.sampleNanos(now - startTimeNs); - histoReadWrite.sampleNanos(now - mm.firstLong()); - } - } while (System.currentTimeMillis() < start0 + runTime * 1_000); - running = false; - mf.releaseLast(); - System.gc();// give it time to release the file so the delete on exit will work on windows. - - System.out.println("size=" + size + " padTo=" + padTo); - System.out.println("histoRead =" + histoRead.toMicrosFormat()); - System.out.println("histoWrite =" + histoWrite.toMicrosFormat()); - System.out.println("histoReadWrite=" + histoReadWrite.toMicrosFormat()); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/CASTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/CASTest.java deleted file mode 100644 index be3327d..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/CASTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package net.openhft.chronicle.bytes.readme; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.HexDumpBytes; -import net.openhft.chronicle.bytes.NativeBytes; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeFalse; - -public class CASTest extends BytesTestCommon { - @Test - public void testCAS() { - assumeFalse(NativeBytes.areNewGuarded()); - - final HexDumpBytes bytes = new HexDumpBytes() - .offsetFormat((o, b) -> b.appendBase16(o, 4)); - try { - - bytes.comment("s32").writeUtf8("s32"); - bytes.alignBy(4); - long s32 = bytes.writePosition(); - bytes.writeInt(0); - - bytes.comment("s64").writeUtf8("s64"); - bytes.alignBy(8); - long s64 = bytes.writePosition(); - bytes.writeLong(0); - - final String expected1 = "0000 03 73 33 32 00 00 00 00 # s32\n" + - "0008 03 73 36 34 00 00 00 00 00 00 00 00 00 00 00 00 # s64\n"; - - final String actual1 = bytes.toHexString(); - - assertEquals(expected1, actual1); - - //System.out.println(bytes.toHexString()); - - assertTrue(bytes.compareAndSwapInt(s32, 0, Integer.MAX_VALUE)); - assertTrue(bytes.compareAndSwapLong(s64, 0, Long.MAX_VALUE)); - - // System.out.println(bytes.toHexString()); - - final String expected2 = "0000 03 73 33 32 ff ff ff 7f # s32\n" + - "0008 03 73 36 34 00 00 00 00 ff ff ff ff ff ff ff 7f # s64\n"; - final String actual2 = bytes.toHexString(); - - assertEquals(expected2, actual2); - - } finally { - bytes.releaseLast(); - } - - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/PrimitiveTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/PrimitiveTest.java deleted file mode 100644 index e7565a1..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/PrimitiveTest.java +++ /dev/null @@ -1,310 +0,0 @@ -package net.openhft.chronicle.bytes.readme; - -import net.openhft.chronicle.bytes.*; -import org.junit.Test; - -import java.nio.ByteBuffer; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assume.assumeFalse; - -public class PrimitiveTest extends BytesTestCommon { - - @Test - public void testBinaryNestedDTO() { - final Outer outer = new Outer("name", new Inner("key1", 1.1), new Inner("key2", 2.2)); - - final HexDumpBytes bytes = new HexDumpBytes(); - try { - bytes.comment("outer"); - outer.writeMarshallable(bytes); - - final String expected = - " # outer\n" + - " 04 6e 61 6d 65 # name\n" + - " # innerA\n" + - " 04 6b 65 79 31 # key\n" + - " 9a 99 99 99 99 99 f1 3f # value\n" + - " # innerB\n" + - " 04 6b 65 79 32 # key\n" + - " 9a 99 99 99 99 99 01 40 # value\n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - final Outer outer2 = new Outer(); - outer2.readMarshallable(bytes); - - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testBinaryPrimitiveDTO() { - final PrimitiveDTO dto = new PrimitiveDTO(true, - (byte) 0x11, - (short) 0x2222, - '5', - 0x12345678, - 0x123456789ABCDEFL, - 1.2345f, - Math.PI); - - final HexDumpBytes bytes = new HexDumpBytes(); - try { - bytes.comment("dto"); - dto.writeMarshallable(bytes); - - final String expected = " # dto\n" + - " 59 # flag\n" + - " 11 # s8\n" + - " 22 22 # s16\n" + - " 35 # ch\n" + - " 78 56 34 12 # s32\n" + - " ef cd ab 89 67 45 23 01 # s64\n" + - " 19 04 9e 3f # f32\n" + - " 18 2d 44 54 fb 21 09 40 # f64\n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - PrimitiveDTO dto2 = new PrimitiveDTO(); - dto2.readMarshallable(bytes); - - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testBinaryPrimitive() { - final HexDumpBytes bytes = new HexDumpBytes(); - try { - bytes.comment("flag").writeBoolean(true); - bytes.comment("s8").writeByte((byte) 1); - bytes.comment("u8").writeUnsignedByte(2); - bytes.comment("s16").writeShort((short) 3); - bytes.comment("u16").writeUnsignedShort(4); - bytes.comment("ch").writeStopBit('5'); - bytes.comment("s24").writeInt24(-6_666_666); - bytes.comment("u24").writeUnsignedInt24(16_666_666); - bytes.comment("s32").writeInt(6); - bytes.comment("u32").writeUnsignedInt(7); - bytes.comment("s64").writeLong(8); - bytes.comment("f32").writeFloat(9); - bytes.comment("f64").writeDouble(10); - - final String expected = - "59 # flag\n" + - "01 # s8\n" + - "02 # u8\n" + - "03 00 # s16\n" + - "04 00 # u16\n" + - "35 # ch\n" + - "56 46 9a # s24\n" + - "2a 50 fe # u24\n" + - "06 00 00 00 # s32\n" + - "07 00 00 00 # u32\n" + - "08 00 00 00 00 00 00 00 # s64\n" + - "00 00 10 41 # f32\n" + - "00 00 00 00 00 00 24 40 # f64\n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - // System.out.println(bytes.toHexString()); - - final boolean flag = bytes.readBoolean(); - final byte s8 = bytes.readByte(); - final int u8 = bytes.readUnsignedByte(); - final short s16 = bytes.readShort(); - final int u16 = bytes.readUnsignedShort(); - final char ch = bytes.readStopBitChar(); - final int s24 = bytes.readInt24(); - final long u24 = bytes.readUnsignedInt24(); - final int s32 = bytes.readInt(); - final long u32 = bytes.readUnsignedInt(); - final long s64 = bytes.readLong(); - final float f32 = bytes.readFloat(); - final double f64 = bytes.readDouble(); - - assertTrue(flag); - assertEquals(1, s8); - assertEquals(2, u8); - assertEquals(3, s16); - assertEquals(4, u16); - assertEquals('5', ch); - assertEquals(-6_666_666, s24); - assertEquals(16_666_666, u24); - assertEquals(6, s32); - assertEquals(7, u32); - assertEquals(8, s64); - assertEquals(9, f32, 0.0); - assertEquals(10, f64, 0.0); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testBinaryPrimitiveOffset() { - final Bytes bytes = Bytes.elasticHeapByteBuffer(64); - try { - - bytes.writeBoolean(0, true); - bytes.writeByte(1, (byte) 1); - bytes.writeUnsignedByte(2, 2); - bytes.writeShort(3, (short) 3); - bytes.writeUnsignedShort(5, 4); - bytes.writeInt(7, 6); - bytes.writeUnsignedInt(11, 7); - bytes.writeLong(15, 8); - bytes.writeFloat(23, 9); - bytes.writeDouble(27, 10); - bytes.writePosition(35); - - final String expected = - "00000000 59 01 02 03 00 04 00 06 00 00 00 07 00 00 00 08 Y······· ········\n" + - "00000010 00 00 00 00 00 00 00 00 00 10 41 00 00 00 00 00 ········ ··A·····\n" + - "00000020 00 24 40 ·$@ \n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - boolean flag = bytes.readBoolean(0); - byte s8 = bytes.readByte(1); - int u8 = bytes.readUnsignedByte(2); - short s16 = bytes.readShort(3); - int u16 = bytes.readUnsignedShort(5); - int s32 = bytes.readInt(7); - long u32 = bytes.readUnsignedInt(11); - long s64 = bytes.readLong(15); - float f32 = bytes.readFloat(23); - double f64 = bytes.readDouble(27); - - assertTrue(flag); - assertEquals(1, s8); - assertEquals(2, u8); - assertEquals(3, s16); - assertEquals(4, u16); - assertEquals(6, s32); - assertEquals(7, u32); - assertEquals(8, s64); - assertEquals(9, f32, 0.0); - assertEquals(10, f64, 0.0); - } finally { - bytes.releaseLast(); - } - } - - @Test - public void testTextPrimitive() { - assumeFalse(NativeBytes.areNewGuarded()); - final Bytes bytes = Bytes.elasticHeapByteBuffer(64); - try { - bytes.append(true).append('\n'); - bytes.append(1).append('\n'); - bytes.append(2L).append('\n'); - bytes.append('3').append('\n'); - bytes.append(4.1f).append('\n'); - bytes.append(5.2).append('\n'); - bytes.append(6.2999999, 3).append('\n'); - - final String expected = "00000000 54 0a 31 0a 32 0a 33 0a 34 2e 31 0a 35 2e 32 0a T·1·2·3· 4.1·5.2·\n" + - "00000010 36 2e 33 30 30 0a 6.300· \n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - final boolean flag = bytes.parseBoolean(); - final int s32 = bytes.parseInt(); - final long s64 = bytes.parseLong(); - final String ch = bytes.parseUtf8(StopCharTesters.SPACE_STOP); - final float f32 = bytes.parseFloat(); - final double f64 = bytes.parseDouble(); - final double f64b = bytes.parseDouble(); - - assertTrue(flag); - assertEquals(1, s32); - assertEquals(2, s64); - assertEquals("3", ch); - assertEquals(4.1, f32, 1e-6); - assertEquals(5.2, f64, 0.0); - assertEquals(6.2999999, f64b, 0.5e-4); - } finally { - bytes.releaseLast(); - } - } - - private static final class Outer implements BytesMarshallable { - - private final String name; - private final Inner innerA; - private final Inner innerB; - - public Outer(final String name, - final Inner innerA, - final Inner innerB) { - this.name = name; - this.innerA = innerA; - this.innerB = innerB; - } - - public Outer() { - this(null, new Inner(), new Inner()); - } - } - - private static final class Inner implements BytesMarshallable { - - private String key; - private double value; - - public Inner(String key, double value) { - this.key = key; - this.value = value; - } - - public Inner() { - } - } - - private static final class PrimitiveDTO implements BytesMarshallable { - boolean flag; - byte s8; - short s16; - char ch; - int s32; - long s64; - float f32; - double f64; - - public PrimitiveDTO(final boolean flag, - final byte s8, - final short s16, - final char ch, - final int s32, - final long s64, - final float f32, - final double f64) { - this.flag = flag; - this.s8 = s8; - this.s16 = s16; - this.ch = ch; - this.s32 = s32; - this.s64 = s64; - this.f32 = f32; - this.f64 = f64; - } - - public PrimitiveDTO() { - } - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/StopBitTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/StopBitTest.java deleted file mode 100644 index 66b07cb..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/StopBitTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package net.openhft.chronicle.bytes.readme; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.HexDumpBytes; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class StopBitTest extends BytesTestCommon { - - @Test - public void testString() { - final HexDumpBytes bytes = new HexDumpBytes(); - try { - for (long i : new long[]{ - 0, -1, - 127, -127, - 128, -128, - 1 << 14, 1 << 21, - 1 << 28, 1L << 35, - 1L << 42, 1L << 49, - 1L << 56, Long.MAX_VALUE, - Long.MIN_VALUE}) { - bytes.comment(i + "L").writeStopBit(i); - } - - for (double d : new double[]{ - 0.0, - -0.0, - 1.0, - 1.0625, - -128, - -Double.MIN_NORMAL, - Double.NEGATIVE_INFINITY, - Double.NaN, - Double.POSITIVE_INFINITY}) { - bytes.comment(d + "").writeStopBit(d); - } - - final String actual = bytes.toHexString(); - - final String expected = - "00 # 0L\n" + - "80 00 # -1L\n" + - "7f # 127L\n" + - "fe 00 # -127L\n" + - "80 01 # 128L\n" + - "ff 00 # -128L\n" + - "80 80 01 # 16384L\n" + - "80 80 80 01 # 2097152L\n" + - "80 80 80 80 01 # 268435456L\n" + - "80 80 80 80 80 01 # 34359738368L\n" + - "80 80 80 80 80 80 01 # 4398046511104L\n" + - "80 80 80 80 80 80 80 01 # 562949953421312L\n" + - "80 80 80 80 80 80 80 80 01 # 72057594037927936L\n" + - "ff ff ff ff ff ff ff ff 7f # 9223372036854775807L\n" + - "ff ff ff ff ff ff ff ff ff 00 # -9223372036854775808L\n" + - "00 # 0.0\n" + - "40 # -0.0\n" + - "9f 7c # 1.0\n" + - "9f fc 20 # 1.0625\n" + - "e0 18 # -128.0\n" + - "c0 04 # -2.2250738585072014E-308\n" + - "ff 7c # -Infinity\n" + - "bf 7e # NaN\n" + - "bf 7c # Infinity\n"; - - assertEquals(expected, actual); - - // System.out.println(actual); - - } finally { - bytes.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/StringsTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/StringsTest.java deleted file mode 100644 index 270ff0f..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/readme/StringsTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package net.openhft.chronicle.bytes.readme; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.HexDumpBytes; -import net.openhft.chronicle.bytes.NativeBytes; -import net.openhft.chronicle.bytes.StopCharTesters; -import org.junit.Test; - -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -public class StringsTest extends BytesTestCommon { - - @Test - public void testString() { - assumeFalse(NativeBytes.areNewGuarded()); - - final HexDumpBytes bytes = new HexDumpBytes(); - try { - bytes.comment("write8bit").write8bit("£ 1"); - bytes.comment("writeUtf8").writeUtf8("£ 1"); - bytes.comment("append8bit").append8bit("£ 1").append('\n'); - bytes.comment("appendUtf8").appendUtf8("£ 1").append('\n'); - - // System.out.println(bytes.toHexString()); - - final String a = bytes.read8bit(); - final String b = bytes.readUtf8(); - final String c = bytes.parse8bit(StopCharTesters.CONTROL_STOP); - final String d = bytes.parseUtf8(StopCharTesters.CONTROL_STOP); - assertEquals("£ 1", a); - assertEquals("£ 1", b); - assertEquals("£ 1", c); - assertEquals("£ 1", d); - - // System.out.println(System.identityHashCode(a)); - // System.out.println(System.identityHashCode(b)); - // System.out.println(System.identityHashCode(c)); - // System.out.println(System.identityHashCode(d)); - - // uses the pool but a different hash. - // assertSame(a, c); // uses a string pool - assertSame(b, c); // uses a string pool - assertSame(b, d); // uses a string pool - } finally { - bytes.releaseLast(); - } - - } - - @Test - public void testNull() { - final HexDumpBytes bytes = new HexDumpBytes(); - try { - bytes.comment("write8bit").write8bit((String) null); - bytes.comment("writeUtf8").writeUtf8(null); - - //System.out.println(bytes.toHexString()); - - final String a = bytes.read8bit(); - final String b = bytes.readUtf8(); - assertNull(a); - assertNull(b); - } finally { - bytes.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryIntArrayReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryIntArrayReferenceTest.java deleted file mode 100644 index 7c20502..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryIntArrayReferenceTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesMarshallable; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytes; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.assumeFalse; - -public class BinaryIntArrayReferenceTest extends BytesTestCommon { - @Test - public void getSetValues() { - final int length = 128 * 4 + 2 * 8; - final Bytes bytes = Bytes.allocateDirect(length); - try { - BinaryIntArrayReference.write(bytes, 128); - - try (BinaryIntArrayReference array = new BinaryIntArrayReference()) { - array.bytesStore(bytes, 0, length); - - assertEquals(128, array.getCapacity()); - for (int i = 0; i < 128; i++) - array.setValueAt(i, i + 1); - - for (int i = 0; i < 128; i++) - assertEquals(i + 1, array.getValueAt(i)); - } - } finally { - bytes.releaseLast(); - } - } - - @Test - public void marshallable() { - assumeFalse(NativeBytes.areNewGuarded()); - final Bytes bytes = Bytes.allocateElasticDirect(256); - try { - final IntArrays la = new IntArrays(4, 8); - la.writeMarshallable(bytes); - - final String expected = - "00000000 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000020 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - //System.out.println(bytes.toHexString()); - - final IntArrays la2 = new IntArrays(0, 0); - la2.readMarshallable(bytes); - assertEquals(4, la2.first.getCapacity()); - assertEquals(8, la2.second.getCapacity()); - la.closeAll(); - la2.closeAll(); - } finally { - bytes.releaseLast(); - } - } - - private static final class IntArrays implements BytesMarshallable { - BinaryIntArrayReference first = new BinaryIntArrayReference(); - BinaryIntArrayReference second = new BinaryIntArrayReference(); - - public IntArrays(int firstLength, int secondLength) { - first.capacity(firstLength); - second.capacity(secondLength); - } - - public void closeAll() { - first.close(); - second.close(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryIntReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryIntReferenceTest.java deleted file mode 100644 index 46174a0..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryIntReferenceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class BinaryIntReferenceTest extends BytesTestCommon { - @Test - public void test() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(32); - try (@NotNull BinaryIntReference ref = new BinaryIntReference()) { - ref.bytesStore(nbs, 16, 4); - assertEquals(0, ref.getValue()); - ref.addAtomicValue(1); - assertEquals(1, ref.getVolatileValue()); - ref.addValue(-2); - assertEquals("value: -1", ref.toString()); - assertFalse(ref.compareAndSwapValue(0, 1)); - assertTrue(ref.compareAndSwapValue(-1, 2)); - assertEquals(4, ref.maxSize()); - assertEquals(16, ref.offset()); - assertEquals(nbs, ref.bytesStore()); - assertEquals(0L, nbs.readLong(0)); - assertEquals(0L, nbs.readLong(8)); - assertEquals(2, nbs.readInt(16)); - assertEquals(0L, nbs.readLong(20)); - - ref.setValue(10); - assertEquals(10L, nbs.readInt(16)); - ref.setOrderedValue(20); - Thread.yield(); - assertEquals(20L, nbs.readVolatileInt(16)); - } - nbs.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryLongArrayReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryLongArrayReferenceTest.java deleted file mode 100644 index f235099..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryLongArrayReferenceTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesMarshallable; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytes; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.assumeFalse; - -public class BinaryLongArrayReferenceTest extends BytesTestCommon { - @Test - public void getSetValues() { - final int length = 128 * 8 + 2 * 8; - final Bytes bytes = Bytes.allocateDirect(length); - try { - BinaryLongArrayReference.write(bytes, 128); - - try (BinaryLongArrayReference array = new BinaryLongArrayReference()) { - array.bytesStore(bytes, 0, length); - - assertEquals(128, array.getCapacity()); - for (int i = 0; i < 128; i++) - array.setValueAt(i, i + 1); - - for (int i = 0; i < 128; i++) - assertEquals(i + 1, array.getValueAt(i)); - } - } finally { - bytes.releaseLast(); - } - } - - @Test - public void marshallable() { - assumeFalse(NativeBytes.areNewGuarded()); - final Bytes bytes = Bytes.allocateElasticDirect(256); - try { - final LongArrays la = new LongArrays(4, 8); - la.writeMarshallable(bytes); - - final String expected = - "00000000 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "........\n" + - "00000030 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n" + - "........\n" + - "00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ········ ········\n"; - - final String actual = bytes.toHexString(); - - assertEquals(expected, actual); - - //System.out.println(bytes.toHexString()); - - final LongArrays la2 = new LongArrays(0, 0); - la2.readMarshallable(bytes); - assertEquals(4, la2.first.getCapacity()); - assertEquals(8, la2.second.getCapacity()); - la.closeAll(); - la2.closeAll(); - } finally { - bytes.releaseLast(); - } - } - - private static final class LongArrays implements BytesMarshallable { - BinaryLongArrayReference first = new BinaryLongArrayReference(); - BinaryLongArrayReference second = new BinaryLongArrayReference(); - - public LongArrays(int firstLength, int secondLength) { - first.capacity(firstLength); - second.capacity(secondLength); - } - - public void closeAll() { - first.close(); - second.close(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryLongReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryLongReferenceTest.java deleted file mode 100644 index 67dc155..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryLongReferenceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class BinaryLongReferenceTest extends BytesTestCommon { - @Test - public void test() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(32); - try (@NotNull BinaryLongReference ref = new BinaryLongReference()) { - ref.bytesStore(nbs, 16, 8); - assertEquals(0, ref.getValue()); - ref.addAtomicValue(1); - assertEquals(1, ref.getVolatileValue()); - ref.addValue(-2); - assertEquals("value: -1", ref.toString()); - assertFalse(ref.compareAndSwapValue(0, 1)); - assertTrue(ref.compareAndSwapValue(-1, 2)); - assertEquals(8, ref.maxSize()); - assertEquals(16, ref.offset()); - assertEquals(nbs, ref.bytesStore()); - assertEquals(0L, nbs.readLong(0)); - assertEquals(0L, nbs.readLong(8)); - assertEquals(2L, nbs.readLong(16)); - assertEquals(0L, nbs.readLong(24)); - - ref.setValue(10); - assertEquals(10L, nbs.readLong(16)); - ref.setOrderedValue(20); - Thread.yield(); - assertEquals(20L, nbs.readVolatileLong(16)); - } - nbs.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryTwoLongReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryTwoLongReferenceTest.java deleted file mode 100644 index c68c0a1..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BinaryTwoLongReferenceTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class BinaryTwoLongReferenceTest extends BytesTestCommon { - @Test - public void test() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(32); - try (@NotNull BinaryTwoLongReference ref = new BinaryTwoLongReference()) { - ref.bytesStore(nbs, 16, 16); - assertEquals(0, ref.getValue()); - assertEquals(0, ref.getValue2()); - ref.addAtomicValue(1); - assertEquals(1, ref.getVolatileValue()); - assertEquals(0, ref.getVolatileValue2()); - ref.addAtomicValue2(-1); - assertEquals(1, ref.getVolatileValue()); - assertEquals(-1, ref.getVolatileValue2()); - - ref.addValue(-2); - assertEquals("value: -1, value2: -1", ref.toString()); - assertFalse(ref.compareAndSwapValue(0, 1)); - assertTrue(ref.compareAndSwapValue(-1, 2)); - assertEquals(16, ref.maxSize()); - assertEquals(16, ref.offset()); - assertEquals(nbs, ref.bytesStore()); - assertEquals(0L, nbs.readLong(0)); - assertEquals(0L, nbs.readLong(8)); - assertEquals(2L, nbs.readLong(16)); - assertEquals(-1L, nbs.readLong(24)); - - ref.setValue(10); - assertEquals(10L, nbs.readLong(16)); - ref.setOrderedValue(20); - Thread.yield(); - assertEquals(20L, nbs.readVolatileLong(16)); - } - nbs.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BooleanReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BooleanReferenceTest.java deleted file mode 100644 index bcb30ee..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/BooleanReferenceTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import java.nio.charset.StandardCharsets; - -import static org.junit.Assert.*; - -public class BooleanReferenceTest extends BytesTestCommon { - @Test - public void testBinary() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(1); - try (@NotNull BinaryBooleanReference ref = new BinaryBooleanReference()) { - byte i8 = (byte) 0xB0; - nbs.writeByte(0, i8); - - ref.bytesStore(nbs, 0, 1); - - assertFalse(ref.getValue()); - ref.setValue(true); - - assertTrue(ref.getValue()); - assertEquals(1, ref.maxSize()); - - } - nbs.releaseLast(); - } - - @Test - public void testText() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(5); - try (@NotNull TextBooleanReference ref = new TextBooleanReference()) { - - nbs.write(0, "false".getBytes(StandardCharsets.ISO_8859_1)); - - ref.bytesStore(nbs, 0, 5); - - assertFalse(ref.getValue()); - ref.setValue(true); - - assertTrue(ref.getValue()); - assertEquals(5, ref.maxSize()); - - } - nbs.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/ByteableReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/ByteableReferenceTest.java deleted file mode 100644 index 8ce6675..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/ByteableReferenceTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Byteable; -import net.openhft.chronicle.bytes.NativeBytesStore; -import net.openhft.chronicle.core.io.AbstractCloseable; -import net.openhft.chronicle.core.io.AbstractReferenceCounted; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertEquals; - -@SuppressWarnings({"rawtypes", "unchecked"}) -@RunWith(Parameterized.class) -public class ByteableReferenceTest { - - private final AbstractReference byteable; - - public ByteableReferenceTest(final String className, final AbstractReference byteable) { - this.byteable = byteable; - } - - @Parameterized.Parameters(name = "{0}") - public static List testData() { - List objects = Arrays.asList( - datum(new BinaryLongReference()), - datum(new BinaryTwoLongReference()), - datum(new BinaryBooleanReference()), - datum(new BinaryIntReference()), - datum(new TextBooleanReference()), - datum(new TextIntReference()), - datum(new TextLongReference()) - /*, - unhelpful implementations below this point - datum(new TextLongArrayReference()), - datum(new BinaryLongArrayReference()), - datum(new UncheckedLongReference())*/ - ); - AbstractCloseable.disableCloseableTracing(); - AbstractReferenceCounted.disableReferenceTracing(); - return objects; - } - - private static Object[] datum(final Byteable reference) { - return new Object[]{reference.getClass().getSimpleName(), reference}; - } - - @Test - public void shouldMakeReservationOnCurrentStore() { - final NativeBytesStore firstStore = NativeBytesStore.nativeStore(64); - try { - firstStore.writeLong(0, 17); - final NativeBytesStore secondStore = NativeBytesStore.nativeStore(64); - try { - secondStore.writeLong(0, 17); - final long startCount = firstStore.refCount(); - byteable.bytesStore(firstStore, 0, byteable.maxSize()); - - assertEquals(startCount + 1, firstStore.refCount()); - - byteable.bytesStore(secondStore, 0, byteable.maxSize()); - - assertEquals(startCount, firstStore.refCount()); - byteable.close(); - } finally { - secondStore.releaseLast(); - } - } finally { - firstStore.releaseLast(); - } - } - -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextIntArrayReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextIntArrayReferenceTest.java deleted file mode 100644 index 79ac77c..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextIntArrayReferenceTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class TextIntArrayReferenceTest extends BytesTestCommon { - @SuppressWarnings("rawtypes") - @Test - public void getSetValues() { - int length = 5 * 12 + 70; - Bytes bytes = Bytes.allocateDirect(length); - TextIntArrayReference.write(bytes, 5); - - try (@NotNull TextIntArrayReference array = new TextIntArrayReference()) { - array.bytesStore(bytes, 0, length); - - assertEquals(5, array.getCapacity()); - for (int i = 0; i < 5; i++) - array.setValueAt(i, i + 1); - - for (int i = 0; i < 5; i++) - assertEquals(i + 1, array.getValueAt(i)); - - @NotNull final String expected = "{ locked: false, capacity: 5 , used: 0000000000, values: [ 0000000001, 0000000002, 0000000003, 0000000004, 0000000005 ] }\n"; -// System.out.println(expected.length()); - assertEquals(expected, - bytes.toString()); - bytes.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextIntReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextIntReferenceTest.java deleted file mode 100644 index ce78dbd..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextIntReferenceTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import net.openhft.chronicle.bytes.StopCharTesters; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class TextIntReferenceTest extends BytesTestCommon { - @Test - public void test() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(64); - try (@NotNull TextIntReference ref = new TextIntReference()) { - ref.bytesStore(nbs, 16, ref.maxSize()); - assertEquals(0, ref.getValue()); - ref.addAtomicValue(1); - assertEquals(1, ref.getVolatileValue()); - ref.addValue(-2); - assertEquals("value: -1", ref.toString()); - assertFalse(ref.compareAndSwapValue(0, 1)); - assertTrue(ref.compareAndSwapValue(-1, 2)); - assertEquals(46, ref.maxSize()); - assertEquals(16, ref.offset()); - assertEquals(nbs, ref.bytesStore()); - assertEquals(0L, nbs.readLong(0)); - assertEquals(0L, nbs.readLong(8)); - Bytes bytes = nbs.bytesForRead(); - bytes.readPosition(16); - assertEquals("!!atomic { locked: false, value: 0000000002 }", bytes.parseUtf8(StopCharTesters.CONTROL_STOP)); - bytes.releaseLast(); - } - nbs.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextLongArrayReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextLongArrayReferenceTest.java deleted file mode 100644 index a947324..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextLongArrayReferenceTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class TextLongArrayReferenceTest extends BytesTestCommon { - @SuppressWarnings("rawtypes") - @Test - public void getSetValues() { - int length = 5 * 22 + 90; - Bytes bytes = Bytes.allocateDirect(length); - TextLongArrayReference.write(bytes, 5); - - try (@NotNull TextLongArrayReference array = new TextLongArrayReference()) { - array.bytesStore(bytes, 0, length); - - assertEquals(5, array.getCapacity()); - for (int i = 0; i < 5; i++) - array.setValueAt(i, i + 1); - - for (int i = 0; i < 5; i++) - assertEquals(i + 1, array.getValueAt(i)); - - @NotNull final String expected = "{ locked: false, capacity: 5 , used: 00000000000000000000, " + - "values: [ 00000000000000000001, 00000000000000000002, 00000000000000000003, 00000000000000000004, 00000000000000000005 ] }\n"; -// System.out.println(expected.length()); - assertEquals(expected, - bytes.toString()); - bytes.releaseLast(); - } - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextLongReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextLongReferenceTest.java deleted file mode 100644 index 104841a..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/TextLongReferenceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import net.openhft.chronicle.bytes.StopCharTesters; -import org.jetbrains.annotations.NotNull; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class TextLongReferenceTest extends BytesTestCommon { - - @Test - public void testSetValue() { - @NotNull NativeBytesStore bytesStore = NativeBytesStore.nativeStoreWithFixedCapacity(64); - try (@NotNull final TextLongReference value = new TextLongReference()) { - value.bytesStore(bytesStore, 0, value.maxSize()); - int expected = 10; - value.setValue(expected); - - long l = bytesStore.parseLong(TextLongReference.VALUE); - - Assert.assertEquals(expected, value.getValue()); - Assert.assertEquals(expected, l); - - assertFalse(value.compareAndSwapValue(0, 1)); - assertTrue(value.compareAndSwapValue(10, 2)); - assertEquals(56, value.maxSize()); - assertEquals(0, value.offset()); - - Bytes bytes = bytesStore.bytesForRead(); - bytes.readPosition(0); - assertEquals("!!atomic { locked: false, value: 00000000000000000002 }", bytes.parseUtf8(StopCharTesters.CONTROL_STOP)); - bytes.releaseLast(); - } - bytesStore.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/UncheckedLongReferenceTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/UncheckedLongReferenceTest.java deleted file mode 100644 index b306025..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/ref/UncheckedLongReferenceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.ref; - -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytesStore; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class UncheckedLongReferenceTest extends BytesTestCommon { - @Test - public void test() { - @NotNull NativeBytesStore nbs = NativeBytesStore.nativeStoreWithFixedCapacity(32); - try (@NotNull UncheckedLongReference ref = new UncheckedLongReference()) { - ref.bytesStore(nbs, 16, 8); - assertEquals(0, ref.getValue()); - ref.addAtomicValue(1); - assertEquals(1, ref.getVolatileValue()); - ref.addValue(-2); - assertEquals("value: -1", ref.toString()); - assertFalse(ref.compareAndSwapValue(0, 1)); - assertTrue(ref.compareAndSwapValue(-1, 2)); - assertEquals(8, ref.maxSize()); - assertEquals(nbs.addressForRead(16), ref.offset()); - assertEquals(nbs, ref.bytesStore()); - assertEquals(0L, nbs.readLong(0)); - assertEquals(0L, nbs.readLong(8)); - assertEquals(2L, nbs.readLong(16)); - assertEquals(0L, nbs.readLong(24)); - - ref.setValue(10); - assertEquals(10L, nbs.readLong(16)); - ref.setOrderedValue(20); - Thread.yield(); - assertEquals(20L, nbs.readLong(16)); - } - nbs.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/GzipTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/GzipTest.java deleted file mode 100644 index 5b06997..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/GzipTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.bytes.NativeBytes; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.Arrays; -import java.util.Random; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.util.Compressions.GZIP; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeFalse; - -public class GzipTest extends BytesTestCommon { - - private ThreadDump threadDump; - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testCompress() throws IORuntimeException { - @NotNull byte[] bytes = "hello world".getBytes(ISO_8859_1); - byte[] bytes2 = GZIP.uncompress(GZIP.compress(bytes)); - assertArrayEquals(bytes, bytes2); - } - - @SuppressWarnings("rawtypes") - @Test - public void testCompressionRatio() throws IORuntimeException { - assumeFalse(NativeBytes.areNewGuarded()); - @NotNull byte[] bytes = new byte[1 << 20]; - Arrays.fill(bytes, (byte) 'X'); - @NotNull Random rand = new Random(); - for (int i = 0; i < bytes.length; i += 40) - bytes[rand.nextInt(bytes.length)] = '1'; - byte[] compress = GZIP.compress(bytes); -// System.out.println(compress.length); - - Bytes bytes2 = Bytes.wrapForRead(bytes); - @NotNull Bytes bytes3 = Bytes.allocateElasticDirect(); - GZIP.compress(bytes2, bytes3); - @NotNull byte[] bytes4 = bytes3.toByteArray(); - byte[] bytes5 = GZIP.uncompress(bytes4); - - assertNotNull(bytes5); -// assertEquals(Arrays.toString(bytes).replace(", ", "\n"), -// Arrays.toString(bytes5).replace(", ", "\n")); -// assertEquals(Arrays.toString(compress).replace(", ", "\n"), -// Arrays.toString(bytes4).replace(", ", "\n")); - assertEquals(compress.length, bytes4.length); - assertArrayEquals(compress, bytes4); - - @NotNull Bytes bytes6 = Bytes.allocateElasticDirect(); - GZIP.uncompress(bytes3, bytes6); - assertArrayEquals(bytes, bytes6.toByteArray()); -// assertEquals(Arrays.toString(bytes).replace(", ", "\n"), -// Arrays.toString(bytes6.toByteArray()).replace(", ", "\n")); - bytes2.releaseLast(); - bytes3.releaseLast(); - bytes6.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/LZWTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/LZWTest.java deleted file mode 100644 index 432c38e..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/LZWTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import net.openhft.chronicle.core.io.IORuntimeException; -import net.openhft.chronicle.core.threads.ThreadDump; -import org.jetbrains.annotations.NotNull; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.Arrays; -import java.util.Random; - -import static java.nio.charset.StandardCharsets.ISO_8859_1; -import static net.openhft.chronicle.bytes.util.Compressions.LZW; -import static org.junit.Assert.*; - -public class LZWTest extends BytesTestCommon { - - private ThreadDump threadDump; - - @Before - public void threadDump() { - threadDump = new ThreadDump(); - } - - @After - public void checkThreadDump() { - threadDump.assertNoNewThreads(); - } - - @Test - public void testCompress() throws IORuntimeException { - @NotNull byte[] bytes = "hello world".getBytes(ISO_8859_1); - byte[] bytes2 = LZW.uncompress(LZW.compress(bytes)); - assertArrayEquals(bytes, bytes2); - } - - @SuppressWarnings("rawtypes") - @Test - public void testCompressionRatio() throws IORuntimeException { - @NotNull byte[] bytes = new byte[1 << 20]; - Arrays.fill(bytes, (byte) 'X'); - @NotNull Random rand = new Random(); - for (int i = 0; i < bytes.length; i += 40) - bytes[rand.nextInt(bytes.length)] = '1'; - byte[] compress = LZW.compress(bytes); -// System.out.println(compress.length); - - Bytes bytes2 = Bytes.wrapForRead(bytes); - @NotNull Bytes bytes3 = Bytes.allocateElasticDirect(); - LZW.compress(bytes2, bytes3); - @NotNull byte[] bytes4 = bytes3.toByteArray(); - byte[] bytes5 = LZW.uncompress(bytes4); - assertNotNull(bytes5); - -// assertEquals(Arrays.toString(bytes).replace(", ", "\n"), -// Arrays.toString(bytes5).replace(", ", "\n")); -// assertEquals(Arrays.toString(compress).replace(", ", "\n"), -// Arrays.toString(bytes4).replace(", ", "\n")); - assertEquals(compress.length, bytes4.length); - assertArrayEquals(compress, bytes4); - - @NotNull Bytes bytes6 = Bytes.allocateElasticDirect(); - LZW.uncompress(bytes3, bytes6); - assertArrayEquals(bytes, bytes6.toByteArray()); -// assertEquals(Arrays.toString(bytes).replace(", ", "\n"), -// Arrays.toString(bytes6.toByteArray()).replace(", ", "\n")); - bytes2.releaseLast(); - bytes3.releaseLast(); - bytes6.releaseLast(); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/PropertyReplacerTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/PropertyReplacerTest.java deleted file mode 100644 index 038ae44..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/PropertyReplacerTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2016-2020 Chronicle Software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import org.junit.Test; - -import java.util.Properties; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class PropertyReplacerTest { - @Test - public void testSystemPropertyMissing() { - try { - PropertyReplacer.replaceTokensWithProperties("plainText ${missingPropertyToReplace}"); - } catch (IllegalArgumentException e) { - assertEquals("System property is missing: [property=missingPropertyToReplace, " + - "expression=plainText ${missingPropertyToReplace}]", e.getMessage()); - - return; - } - - fail("Exception is expected"); - } - - @Test - public void testPropertyMissing() { - try { - final Properties properties = new Properties(); - properties.setProperty("wrongProperty", "wrongValue"); - - PropertyReplacer.replaceTokensWithProperties("plainText ${missingPropertyToReplace}", properties); - } catch (IllegalArgumentException e) { - assertEquals("Property is missing: [property=missingPropertyToReplace, " + - "expression=plainText ${missingPropertyToReplace}, properties={wrongProperty=wrongValue}]", - e.getMessage()); - - return; - } - - fail("Exception is expected"); - } - - @Test - public void testLeadingAndTrailingSpacesInsideBracketsIgnored() { - final Properties props = new Properties(); - props.setProperty("myFancyProperty", "myFancyValue"); - - String res = PropertyReplacer.replaceTokensWithProperties("plainKey: ${ myFancyProperty }", props); - assertEquals("plainKey: myFancyValue", res); - - res = PropertyReplacer.replaceTokensWithProperties("plainKey: ${myFancyProperty}", props); - assertEquals("plainKey: myFancyValue", res); - - res = PropertyReplacer.replaceTokensWithProperties("plainKey: ${ myFancyProperty }", props); - assertEquals("plainKey: myFancyValue", res); - - res = PropertyReplacer.replaceTokensWithProperties("plainKey: ${ myFancyProperty }", props); - assertEquals("plainKey: myFancyValue", res); - - res = PropertyReplacer.replaceTokensWithProperties("plainKey: ${\tmyFancyProperty\t}", props); - assertEquals("plainKey: myFancyValue", res); - - res = PropertyReplacer.replaceTokensWithProperties("plainKey: ${ \t\t\nmyFancyProperty \r\f}", props); - assertEquals("plainKey: myFancyValue", res); - } -} diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/StringInternerBytesTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/StringInternerBytesTest.java deleted file mode 100644 index deef535..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/StringInternerBytesTest.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016-2020 chronicle.software - * - * https://chronicle.software - * - * 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 net.openhft.chronicle.bytes.util; - -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.BytesTestCommon; -import org.jetbrains.annotations.NotNull; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class StringInternerBytesTest extends BytesTestCommon { - - @SuppressWarnings("rawtypes") - @Test - public void testIntern() { - @NotNull StringInternerBytes si = new StringInternerBytes(128); - for (int i = 0; i < 100; i++) { - Bytes b = Bytes.from("key" + i); - si.intern(b, (int) b.readRemaining()); - b.releaseLast(); - } - assertEquals(89, si.valueCount()); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/ThreadIndexAssignerTest.java b/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/ThreadIndexAssignerTest.java deleted file mode 100644 index ecda178..0000000 --- a/datastructures-bytes/src/test/java/net/openhft/chronicle/bytes/util/ThreadIndexAssignerTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package net.openhft.chronicle.bytes.util; - -import net.openhft.affinity.Affinity; -import net.openhft.chronicle.bytes.Bytes; -import net.openhft.chronicle.bytes.ref.BinaryIntArrayReference; -import net.openhft.chronicle.core.Jvm; -import net.openhft.chronicle.core.OS; -import net.openhft.chronicle.core.util.ThreadIndexAssigner; -import org.junit.Assert; -import org.junit.Test; - -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; - -public class ThreadIndexAssignerTest { - @Test - public void assignTwo() throws InterruptedException { - assumeTrue(OS.isLinux() && !Jvm.isArm()); - BlockingQueue t0started = new LinkedBlockingQueue(); - BlockingQueue t1started = new LinkedBlockingQueue(); - BlockingQueue t2started = new LinkedBlockingQueue(); - BlockingQueue testDone = new LinkedBlockingQueue(); - - Bytes bytes = Bytes.allocateDirect(64); - BinaryIntArrayReference iav = new BinaryIntArrayReference(2); - // write the template - iav.writeMarshallable(bytes); - // bind to the template - iav.readMarshallable(bytes); - ThreadIndexAssigner ta = new ThreadIndexAssigner(iav) { - final int next = 0; - - @Override - protected int nextIndex(int size) { - return next % size; - } - }; - BlockingQueue throwables = new LinkedBlockingQueue<>(); - Thread t0 = new Thread(() -> { - try { -// System.out.println("0 started tid: " + Affinity.getThreadId()); - Assert.assertEquals(0, ta.getId()); - t0started.put(""); - testDone.poll(10, TimeUnit.SECONDS); - } catch (Throwable ex) { - ex.printStackTrace(); - throwables.add(ex); - } -// System.out.println("0 stopped tid: " + Affinity.getThreadId()); - }); - t0.start(); - - t0started.poll(1, TimeUnit.SECONDS); - - Thread t1 = new Thread(() -> { - try { -// System.out.println("1 started tid: " + Affinity.getThreadId()); - Assert.assertEquals(1, ta.getId()); - t1started.put(""); - t2started.poll(1, TimeUnit.SECONDS); - } catch (Throwable ex) { - throwables.add(ex); - } -// System.out.println("1 stopped tid: " + Affinity.getThreadId()); - }); - t1.start(); - t1started.poll(1, TimeUnit.SECONDS); - try { - int id = ta.getId(); - System.out.println("id=" + id); - fail(); - } catch (IllegalStateException e) { - // expected - } - t2started.put(""); - t1.join(); - for (int i = 1; i <= 5; i++) { - Jvm.pause(i); - try { - Assert.assertEquals(1, ta.getId()); - break; - } catch (IllegalStateException e) { - if (i == 5) - throw e; - } - } - testDone.put(""); - t0.join(); - - // unchanged - Assert.assertEquals(1, ta.getId()); - bytes.releaseLast(); - - Throwable th = throwables.poll(); - if (th != null) - throw Jvm.rethrow(th); - } -} \ No newline at end of file diff --git a/datastructures-bytes/src/test/resources/btmtt/prim-input.txt b/datastructures-bytes/src/test/resources/btmtt/prim-input.txt deleted file mode 100644 index 8be85d1..0000000 --- a/datastructures-bytes/src/test/resources/btmtt/prim-input.txt +++ /dev/null @@ -1,59 +0,0 @@ -81 01 # myByteable - 4e # flag - 01 # b - 02 00 # s - 33 # c - 04 00 00 00 # i - 00 00 b0 40 # f - 06 00 00 00 00 00 00 00 # l - cd cc cc cc cc cc 1e 40 # d -### End Of Block -81 01 # myByteable - 59 # flag - 0b # b - 16 00 # s - 54 # c - 2c 00 00 00 # i - 8f c2 b1 40 # f - 42 00 00 00 00 00 00 00 # l - e1 7a 14 ae 47 71 53 40 # d -### End Of Block -82 01 # myScalars - 05 48 65 6c 6c 6f # s - 01 31 # bi - 02 31 30 # bd - 0a 32 30 31 37 2d 31 31 2d 30 36 # date - 0c 31 32 3a 33 35 3a 35 36 2e 37 37 35 # time - 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a 33 # dateTime - 35 3a 35 36 2e 37 37 35 27 32 30 31 37 2d 31 31 # zonedDateTime - 2d 30 36 54 31 32 3a 33 35 3a 35 36 2e 37 37 35 - 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f 6e 5d # uuid - 24 30 30 30 30 30 30 30 31 2d 32 33 34 35 2d 36 - 37 38 39 2d 30 30 30 30 2d 30 30 30 30 30 30 61 - 62 63 64 65 66 -### End Of Block -83 01 # myNested - # byteable - 59 # flag - 0b # b - 16 00 # s - 54 # c - 2c 00 00 00 # i - 8f c2 b1 40 # f - 42 00 00 00 00 00 00 00 # l - e1 7a 14 ae 47 71 53 40 # d - # scalars - 05 57 6f 72 6c 64 # s - 01 30 # bi - 01 30 # bd - 0a 32 30 31 36 2d 31 30 2d 30 35 # date - 0c 30 31 3a 33 34 3a 35 36 2e 37 37 35 # time - 17 32 30 31 36 2d 31 30 2d 30 35 54 30 31 3a 33 # dateTime - 34 3a 35 36 2e 37 37 35 2c 32 30 31 36 2d 31 30 # zonedDateTime - 2d 30 35 54 30 31 3a 33 34 3a 35 36 2e 37 37 35 - 2b 30 31 3a 30 30 5b 45 75 72 6f 70 65 2f 4c 6f - 6e 64 6f 6e 5d 24 31 31 31 31 31 31 31 31 2d 31 # uuid - 31 31 31 2d 31 31 31 31 2d 32 32 32 32 2d 32 32 - 32 32 32 32 32 32 32 32 32 32 -### End Of Block -### End Of Test diff --git a/datastructures-bytes/src/test/resources/btmtt/prim-output.txt b/datastructures-bytes/src/test/resources/btmtt/prim-output.txt deleted file mode 100644 index 8be85d1..0000000 --- a/datastructures-bytes/src/test/resources/btmtt/prim-output.txt +++ /dev/null @@ -1,59 +0,0 @@ -81 01 # myByteable - 4e # flag - 01 # b - 02 00 # s - 33 # c - 04 00 00 00 # i - 00 00 b0 40 # f - 06 00 00 00 00 00 00 00 # l - cd cc cc cc cc cc 1e 40 # d -### End Of Block -81 01 # myByteable - 59 # flag - 0b # b - 16 00 # s - 54 # c - 2c 00 00 00 # i - 8f c2 b1 40 # f - 42 00 00 00 00 00 00 00 # l - e1 7a 14 ae 47 71 53 40 # d -### End Of Block -82 01 # myScalars - 05 48 65 6c 6c 6f # s - 01 31 # bi - 02 31 30 # bd - 0a 32 30 31 37 2d 31 31 2d 30 36 # date - 0c 31 32 3a 33 35 3a 35 36 2e 37 37 35 # time - 17 32 30 31 37 2d 31 31 2d 30 36 54 31 32 3a 33 # dateTime - 35 3a 35 36 2e 37 37 35 27 32 30 31 37 2d 31 31 # zonedDateTime - 2d 30 36 54 31 32 3a 33 35 3a 35 36 2e 37 37 35 - 5a 5b 45 75 72 6f 70 65 2f 4c 6f 6e 64 6f 6e 5d # uuid - 24 30 30 30 30 30 30 30 31 2d 32 33 34 35 2d 36 - 37 38 39 2d 30 30 30 30 2d 30 30 30 30 30 30 61 - 62 63 64 65 66 -### End Of Block -83 01 # myNested - # byteable - 59 # flag - 0b # b - 16 00 # s - 54 # c - 2c 00 00 00 # i - 8f c2 b1 40 # f - 42 00 00 00 00 00 00 00 # l - e1 7a 14 ae 47 71 53 40 # d - # scalars - 05 57 6f 72 6c 64 # s - 01 30 # bi - 01 30 # bd - 0a 32 30 31 36 2d 31 30 2d 30 35 # date - 0c 30 31 3a 33 34 3a 35 36 2e 37 37 35 # time - 17 32 30 31 36 2d 31 30 2d 30 35 54 30 31 3a 33 # dateTime - 34 3a 35 36 2e 37 37 35 2c 32 30 31 36 2d 31 30 # zonedDateTime - 2d 30 35 54 30 31 3a 33 34 3a 35 36 2e 37 37 35 - 2b 30 31 3a 30 30 5b 45 75 72 6f 70 65 2f 4c 6f - 6e 64 6f 6e 5d 24 31 31 31 31 31 31 31 31 2d 31 # uuid - 31 31 31 2d 31 31 31 31 2d 32 32 32 32 2d 32 32 - 32 32 32 32 32 32 32 32 32 32 -### End Of Block -### End Of Test diff --git a/datastructures-bytes/src/test/resources/file-to-find.txt b/datastructures-bytes/src/test/resources/file-to-find.txt deleted file mode 100644 index 7b4d68d..0000000 --- a/datastructures-bytes/src/test/resources/file-to-find.txt +++ /dev/null @@ -1 +0,0 @@ -empty \ No newline at end of file diff --git a/datastructures-common/src/main/java/org/xbib/datastructures/common/TreeMultiMap.java b/datastructures-common/src/main/java/org/xbib/datastructures/common/TreeMultiMap.java new file mode 100644 index 0000000..dd6fcb8 --- /dev/null +++ b/datastructures-common/src/main/java/org/xbib/datastructures/common/TreeMultiMap.java @@ -0,0 +1,33 @@ +package org.xbib.datastructures.common; + +import java.util.Collection; +import java.util.Map; +import java.util.TreeMap; +import java.util.TreeSet; + +/** + * Tree-set multi map. + * + * @param the key type parameter + * @param the value type parameter + */ +public class TreeMultiMap, V extends Comparable> extends AbstractMultiMap { + + public TreeMultiMap() { + super(); + } + + public TreeMultiMap(MultiMap map) { + super(map); + } + + @Override + protected Collection newValues() { + return new TreeSet<>(); + } + + @Override + protected Map> newMap() { + return new TreeMap<>(); + } +} diff --git a/datastructures-common/src/test/java/org/xbib/datastructures/common/MapsTest.java b/datastructures-common/src/test/java/org/xbib/datastructures/common/MapsTest.java index fc2dd9e..ea2d1ac 100644 --- a/datastructures-common/src/test/java/org/xbib/datastructures/common/MapsTest.java +++ b/datastructures-common/src/test/java/org/xbib/datastructures/common/MapsTest.java @@ -1,13 +1,11 @@ package org.xbib.datastructures.common; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; import java.util.LinkedHashMap; import java.util.Map; -/** - * - */ public class MapsTest { @Test @@ -23,4 +21,41 @@ public class MapsTest { m3.put("c", m4); assertEquals("{d=e, c={e=f, a=b}}", Maps.deepMerge(m3, m2).toString()); } + + @Test + public void testHashMultiMap() { + LinkedHashSetMultiMap hashSetMultiMap = new LinkedHashSetMultiMap<>(); + assertTrue(hashSetMultiMap.isEmpty()); + hashSetMultiMap.put("a", "b"); + assertEquals("[b]", hashSetMultiMap.get("a").toString()); + hashSetMultiMap.put("a", "b"); + assertEquals("[b]", hashSetMultiMap.get("a").toString()); + hashSetMultiMap.put("a", "c"); + assertEquals("[b, c]", hashSetMultiMap.get("a").toString()); + hashSetMultiMap.put("b", "b"); + assertEquals("[b]", hashSetMultiMap.get("b").toString()); + hashSetMultiMap.put("b", "c"); + assertEquals("[b, c]", hashSetMultiMap.get("b").toString()); + hashSetMultiMap.put("b", "c"); + assertEquals("[b, c]", hashSetMultiMap.get("b").toString()); + } + + + @Test + public void testTreeMultiMap() { + TreeMultiMap treeMultiMap = new TreeMultiMap<>(); + assertTrue(treeMultiMap.isEmpty()); + treeMultiMap.put("a", "b"); + assertEquals("[b]", treeMultiMap.get("a").toString()); + treeMultiMap.put("a", "b"); + assertEquals("[b]", treeMultiMap.get("a").toString()); + treeMultiMap.put("a", "c"); + assertEquals("[b, c]", treeMultiMap.get("a").toString()); + treeMultiMap.put("b", "b"); + assertEquals("[b]", treeMultiMap.get("b").toString()); + treeMultiMap.put("b", "c"); + assertEquals("[b, c]", treeMultiMap.get("b").toString()); + treeMultiMap.put("b", "c"); + assertEquals("[b, c]", treeMultiMap.get("b").toString()); + } } diff --git a/settings.gradle b/settings.gradle index fbabc01..73d2a86 100644 --- a/settings.gradle +++ b/settings.gradle @@ -32,7 +32,6 @@ dependencyResolutionManagement { include 'datastructures-api' include 'datastructures-io' -include 'datastructures-bytes' include 'datastructures-charset' include 'datastructures-common' include 'datastructures-xml'