private void acquireNextByteStore(long offset, boolean set) throws BufferOverflowException { if (bytesStore.inside(offset)) return; acquireNextByteStore0(offset, set); }
@NotNull @Override public Bytes<Void> readPosition(long position) throws BufferUnderflowException { if (bytesStore.inside(position)) { return super.readPosition(position); } else { acquireNextByteStore0(position, true); return this; } }
@Override public void ensureCapacity(long size) throws IllegalArgumentException { assert singleThreadedAccess(); if (!bytesStore.inside(writePosition, Math.toIntExact(size))) { acquireNextByteStore0(writePosition, false); } }
@Override public long addressForRead(long offset) throws BufferUnderflowException { if (!bytesStore.inside(offset)) acquireNextByteStore0(offset, true); return bytesStore.addressForRead(offset); }
@Override public long readVolatileLong(long offset) throws BufferUnderflowException { if (!bytesStore.inside(offset, 8)) { acquireNextByteStore0(offset, false); } return bytesStore.readVolatileLong(offset); }
@Override public long addressForWrite(long offset) throws UnsupportedOperationException, BufferOverflowException { if (!bytesStore.inside(offset)) acquireNextByteStore0(offset, true); return bytesStore.addressForWrite(offset); }
@Override public short readVolatileShort(long offset) throws BufferUnderflowException { if (!bytesStore.inside(offset, 2)) { acquireNextByteStore0(offset, false); } return bytesStore.readVolatileShort(offset); }
@Override public long addressForRead(long offset, int buffer) throws UnsupportedOperationException, BufferUnderflowException { if (!bytesStore.inside(offset, buffer)) acquireNextByteStore0(offset, true); return bytesStore.addressForRead(offset); }
@Override public int readVolatileInt(long offset) throws BufferUnderflowException { if (!bytesStore.inside(offset, 4)) { acquireNextByteStore0(offset, false); } return bytesStore.readVolatileInt(offset); }
@Override public byte readVolatileByte(long offset) throws BufferUnderflowException { if (!bytesStore.inside(offset, 1)) { acquireNextByteStore0(offset, false); } return bytesStore.readVolatileByte(offset); }
@NotNull @Override public Bytes<Void> readSkip(long bytesToSkip) throws BufferUnderflowException { if (readPosition + bytesToSkip > readLimit()) throw new BufferUnderflowException(); long check = bytesToSkip >= 0 ? this.readPosition : this.readPosition + bytesToSkip; if (bytesStore instanceof NoBytesStore || bytesToSkip != (int) bytesToSkip || !bytesStore.inside(readPosition, (int) bytesToSkip)) { acquireNextByteStore0(check, false); } this.readPosition += bytesToSkip; return this; }
@Override public int peekVolatileInt() { if (!bytesStore.inside(readPosition, 4)) { acquireNextByteStore0(readPosition, true); } @Nullable MappedBytesStore bytesStore = (MappedBytesStore) (BytesStore) this.bytesStore; long address = bytesStore.address + bytesStore.translate(readPosition); @Nullable Memory memory = bytesStore.memory; // are we inside a cache line? if ((address & 63) <= 60) { // if (memory == null) throw new NullPointerException(); memory.getClass(); // faster check for null. return UnsafeMemory.UNSAFE.getIntVolatile(null, address); } else { return memory.readVolatileInt(address); } }
@Override protected void readCheckOffset(long offset, long adding, boolean given) throws BufferUnderflowException { long check = adding >= 0 ? offset : offset + adding; if (adding < 1 << 10 ? !bytesStore.inside(check, (int) adding) : !bytesStore.inside(check)) { acquireNextByteStore0(offset, false); } super.readCheckOffset(offset, adding, given); }
@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); }
@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> 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; }
public MappedBytes write(long offsetInRDO, byte[] bytes, int offset, int length) { 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; }
@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; }
public MappedBytes write(long writeOffset, RandomDataInput bytes, long readOffset, long length) throws BufferOverflowException, BufferUnderflowException { 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; }