ByteUtil { public static void writeLongAsAscii(final byte[] outputBuffer, final int offset, final long valueParam) { long value = valueParam; if (value < 0 || value > 9000000000000000000L) { throw new IllegalArgumentException("Value out of range: value=" + value); } int outputBufferIndex = offset + 18; long factor = 1; while (value >= factor) { long remainder = value % (factor * 10); outputBuffer[outputBufferIndex--] = (byte) ((remainder / factor) + 48); value -= remainder; factor *= 10; } final byte zero = (byte) 48; while (outputBufferIndex >= offset) { outputBuffer[outputBufferIndex--] = zero; } }  static void writeLongAsAscii(final byte[] outputBuffer, final int offset, final long valueParam); static boolean isAsciiDigit(final byte asciiCharacter); static String formatIntAsIpAddress(final int address); static void formatIntAsIpAddress(final StringBuilder buffer, final int address); static long packLongWithUnsignedShortInts(final int int1, final int int2, final int int3, final int int4); static boolean isEqual(final byte[] lhs, final int lhsOffset, final byte[] rhs, final int rhsOffset, final int length); static int readIntFromAscii(final byte[] asciiBuffer, final int offset, final int length); static long readLongFromAscii(final byte[] asciiBuffer, final int offset, final int length); static boolean isInteger(final byte[] asciiBuffer, final int offset, final int length); static void replace(final byte[] buffer, final int offset, final int length,
                               final byte target, final byte replacement); static void asciiEncode(final CharSequence chars, final int charOffset,
                                   final byte[] buffer, final int bufferOffset, final int length); static final int MAX_UNSIGNED_SHORT; }
@Test public void shouldWriteLongAsAscii() { final byte[] longAsAsciiBytes = new byte[19]; final int offset = 0; ByteUtil.writeLongAsAscii(longAsAsciiBytes, offset, 0L); assertArrayEquals("0000000000000000000".getBytes(StandardCharsets.US_ASCII), longAsAsciiBytes); ByteUtil.writeLongAsAscii(longAsAsciiBytes, offset, 7L); assertArrayEquals("0000000000000000007".getBytes(StandardCharsets.US_ASCII), longAsAsciiBytes); ByteUtil.writeLongAsAscii(longAsAsciiBytes, offset, 900000000000000000L); assertArrayEquals("0900000000000000000".getBytes(StandardCharsets.US_ASCII), longAsAsciiBytes); ByteUtil.writeLongAsAscii(longAsAsciiBytes, offset, 99999999999999999L); assertArrayEquals("0099999999999999999".getBytes(StandardCharsets.US_ASCII), longAsAsciiBytes); ByteUtil.writeLongAsAscii(longAsAsciiBytes, offset, 899999999999999999L); assertArrayEquals("0899999999999999999".getBytes(StandardCharsets.US_ASCII), longAsAsciiBytes); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenOutOfRangeForMaxLong() { final byte[] longAsAscii = new byte[19]; ByteUtil.writeLongAsAscii(longAsAscii, 0, Long.MAX_VALUE); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenOutOfRangeForMinLong() { final byte[] longAsAscii = new byte[19]; ByteUtil.writeLongAsAscii(longAsAscii, 0, Long.MIN_VALUE); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowExceptionWhenJustOutOfRange() { final byte[] longAsAscii = new byte[19]; ByteUtil.writeLongAsAscii(longAsAscii, 0, 9000000000000000001L); }