Skip to content
Snippets Groups Projects
Utils.java 1.45 KiB
Newer Older
  • Learn to ignore specific revisions
  • package quantization.utilities;
    
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Utils {
        public static byte[] readFileBytes(final String path) throws FileNotFoundException {
            FileInputStream fileStream = new FileInputStream(path);
            try {
                return fileStream.readAllBytes();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return new byte[0];
        }
    
    
        public static <T> boolean arrayContains(final T[] array, final T element) {
            for (int i = 0; i < array.length; i++) {
                if (array[i].equals(element))
                    return true;
            }
            return false;
        }
    
        public static boolean arrayContainsToIndex(final int[] array, final int toIndex, final int element) {
            for (int i = 0; i < toIndex; i++) {
                if (array[i] == element)
                    return true;
            }
            return false;
        }
    
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
        public static int[] convertU16BytesToInt(final byte[] bytes) {
            assert (bytes.length % 2 == 0);
            int[] values = new int[bytes.length / 2];
    
    
            int index = 0;
            for (int i = 0; i < bytes.length; i += 2) {
    
                final int value = (int) (((bytes[i] & 0xff) << 8) | (bytes[i + 1] & 0xff));
                if (value > 0) {
    
    Vojtech Moravec's avatar
    Vojtech Moravec committed
                    values[index++] = value;
                    continue;
                }
                values[index++] = value;