@Test public void testCheckIndexWithWriteBytes3() { doTestCheckIndex( (appender, n) -> appender.writeBytes(b -> b.writeUtf8("Hello") .writeStopBit(12345) .writeStopBit(1.2) // float also supported. .writeInt(1))); }
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)); }
default void writeWithLength(RandomDataInput bytes) { writeStopBit(bytes.readRemaining()); write(bytes); } }
private static void appendInt0(@org.jetbrains.annotations.NotNull @NotNull StreamingDataOutput out, int num) throws IllegalArgumentException, BufferOverflowException { if (num < 10) { out.writeByte((byte) ('0' + num)); } else if (num < 100) { out.writeShort((short) (num / 10 + (num % 10 << 8) + '0' * 0x101)); } else { byte[] numberBuffer = NUMBER_BUFFER.get(); // Extract digits into the end of the numberBuffer int endIndex = appendInt1(numberBuffer, num); // Bulk copy the digits into the front of the buffer out.write(numberBuffer, endIndex, numberBuffer.length - endIndex); } }
@org.jetbrains.annotations.NotNull @NotNull default S write8bit(@Nullable String s) throws BufferOverflowException { if (s == null) writeStopBit(-1); else write8bit(s, 0, (int) Math.min(writeRemaining(), s.length())); return (S) this; }
@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; }
@org.jetbrains.annotations.NotNull @NotNull default S writeSome(@org.jetbrains.annotations.NotNull @NotNull Bytes bytes) { try { long length = Math.min(bytes.readRemaining(), writeRemaining()); if (length + writePosition() >= 1 << 20) length = Math.min(bytes.readRemaining(), realCapacity() - writePosition()); write(bytes, bytes.readPosition(), length); if (length == bytes.readRemaining()) { bytes.clear(); } else { bytes.readSkip(length); if (bytes.writePosition() > bytes.realCapacity() / 2) bytes.compact(); } return (S) this; } catch (BufferOverflowException | BufferUnderflowException | IllegalArgumentException e) { throw new AssertionError(e); } }
public static void appendUtf8Char(@org.jetbrains.annotations.NotNull @NotNull StreamingDataOutput bytes, int c) throws BufferOverflowException { if (c <= 0x007F) { bytes.writeByte((byte) c); } else if (c <= 0x07FF) { bytes.writeByte((byte) (0xC0 | ((c >> 6) & 0x1F))); bytes.writeByte((byte) (0x80 | c & 0x3F)); } else if (c <= 0xFFFF) { bytes.writeByte((byte) (0xE0 | ((c >> 12) & 0x0F))); bytes.writeByte((byte) (0x80 | ((c >> 6) & 0x3F))); bytes.writeByte((byte) (0x80 | (c & 0x3F))); } else { bytes.writeByte((byte) (0xF0 | ((c >> 18) & 0x07))); bytes.writeByte((byte) (0x80 | ((c >> 12) & 0x3F))); bytes.writeByte((byte) (0x80 | ((c >> 6) & 0x3F))); bytes.writeByte((byte) (0x80 | (c & 0x3F))); } }
@org.jetbrains.annotations.NotNull @NotNull default S write8bit(@Nullable CharSequence cs) throws BufferOverflowException { if (cs == null) return writeStopBit(-1); if (cs instanceof BytesStore) return write8bit((BytesStore) cs); if (cs instanceof String) return write8bit((String) cs); return write8bit(cs, 0, cs.length()); }
default void writeBigDecimal(@org.jetbrains.annotations.NotNull @NotNull BigDecimal bd) { writeBigInteger(bd.unscaledValue()); writeStopBit(bd.scale()); }
@org.jetbrains.annotations.NotNull @NotNull default S write8bit(@org.jetbrains.annotations.NotNull @NotNull CharSequence s, int start, int length) throws BufferOverflowException, IllegalArgumentException, IndexOutOfBoundsException { writeStopBit(length); for (int i = 0; i < length; i++) { char c = s.charAt(i + start); writeUnsignedByte(c); } return (S) this; }
public static void writeUtf8(@org.jetbrains.annotations.NotNull @NotNull StreamingDataOutput bytes, @org.jetbrains.annotations.Nullable @Nullable String str) throws BufferOverflowException { if (str == null) { bytes.writeStopBit(-1); return; } try { if (Jvm.isJava9Plus()) { byte[] strBytes = extractBytes(str); byte coder = StringUtils.getStringCoder(str); long utfLength = AppendableUtil.findUtf8Length(strBytes, coder); bytes.writeStopBit(utfLength); bytes.appendUtf8(strBytes, 0, str.length(), coder); } else { char[] chars = extractChars(str); long utfLength = AppendableUtil.findUtf8Length(chars); bytes.writeStopBit(utfLength); bytes.appendUtf8(chars, 0, chars.length); } } catch (IllegalArgumentException 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); } }
public static void writeHistogram(@org.jetbrains.annotations.NotNull StreamingDataOutput out, @org.jetbrains.annotations.NotNull Histogram histogram) throws BufferOverflowException { out.writeStopBit(histogram.powersOf2()); out.writeStopBit(histogram.fractionBits()); out.writeStopBit(histogram.overRange()); out.writeStopBit(histogram.totalCount()); out.writeStopBit(histogram.floor()); int[] ints = histogram.sampleCount(); out.writeStopBit(ints.length); for (int i : ints) out.writeStopBit(i); }
default void writePositionRemaining(long position, long length) { writeLimit(position + length); writePosition(position); }
@org.jetbrains.annotations.NotNull @NotNull default S writeUnsignedInt(long i) throws BufferOverflowException, IllegalArgumentException { return writeInt((int) Maths.toUInt32(i)); }
long mantissa = val & ((1L << 52) - 1); if (sign != 0) { out.writeByte((byte) '-'); out.writeByte((byte) '0'); out.writeByte((byte) '.'); out.writeByte((byte) '0'); return; out.write(Infinity); out.write(NaN); mantissa -= intValue << shift; if (mantissa > 0) { out.writeByte((byte) '.'); mantissa <<= 1; mantissa++; long num = (mantissa >> precision); value = value * 10 + num; out.writeByte((byte) ('0' + num)); mantissa -= num << precision; out.writeByte((byte) '.'); out.writeByte((byte) '0'); out.writeByte((byte) '0'); out.writeByte((byte) '.');
@Override public void write(byte[] b, int off, int len) throws IOException { try { sdo.write(b, off, len); } catch (@NotNull BufferOverflowException | IllegalArgumentException e) { throw new IOException(e); } }
@org.jetbrains.annotations.NotNull @NotNull default S appendUtf8(@org.jetbrains.annotations.NotNull @NotNull CharSequence cs) throws BufferOverflowException { return appendUtf8(cs, 0, cs.length()); }