Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public final static int getInt(byte[] buf) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (buf.length > 4) {
            throw new IllegalArgumentException("byte array size > 4 !");
        }
        int r = 0;
        for (int i = 0; i < buf.length; i++) {
            r <<= 8;
            r |= (buf[i] & 0x000000ff);
        }
        return r;
    }

    /**
     * Get integer value of bytes at given position in given buffer. (big-endian)
     * @param buf      : buffer that contain the bytes.
     * @param idxStart : index of the 1st byte to use.
     * @return integer value that is represented by the bytes in the buffer.
     */
    public final static int getInt(byte[] buf, int idxStart) {
        int r = 0; // Result.
        for (int i = 0; i < 4; i++) {
            r <<= 8;
            r |= (buf[idxStart + i] & 0xff);
        }
        return r;
    }
}