Java Array Range Copy copyOfRange(byte[] original, int from, int to)

Here you can find the source of copyOfRange(byte[] original, int from, int to)

Description

Makes a new byte[] as a subset of an original given one.

License

Open Source License

Parameter

Parameter Description
original the original array.
from initial index position from where to copy.
to final index position until where to copy.

Return

the produced subset.

Declaration

public static byte[] copyOfRange(byte[] original, int from, int to) 

Method Source Code

//package com.java2s;
/**/*from   w  w w  .  j  a v  a2  s .c om*/
 * This file is part of JEMMA - http://jemma.energy-home.org
 * (C) Copyright 2013 Telecom Italia (http://www.telecomitalia.it)
 *
 * JEMMA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL) version 3
 * or later as published by the Free Software Foundation, which accompanies
 * this distribution and is available at http://www.gnu.org/licenses/lgpl.html
 *
 * JEMMA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License (LGPL) for more details.
 *
 */

public class Main {
    /**
     * Makes a new {@code byte[]} as a subset of an original given one.
     * 
     * @param original
     *            the original array.
     * @param from
     *            initial index position from where to copy.
     * @param to
     *            final index position until where to copy.
     * @return the produced subset.
     */
    public static byte[] copyOfRange(byte[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        byte[] copy = new byte[newLength];
        System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
        return copy;
    }

    public static byte[] copyOfRange(short[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        byte[] copy = new byte[newLength];
        System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
        return copy;
    }
}

Related

  1. copyOfArray(int[] a)
  2. copyOfRange(byte[] bytes, int offset, int len)
  3. copyOfRange(byte[] original, int from, int to)
  4. copyOfRange(byte[] original, int from, int to)
  5. copyOfRange(byte[] original, int from, int to)
  6. copyOfRange(byte[] original, int from, int to)
  7. copyOfRange(char[] original, int from, int to)
  8. copyOfRange(final byte[] array, final int startIndex, final int endIndex)
  9. copyOfRange(final byte[] original, final int from, final int to)