Java Byte Array Copy copyBytes(final byte[] source, final int sOffset, final byte[] destination, final int dOffset, final int length)

Here you can find the source of copyBytes(final byte[] source, final int sOffset, final byte[] destination, final int dOffset, final int length)

Description

copy Bytes

License

Open Source License

Parameter

Parameter Description
source the byte array to copy from
sOffset offset in the source array (ie first byte copied is source[sOffset]
destination the array to copy into
dOffset offset in the destination array (ie first byte overwritten is destination[dOffset]
length the number of bytes to copy

Return

the number of bytes copied

Declaration

public static int copyBytes(final byte[] source, final int sOffset, final byte[] destination, final int dOffset,
        final int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/* w  w w .  ja va  2 s  .co m*/
     * @param source      the byte array to copy from
     * @param sOffset     offset in the source array (ie first byte copied is source[sOffset]
     * @param destination the array to copy into
     * @param dOffset     offset in the destination array (ie first byte overwritten is destination[dOffset]
     * @param length      the number of bytes to copy
     * @return the number of bytes copied
     */
    public static int copyBytes(final byte[] source, final int sOffset, final byte[] destination, final int dOffset,
            final int length) {
        requireSourceLength(source, sOffset, length);
        requireDestinationLength(destination, dOffset, length);
        for (int i = 0; i < length; ++i) {
            destination[i + dOffset] = source[i + sOffset];
        }
        return length;
    }

    /**
     * Helper method to check the Contract precondition of read methods
     */
    private static void requireSourceLength(final byte[] source, final int offset, final int length) {
        if (source.length < offset + length - 1) {
            throw new IllegalArgumentException("Not enough data available in source array:\n have " + source.length
                    + " bytes, need " + (offset + length));
        }
    }

    /**
     * Helper method to check the Contract precondition of write methods
     */
    private static void requireDestinationLength(final byte[] destination, final int offset, final int length) {
        if (destination.length < offset + length - 1) {
            throw new IllegalArgumentException("Not enough room available in destination array:\n have "
                    + destination.length + " bytes, need " + (offset + length));
        }
    }
}

Related

  1. copyBytes(byte[] results, int offset, int int32)
  2. copyBytes(byte[] source, byte[] dest, int sourceFrom, int copyLength, int destFrom)
  3. copyBytes(byte[] src)
  4. copyBytes(byte[] src, byte[] target)
  5. copyBytes(byte[] src, int start, int end)
  6. copyBytes(int[] srcArray, int srcPos, byte[] destBytes)
  7. copyBytes(long sourceOffset, byte[] source, int sourceOff, int sourceLength, long destOffset, byte[] dest, int destLength)
  8. copyBytesAtOffset(byte[] dst, byte[] src, int offset)
  9. copyBytesIntoByteArray(byte[] dest, byte[] src)