Here you can find the source of toByteArray(byte byteArray)
public static byte[] toByteArray(byte byteArray)
//package com.java2s; /**/*from w w w . j av a 2s. c o m*/ * Copyright 2007 DFKI GmbH. * All Rights Reserved. Use is subject to license terms. * * This file is part of MARY TTS. * * MARY TTS is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, version 3 of the License. * * 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ public class Main { public static byte[] toByteArray(byte byteArray) { return new byte[] { byteArray }; } public static byte[] toByteArray(byte[] byteArray) { return byteArray; } public static byte[] toByteArray(short data) { return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByteArray(short[] data) { if (data == null) return null; byte[] byts = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) System.arraycopy(toByteArray(data[i]), 0, byts, i * 2, 2); return byts; } public static byte[] toByteArray(char data) { return new byte[] { (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByteArray(char[] data) { if (data == null) return null; byte[] byts = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) System.arraycopy(toByteArray(data[i]), 0, byts, i * 2, 2); return byts; } public static byte[] toByteArray(int data) { return new byte[] { (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } public static byte[] toByteArray(int[] data) { if (data == null) return null; byte[] byts = new byte[data.length * 4]; for (int i = 0; i < data.length; i++) System.arraycopy(toByteArray(data[i]), 0, byts, i * 4, 4); return byts; } public static byte[] toByteArray(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 >> 0) & 0xff), }; } public static byte[] toByteArray(long[] data) { if (data == null) return null; byte[] byts = new byte[data.length * 8]; for (int i = 0; i < data.length; i++) System.arraycopy(toByteArray(data[i]), 0, byts, i * 8, 8); return byts; } public static byte[] toByteArray(float data) { return toByteArray(Float.floatToRawIntBits(data)); } public static byte[] toByteArray(float[] data) { if (data == null) return null; byte[] byts = new byte[data.length * 4]; for (int i = 0; i < data.length; i++) System.arraycopy(toByteArray(data[i]), 0, byts, i * 4, 4); return byts; } public static byte[] toByteArray(double data) { return toByteArray(Double.doubleToRawLongBits(data)); } public static byte[] toByteArray(double[] data) { if (data == null) return null; byte[] byts = new byte[data.length * 8]; for (int i = 0; i < data.length; i++) System.arraycopy(toByteArray(data[i]), 0, byts, i * 8, 8); return byts; } public static byte[] toByteArray(boolean data) { return new byte[] { (byte) (data ? 0x01 : 0x00) }; } public static byte[] toByteArray(boolean[] data) { if (data == null) return null; int len = data.length; byte[] lena = toByteArray(len); byte[] byts = new byte[lena.length + (len / 8) + (len % 8 != 0 ? 1 : 0)]; System.arraycopy(lena, 0, byts, 0, lena.length); for (int i = 0, j = lena.length, k = 7; i < data.length; i++) { byts[j] |= (data[i] ? 1 : 0) << k--; if (k < 0) { j++; k = 7; } } return byts; } public static byte[] toByteArray(String data) { return (data == null) ? null : data.getBytes(); } public static byte[] toByteArray(String[] data) { if (data == null) return null; int totalLength = 0; int bytesPos = 0; byte[] dLen = toByteArray(data.length); totalLength += dLen.length; int[] sLens = new int[data.length]; totalLength += (sLens.length * 4); byte[][] strs = new byte[data.length][]; for (int i = 0; i < data.length; i++) { if (data[i] != null) { strs[i] = toByteArray(data[i]); sLens[i] = strs[i].length; totalLength += strs[i].length; } else { sLens[i] = 0; strs[i] = new byte[0]; } } byte[] bytes = new byte[totalLength]; System.arraycopy(dLen, 0, bytes, 0, 4); byte[] bsLens = toByteArray(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; } }