Thread tailerThread = new Thread(() -> { AffinityLock rlock = AffinityLock.acquireLock(); Bytes bytes = NativeBytes.nativeBytes(BYTES_LENGTH).unchecked(true); try (ChronicleQueue rqueue = SingleChronicleQueueBuilder .fieldlessBinary(name)
public BytesWithIndex(Bytes<?> bytes, long index) { this.bytes = Bytes.allocateElasticDirect(bytes.readRemaining()).write(bytes); this.index = index; }
if (endOfBuffer < 0) throw new BufferOverflowException(); if (endOfBuffer > capacity()) throw new BufferOverflowException(); final long realCapacity = realCapacity(); if (endOfBuffer <= realCapacity) { size = Math.min(size, capacity()); if (isDirectMemory() || size > MAX_BYTE_BUFFER_CAPACITY) { size = alignToPageSize(size); size = Math.min(size, capacity()); if (isByteBufferBacked && size <= MAX_BYTE_BUFFER_CAPACITY) { position = ((ByteBuffer) bytesStore.underlyingObject()).position(); store = allocateNewByteBufferBackedStore(Maths.toInt32(size)); } else { store = NativeBytesStore.lazyNativeBytesStoreWithFixedCapacity(size);
@NotNull @Override public Bytes<Underlying> write8bit(@Nullable 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 this; }
@Override @NotNull @net.openhft.chronicle.core.annotation.NotNull public Bytes<Void> 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); } }
/** * Allocate an elastic buffer with {@code initialCapacity} size. * * @return Bytes for writing. */ static NativeBytes<Void> allocateElasticDirect(long initialCapacity) throws IllegalArgumentException { return NativeBytes.nativeBytes(initialCapacity); }
@Override public void ensureCapacity(long size) throws IllegalArgumentException { try { assert size >= 0; writeCheckOffset(size, 0L); } catch (BufferOverflowException e) { throw new IllegalArgumentException("Bytes cannot be resized to " + size + " limit: " + capacity()); } }
@Override @NotNull public Bytes<Void> clearAndPad(long length) throws BufferOverflowException { long pos = base.writePosition(); try { base.clearAndPad(length); return this; } finally { copyToText(pos); } }
@NotNull private BytesStore allocateNewByteBufferBackedStore(int size) { if (isDirectMemory()) { return NativeBytesStore.elasticByteBuffer(size, capacity()); } else { return HeapBytesStore.wrap(ByteBuffer.allocate(size)); } }
@NotNull public static NativeBytes<Void> nativeBytes() { try { return new NativeBytes<>(noBytesStore()); } catch (IllegalStateException e) { throw new AssertionError(e); } }
@Override protected void writeCheckOffset(long offset, long adding) throws BufferOverflowException { if (offset < bytesStore.start()) throw new BufferOverflowException(); long writeEnd = offset + adding; if (writeEnd > bytesStore.safeLimit()) checkResize(writeEnd); }
@Override public long capacity() { return base.capacity(); }
@Override public long addressForRead(long offset) throws UnsupportedOperationException { return base.addressForRead(offset); }
@Override @NotNull public Bytes clear() { return base.clear(); }
/** * Allocate an elastic buffer with initially no size. * * @return Bytes for writing. */ static NativeBytes<Void> allocateElasticDirect() { return NativeBytes.nativeBytes(); }
@NotNull @Override public VanillaBytes<Underlying> bytesForWrite() throws IllegalStateException { return elastic ? new NativeBytes<>(this) : new VanillaBytes<>(this); }
@Override protected long writeOffsetPositionMoved(long adding, long advance) throws BufferOverflowException { long oldPosition = writePosition; if (writePosition < bytesStore.start()) throw new BufferOverflowException(); long writeEnd = writePosition + adding; if (writeEnd > bytesStore.safeLimit()) checkResize(writeEnd); this.writePosition = writePosition + advance; return oldPosition; }
HexDumpBytes(BytesStore base, Bytes text) { try { this.base.write(base); this.text.write(text); } catch (BufferOverflowException e) { throw new AssertionError(e); } }
@NotNull public static NativeBytes<Void> nativeBytes(long initialCapacity) throws IllegalArgumentException { @NotNull NativeBytesStore<Void> store = nativeStoreWithFixedCapacity(initialCapacity); try { try { return new NativeBytes<>(store); } finally { store.release(); } } catch (IllegalStateException e) { throw new AssertionError(e); } }
public static BytesStore<Bytes<Void>, Void> copyOf(@NotNull Bytes bytes) { long remaining = bytes.readRemaining(); NativeBytes<Void> bytes2; try { bytes2 = Bytes.allocateElasticDirect(remaining); bytes2.write(bytes, 0, remaining); return bytes2; } catch (IllegalArgumentException | BufferOverflowException | BufferUnderflowException e) { throw new AssertionError(e); } }