BufferUtils { public static void putHexInt(ByteBuffer buffer, int n) { if (n < 0) { buffer.put((byte) '-'); if (n == Integer.MIN_VALUE) { buffer.put((byte) (0x7f & '8')); buffer.put((byte) (0x7f & '0')); buffer.put((byte) (0x7f & '0')); buffer.put((byte) (0x7f & '0')); buffer.put((byte) (0x7f & '0')); buffer.put((byte) (0x7f & '0')); buffer.put((byte) (0x7f & '0')); buffer.put((byte) (0x7f & '0')); return; } n = -n; } if (n < 0x10) { buffer.put(DIGIT[n]); } else { boolean started = false; for (int hexDivisor : hexDivisors) { if (n < hexDivisor) { if (started) buffer.put((byte) '0'); continue; } started = true; int d = n / hexDivisor; buffer.put(DIGIT[d]); n = n - d * hexDivisor; } } }  static ByteBuffer allocate(int capacity); static ByteBuffer allocateDirect(int capacity); static void clear(ByteBuffer buffer); static void clearToFill(ByteBuffer buffer); static int flipToFill(ByteBuffer buffer); static void flipToFlush(ByteBuffer buffer, int position); static byte[] toArray(ByteBuffer buffer); static byte[] toArray(Collection<ByteBuffer> buffers); static boolean isTheEmptyBuffer(ByteBuffer buf); static boolean isEmpty(ByteBuffer buf); static boolean hasContent(ByteBuffer buf); static boolean isFull(ByteBuffer buf); static int length(ByteBuffer buffer); static int space(ByteBuffer buffer); static boolean compact(ByteBuffer buffer); static int put(ByteBuffer from, ByteBuffer to); static int flipPutFlip(ByteBuffer from, ByteBuffer to); static void append(ByteBuffer to, byte[] b, int off, int len); static void append(ByteBuffer to, byte b); static int append(ByteBuffer to, ByteBuffer b); static int fill(ByteBuffer to, byte[] b, int off, int len); static void readFrom(File file, ByteBuffer buffer); static void readFrom(InputStream is, int needed, ByteBuffer buffer); static void writeTo(ByteBuffer buffer, OutputStream out); static String toString(ByteBuffer buffer); static String toUTF8String(ByteBuffer buffer); static String toString(ByteBuffer buffer, Charset charset); static String toString(List<ByteBuffer> buffers, Charset charset); static String toString(ByteBuffer buffer, int position, int length, Charset charset); static int toInt(ByteBuffer buffer); static int toInt(ByteBuffer buffer, int position, int length); static int takeInt(ByteBuffer buffer); static long toLong(ByteBuffer buffer); static void putHexInt(ByteBuffer buffer, int n); static void putDecInt(ByteBuffer buffer, int n); static void putDecLong(ByteBuffer buffer, long n); static ByteBuffer toBuffer(int value); static ByteBuffer toBuffer(long value); static ByteBuffer toBuffer(String s); static ByteBuffer toBuffer(String s, Charset charset); static ByteBuffer toBuffer(byte[] array); static ByteBuffer toBuffer(byte[] array, int offset, int length); static ByteBuffer toDirectBuffer(String s); static ByteBuffer toDirectBuffer(String s, Charset charset); static ByteBuffer toMappedBuffer(File file); @Deprecated static boolean isMappedBuffer(ByteBuffer buffer); static String toSummaryString(ByteBuffer buffer); static String toDetailString(ByteBuffer[] buffer); static String toIDString(ByteBuffer buffer); static String toDetailString(ByteBuffer buffer); static String toHexSummary(ByteBuffer buffer); static String toHexString(ByteBuffer buffer); static void putCRLF(ByteBuffer buffer); static boolean isPrefix(ByteBuffer prefix, ByteBuffer buffer); static ByteBuffer ensureCapacity(ByteBuffer buffer, int capacity); static ByteBuffer addCapacity(ByteBuffer buffer, int capacity); static ByteBuffer merge(List<ByteBuffer> buffers); static final ByteBuffer EMPTY_BUFFER; }
@Test void testPutHexInt() { int[] val = { 0, 42, 43, -44, -45, -2147483648, 2147483647 }; String[] str = { "0", "2A", "2B", "-2C", "-2D", "-80000000", "7FFFFFFF" }; ByteBuffer buffer = ByteBuffer.allocate(50); for (int i = 0; i < val.length; i++) { BufferUtils.clearToFill(buffer); BufferUtils.putHexInt(buffer, val[i]); BufferUtils.flipToFlush(buffer, 0); assertEquals(str[i], BufferUtils.toString(buffer), "t" + i); } }