/** * Writing out to bytes ... just to save some time and space. * * @return */ @Override public byte[] getByteArrayRepresentation() { byte[] result = new byte[descriptor.length * 4 + 4 * 4]; byte[] tmp; tmp = SerializationUtils.toBytes(scale); for (int j = 0; j < 4; j++) result[j] = tmp[j]; tmp = SerializationUtils.toBytes(orientation); for (int j = 0; j < 4; j++) result[4 + j] = tmp[j]; tmp = SerializationUtils.toBytes(location[0]); for (int j = 0; j < 4; j++) result[8 + j] = tmp[j]; tmp = SerializationUtils.toBytes(location[1]); for (int j = 0; j < 4; j++) result[12 + j] = tmp[j]; for (int i = 16; i < result.length; i += 4) { tmp = SerializationUtils.toBytes(descriptor[(i - 16) / 4]); for (int j = 0; j < 4; j++) { result[i + j] = tmp[j]; } } return result; }
/** * Writing out to bytes ... just to save some time and space. * * @return */ @Override public byte[] getByteArrayRepresentation() { byte[] result = new byte[descriptor.length * 4 + 4 * 4]; byte[] tmp; tmp = SerializationUtils.toBytes(scale); for (int j = 0; j < 4; j++) result[j] = tmp[j]; tmp = SerializationUtils.toBytes(orientation); for (int j = 0; j < 4; j++) result[4 + j] = tmp[j]; tmp = SerializationUtils.toBytes(location[0]); for (int j = 0; j < 4; j++) result[8 + j] = tmp[j]; tmp = SerializationUtils.toBytes(location[1]); for (int j = 0; j < 4; j++) result[12 + j] = tmp[j]; for (int i = 16; i < result.length; i += 4) { tmp = SerializationUtils.toBytes(descriptor[(i - 16) / 4]); for (int j = 0; j < 4; j++) { result[i + j] = tmp[j]; } } return result; }
/** * 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)); }
/** * 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 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)); }
/** * 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 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(short[] data) { byte[] tmp, result = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) { tmp = toBytes(data[i]); System.arraycopy(tmp, 0, result, i * 2, 2); } 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; }
/** * 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 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; }
/** * 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; }
/** * 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)); }
/** * 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(short[] data) { byte[] tmp, result = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) { tmp = toBytes(data[i]); System.arraycopy(tmp, 0, result, i * 2, 2); } 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; }
public static void writeClusters(Cluster[] clusters, String path) throws IOException { File file = new File(path); if(file.exists()) { System.out.println("File " + path + " already exists and will be overwritten!!"); } FileOutputStream fout = new FileOutputStream(file); fout.write(SerializationUtils.toBytes(clusters.length)); fout.write(SerializationUtils.toBytes((clusters[0].getMean()).length)); for (Cluster cluster : clusters) { fout.write(cluster.getByteRepresentation()); } fout.close(); }
public static void writeClusters(Cluster[] clusters, String path) throws IOException { File file = new File(path); if(file.exists()) { System.out.println("File " + path + " already exists and will be overwritten!!"); } FileOutputStream fout = new FileOutputStream(file); fout.write(SerializationUtils.toBytes(clusters.length)); fout.write(SerializationUtils.toBytes((clusters[0].getMean()).length)); for (Cluster cluster : clusters) { fout.write(cluster.getByteRepresentation()); } fout.close(); }