Android Byte Array Sub Array Get subByte(byte[] data, int start, int end)

Here you can find the source of subByte(byte[] data, int start, int end)

Description

sub Byte

Parameter

Parameter Description
data a parameter
start a parameter
end a parameter

Declaration

public static byte[] subByte(byte[] data, int start, int end) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w  w  w  .  j a  va  2s. c  o  m
     * @param data
     * @param start
     * @param end
     * @return
     */
    public static byte[] subByte(byte[] data, int start, int end) {
        int len = data.length;
        if (start >= len || end <= start || end < 1) {
            return null;
        }
        byte[] result = new byte[(end - start)];
        for (int i = start, j = 0; i < len; i++, j++) {
            if (i < end) {
                result[j] = data[i];
            } else {
                break;
            }
        }
        return result;
    }

    public static byte[] subByte(byte[] data, int start) {
        int len = data.length;
        if (start >= len) {
            return null;
        }
        if (start < 0) {
            start = len + start;
            if (start < 0) {
                return null;
            }
        }
        byte[] result = new byte[(len - start)];
        for (int i = start, j = 0; i < len; i++, j++) {
            result[j] = data[i];
        }
        return result;
    }
}

Related

  1. getBytes(byte[] data, int offset, int len)
  2. subByte(byte[] data, int start)
  3. subarray(byte[] in, int a, int b)