Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static final int FIFTH_BITMASK = 16;
    public static final int FIRST_BITMASK = 1;
    public static final int FORMAT_SINT16 = 34;
    public static final int FORMAT_SINT32 = 36;
    public static final int FORMAT_SINT8 = 33;
    public static final int FORMAT_UINT16 = 18;
    public static final int FORMAT_UINT32 = 20;
    public static final int FORMAT_UINT8 = 17;
    public static final int FOURTH_BITMASK = 8;
    public static final int SECOND_BITMASK = 2;
    public static final int SIXTH_BITMASK = 32;

    public static Integer getIntValue(byte[] value, int format, int position) {
        if (value == null || (format & 15) + position > value.length) {
            return null;
        }
        switch (format) {
        case FORMAT_UINT8 /*17*/:
            return Integer.valueOf(value[position] & 255);
        case FORMAT_UINT16 /*18*/:
            return Integer.valueOf(add(value[position], value[position + FIRST_BITMASK]));
        case FORMAT_UINT32 /*20*/:
            return Integer.valueOf(add(value[position], value[position + FIRST_BITMASK],
                    value[position + SECOND_BITMASK], value[position + 3]));
        case FORMAT_SINT8 /*33*/:
            return Integer.valueOf(signed(value[position] & 255, FOURTH_BITMASK));
        case FORMAT_SINT16 /*34*/:
            return Integer.valueOf(signed(add(value[position], value[position + FIRST_BITMASK]), FIFTH_BITMASK));
        case FORMAT_SINT32 /*36*/:
            return Integer.valueOf(signed(add(value[position], value[position + FIRST_BITMASK],
                    value[position + SECOND_BITMASK], value[position + 3]), SIXTH_BITMASK));
        default:
            return null;
        }
    }

    private static int add(byte byte1, byte byte2) {
        return (byte1 & 255) + ((byte2 & 255) << FOURTH_BITMASK);
    }

    private static int add(byte byte1, byte byte2, byte byte3, byte byte4) {
        return (((byte1 & 255) + ((byte2 & 255) << FOURTH_BITMASK)) + ((byte3 & 255) << FIFTH_BITMASK))
                + ((byte4 & 255) << 24);
    }

    private static int signed(int value, int length) {
        if (((FIRST_BITMASK << (length - 1)) & value) != 0) {
            return ((FIRST_BITMASK << (length - 1)) - (((FIRST_BITMASK << (length - 1)) - 1) & value)) * -1;
        }
        return value;
    }
}