/*
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package org.jsmpp.util;

/**
 * The octet util.
 * 
 * @author uudashr
 * @version 1.0
 */
public class OctetUtil {

    private OctetUtil() {
        throw new InstantiationError("This class must not be instantiated");
    }

    /**
     * Convert integer (4 octets) value to bytes.
     * 
     * @param value as 4 bytes representing integer in bytes.
     * @return bytes in big endian format
     */
    public static byte[] intToBytes(int value) {
        byte[] result = new byte[4];
        result[0] = (byte)(value >> 24 & 0xff);
        result[1] = (byte)(value >> 16 & 0xff);
        result[2] = (byte)(value >> 8 & 0xff);
        result[3] = (byte)(value & 0xff);
        return result;
    }

    /**
     * Convert integer (2 octets) value to bytes.
     * 
     * @param value as 2 bytes representing short in bytes.
     * @return bytes in big endian format
     */
    public static byte[] shortToBytes(short value) {
        byte[] result = new byte[2];
        result[0] = (byte)(value >> 8 & 0xff);
        result[1] = (byte)(value & 0xff);
        return result;
    }

    /**
     * 32 bit.
     * 
     * @param bytes in big endian format
     * @return integer
     */
    public static int bytesToInt(byte[] bytes) {
        return bytesToInt(bytes, 0);
    }

    /**
     * 32 bit.
     * 
     * @param bytes in big endian format
     * @param offset the offset in bytes
     * @return integer
     */
    public static int bytesToInt(byte[] bytes, int offset) {
        // 
        int result = 0x00000000;

        int length;
        // maximum byte size for int data type is 4
        if (bytes.length - offset < 4) {
            length = bytes.length - offset;
        } else {
            length = 4;
        }

        int end = offset + length - 1;
        for (int i = 0; i < length; i++) {
            // TODO uudashr: CHECK FOR IMPROVEMENT
            result |= (bytes[end - i] & 0xff) << (8 * i);
        }
        return result;
    }

    /**
     * 16 bit.
     * 
     * @param bytes in big endian format
     * @return short
     */
    public static short bytesToShort(byte[] bytes) {
        return bytesToShort(bytes, 0);
    }

    /**
     * 16 bit.
     * 
     * @param bytes in big endian format
     * @param offset the offset in bytes
     * @return short
     */
    public static short bytesToShort(byte[] bytes, int offset) {
        short result = 0x0000;
        int end = offset + 1;
        for (int i = 0; i < 2; i++) {
            result |= (bytes[end - i] & 0xff) << (8 * i);
        }
        return result;
    }
}

@@ -62,17 +62,16 @@
 cense%22);
- 
 %0A * you 
@@ -177,17 +177,16 @@
 se at%0A *
- 
 %0A *    h
@@ -229,17 +229,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -553,17 +553,16 @@
 ense.%0A *
- 
 %0A */%0Apac
@@ -819,19 +819,8 @@
 eger
- (4 octets)
  val
@@ -822,32 +822,43 @@
 r value to bytes
+ (4 octets)
 .%0A     * %0A     *
@@ -1291,26 +1291,13 @@
 ert 
-integer (2 octets)
+short
  val
@@ -1303,24 +1303,36 @@
 lue to bytes
+ (2 octets) 
 .%0A     * %0A  
@@ -1654,32 +1654,54 @@
 %0A%0A    /**%0A     *
+ Construct an int from
  32 bit.%0A     * 
@@ -1881,22 +1881,55 @@
 %0A     * 
-32 bit
+Construct an int from 32 bit (4 octets)
 .%0A     *
@@ -2108,20 +2108,8 @@
 ) %7B%0A
-        // %0A
     
@@ -2130,38 +2130,8 @@
  = 0
-x00000000;%0A%0A        int length
 ;%0A  
@@ -2193,51 +2193,10 @@
    i
-f (bytes.length - offset %3C 4) %7B%0A           
+nt
  len
@@ -2201,16 +2201,25 @@
 ength = 
+Math.min(
 bytes.le
@@ -2235,61 +2235,13 @@
 fset
-;%0A        %7D else %7B%0A            length = 4;%0A        %7D%0A
+, 4);
 %0A   
@@ -2475,38 +2475,72 @@
 %0A    /**%0A     * 
-16 bit
+Construct a short from 16 bit (2 octets)
 .%0A     * %0A     *
@@ -2717,22 +2717,56 @@
 %0A     * 
-16 bit
+Construct a short from 16 bit (2 octets)
 .%0A     *
@@ -2970,13 +2970,8 @@
 t = 
-0x000
 0;%0A 

Use Math.min to calculate the length