Java Array Swap swapByteArray(byte[] source)

Here you can find the source of swapByteArray(byte[] source)

Description

Swaps the given byte array order.

License

Mozilla Public License

Parameter

Parameter Description
source Byte array to swap.

Exception

Parameter Description
NullPointerException if source == null.

Return

The swapped byte array.

Declaration

public static byte[] swapByteArray(byte[] source) 

Method Source Code

//package com.java2s;
/**/*from ww  w. ja va2s.  c o  m*/
 * Copyright (c) 2014-2016 Digi International Inc.,
 * All rights not expressly granted are reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
 * =======================================================================
 */

public class Main {
    /**
     * Swaps the given byte array order.
     * 
     * @param source Byte array to swap.
     * 
     * @return The swapped byte array.
     * 
     * @throws NullPointerException if {@code source == null}.
     */
    public static byte[] swapByteArray(byte[] source) {
        if (source == null)
            throw new NullPointerException("Source cannot be null.");

        if (source.length == 0)
            return new byte[0];

        byte[] swapped = new byte[source.length];
        for (int i = 0; i < source.length; i++)
            swapped[source.length - i - 1] = source[i];
        return swapped;
    }
}

Related

  1. swapAndPrint(int[] arr, int m, int n)
  2. swapArray(int[] v, int i, int j)
  3. swapArrayStr(String[] arr)
  4. swapArrayValues(int i1, int i2, int[] indices)
  5. swapBlocks(byte[] array, int fromA, int toA, int fromB, int toB)
  6. swapByteOrder(byte[] a)
  7. swapByteOrder32(byte[] data, int ofs, int len)
  8. swapBytes(byte[] bytes)
  9. swapBytes(byte[] data)