@Test public void testCheckIndexWithWriteBytes2() { doTestCheckIndex( (appender, n) -> appender.writeBytes(b -> b.append8bit("Message-").append(n))); }
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')); }
/** * 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); }
endIndex = MIN_VALUE_TEXT.length; } else { out.writeByte((byte) '-'); num = -num; endIndex = appendLong1(numberBuffer, num); out.writeUnsignedByte('0'); out.writeUnsignedByte('.'); while (decimalPlaces-- > digits) out.writeUnsignedByte('0'); out.write(numberBuffer, endIndex, digits); return; out.write(numberBuffer, endIndex, decimalLength); out.writeUnsignedByte('.'); out.write(numberBuffer, endIndex + decimalLength, digits - decimalLength);
/** * Append a portion of a String to the Bytes in ISO-8859-1 * * @param cs to copy * @param start index of the first char inclusive * @param end index of the last char exclusive. * @return this * @throws BufferOverflowException If the string as too large to write in the capacity available * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded * @throws IndexOutOfBoundsException if the start or the end are not valid for the CharSequence */ default B append8bit(@NotNull CharSequence cs, int start, int end) throws IllegalArgumentException, BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { if (cs instanceof BytesStore) { return write((BytesStore) cs, (long) start, end); } for (int i = start; i < end; i++) { char c = cs.charAt(i); if (c > 255) c = '?'; writeByte((byte) c); } return (B) this; }
public static void appendBase16(@org.jetbrains.annotations.NotNull @NotNull ByteStringAppender out, long num, int minDigits) throws IllegalArgumentException, BufferOverflowException { byte[] numberBuffer = NUMBER_BUFFER.get(); int len = 0; do { int digit = (int) (num & 0xF); num >>>= 4; numberBuffer[len++] = (byte) HEXADECIMAL[digit]; } while (--minDigits > 0 | num > 0); for (int i = len - 1; i >= 0; i--) out.writeByte(numberBuffer[i]); }
default B append8bit(@NotNull String cs) throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { return append8bit(cs, 0, cs.length()); }
public static void appendDateMillis(@org.jetbrains.annotations.NotNull @NotNull ByteStringAppender b, long timeInMS) throws BufferOverflowException { DateCache dateCache = dateCacheTL.get(); if (dateCache == null) { dateCacheTL.set(dateCache = new DateCache()); } long date = timeInMS / 86400000; if (dateCache.lastDay != date) { dateCache.lastDateStr = dateCache.dateFormat.format(new Date(timeInMS)).getBytes(ISO_8859_1); dateCache.lastDay = date; } else { assert dateCache.lastDateStr != null; } b.write(dateCache.lastDateStr); }
public static void appendBase10(@org.jetbrains.annotations.NotNull @NotNull ByteStringAppender out, long num) throws IllegalArgumentException, BufferOverflowException { if (num < 0) { if (num == Long.MIN_VALUE) { out.write(MIN_VALUE_TEXT); return; } out.writeByte((byte) '-'); num = -num; } appendLong0(out, num); }
public static void appendBase10(@org.jetbrains.annotations.NotNull @NotNull ByteStringAppender out, int num) throws IllegalArgumentException, BufferOverflowException { if (num < 0) { out.writeByte((byte) '-'); if (num == Integer.MIN_VALUE) { appendLong0(out, -(long) num); return; } num = -num; } appendInt0(out, num); }
/** * Append a String to the Bytes in ISO-8859-1 * * @param cs to write * @return this * @throws BufferOverflowException If the string as too large to write in the capacity available * @throws BufferUnderflowException if the capacity of the underlying buffer was exceeded */ @NotNull default B append8bit(@NotNull CharSequence cs) throws BufferOverflowException, BufferUnderflowException, IndexOutOfBoundsException { return append8bit(cs, 0, cs.length()); }
@Override public void write(char[] cbuf, int off, int len) throws IOException { for (int i = 0; i < len; i++) out.append(cbuf[i + off]); } }
public static void append(@org.jetbrains.annotations.NotNull @NotNull ByteStringAppender out, long num, int base) throws IllegalArgumentException, BufferOverflowException { if (num < 0) { if (num == Long.MIN_VALUE) { if (base == 10) out.write(MIN_VALUE_TEXT); else out.write(Long.toString(Long.MIN_VALUE, base)); return; } out.writeByte((byte) '-'); num = -num; } if (num == 0) { out.writeByte((byte) '0'); } else { switch (base) { case 10: appendLong0(out, num); break; case 16: appendBase16(out, num, 1); break; default: out.write(Long.toString(num, base)); break; } } }
@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; }