/** * Assume ISO-8859-1 encoding, subclasses can override this. */ @Override default char charAt(int index) throws IndexOutOfBoundsException { try { return (char) readUnsignedByte(readPosition() + index); } catch (BufferUnderflowException e) { throw new IndexOutOfBoundsException((readPosition() + index) + " >= " + readLimit()); } }
private static int hash32(@NotNull BytesStore bs, int length) { return bs.fastHash(bs.readPosition(), length); }
public static void parseUtf8(@org.jetbrains.annotations.NotNull BytesStore bs, StringBuilder sb, int utflen) throws UTFDataFormatRuntimeException { BytesInternal.parseUtf8(bs, bs.readPosition(), sb, utflen); }
@Override @NotNull protected String getValue(@NotNull BytesStore cs, int length) { StringBuilder sb = SBP.acquireStringBuilder(); for (int i = 0; i < length; i++) sb.append((char) cs.readUnsignedByte(cs.readPosition() + i)); return sb.toString(); } }
@NotNull R write(long writeOffset, RandomDataInput bytes, long readOffset, long length) throws BufferOverflowException, BufferUnderflowException;
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); } }
/** * Check if a portion of a BytesStore matches this one. * * @param bytesStore to match against * @param length to match. * @return true if the bytes up to min(length, this.length(), bytesStore.length()) matched. */ default boolean equalBytes(@NotNull BytesStore bytesStore, long length) throws BufferUnderflowException { return length == 8 && bytesStore.length() >= 8 ? readLong(readPosition()) == bytesStore.readLong(bytesStore.readPosition()) : BytesInternal.equalBytesAny(this, bytesStore, length); }
/** * Return the bytes sum of the readable bytes. * * @return unsigned byte sum. */ default int byteCheckSum() throws IORuntimeException { return byteCheckSum(readPosition(), readLimit()); }
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); }
static long applyAsLong1to7(@NotNull BytesStore store, int remaining) { final long address = store.addressForRead(store.readPosition()); return hash(readIncompleteLong(address, remaining)); }
@NotNull @Override public Bytes<Underlying> write8bit(@Nullable BytesStore bs) throws BufferOverflowException { if (bs == null) { writeStopBit(-1); } else { long offset = bs.readPosition(); long readRemaining = Math.min(writeRemaining(), bs.readLimit() - offset); writeStopBit(readRemaining); write(bs, offset, readRemaining); } return this; }
public boolean equalsBytes(@NotNull BytesStore b2, long remaining) { long i = 0; for (; i < (remaining - 7); i += 8) { if (readLong(readPosition() + i) != b2.readLong(b2.readPosition() + i)) { return false; } } for (; i < remaining; i++) { if (readByte(readPosition() + i) != b2.readByte(b2.readPosition() + i)) { return false; } } return true; }
static long applyAsLong8(@NotNull BytesStore store) { final long address = store.addressForRead(store.readPosition()); return hash0(MEMORY.readLong(address), MEMORY.readInt(address + TOP_BYTES)); }
public MappedBytes write8bit(@Nullable BytesStore bs) throws BufferOverflowException { if (bs == null) { writeStopBit(-1); } else { long offset = bs.readPosition(); long readRemaining = Math.min(writeRemaining(), bs.readLimit() - offset); writeStopBit(readRemaining); write(bs, offset, readRemaining); } return this; } }
AbstractBytes(@NotNull BytesStore<Bytes<Underlying>, Underlying> bytesStore, long writePosition, long writeLimit, String name) throws IllegalStateException { this.bytesStore = bytesStore; bytesStore.reserve(); readPosition = bytesStore.readPosition(); this.uncheckedWritePosition(writePosition); this.writeLimit = writeLimit; // used for debugging this.name = name; assert !bytesStore.isDirectMemory() || BytesUtil.register(this); }
/** * Return the long sum of the readable bytes. * * @return signed long sum. */ @Deprecated(/* remove in 1.13 */) default long longCheckSum() { long sum = 0; long i; try { for (i = readPosition(); i < readLimit() - 7; i += 8) sum += readLong(i); if (i < readLimit()) sum += readIncompleteLong(i); } catch (BufferUnderflowException e) { throw new AssertionError(e); } return sum; }
@org.jetbrains.annotations.NotNull @NotNull default S write8bit(@Nullable BytesStore bs) throws BufferOverflowException { if (bs == null) { writeStopBit(-1); } else { long offset = bs.readPosition(); long readRemaining = Math.min(writeRemaining(), bs.readLimit() - offset); writeStopBit(readRemaining); write(bs, offset, readRemaining); } return (S) this; }
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); }