Android Byte Array Sub Array Get subarray(byte[] in, int a, int b)

Here you can find the source of subarray(byte[] in, int a, int b)

Description

Creates and returns a new array with the values of the original from index a to index b and of size (b-a).

License

Open Source License

Parameter

Parameter Description
in input array
a first index
b last index

Return

a new array based on the desired range of the input

Declaration

public static byte[] subarray(byte[] in, int a, int b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  ww  w .jav  a  2  s.  c  o  m
     * Creates and returns a new array with the values of the original from index <code>a</code> to index <code>b</code>
     * and of size <code>(b-a)</code>.
     * @param in input array
     * @param a first index
     * @param b last index
     * @return a new array based on the desired range of the input
     */
    public static byte[] subarray(byte[] in, int a, int b) {
        if (b - a > in.length)
            return in;// TODO better error checking

        byte[] out = new byte[(b - a) + 1];

        for (int i = a; i <= b; i++) {
            out[i - a] = in[i];
        }
        return out;
    }
}

Related

  1. getBytes(byte[] data, int offset, int len)
  2. subByte(byte[] data, int start)
  3. subByte(byte[] data, int start, int end)