1 package org.rocksdb.util; 2 3 import java.nio.ByteBuffer; 4 5 import static java.nio.charset.StandardCharsets.UTF_8; 6 7 public class ByteUtil { 8 9 /** 10 * Convert a String to a UTF-8 byte array. 11 * 12 * @param str the string 13 * 14 * @return the byte array. 15 */ bytes(final String str)16 public static byte[] bytes(final String str) { 17 return str.getBytes(UTF_8); 18 } 19 20 /** 21 * Compares the first {@code count} bytes of two areas of memory. Returns 22 * zero if they are the same, a value less than zero if {@code x} is 23 * lexically less than {@code y}, or a value greater than zero if {@code x} 24 * is lexically greater than {@code y}. Note that lexical order is determined 25 * as if comparing unsigned char arrays. 26 * 27 * Similar to <a href="https://github.com/gcc-mirror/gcc/blob/master/libiberty/memcmp.c">memcmp.c</a>. 28 * 29 * @param x the first value to compare with 30 * @param y the second value to compare against 31 * @param count the number of bytes to compare 32 * 33 * @return the result of the comparison 34 */ memcmp(final ByteBuffer x, final ByteBuffer y, final int count)35 public static int memcmp(final ByteBuffer x, final ByteBuffer y, 36 final int count) { 37 for (int idx = 0; idx < count; idx++) { 38 final int aa = x.get(idx) & 0xff; 39 final int bb = y.get(idx) & 0xff; 40 if (aa != bb) { 41 return aa - bb; 42 } 43 } 44 return 0; 45 } 46 } 47