Java Byte Array Create toBytes(byte data)

Here you can find the source of toBytes(byte data)

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(byte data) 

Method Source Code

//package com.java2s;
/*//from ww  w .  jav a 2 s .c  o  m
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static byte[] toBytes(byte data) {
        return new byte[] { data };
    }

    public static byte[] toBytes(byte[] data) {
        return data;
    }

    /**
     * Convert a SHORT into a two-byte array.
     *
     * @param data a short number
     * @return a two-byte array
     */
    public static byte[] toBytes(short data) {
        return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data) & 0xff), };
    }

    public static byte[] toBytes(short[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 2, 2);
        }
        return bytes;
    }

    public static byte[] toBytes(char data) {
        return new byte[] { (byte) ((data >> 8) & 0xff), (byte) (data & 0xff), };
    }

    public static byte[] toBytes(char[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 2, 2);
        }
        return bytes;
    }

    public static byte[] toBytes(int data) {
        return new byte[] { (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff),
                (byte) ((data) & 0xff) };
    }

    public static byte[] toBytes(int[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 4];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 4, 4);
        }
        return bytes;
    }

    public static byte[] toBytes(long data) {
        return new byte[] { (byte) ((data >> 56) & 0xff), (byte) ((data >> 48) & 0xff),
                (byte) ((data >> 40) & 0xff), (byte) ((data >> 32) & 0xff), (byte) ((data >> 24) & 0xff),
                (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data) & 0xff), };
    }

    public static byte[] toBytes(long[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 8];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 8, 8);
        }
        return bytes;
    }

    public static byte[] toBytes(float data) {
        return toBytes(Float.floatToRawIntBits(data));
    }

    public static byte[] toBytes(float[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 4];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 4, 4);
        }
        return bytes;
    }

    public static byte[] toBytes(Double data) {
        return toBytes(Double.doubleToRawLongBits(data));
    }

    public static byte[] toBytes(Double[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 8];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 8, 8);
        }
        return bytes;
    }

    public static byte[] toBytes(double data) {
        return toBytes(Double.doubleToRawLongBits(data));
    }

    public static byte[] toBytes(double[] data) {
        if (data == null) {
            return null;
        }
        byte[] bytes = new byte[data.length * 8];
        for (int i = 0; i < data.length; i++) {
            System.arraycopy(toBytes(data[i]), 0, bytes, i * 8, 8);
        }
        return bytes;
    }

    public static byte[] toBytes(boolean data) {
        return new byte[] { (byte) (data ? 0x01 : 0x00) }; // bool -> {1 byte}
    }

    public static byte[] toBytes(boolean[] data) {
        /**
         * Advanced Technique: The byte array contains information about how many
         * boolean values are involved, so the exact array is returned when later
         * decoded.
         */
        if (data == null) {
            return null;
        }
        int len = data.length;
        byte[] lena = toBytes(len); // int conversion; length array = lena
        byte[] bytes = new byte[lena.length + (len / 8) + (len % 8 != 0 ? 1 : 0)];
        // (Above) length-array-length + sets-of-8-booleans +? byte-for-remainder
        System.arraycopy(lena, 0, bytes, 0, lena.length);
        // (Below) algorithm by Matthew Cudmore: boolean[] -> bits -> byte[]
        for (int i = 0, j = lena.length, k = 7; i < data.length; i++) {
            bytes[j] |= (data[i] ? 1 : 0) << k--;
            if (k < 0) {
                j++;
                k = 7;
            }
        }
        return bytes;
    }

    public static byte[] toBytes(String data) {
        return (data == null) ? null : data.getBytes();
    }

    public static byte[] toBytes(String[] data) {
        /**
         * Advanced Technique: Generates an indexed byte array which contains the
         * array of Strings. The byte array contains information about the number of
         * Strings and the length of each String.
         */
        if (data == null) {
            return null;
        }
        // ---------- flags:
        int totalLength = 0; // Measure length of final byte array
        int bytesPos = 0; // Used later
        // ----- arrays:
        byte[] dLen = toBytes(data.length); // byte array of data length
        totalLength += dLen.length;
        int[] sLens = new int[data.length]; // String lengths = sLens
        totalLength += (sLens.length * 4);
        byte[][] strs = new byte[data.length][]; // array of String bytes
        // ----- pack strs:
        for (int i = 0; i < data.length; i++) {
            if (data[i] != null) {
                strs[i] = toBytes(data[i]);
                sLens[i] = strs[i].length;
                totalLength += strs[i].length;
            } else {
                sLens[i] = 0;
                strs[i] = new byte[0]; // prevent null entries
            }
        }
        byte[] bytes = new byte[totalLength]; // final array
        System.arraycopy(dLen, 0, bytes, 0, 4);
        byte[] bsLens = toBytes(sLens); // byte version of String sLens
        System.arraycopy(bsLens, 0, bytes, 4, bsLens.length);
        // -----
        bytesPos += 4 + bsLens.length; // mark position
        // -----
        for (byte[] sba : strs) {
            System.arraycopy(sba, 0, bytes, bytesPos, sba.length);
            bytesPos += sba.length;
        }
        return bytes;
    }
}

Related

  1. ToByteArrayString(byte[] bytes)
  2. toByteArrayV4(String address)
  3. toByteArrayV6(String address)
  4. toByteInfo(long bytes)
  5. toBytes(boolean[] input)
  6. toBytes(byte[] b, int offset, long val)
  7. toBytes(byte[] buffer, int pos, String string)
  8. toBytes(byte[] ipAddress, int port)
  9. toBytes(Byte[] values)