Here you can find the source of toByteArray(int[] data)
Parameter | Description |
---|---|
data | the int[] to convert |
public static byte[] toByteArray(int[] data)
//package com.java2s; /*/*from w ww . ja v a 2 s . c om*/ * This file is part of the LIRe project: http://www.semanticmetadata.net/lire * LIRe 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 2 of the License, or * (at your option) any later version. * * LIRe 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 LIRe; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * We kindly ask you to refer the following paper in any publication mentioning Lire: * * Lux Mathias, Savvas A. Chatzichristofis. Lire: Lucene Image Retrieval ? * An Extensible Java CBIR Library. In proceedings of the 16th ACM International * Conference on Multimedia, pp. 1085-1088, Vancouver, Canada, 2008 * * http://doi.acm.org/10.1145/1459359.1459577 * * Copyright statement: * -------------------- * (c) 2002-2011 by Mathias Lux (mathias@juggle.at) * http://www.semanticmetadata.net/lire */ public class Main { /** * Convenience method to transform an int[] array to a byte array for serialization. * * @param data the int[] to convert * @return the resulting byte[] 4 times in size (4 bytes per int) */ public static byte[] toByteArray(int[] data) { byte[] tmp, result = new byte[data.length * 4]; for (int i = 0; i < data.length; i++) { tmp = toBytes(data[i]); System.arraycopy(tmp, 0, result, i * 4, 4); } return result; } /** * Convenience method for creating a byte array from a float array. * * @param data the input float array * @return a byte array for serialization. */ public static byte[] toByteArray(float[] data) { byte[] tmp, result = new byte[data.length * 4]; for (int i = 0; i < data.length; i++) { tmp = toBytes(data[i]); System.arraycopy(tmp, 0, result, i * 4, 4); } return result; } /** * Convenience method for creating a byte array from a double array. * * @param data the input float array * @return a byte array for serialization. */ public static byte[] toByteArray(double[] data) { byte[] tmp, result = new byte[data.length * 8]; for (int i = 0; i < data.length; i++) { tmp = toBytes(data[i]); System.arraycopy(tmp, 0, result, i * 8, 8); } return result; } /** * Converts an int to a byte array with 4 elements. Used to put ints into a byte[] payload in a convenient * and fast way by shifting without using streams (which is kind of slow). <br/> * Taken from http://www.daniweb.com/code/snippet216874.html * * @param data the int to convert * @return the resulting byte[] array * @see net.semanticmetadata.lire.utils.SerializationUtils#toInt(byte[]) */ public static byte[] toBytes(int data) { return new byte[] { (byte) ((data >> 24) & 0xff), (byte) ((data >> 16) & 0xff), (byte) ((data >> 8) & 0xff), (byte) ((data >> 0) & 0xff), }; } /** * Converts a long to a byte[] array.<br/> * Taken from http://www.daniweb.com/software-development/java/code/216874 * * @param data the long to convert * @return the resulting byte[] array * @see #toLong(byte[]) */ 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 >> 0) & 0xff), }; } /** * Converts a float to a byte array with 4 elements. Used to put floats into a byte[] payload in a convenient * and fast way by shifting without using streams (which is kind of slow). Use * {@link net.semanticmetadata.lire.utils.SerializationUtils#toFloat(byte[])} to decode. * * @param data the float to convert * @return the resulting byte array * @see net.semanticmetadata.lire.utils.SerializationUtils#toFloat(byte[]) */ public static byte[] toBytes(float data) { return toBytes(Float.floatToRawIntBits(data)); } /** * Converts a double to a byte array with 4 elements. Used to put doubles into a byte[] payload in a convenient * and fast way by shifting without using streams (which is kind of slow). Use * {@link net.semanticmetadata.lire.utils.SerializationUtils#toDouble(byte[])} to decode. Note that there is a loss * in precision as the double is converted to a float in the course of conversion. * * @param data the double to convert * @return the resulting byte array * @see net.semanticmetadata.lire.utils.SerializationUtils#toDouble(byte[]) */ public static byte[] toBytes(double data) { return toBytes(Double.doubleToLongBits(data)); } }