Java Convert via ByteBuffer toByteArray(int[] data, boolean bigEndian)

Here you can find the source of toByteArray(int[] data, boolean bigEndian)

Description

to Byte Array

License

Open Source License

Declaration

public static byte[] toByteArray(int[] data, boolean bigEndian) 

Method Source Code

//package com.java2s;
/**//from   ww  w  .j a v  a  2s . c o  m
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 * 
 * Change History - most recent changes go on top of previous changes
 *
 * ArrayUtils.java
 *
 * Who   Date       Description
 * ====  =========  ======================================================================
 * WY    06Apr2015  Added reverse(byte[]) to reverse byte array elements
 * WY    06Jan2015  Added reverse() to reverse array elements
 * WY    10Dec2014  Moved reverseBits() from IMGUtils to here along with BIT_REVERSE_TABLE
 * WY    08Dec2014  Fixed bug for flipEndian() with more than 32 bit sample data 
 * WY    07Dec2014  Changed method names for byte array to other array types conversion
 * WY    07Dec2014  Added new methods to work with floating point TIFF images
 * WY    03Dec2014  Added byteArrayToFloatArray() and byteArrayToDoubleArray()
 * WY    25Nov2014  Added removeDuplicates() to sort and remove duplicates from int arrays
 * WY    12Nov2014  Changed the argument sequence for flipEndian()
 * WY    11Nov2014  Changed flipEndian() to include scan line stride to skip bits
 * WY    11Nov2014  Added toNBits() to convert byte array to nBits data unit
 * WY    28Oct2014  Added flipEndian() to work with TIFTweaker mergeTiffImagesEx()
 */

import java.nio.ByteOrder;

import java.nio.ByteBuffer;

import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;

public class Main {
    public static byte[] toByteArray(int value) {
        return new byte[] { (byte) value, (byte) (value >>> 8), (byte) (value >>> 16), (byte) (value >>> 24) };
    }

    public static byte[] toByteArray(int[] data, boolean bigEndian) {

        ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 4);

        if (bigEndian) {
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        } else {
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        }

        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(data);

        byte[] array = byteBuffer.array();

        return array;
    }

    public static byte[] toByteArray(long[] data, boolean bigEndian) {

        ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 8);

        if (bigEndian) {
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        } else {
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        }

        LongBuffer longBuffer = byteBuffer.asLongBuffer();
        longBuffer.put(data);

        byte[] array = byteBuffer.array();

        return array;
    }

    public static byte[] toByteArray(short value) {
        return new byte[] { (byte) value, (byte) (value >>> 8) };
    }

    public static byte[] toByteArray(short[] data, boolean bigEndian) {

        ByteBuffer byteBuffer = ByteBuffer.allocate(data.length * 2);

        if (bigEndian) {
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        } else {
            byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        }

        ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
        shortBuffer.put(data);

        byte[] array = byteBuffer.array();

        return array;
    }
}

Related

  1. toByteArray(InputStream input, boolean nioCopy)
  2. toByteArray(int i)
  3. toByteArray(int integer)
  4. toByteArray(int value)
  5. toByteArray(int value)
  6. toByteArray(int[] intArray)
  7. toByteArray(long[] data)
  8. ToByteArray(long[] data)
  9. toByteArray(ReadableByteChannel channel)