Generates a subarray of a given byte array. - Java java.lang

Java examples for java.lang:byte Array

Description

Generates a subarray of a given byte array.

Demo Code

/*******************************************************************************
 * Copyright (c) 2008 JCrypTool Team and Contributors
 * /*from  ww w . j av  a  2s  . c o  m*/
 * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] input = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int start = 2;
        System.out.println(java.util.Arrays
                .toString(subArray(input, start)));
    }

    /**
     * Generates a subarray of a given byte array.
     * 
     * @param input - the input byte array
     * @param start - the start index
     * @param end - the end index
     * @return a subarray of <code>input</code>, ranging from <code>start</code> to <code>end</code>
     */
    public static byte[] subArray(byte[] input, int start, int end) {
        byte[] result = new byte[end - start];
        System.arraycopy(input, start, result, 0, end - start);
        return result;
    }

    /**
     * Generates a subarray of a given byte array.
     * 
     * @param input - the input byte array
     * @param start - the start index
     * @return a subarray of <code>input</code>, ranging from <code>start</code> to the end of the array
     */
    public static byte[] subArray(byte[] input, int start) {
        return subArray(input, start, input.length);
    }
}

Related Tutorials