Java Convert via ByteBuffer ToByteArray(long[] data)

Here you can find the source of ToByteArray(long[] data)

Description

To Byte Array

License

Open Source License

Declaration

private static byte[] ToByteArray(long[] data) 

Method Source Code

//package com.java2s;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.util.ArrayList;
import java.util.List;

public class Main {
    private static char SPECIAL_CHAR = '\0';

    private static byte[] ToByteArray(long[] data) {
        List<Byte> result = new ArrayList<Byte>();

        for (int i = 0; i < data.length; i++) {
            byte[] bs = long2bytes(data[i]);
            for (int j = 0; j < 8; j++) {
                result.add(bs[j]);//from  www .  j  av a  2 s  .c  o  m
            }
        }

        while (result.get(result.size() - 1) == SPECIAL_CHAR) {
            result.remove(result.size() - 1);
        }

        byte[] ret = new byte[result.size()];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = result.get(i);
        }
        return ret;
    }

    public static byte[] long2bytes(long num) {
        ByteBuffer buffer = ByteBuffer.allocate(8).order(
                ByteOrder.LITTLE_ENDIAN);
        buffer.putLong(num);
        return buffer.array();
    }
}

Related

  1. toByteArray(int value)
  2. toByteArray(int value)
  3. toByteArray(int[] data, boolean bigEndian)
  4. toByteArray(int[] intArray)
  5. toByteArray(long[] data)
  6. toByteArray(ReadableByteChannel channel)
  7. toByteArray(String bits)
  8. ToByteArray(String hexString)
  9. toByteArray(String value)