Java Array Slice sliceBytes(byte[] bytes, int offset, int length)

Here you can find the source of sliceBytes(byte[] bytes, int offset, int length)

Description

slice Bytes

License

Apache License

Parameter

Parameter Description
bytes a parameter
offset a parameter
length a parameter

Exception

Parameter Description
UtilityException an exception

Declaration

public static byte[] sliceBytes(byte[] bytes, int offset, int length) throws Exception 

Method Source Code

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

public class Main {
    /**//  w  w  w .  j a v a 2s .co m
     * 
     * @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;
    }
}

Related

  1. slice(T[] array, int index, int length)
  2. slice(T[] array, int start, int finish)
  3. slice(T[] items, int offset, int length)
  4. sliceArray(final String[] array, final int start)
  5. SliceByteArray(byte data[], int offset, int length)
  6. sliceFromFinalBoundary(Object[] sequence, Boolean[] boundaries)
  7. slicesFromAllBoundaries(Object[] sequence, Boolean[] boundaries)
  8. sliceStringArray(String[] a, Integer l)