Java Array Swap swapBytes(byte[] dataToSwap, int wordByteLength)

Here you can find the source of swapBytes(byte[] dataToSwap, int wordByteLength)

Description

Return an array which contains the same data as dataToSwap but with byte reversed.

License

Open Source License

Parameter

Parameter Description
dataToSwap a parameter
wordByteLength a parameter

Declaration

public static final byte[] swapBytes(byte[] dataToSwap, int wordByteLength) throws IndexOutOfBoundsException 

Method Source Code

//package com.java2s;
/**/*from   w  ww.j a v  a2s  . c  o  m*/
 * stratum-proxy is a proxy supporting the crypto-currency stratum pool mining
 * protocol.
 * Copyright (C) 2014  Stratehm (stratehm@hotmail.com)
 *
 * 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 3 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 multipool-stats-backend. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Return an array which contains the same data as dataToSwap but with byte
     * reversed. The word length is used to know on which base the swap has to
     * be done.
     * 
     * The wordByteLength has to be a multiple of the length of the given
     * dataToSwap, else an IndexOutOfBoundException is thrown.
     * 
     * For example, with parameters: dataToSwap = B0 B1 B2 B3 B4 B5 B6 B7
     * 
     * wordByteLength=1 return B0 B1 B2 B3 B4 B5 B6 B7
     * 
     * wordByteLength=2 return B1 B0 B3 B2 B5 B4 B7 B6
     * 
     * wordByteLength=4 return B3 B2 B1 B0 B7 B6 B5 B4
     * 
     * wordByteLength=8 return B7 B6 B5 B4 B3 B2 B1 B0
     * 
     * @param dataToSwap
     * @param wordByteLength
     * @return
     */
    public static final byte[] swapBytes(byte[] dataToSwap, int wordByteLength) throws IndexOutOfBoundsException {
        byte[] result = null;
        if (dataToSwap != null) {

            if (wordByteLength < 1 || dataToSwap.length % wordByteLength > 0) {
                throw new IndexOutOfBoundsException(
                        "The wordByteLength is not a multiple of input data. wordByteLength=" + wordByteLength
                                + ", inputDataSize=" + dataToSwap.length);
            }

            result = new byte[dataToSwap.length];

            for (int i = 0; i < dataToSwap.length; i += wordByteLength) {
                for (int resultOffset = 0, inputOffset = wordByteLength
                        - 1; resultOffset < wordByteLength; resultOffset++, inputOffset--) {
                    result[i + resultOffset] = dataToSwap[i + inputOffset];
                }
            }
        }

        return result;
    }
}

Related

  1. swapByteArray(byte[] source)
  2. swapByteOrder(byte[] a)
  3. swapByteOrder32(byte[] data, int ofs, int len)
  4. swapBytes(byte[] bytes)
  5. swapBytes(byte[] data)
  6. swapBytesAt(byte[] bytes, int p1, int p2)
  7. swapElements(final E[] arr, final int idx1, final int idx2)
  8. swapEndianFormat(byte[] b)
  9. swapInt(byte[] b, int i)