@Override public int toCycle(long index) { return Maths.toUInt31(index >> cycleShift); } }
if (bytes instanceof MappedBytes) { MappedBytes mb = (MappedBytes) bytes; sizeToSkip = Maths.toUInt31(mb.mappedFile().overlapSize() / 2);
int safeLength = Maths.toUInt31(mappedBytes.realCapacity() - mappedBytes.readPosition()); mappedBytes.writeLimit(mappedBytes.realCapacity()); mappedBytes.writePosition(mappedBytes.readPosition());
@Override public int toCycle(long index) { return Maths.toUInt31(index >> cycleShift); } }
@NotNull @Override public ByteBuffer toTemporaryDirectByteBuffer() { ByteBuffer bb = ByteBuffer.allocateDirect(0); try { BB_ADDRESS.setLong(bb, address); BB_CAPACITY.setInt(bb, Maths.toUInt31(readRemaining())); BB_ATT.set(bb, this); } catch (Exception e) { throw new AssertionError(e); } bb.clear(); return bb; }
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); } }
@NotNull default BigDecimal readBigDecimal() { return new BigDecimal(readBigInteger(), Maths.toUInt31(readStopBit())); }
public String intern(@NotNull final Bytes bytes) { return intern(bytes, Maths.toUInt31(bytes.readRemaining())); }
@org.jetbrains.annotations.NotNull public static char[] toCharArray(@org.jetbrains.annotations.NotNull Bytes bytes) { @org.jetbrains.annotations.NotNull final char[] chars = new char[Maths.toUInt31(bytes.readRemaining())]; for (int i = 0; i < bytes.readRemaining(); i++) { chars[i] = (char) bytes.readUnsignedByte(i + bytes.readPosition()); } return chars; }
private BytesStore allocateBytesStoreForInstance() { long instanceSize = nativeInstance.maxSize(); if (instanceSize > Bytes.MAX_BYTE_BUFFER_CAPACITY) { return NativeBytesStore.nativeStoreWithFixedCapacity(instanceSize); } else { return BytesStore.wrap(ByteBuffer.allocate(Maths.toUInt31(instanceSize))); } }
@NotNull default BigInteger readBigInteger() { int length = Maths.toUInt31(readStopBit()); if (length == 0) if (lenient()) return BigInteger.ZERO; else throw new BufferUnderflowException(); @NotNull byte[] bytes = new byte[length]; read(bytes); return new BigInteger(bytes); }
default boolean read8bit(@NotNull StringBuilder sb) throws IORuntimeException, BufferUnderflowException { sb.setLength(0); long len0 = BytesInternal.readStopBit(this); if (len0 == -1) return false; int len = Maths.toUInt31(len0); try { AppendableUtil.parse8bit(this, sb, len); } catch (IOException e) { throw new IORuntimeException(e); } return true; }
public static void readHistogram(@org.jetbrains.annotations.NotNull StreamingDataInput in, @org.jetbrains.annotations.NotNull Histogram histogram) { try { int powersOf2 = Maths.toUInt31(in.readStopBit()); int fractionBits = Maths.toUInt31(in.readStopBit()); long overRange = in.readStopBit(); long totalCount = in.readStopBit(); long floor = in.readStopBit(); histogram.init(powersOf2, fractionBits, overRange, totalCount, floor); int length = Maths.toUInt31(in.readStopBit()); int[] ints = histogram.sampleCount(); for (int i = 0; i < length; i++) ints[i] = Maths.toUInt31(in.readStopBit()); } catch (IllegalArgumentException e) { throw new IORuntimeException(e); } }
default <ACS extends Appendable & CharSequence> boolean read8bit(@NotNull ACS sb) throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { AppendableUtil.setLength(sb, 0); long len0 = BytesInternal.readStopBit(this); if (len0 == -1) return false; int len = Maths.toUInt31(len0); try { AppendableUtil.parse8bit(this, sb, len); } catch (IOException e) { throw new IORuntimeException(e); } return true; }
/** * The same as readUtf8() except the chars are copied to a truncated StringBuilder. * * @param sb to copy chars to * @return <code>true</code> if there was a String, or <code>false</code> if it was <code>null</code> */ default <ACS extends Appendable & CharSequence> boolean readUtf8(@NotNull ACS sb) throws IORuntimeException, IllegalArgumentException, BufferUnderflowException { AppendableUtil.setLength(sb, 0); if (readRemaining() <= 0) // TODO throw BufferUnderflowException here? please review return false; long len0 = BytesInternal.readStopBit(this); if (len0 == -1) return false; int len = Maths.toUInt31(len0); if (len > 0) BytesInternal.parseUtf8(this, sb, len); return true; }
default boolean read8bit(@NotNull Bytes b) throws BufferUnderflowException, IllegalStateException, BufferOverflowException { b.clear(); if (readRemaining() <= 0) return false; long len0 = BytesInternal.readStopBit(this); if (len0 == -1) return false; int len = Maths.toUInt31(len0); b.write((BytesStore) this, readPosition(), (long) len); readSkip(len); return true; }
@Override protected void setValue(Object o, @NotNull BytesIn read) throws IllegalAccessException, IORuntimeException { @NotNull Bytes bytes = (Bytes) field.get(o); long stopBit = read.readStopBit(); if (stopBit == -1) { if (bytes != null) bytes.release(); field.set(o, null); return; } int length = Maths.toUInt31(stopBit); @NotNull Bytes bs; if (bytes == null) { bs = Bytes.elasticHeapByteBuffer(length); } else { bs = bytes; } Object uo = bs.underlyingObject(); if (uo instanceof ByteBuffer && !(bs.bytesStore() instanceof NativeBytesStore)) { read.read(((ByteBuffer) uo).array(), 0, length); } else { bs.clear(); read.read(bs, length); } bs.readLimit(length); field.set(o, bs); } }
default void readWithLength(Bytes bytes) { bytes.clear(); int length = Maths.toUInt31(readStopBit()); int i; for (i = 0; i < length - 7; i++) bytes.writeLong(readLong()); for (; i < length; i++) bytes.writeByte(readByte()); }