org.forstdb.util.ByteUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of forstjni Show documentation
Show all versions of forstjni Show documentation
ForSt fat jar with modifications specific for Apache Flink that contains .so files for linux32 and linux64 (glibc and musl-libc), jnilib files
for Mac OSX, and a .dll for Windows x64.
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
package org.forstdb.util;
import java.nio.ByteBuffer;
import static java.nio.charset.StandardCharsets.UTF_8;
public class ByteUtil {
/**
* Convert a String to a UTF-8 byte array.
*
* @param str the string
*
* @return the byte array.
*/
public static byte[] bytes(final String str) {
return str.getBytes(UTF_8);
}
/**
* Compares the first {@code count} bytes of two areas of memory. Returns
* zero if they are the same, a value less than zero if {@code x} is
* lexically less than {@code y}, or a value greater than zero if {@code x}
* is lexically greater than {@code y}. Note that lexical order is determined
* as if comparing unsigned char arrays.
*
* Similar to memcmp.c.
*
* @param x the first value to compare with
* @param y the second value to compare against
* @param count the number of bytes to compare
*
* @return the result of the comparison
*/
public static int memcmp(final ByteBuffer x, final ByteBuffer y,
final int count) {
for (int idx = 0; idx < count; idx++) {
final int aa = x.get(idx) & 0xff;
final int bb = y.get(idx) & 0xff;
if (aa != bb) {
return aa - bb;
}
}
return 0;
}
}