@NotNull public MappedBytes write(long offsetInRDO, @NotNull RandomDataInput bytes) throws BufferOverflowException { write(offsetInRDO, bytes, bytes.readPosition(), bytes.readRemaining()); return this; }
default ByteBuffer toTemporaryDirectByteBuffer() throws IllegalArgumentException { int len = Maths.toUInt31(readRemaining()); try { ByteBuffer bb = ByteBuffer.allocateDirect(len); copyTo(bb); bb.clear(); return bb; } catch (BufferUnderflowException e) { throw new AssertionError(e); } }
/** * Read a byte[] from memory. * * @return the length actually read. * @throws BufferUnderflowException if the offset is outside the limits of the Bytes */ default int copyTo(@NotNull byte[] bytes) throws BufferUnderflowException { int len = (int) Math.min(bytes.length, readRemaining()); for (int i = 0; i < len; i++) bytes[i] = readByte(start() + i); return len; }
@org.jetbrains.annotations.NotNull public static byte[] toByteArray(@org.jetbrains.annotations.NotNull RandomDataInput in) { int len = (int) Math.min(Integer.MAX_VALUE, in.readRemaining()); @org.jetbrains.annotations.NotNull byte[] bytes = new byte[len]; in.read(in.readPosition(), bytes, 0, bytes.length); return bytes; }
@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<Underlying> write(@NotNull RandomDataInput 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); } } }
public static long findByte(@org.jetbrains.annotations.NotNull RandomDataInput bytes, byte stopByte) { try { long start = bytes.readPosition(); long remaining = bytes.readRemaining(); for (long i = 0; i < remaining; i++) { if (bytes.readByte(start + i) == stopByte) return i; } return -1; } catch (BufferUnderflowException e) { throw new AssertionError(e); } }
default void writeWithLength(RandomDataInput bytes) { writeStopBit(bytes.readRemaining()); write(bytes); } }
/** * Copy data from this RandomDataInput to the ByteBuffer. The minimum of {@link #readRemaining()} and * {@link ByteBuffer#remaining()}. Starting from {@link #start()} in this RandomDataInput and from {@link * ByteBuffer#position()} of the given bb. Does NOT change the position or limit or mark of the given ByteBuffer. * Returns the number of the copied bytes. */ default int copyTo(@NotNull ByteBuffer bb) throws BufferUnderflowException { int pos = bb.position(); int len = (int) Math.min(bb.remaining(), readRemaining()); int i; for (i = 0; i < len - 7; i += 8) bb.putLong(pos + i, readLong(start() + i)); for (; i < len; i++) bb.put(pos + i, readByte(start() + i)); return len; }
/** * Write all data or fail. */ @org.jetbrains.annotations.NotNull @NotNull default S write(@org.jetbrains.annotations.NotNull @NotNull RandomDataInput 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); } }
@org.jetbrains.annotations.NotNull public static String toString(@org.jetbrains.annotations.NotNull @NotNull RandomDataInput bytes) throws IllegalStateException { // the output will be no larger than this final int size = MAX_STRING_LEN; @org.jetbrains.annotations.NotNull final StringBuilder sb = new StringBuilder(size); if (bytes.readRemaining() > size) { final Bytes bytes1 = bytes.bytesForRead(); try { bytes1.readLimit(bytes1.readPosition() + size); toString(bytes1, sb); return sb.toString() + "..."; } finally { bytes1.release(); } } else { toString(bytes, sb); return sb.toString(); } }