All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.forstdb.util.ByteUtil Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 0.1.2-beta
Show newest version
//  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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy