Extract a partition of a byte array. - Java java.lang

Java examples for java.lang:byte Array

Description

Extract a partition of a byte array.

Demo Code

/*/*from   w ww.  j  a v a2 s.  c  o  m*/
 * @(#) ByteArrayUtils.java
 *
 * This code is part of the JAviator project: javiator.cs.uni-salzburg.at
 * Copyright (c) 2009  Clemens Krainer
 *
 * This program is free software; 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
//package com.java2s;

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

    /**
     * Extract a partition of a byte array.
     * 
     * @param a the byte array
     * @param from the index of the first byte in the given byte array
     * @param length the number of bytes to extract
     * @return the result as a byte array
     */
    public static byte[] partition(byte[] a, int from, int length) {
        byte[] b = new byte[length];
        for (int k = 0; k < length; k++)
            b[k] = a[from + k];
        return b;
    }
}

Related Tutorials