Copy block of data from source array - Java java.nio.file

Java examples for java.nio.file:Path

Description

Copy block of data from source array

Demo Code

/*/* w  w  w.  ja v a 2  s  .co m*/
 * Copyright (c) 2015, Alachisoft. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    /**
     * Copy block of data from source array
     *
     * @param copyFrom Source array
     * @param startIndex Start index in the source array from where copy begins
     * @param endIndex End index, until which the bytes are copied
     * @return Resultant array
     */
    public static byte[] CopyPartial(byte[] copyFrom, int startIndex,
            int endIndex) {
        byte[] copyIn = new byte[endIndex - startIndex];

        for (int i = startIndex, count = 0; i < endIndex; i++, count++) {
            copyIn[count] = copyFrom[i];
        }

        return copyIn;
    }

    /**
     * Copy block of data from source array
     *
     * @param copyFrom Source array
     * @param startIndex Start index in the source array from where copy begins
     * @param endIndex End index, until which the bytes are copied
     * @return Resultant array
     */
    public static void CopyPartial(byte[] copyFrom, byte[] copyTo,
            int startIndex, int endIndex) {
        for (int i = startIndex, count = 0; i < endIndex; i++, count++) {
            copyTo[count] = copyFrom[i];
        }
    }
}

Related Tutorials