@Test public void testCheckIndexWithWriteBytes2() { doTestCheckIndex( (appender, n) -> appender.writeBytes(b -> b.append8bit("Message-").append(n))); }
/** * Append a double in decimal notation to a specific number of decimal places. Trailing zeros are not truncated. * <p> * If the number would normally be printed with more decimal places, the number is rounded. * * @param d to append * @param decimalPlaces to always produce * @return this * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer */ @NotNull default B append(double d, int decimalPlaces) throws BufferOverflowException { if (decimalPlaces < 0) throw new IllegalArgumentException(); if (decimalPlaces < 20) { double d2 = d * Maths.tens(decimalPlaces); if (d2 <= Long.MAX_VALUE && d2 >= Long.MIN_VALUE) { // changed from java.lang.Math.round(d2) as this was shown up to cause latency long round = d2 > 0.0 ? (long) (d2 + 0.5) : (long) (d2 - 0.5); return appendDecimal(round, decimalPlaces); } } return append(d); }
@Override public void write(char[] cbuf, int off, int len) throws IOException { for (int i = 0; i < len; i++) out.append(cbuf[i + off]); } }
@Override public void write(String str, int off, int len) throws IOException { out.append(str, off, off + len); }
@Override public void write(String str) throws IOException { out.append(str); }
/** * Append a boolean as T or F * * @param flag to append * @return this * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer */ @NotNull default B append(boolean flag) throws BufferOverflowException { return append(flag ? 'T' : 'F'); }
@NotNull @Override public Writer append(char c) throws IOException { out.append(c); return this; }
@NotNull @Override public Writer append(@NotNull CharSequence csq) throws IOException { out.append(csq); return this; }
@Override public void write(int c) throws IOException { try { out.append((char) c); } catch (@NotNull BufferOverflowException e) { throw new IOException(e); } }
@NotNull @Override public Writer append(@NotNull CharSequence csq, int start, int end) throws IOException { out.append(csq, start, end); return this; }
@NotNull default B append(@NotNull BigDecimal bigDecimal) { append(bigDecimal.toString()); return (B) this; } }
/** * Append a characters in UTF-8 * * @param cs to append * @return this * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded */ @Override @NotNull default B append(@NotNull CharSequence cs) throws BufferOverflowException { try { return append(cs, 0, cs.length()); } catch (IndexOutOfBoundsException e) { throw new AssertionError(e); } }
/** * Append a float in decimal notation * * @param f to append * @return this * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded * @throws IORuntimeException if an error occurred while attempting to resize the underlying buffer */ @NotNull default B append(float f) throws BufferOverflowException { float f2 = Math.abs(f); if (f2 > 1e6 || f2 < 1e-3) { return append(Float.toString(f)); } int precision = (int) Math.floor(6 - Math.log10(f2)); long tens = Maths.tens(precision); return append((double) Math.round(f * tens) / tens); }
public static void appendTimeMillis(@org.jetbrains.annotations.NotNull @NotNull ByteStringAppender b, long timeInMS) throws BufferOverflowException { int hours = (int) (timeInMS / (60 * 60 * 1000)); if (hours > 99) { b.append(hours); // can have over 24 hours. } else { b.writeByte((byte) (hours / 10 + '0')); b.writeByte((byte) (hours % 10 + '0')); } b.writeByte((byte) ':'); int minutes = (int) ((timeInMS / (60 * 1000)) % 60); b.writeByte((byte) (minutes / 10 + '0')); b.writeByte((byte) (minutes % 10 + '0')); b.writeByte((byte) ':'); int seconds = (int) ((timeInMS / 1000) % 60); b.writeByte((byte) (seconds / 10 + '0')); b.writeByte((byte) (seconds % 10 + '0')); b.writeByte((byte) '.'); int millis = (int) (timeInMS % 1000); b.writeByte((byte) (millis / 100 + '0')); b.writeByte((byte) (millis / 10 % 10 + '0')); b.writeByte((byte) (millis % 10 + '0')); }