Return a subarray of the byte array in parameter. : Array « Date Type « Android

Return a subarray of the byte array in parameter.

import java.nio.ByteBuffer;
import java.util.Random;

 * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol
 * This project contains two packages:
 * 1. jBittorrentAPI is the "client" part, i.e. it implements all classes needed to publish
 *    files, share them and download them.
 *    This package also contains example classes on how a developer could create new applications.
 * 2. trackerBT is the "tracker" part, i.e. it implements a all classes needed to run
 *    a Bittorrent tracker that coordinates peers exchanges. *
 * Copyright (C) 2007 Baptiste Dubuis, Artificial Intelligence Laboratory, EPFL
 * This file is part of
 * Java Bittorrent API is free software and a free user study set-up;
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * Java Bittorrent API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Java Bittorrent API; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * @version 1.0
 * @author Baptiste Dubuis
 * To contact the author:
 * email:
 * More information about Java Bittorrent API:

//package atorrentapi;

class Main {
   * Return a subarray of the byte array in parameter.
   * @param b
   *            The original array
   * @param offset
   *            Begin index of the subarray
   * @param length
   *            Length of the subarray
   * @return byte[]
  public static byte[] subArray(byte[] b, int offset, int length) {
    byte[] sub = new byte[length];
    for (int i = offset; i < offset + length; i++) {
      try {
        sub[i - offset] = b[i];
      } catch (Exception e) {

    return sub;


Related examples in the same category

1.Load string array value from strings.xml
2.Returns a clone of the specified array.
3.Tests two arrays for equality.
4.Transforms a list of Float values into an array of float.
5.Split the string into an array of strings using one of the separator in 'sep'.
6.Int array to HashSet
7.Array Iterator
8.Convert an array of floats to 16.16 fixed-point
9.Convert an array of 16.16 fixed-point values to floating point
10.Reverse an array
11.Using android.util.SparseArray
12.Join an array of Strings together.
13.Compare Arrays
14.Does Array contain a certain item
15.Create Array with Unique Value
16.Resize a Java array
17.Get Node value and save to ArrayList the photo file into a byte array
19.byte Array To Int
20.load 16 Bit PCM Raw Data File As Double Array
21.convert From Short Array To Double Array
22.convert From Double Array To Short Array
23.get Set From Array
24.Convert an array of floats to 16.16 fixed-point 2 To Byte Array 4
26.Convert a byte array to a boolean array. Bit 0 is represented with false, Bit 1 is represented with 1