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