PageRenderTime 315ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/pride-inspector/branches/development/src/main/java/uk/ac/ebi/pride/data/utils/BinaryDataUtils.java

http://pride-toolsuite.googlecode.com/
Java | 184 lines | 116 code | 16 blank | 52 comment | 7 complexity | 1b3fcb7e84c148f30dfaba9c2e1223ff MD5 | raw file
  1. package uk.ac.ebi.pride.data.utils;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import uk.ac.ebi.pride.term.CvTermReference;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.IOException;
  7. import java.nio.ByteBuffer;
  8. import java.nio.ByteOrder;
  9. import java.util.zip.DataFormatException;
  10. import java.util.zip.Deflater;
  11. import java.util.zip.Inflater;
  12. /**
  13. * This class need to be deleted in the future.
  14. * <p/>
  15. * Endianess is often simply referred to as byte order
  16. * big endian is the most significant byte first. for example: 149 -> 10010101
  17. * little endian is the least significant byte first.
  18. * <p/>
  19. * User: rwang
  20. * Date: 29-Mar-2010
  21. * Time: 11:58:24
  22. */
  23. public class BinaryDataUtils {
  24. private static final Logger logger = LoggerFactory.getLogger(BinaryDataUtils.class);
  25. /**
  26. * Convert a byte array to a number array
  27. *
  28. * @param arr byte array
  29. * @param dataType data type
  30. * @param order endianess
  31. * @return Number[] number array
  32. */
  33. public static Number[] toNumberArray(byte[] arr, CvTermReference dataType, ByteOrder order) {
  34. int numOfByte = getNumOfByte(dataType);
  35. int arrLength = arr.length;
  36. Number[] results = new Number[arrLength / numOfByte];
  37. ByteBuffer buffer = ByteBuffer.wrap(arr);
  38. buffer.order(order);
  39. try {
  40. for (int i = 0; i < arrLength; i += numOfByte) {
  41. Number num;
  42. switch (dataType) {
  43. case INT_32_BIT:
  44. num = buffer.getInt(i);
  45. break;
  46. case FLOAT_16_BIT: //ToDo: *** provide implementation here ; break;
  47. case FLOAT_32_BIT:
  48. num = buffer.getFloat(i);
  49. break;
  50. case INT_64_BIT:
  51. num = buffer.getLong(i);
  52. break;
  53. case FLOAT_64_BIT:
  54. num = buffer.getDouble(i);
  55. break;
  56. default:
  57. num = null;
  58. }
  59. results[i / numOfByte] = num;
  60. }
  61. } catch (Exception ex) {
  62. logger.error("Failed to byte array to number array: " + dataType.getName() + "\t" + order.toString());
  63. return new Number[0];
  64. }
  65. return results;
  66. }
  67. /**
  68. * Convert to double array
  69. *
  70. * @param arr byte array
  71. * @param dataType data type
  72. * @param order endianess
  73. * @return double[] double array
  74. */
  75. public static double[] toDoubleArray(byte[] arr, CvTermReference dataType, ByteOrder order) {
  76. Number[] numArr = toNumberArray(arr, dataType, order);
  77. double[] doubleArr = new double[numArr.length];
  78. for (int i = 0; i < numArr.length; i++) {
  79. doubleArr[i] = numArr[i].doubleValue();
  80. }
  81. return doubleArr;
  82. }
  83. /**
  84. * Get the number of bytes on a given data type
  85. *
  86. * @param dataType data type
  87. * @return int number of bytes
  88. */
  89. private static int getNumOfByte(CvTermReference dataType) {
  90. int numOfByte;
  91. switch (dataType) {
  92. case INT_32_BIT:
  93. numOfByte = 4;
  94. break;
  95. case FLOAT_16_BIT:
  96. numOfByte = 2;
  97. break;
  98. case FLOAT_32_BIT:
  99. numOfByte = 4;
  100. break;
  101. case INT_64_BIT:
  102. numOfByte = 8;
  103. break;
  104. case FLOAT_64_BIT:
  105. numOfByte = 8;
  106. break;
  107. default:
  108. numOfByte = -1;
  109. }
  110. return numOfByte;
  111. }
  112. /**
  113. * Decompress a compressed byte array
  114. *
  115. * @param compressedData compressed byte array
  116. * @return byte[] decompressed data
  117. */
  118. public static byte[] decompress(byte[] compressedData) {
  119. byte[] decompressedData;
  120. // using a ByteArrayOutputStream to not having to define the result array size beforehand
  121. Inflater decompressor = new Inflater();
  122. decompressor.setInput(compressedData);
  123. // Create an expandable byte array to hold the decompressed data
  124. ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
  125. byte[] buf = new byte[1024];
  126. while (!decompressor.finished()) {
  127. try {
  128. int count = decompressor.inflate(buf);
  129. bos.write(buf, 0, count);
  130. } catch (DataFormatException e) {
  131. throw new IllegalStateException("Encountered wrong data format " +
  132. "while trying to decompress binary data!", e);
  133. }
  134. }
  135. try {
  136. bos.close();
  137. } catch (IOException e) {
  138. logger.error("Error while closing byte array output stream");
  139. }
  140. // Get the decompressed data
  141. decompressedData = bos.toByteArray();
  142. if (decompressedData == null) {
  143. throw new IllegalStateException("Decompression of binary data prodeuced no result (null)!");
  144. }
  145. return decompressedData;
  146. }
  147. /**
  148. * Compress byte array
  149. *
  150. * @param uncompressedData uncompressed byte array
  151. * @return byte[] compressed byte array
  152. */
  153. public static byte[] compress(byte[] uncompressedData) {
  154. byte[] data;// Decompress the data
  155. // create a temporary byte array big enough to hold the compressed data
  156. // with the worst compression (the length of the initial (uncompressed) data)
  157. byte[] temp = new byte[uncompressedData.length];
  158. // compress
  159. Deflater compresser = new Deflater();
  160. compresser.setInput(uncompressedData);
  161. compresser.finish();
  162. int cdl = compresser.deflate(temp);
  163. // create a new array with the size of the compressed data (cdl)
  164. data = new byte[cdl];
  165. System.arraycopy(temp, 0, data, 0, cdl);
  166. return data;
  167. }
  168. }