@Override default String printable(S handle, long offset) { return handle.printable(offset); }
/** * Read a long which is zero padded (high bytes) if the available bytes is less than 8. * If the offset is at or beyond the readLimit, this will return 0L. * * @param offset to read from * @return the long which might be padded. */ default long readIncompleteLong(long offset) { long left = readLimit() - offset; long l; try { if (left >= 8) return readLong(offset); if (left == 4) return readInt(offset); l = 0; for (int i = 0, remaining = (int) left; i < remaining; i++) { l |= (long) readUnsignedByte(offset + i) << (i * 8); } } catch (BufferUnderflowException e) { throw new AssertionError(e); } return l; }
default int fastHash(long offset, int length) throws BufferUnderflowException { long hash = 0; int i = 0; if (length >= 4) { hash = readInt(offset + i); i += 4; } for (; i < length - 3; i += 4) { hash *= 0x6d0f27bd; hash += readInt(offset + i); } if (i < length - 1) { hash *= 0x6d0f27bdL; hash += readShort(offset + i); i += 2; } if (i < length) hash += readByte(offset + i); hash *= 0x855dd4db; return (int) (hash ^ (hash >> 32)); } }
default int peekVolatileInt() throws BufferUnderflowException { return readVolatileInt(readPosition()); }
/** * Read an unsigned int at an offset * * @param offset to read * @return the int * @throws BufferUnderflowException if the offset is outside the limits of the Bytes */ default int readUnsignedInt24(long offset) throws BufferUnderflowException { return readUnsignedShort(offset) | (readUnsignedByte(offset) << 16); }
public static String toDebugString(@org.jetbrains.annotations.NotNull @NotNull RandomDataInput bytes, long maxLength) { if (bytes.refCount() < 1) bytes.reserve(); try { int len = Maths.toUInt31(maxLength + 40); @org.jetbrains.annotations.NotNull StringBuilder sb = new StringBuilder(len); long readPosition = bytes.readPosition(); long readLimit = bytes.readLimit(); sb.append("[") .append("pos: ").append(readPosition) .append(", rlim: ").append(readLimit) .append(", wlim: ").append(asSize(bytes.writeLimit())) .append(", cap: ").append(asSize(bytes.capacity())) .append(" ] "); try { long start = Math.max(bytes.start(), readPosition - 64); long end = Math.min(readLimit + 64, start + maxLength); end = Math.min(end, bytes.realCapacity()); try { for (; end >= start + 16 && end >= readLimit + 16; end -= 8) { if (bytes.readLong(end - 8) != 0) break; toString(bytes, sb, start, readPosition, bytes.writePosition(), end); if (end < bytes.readLimit()) sb.append("..."); } catch (Exception e) {
public static boolean bytesEqual( @org.jetbrains.annotations.NotNull @NotNull RandomDataInput a, long offset, @org.jetbrains.annotations.NotNull @NotNull RandomDataInput second, long secondOffset, long len) throws BufferUnderflowException { long i = 0; while (len - i >= 8L) { if (a.readLong(offset + i) != second.readLong(secondOffset + i)) return false; i += 8L; } if (len - i >= 4L) { if (a.readInt(offset + i) != second.readInt(secondOffset + i)) return false; i += 4L; } if (len - i >= 2L) { if (a.readShort(offset + i) != second.readShort(secondOffset + i)) return false; i += 2L; } if (i < len) return a.readByte(offset + i) == second.readByte(secondOffset + i); return true; }
private static void toString(@org.jetbrains.annotations.NotNull @NotNull RandomDataInput bytes, @org.jetbrains.annotations.NotNull @NotNull StringBuilder sb) throws IllegalStateException { bytes.reserve(); assert bytes.start() <= bytes.readPosition(); assert bytes.readPosition() <= bytes.readLimit(); assert bytes.readLimit() <= bytes.realCapacity(); try { for (long i = bytes.readPosition(); i < bytes.readLimit(); i++) { sb.append((char) bytes.readUnsignedByte(i)); } } catch (BufferUnderflowException e) { sb.append(' ').append(e); } bytes.release(); }
public static void writeFully(@org.jetbrains.annotations.NotNull @NotNull RandomDataInput bytes, long offset, long length, @org.jetbrains.annotations.NotNull StreamingDataOutput sdo) throws BufferUnderflowException, BufferOverflowException { long i = 0; for (; i < length - 7; i += 8) sdo.writeLong(bytes.readLong(offset + i)); if (i < length - 3) { sdo.writeInt(bytes.readInt(offset + i)); i += 4; } for (; i < length; i++) sdo.writeByte(bytes.readByte(offset + i)); }
/** * 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; }
@Override default long readLong(S handle, long offset) { return handle.readLong(offset); }
public void write0(long offsetInRDO, @NotNull RandomDataInput bytes, long offset, long length) throws BufferUnderflowException { long i = 0; for (; i < length - 7; i += 8) { writeLong(offsetInRDO + i, bytes.readLong(offset + i)); } for (; i < length; i++) { writeByte(offsetInRDO + i, bytes.readByte(offset + i)); } }
@Override public boolean equivalent(RandomDataInput source, long sourceOffset) { return source.readInt(sourceOffset) == instance; }
public static void parse8bit1(long offset, @org.jetbrains.annotations.NotNull @NotNull RandomDataInput bytes, @org.jetbrains.annotations.NotNull @NotNull Appendable appendable, int utflen) throws BufferUnderflowException, IOException { assert bytes.realCapacity() >= utflen + offset; for (int count = 0; count < utflen; count++) { int c = bytes.readUnsignedByte(offset + count); appendable.append((char) c); } }
/** * 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; }
@Override default byte readByte(S handle, long offset) { return handle.readByte(offset); }
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); } }
/** * Read the byte at an offset and converts it into a printable * * @param offset to read * @return the byte in a printable form. * @throws BufferUnderflowException if the offset is outside the limits of the Bytes */ default String printable(long offset) throws BufferUnderflowException { return charToString[readUnsignedByte(offset)]; }
private static boolean compareUtf8( @org.jetbrains.annotations.NotNull RandomDataInput input, long offset, long utfLen, @org.jetbrains.annotations.NotNull CharSequence other) throws UTFDataFormatRuntimeException, BufferUnderflowException, IndexOutOfBoundsException { if (offset + utfLen > input.realCapacity()) throw new BufferUnderflowException(); int i = 0; while (i < utfLen && i < other.length()) { int c = input.readByte(offset + i); if (c < 0) break; if ((char) c != other.charAt(i)) return false; i++; } if (i < utfLen && i < other.length()) return compareUtf82(input, offset + i, i, utfLen, other); return utfLen == other.length(); }
@NotNull @Override public Bytes<Underlying> write(@NotNull RandomDataInput bytes, long offset, long length) throws BufferUnderflowException, BufferOverflowException { if (length == 8) { writeLong(bytes.readLong(offset)); } else if (length >= 16 && bytes.isDirectMemory()) { rawCopy(bytes, offset, length); } else { BytesInternal.writeFully(bytes, offset, length, this); } return this; }