default boolean isEmpty() { return readRemaining() == 0; }
/** * @return length in bytes to read or Integer.MAX_VALUE if longer. */ @Override default int length() { return (int) Math.min(Integer.MAX_VALUE, readRemaining()); }
@Override public long applyAsLong(BytesStore bytes) { return applyAsLong(bytes, bytes.readRemaining()); }
public T intern(@NotNull BytesStore cs) throws IllegalArgumentException, IORuntimeException, BufferUnderflowException { return intern(cs, (int) cs.readRemaining()); }
@NotNull R write(long writeOffset, RandomDataInput bytes, long readOffset, long length) throws BufferOverflowException, BufferUnderflowException;
@Override public long applyAsLong(@NotNull BytesStore store) { final int remaining = Maths.toInt32(store.readRemaining()); return applyAsLong(store, remaining); }
@NotNull @Override public Bytes<Underlying> prewrite(@NotNull BytesStore bytes) throws BufferOverflowException { long offset = prewriteOffsetPositionMoved(bytes.readRemaining()); bytesStore.write(offset, bytes); return this; }
/** * 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); } }
@Override public boolean equals(Object obj) { if (!(obj instanceof BytesStore)) { return false; } @NotNull BytesStore b2 = (BytesStore) obj; long remaining = readRemaining(); return (b2.readRemaining() == remaining) && equalsBytes(b2, remaining); }
@Override public long applyAsLong(@NotNull BytesStore store) { int remaining = Maths.toInt32(store.readRemaining()); return applyAsLong(store, remaining); }
public static void assignBytesStoreToByteBuffer(@org.jetbrains.annotations.NotNull BytesStore bytesStore, @org.jetbrains.annotations.NotNull ByteBuffer byteBuffer) throws BufferUnderflowException { long address = bytesStore.addressForRead(bytesStore.readPosition()); long capacity = bytesStore.readRemaining(); ByteBuffers.setAddressCapacity(byteBuffer, address, capacity); byteBuffer.clear(); }
/** * 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); } }
default S write(@org.jetbrains.annotations.NotNull @NotNull 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); } }
@Override protected void getValue(Object o, @NotNull BytesOut write) throws IllegalAccessException { @NotNull 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); }
public static boolean contentEqual(@org.jetbrains.annotations.Nullable @Nullable BytesStore a, @org.jetbrains.annotations.Nullable @Nullable BytesStore b) { if (a == null) return b == null; if (b == null) return false; if (a.readRemaining() != b.readRemaining()) return false; long aPos = a.readPosition(); long bPos = b.readPosition(); long length = a.readRemaining(); long i; for (i = 0; i < length - 7; i += 8) { if (a.readLong(aPos + i) != b.readLong(bPos + i)) return false; } for (; i < length; i++) { if (a.readByte(aPos + i) != b.readByte(bPos + i)) return false; } return true; }
public static String to8bitString(@org.jetbrains.annotations.NotNull @NotNull BytesStore bytes) throws IllegalArgumentException { long pos = bytes.readPosition(); int len = Maths.toInt32(bytes.readRemaining()); @org.jetbrains.annotations.NotNull 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); }
long writePos = outBytes.writePosition(); BytesStore inBytes; long size = readRemaining(); if (this.isDirectMemory()) { inBytes = this;
/** * 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(@NotNull 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; }
public long rawCopy(@NotNull BytesStore bytes, long offset, long length) throws BufferOverflowException, BufferUnderflowException { assert length < safeCopySize(); this.acquireNextByteStore(writePosition(), false); long len = Math.min(writeRemaining(), Math.min(bytes.readRemaining(), length)); if (len > 0) { OS.memory().copyMemory(bytes.addressForRead(offset), addressForWrite(writePosition()), len); uncheckedWritePosition(writePosition() + len); } return len; }
static boolean startsWith(@org.jetbrains.annotations.NotNull @NotNull BytesStore a, @org.jetbrains.annotations.NotNull @NotNull 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; }