@NotNull @Override public MappedBytes write(@NotNull RandomDataInput bytes) throws BufferOverflowException { assert singleThreadedAccess(); assert bytes != this : "you should not write to yourself !"; long remaining = bytes.readRemaining(); write(writePosition, bytes); writePosition += remaining; return this; }
@NotNull @Override public Bytes<Void> writePosition(long position) throws BufferOverflowException { assert singleThreadedAccess(); 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 void ensureCapacity(long size) throws IllegalArgumentException { assert singleThreadedAccess(); if (!bytesStore.inside(writePosition, Math.toIntExact(size))) { acquireNextByteStore0(writePosition, false); } }
@Override @NotNull public Bytes<Void> write(@NotNull RandomDataInput bytes, long offset, long length) throws BufferUnderflowException, BufferOverflowException { assert singleThreadedAccess(); 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 protected void writeCheckOffset(long offset, long adding) throws BufferOverflowException { assert singleThreadedAccess(); if (offset < 0 || offset > mappedFile.capacity() - adding) throw writeBufferOverflowException(offset); if (!bytesStore.inside(offset, Math.toIntExact(adding))) { acquireNextByteStore0(offset, false); } // super.writeCheckOffset(offset, adding); }
@NotNull @Override public Bytes<Void> append8bit(@NotNull CharSequence cs, int start, int end) throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { assert singleThreadedAccess(); // 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 @NotNull public Bytes<Void> writeOrderedInt(long offset, int i) throws BufferOverflowException { assert singleThreadedAccess(); assert writeCheckOffset0(offset, (long) 4); if (!bytesStore.inside(offset, 4)) { acquireNextByteStore0(offset, false); } bytesStore.writeOrderedInt(offset, i); return this; }
@NotNull @Override public Bytes<Void> write(@NotNull BytesStore bytes, long offset, long length) throws BufferUnderflowException, BufferOverflowException { assert singleThreadedAccess(); if (length == 8) writeLong(bytes.readLong(offset)); else if (bytes.isDirectMemory() && length <= Math.min(writeRemaining(), safeCopySize())) rawCopy(bytes, offset, length); else if (length > 0) BytesInternal.writeFully(bytes, offset, length, this); return this; }
@NotNull @Override public Bytes<Void> writeUtf8(CharSequence str) throws BufferOverflowException { assert singleThreadedAccess(); 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; }
@NotNull @Override public Bytes<Void> writeByte(byte i8) throws BufferOverflowException { assert singleThreadedAccess(); long oldPosition = writePosition; if (writePosition < 0 || writePosition > capacity() - (long) 1) throw writeBufferOverflowException(writePosition); if (!bytesStore.inside(writePosition, 1)) { // already determined we need it acquireNextByteStore0(writePosition, false); } this.writePosition = writePosition + (long) 1; bytesStore.writeByte(oldPosition, i8); return this; }
@NotNull @Override public Bytes<Void> writeStopBit(long n) throws BufferOverflowException { assert singleThreadedAccess(); 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 @NotNull public MappedBytes write8bit(CharSequence s, int start, int length) { assert singleThreadedAccess(); if (s == null) { writeStopBit(-1); return this; } // 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); }
@NotNull @Override public Bytes<Void> appendUtf8(char[] chars, int offset, int length) throws BufferOverflowException, IllegalArgumentException { assert singleThreadedAccess(); 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 ((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 Bytes<Void> writeUtf8(String str) throws BufferOverflowException { assert singleThreadedAccess(); 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); } }
@NotNull private MappedBytes append8bit0(@NotNull String s, int start, int length) throws BufferOverflowException { assert singleThreadedAccess(); if (Jvm.isJava9Plus()) { byte[] bytes = extractBytes(s);
public Bytes<Void> appendUtf8(CharSequence cs, int start, int length) throws BufferOverflowException, IllegalArgumentException { assert singleThreadedAccess();