Java Byte Array Copy copyBytes(long sourceOffset, byte[] source, int sourceOff, int sourceLength, long destOffset, byte[] dest, int destLength)

Here you can find the source of copyBytes(long sourceOffset, byte[] source, int sourceOff, int sourceLength, long destOffset, byte[] dest, int destLength)

Description

Byte arrays source and dest each begin at an offset in the common space.

License

Open Source License

Declaration

public static int copyBytes(long sourceOffset, byte[] source, int sourceOff, int sourceLength, long destOffset,
        byte[] dest, int destLength) 

Method Source Code

//package com.java2s;

public class Main {
    /**// w  w  w  . j  a  v  a  2s  . com
     * Byte arrays source and dest each begin at an offset in the common space.
     * If there is an overlap between dest and the first sourceLength elements of
     * the source, the overlapping elements are copied to dest. Returns count
     * of copied bytes.
     */
    public static int copyBytes(long sourceOffset, byte[] source, int sourceOff, int sourceLength, long destOffset,
            byte[] dest, int destLength) {

        if (sourceOff >= source.length) {
            return 0;
        }

        if (sourceOff + sourceLength > source.length) {
            sourceLength = source.length - sourceOff;
        }

        if (destLength > dest.length) {
            destLength = dest.length;
        }

        if (sourceOffset + sourceOff >= destOffset + destLength
                || sourceOffset + sourceOff + sourceLength <= destOffset) {
            return 0;
        }

        long sourceIndex = destOffset - sourceOffset;
        long destIndex = 0;
        int sourceLimit = sourceOff + sourceLength;

        if (sourceIndex >= 0) {
            if (sourceIndex < sourceOff) {
                sourceIndex = sourceOff;
            }
        } else {
            destIndex = -sourceIndex + sourceOff;
            sourceIndex = sourceOff;
        }

        sourceLength = sourceLimit - (int) sourceIndex;

        if (sourceLength > destLength - destIndex) {
            sourceLength = destLength - (int) destIndex;
        }

        System.arraycopy(source, (int) sourceIndex, dest, (int) destIndex, sourceLength);

        return sourceLength;
    }

    /**
     * Copy the source to dest, returning dest or an enlarged array of result is
     * larger than dest.
     */
    public static byte[] copyBytes(byte[] source, byte[] dest, int destOffset) {

        if (source.length + destOffset > dest.length) {
            byte[] newDest = new byte[source.length + destOffset];

            System.arraycopy(dest, 0, newDest, 0, dest.length);

            dest = newDest;
        }

        System.arraycopy(source, 0, dest, destOffset, source.length);

        return dest;
    }
}

Related

  1. copyBytes(byte[] src)
  2. copyBytes(byte[] src, byte[] target)
  3. copyBytes(byte[] src, int start, int end)
  4. copyBytes(final byte[] source, final int sOffset, final byte[] destination, final int dOffset, final int length)
  5. copyBytes(int[] srcArray, int srcPos, byte[] destBytes)
  6. copyBytesAtOffset(byte[] dst, byte[] src, int offset)
  7. copyBytesIntoByteArray(byte[] dest, byte[] src)
  8. copyBytesToString(byte[] src, int arg1, int arg2)
  9. copyBytesWithin(byte[] bytes, int target, int start, int end)