try (ChronicleQueue chronicle = single(file).blockSize(64 << 20).build()) { ExcerptAppender appender = chronicle.createAppender(); UncheckedBytes bytes = new UncheckedBytes(NoBytesStore.NO_BYTES); for (int i = 0; i < count; i++) { long start = System.nanoTime(); Bytes<?> bytes0 = dc.wire().bytes(); bytes0.ensureCapacity(size); bytes.setBytes(bytes0); bytes.readPosition(bytes.writePosition()); writer.writeTo(bytes); bytes0.writePosition(bytes.writePosition()); bytes.setBytes(bytes0); reader.readFrom(bytes);
public long rawCopy(@NotNull 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); OS.memory().copyMemory(bytes.addressForRead(offset), addressForWrite(writePosition()), len); writeSkip(len); } return len; }
@Override protected long writeOffsetPositionMoved(long adding, long advance) { long oldPosition = writePosition(); uncheckedWritePosition(writePosition() + advance); return oldPosition; }
@NotNull @Override public Bytes<Underlying> 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; }
public void setBytes(@NotNull Bytes bytes) throws IllegalStateException { BytesStore underlyingBytes = bytes.bytesStore(); if (bytesStore != underlyingBytes) { bytesStore.release(); this.bytesStore = underlyingBytes; bytesStore.reserve(); } readPosition(bytes.readPosition()); this.uncheckedWritePosition(bytes.writePosition()); this.writeLimit = bytes.writeLimit(); assert !bytesStore.isDirectMemory() || BytesUtil.register(this); this.underlyingBytes = bytes; }
@NotNull public Bytes<Underlying> write(@NotNull 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; }
public UncheckedBytes(@NotNull Bytes underlyingBytes) throws IllegalStateException { super(underlyingBytes.bytesStore(), underlyingBytes.writePosition(), underlyingBytes.writeLimit()); this.underlyingBytes = underlyingBytes; readPosition(underlyingBytes.readPosition()); }
/** * 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 <code>this</code> 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 */ @NotNull default Bytes<Underlying> unchecked(boolean unchecked) throws IllegalStateException { if (unchecked) { if (isElastic()) Jvm.debug().on(getClass(), "Wrapping elastic bytes with unchecked() will require calling ensureCapacity() as needed!"); return start() == 0 && bytesStore().isDirectMemory() ? new UncheckedNativeBytes<>(this) : new UncheckedBytes<>(this); } return this; }
@NotNull @Override public Bytes<Underlying> readLimit(long limit) { uncheckedWritePosition(limit); return this; }
@Override public void ensureCapacity(long size) throws IllegalArgumentException { if (size > realCapacity()) { underlyingBytes.ensureCapacity(size); bytesStore = underlyingBytes.bytesStore(); } }
@NotNull @Override public Bytes<Underlying> writeSkip(long bytesToSkip) { uncheckedWritePosition(writePosition() + bytesToSkip); return this; }
@NotNull @Override public Bytes<Underlying> writePosition(long position) { uncheckedWritePosition(position); return this; }