PageRenderTime 91ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Lire-0.9.3/src/main/java/net/semanticmetadata/lire/utils/SerializationUtils.java

https://bitbucket.org/kcnp/colordiscriptors
Java | 375 lines | 167 code | 28 blank | 180 comment | 20 complexity | e85d3f90787403dd03ac002ff4237cc5 MD5 | raw file
  1. /*
  2. * This file is part of the LIRe project: http://www.semanticmetadata.net/lire
  3. * LIRe is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * LIRe is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with LIRe; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *
  17. * We kindly ask you to refer the following paper in any publication mentioning Lire:
  18. *
  19. * Lux Mathias, Savvas A. Chatzichristofis. Lire: Lucene Image Retrieval –
  20. * An Extensible Java CBIR Library. In proceedings of the 16th ACM International
  21. * Conference on Multimedia, pp. 1085-1088, Vancouver, Canada, 2008
  22. *
  23. * http://doi.acm.org/10.1145/1459359.1459577
  24. *
  25. * Copyright statement:
  26. * --------------------
  27. * (c) 2002-2011 by Mathias Lux (mathias@juggle.at)
  28. * http://www.semanticmetadata.net/lire
  29. */
  30. package net.semanticmetadata.lire.utils;
  31. import java.util.Arrays;
  32. import java.util.Iterator;
  33. import java.util.LinkedList;
  34. import java.util.StringTokenizer;
  35. /**
  36. * Utility class for serialization issues.
  37. * Created by: Mathias Lux, mathias@juggle.at
  38. * Date: 19.03.2010
  39. * Time: 14:58:26
  40. */
  41. public class SerializationUtils {
  42. /**
  43. * Converts a byte array with 4 elements to an int. Used to put ints into a byte[] payload in a convenient
  44. * and fast way by shifting without using streams (which is kind of slow). <br/>
  45. * Taken from http://www.daniweb.com/code/snippet216874.html
  46. *
  47. * @param data the input byte array
  48. * @return the resulting int
  49. * @see net.semanticmetadata.lire.utils.SerializationUtils#toBytes(int)
  50. */
  51. public static int toInt(byte[] data) {
  52. if (data == null || data.length != 4) return 0x0;
  53. return (int) ( // NOTE: type cast not necessary for int
  54. (0xff & data[0]) << 24 |
  55. (0xff & data[1]) << 16 |
  56. (0xff & data[2]) << 8 |
  57. (0xff & data[3]) << 0
  58. );
  59. }
  60. /**
  61. * Converts an int to a byte array with 4 elements. Used to put ints into a byte[] payload in a convenient
  62. * and fast way by shifting without using streams (which is kind of slow). <br/>
  63. * Taken from http://www.daniweb.com/code/snippet216874.html
  64. *
  65. * @param data the int to convert
  66. * @return the resulting byte[] array
  67. * @see net.semanticmetadata.lire.utils.SerializationUtils#toInt(byte[])
  68. */
  69. public static byte[] toBytes(int data) {
  70. return new byte[]{
  71. (byte) ((data >> 24) & 0xff),
  72. (byte) ((data >> 16) & 0xff),
  73. (byte) ((data >> 8) & 0xff),
  74. (byte) ((data >> 0) & 0xff),
  75. };
  76. }
  77. /**
  78. * Converts a long to a byte[] array.<br/>
  79. * Taken from http://www.daniweb.com/software-development/java/code/216874
  80. *
  81. * @param data the long to convert
  82. * @return the resulting byte[] array
  83. * @see #toLong(byte[])
  84. */
  85. public static byte[] toBytes(long data) {
  86. return new byte[]{
  87. (byte) ((data >> 56) & 0xff),
  88. (byte) ((data >> 48) & 0xff),
  89. (byte) ((data >> 40) & 0xff),
  90. (byte) ((data >> 32) & 0xff),
  91. (byte) ((data >> 24) & 0xff),
  92. (byte) ((data >> 16) & 0xff),
  93. (byte) ((data >> 8) & 0xff),
  94. (byte) ((data >> 0) & 0xff),
  95. };
  96. }
  97. /**
  98. * Converts a byte[] array with size 8 to a long. <br/>
  99. * Taken from http://www.daniweb.com/software-development/java/code/216874
  100. *
  101. * @param data the byte[] array to convert
  102. * @return the resulting long.
  103. * @see #toBytes(long)
  104. */
  105. public static long toLong(byte[] data) {
  106. if (data == null || data.length != 8) return 0x0;
  107. // ----------
  108. return (long) (
  109. // (Below) convert to longs before shift because digits
  110. // are lost with ints beyond the 32-bit limit
  111. (long) (0xff & data[0]) << 56 |
  112. (long) (0xff & data[1]) << 48 |
  113. (long) (0xff & data[2]) << 40 |
  114. (long) (0xff & data[3]) << 32 |
  115. (long) (0xff & data[4]) << 24 |
  116. (long) (0xff & data[5]) << 16 |
  117. (long) (0xff & data[6]) << 8 |
  118. (long) (0xff & data[7]) << 0
  119. );
  120. }
  121. /**
  122. * Convenience method to transform an int[] array to a byte array for serialization.
  123. *
  124. * @param data the int[] to convert
  125. * @return the resulting byte[] 4 times in size (4 bytes per int)
  126. */
  127. public static byte[] toByteArray(int[] data) {
  128. byte[] tmp, result = new byte[data.length * 4];
  129. for (int i = 0; i < data.length; i++) {
  130. tmp = toBytes(data[i]);
  131. System.arraycopy(tmp, 0, result, i * 4, 4);
  132. }
  133. return result;
  134. }
  135. /**
  136. * Convenience method to create an int[] array from a byte[] array.
  137. *
  138. * @param data the byte[] array to decode
  139. * @return the decoded int[]
  140. */
  141. public static int[] toIntArray(byte[] data) {
  142. int[] result = new int[data.length / 4];
  143. byte[] tmp = new byte[4];
  144. for (int i = 0; i < result.length; i++) {
  145. System.arraycopy(data, i * 4, tmp, 0, 4);
  146. result[i] = toInt(tmp);
  147. }
  148. return result;
  149. }
  150. public static int[] toIntArray(byte[] in, int offset, int length) {
  151. int[] result = new int[(length >> 2)];
  152. byte[] tmp = new byte[4];
  153. for (int i = offset; i < length >> 2; i++) {
  154. System.arraycopy(in, offset + (i - offset) * 4, tmp, 0, 4);
  155. result[i] = toInt(tmp);
  156. }
  157. return result;
  158. }
  159. /**
  160. * Converts a float to a byte array with 4 elements. Used to put floats into a byte[] payload in a convenient
  161. * and fast way by shifting without using streams (which is kind of slow). Use
  162. * {@link net.semanticmetadata.lire.utils.SerializationUtils#toFloat(byte[])} to decode.
  163. *
  164. * @param data the float to convert
  165. * @return the resulting byte array
  166. * @see net.semanticmetadata.lire.utils.SerializationUtils#toFloat(byte[])
  167. */
  168. public static byte[] toBytes(float data) {
  169. return toBytes(Float.floatToRawIntBits(data));
  170. }
  171. /**
  172. * Converts a byte array with 4 elements to a float. Used to put floats into a byte[] payload in a convenient
  173. * and fast way by shifting without using streams (which is kind of slow). Use
  174. * {@link net.semanticmetadata.lire.utils.SerializationUtils#toBytes(float)} to encode.
  175. *
  176. * @param data the input byte array
  177. * @return the resulting float
  178. * @see net.semanticmetadata.lire.utils.SerializationUtils#toBytes(float)
  179. */
  180. public static float toFloat(byte[] data) {
  181. return Float.intBitsToFloat(toInt(data));
  182. }
  183. /**
  184. * Convenience method for creating a byte array from a float array.
  185. *
  186. * @param data the input float array
  187. * @return a byte array for serialization.
  188. */
  189. public static byte[] toByteArray(float[] data) {
  190. byte[] tmp, result = new byte[data.length * 4];
  191. for (int i = 0; i < data.length; i++) {
  192. tmp = toBytes(data[i]);
  193. System.arraycopy(tmp, 0, result, i * 4, 4);
  194. }
  195. return result;
  196. }
  197. /**
  198. * Convenience method for creating a float array from a byte array.
  199. *
  200. * @param data
  201. * @return
  202. */
  203. public static float[] toFloatArray(byte[] data) {
  204. float[] result = new float[data.length / 4];
  205. byte[] tmp = new byte[4];
  206. for (int i = 0; i < result.length; i++) {
  207. System.arraycopy(data, i * 4, tmp, 0, 4);
  208. result[i] = toFloat(tmp);
  209. }
  210. return result;
  211. }
  212. /**
  213. * Convenience method for creating a float array from a byte array.
  214. *
  215. * @param in
  216. * @param offset
  217. * @param length
  218. * @return
  219. */
  220. public static float[] toFloatArray(byte[] in, int offset, int length) {
  221. float[] result = new float[length / 4];
  222. byte[] tmp = new byte[4];
  223. for (int i = offset; i < length / 4; i++) {
  224. System.arraycopy(in, (i - offset) * 4 + offset, tmp, 0, 4);
  225. result[i] = toFloat(tmp);
  226. }
  227. return result;
  228. }
  229. /**
  230. * Converts a double to a byte array with 4 elements. Used to put doubles into a byte[] payload in a convenient
  231. * and fast way by shifting without using streams (which is kind of slow). Use
  232. * {@link net.semanticmetadata.lire.utils.SerializationUtils#toDouble(byte[])} to decode. Note that there is a loss
  233. * in precision as the double is converted to a float in the course of conversion.
  234. *
  235. * @param data the double to convert
  236. * @return the resulting byte array
  237. * @see net.semanticmetadata.lire.utils.SerializationUtils#toDouble(byte[])
  238. */
  239. public static byte[] toBytes(double data) {
  240. return toBytes(Double.doubleToLongBits(data));
  241. }
  242. /**
  243. * Converts a byte array with 4 elements to a double. Used to put doubles into a byte[] payload in a convenient
  244. * and fast way by shifting without using streams (which is kind of slow). Use
  245. * {@link net.semanticmetadata.lire.utils.SerializationUtils#toBytes(double)} to encode. Note that there is a loss
  246. * in precision as the double is converted to a float in the course of conversion.
  247. *
  248. * @param data the input byte array
  249. * @return the resulting float
  250. * @see net.semanticmetadata.lire.utils.SerializationUtils#toBytes(double)
  251. */
  252. public static double toDouble(byte[] data) {
  253. return Double.longBitsToDouble(toLong(data));
  254. }
  255. /**
  256. * Convenience method for creating a byte array from a double array.
  257. *
  258. * @param data the input float array
  259. * @return a byte array for serialization.
  260. */
  261. public static byte[] toByteArray(double[] data) {
  262. byte[] tmp, result = new byte[data.length * 8];
  263. for (int i = 0; i < data.length; i++) {
  264. tmp = toBytes(data[i]);
  265. System.arraycopy(tmp, 0, result, i * 8, 8);
  266. }
  267. return result;
  268. }
  269. /**
  270. * Convenience method for creating a double array from a byte array.
  271. *
  272. * @param data
  273. * @return
  274. */
  275. public static double[] toDoubleArray(byte[] data) {
  276. double[] result = new double[data.length / 8];
  277. byte[] tmp = new byte[8];
  278. for (int i = 0; i < result.length; i++) {
  279. System.arraycopy(data, i * 8, tmp, 0, 8);
  280. result[i] = toDouble(tmp);
  281. }
  282. return result;
  283. }
  284. /**
  285. * Convenience method for creating a double array from a byte array.
  286. *
  287. * @param data
  288. * @param length
  289. * @param offset
  290. * @return
  291. */
  292. public static double[] toDoubleArray(byte[] data, int offset, int length) {
  293. double[] result = new double[length / 8];
  294. byte[] tmp = new byte[8];
  295. for (int i = offset; i < length / 8; i++) {
  296. System.arraycopy(data, (i - offset) * 8 + offset, tmp, 0, 8);
  297. result[i] = toDouble(tmp);
  298. }
  299. return result;
  300. }
  301. /**
  302. * Convenience method for creating a String from an array.
  303. *
  304. * @param array
  305. * @return
  306. */
  307. public static String arrayToString(int[] array) {
  308. return Arrays.toString(array).replace('[', ' ').replace(']', ' ').replace(',', ' ');
  309. // StringBuilder sb = new StringBuilder(256);
  310. // for (int i = 0; i < array.length; i++) {
  311. // sb.append(array[i]);
  312. // sb.append(' ');
  313. // }
  314. // return sb.toString().trim();
  315. }
  316. /**
  317. * Parses and returns a double array from a Sting with an arbitrary number of doubles.
  318. *
  319. * @param data
  320. * @return
  321. */
  322. public static double[] doubleArrayFromString(String data) {
  323. double[] result = null;
  324. LinkedList<Double> tmp = new LinkedList<Double>();
  325. data = data.replace('[', ' ');
  326. data = data.replace(']', ' ');
  327. data = data.replace(',', ' ');
  328. StringTokenizer st = new StringTokenizer(data);
  329. while (st.hasMoreTokens())
  330. tmp.add(Double.parseDouble(st.nextToken()));
  331. result = new double[tmp.size()];
  332. int i = 0;
  333. for (Iterator<Double> iterator = tmp.iterator(); iterator.hasNext(); ) {
  334. Double next = iterator.next();
  335. result[i] = next;
  336. i++;
  337. }
  338. return result;
  339. }
  340. public static double[] toDoubleArray(float[] d) {
  341. double[] result = new double[d.length];
  342. for (int i = 0; i < result.length; i++) {
  343. result[i] = (double) d[i];
  344. }
  345. return result;
  346. }
  347. }