Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**
     * 
     * @param bytes
     * @param offset
     * @param length
     * @return
     * @throws UtilityException
     */
    public static byte[] sliceBytes(byte[] bytes, int offset, int length) throws Exception {
        if (null == bytes || bytes.length <= 0) {
            return null;
        }
        if (bytes.length < length) {
            throw new Exception("Array index out of bound : " + length);
        }
        if ((offset + length) > bytes.length) {
            throw new Exception("Array index out of bound : " + (offset + length));
        }
        if (offset < 0 || offset > bytes.length - 1) {
            throw new Exception("Array index out of bound : " + (offset + length));
        }

        byte[] newArray = new byte[length];
        for (int i = offset, j = 0; j < length; i++, j++) {
            newArray[j] = bytes[i];
        }
        return newArray;
    }
}