PageRenderTime 62ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/components/forks/poi/src/loci/poi/hpsf/ClassID.java

http://github.com/openmicroscopy/bioformats
Java | 265 lines | 92 code | 44 blank | 129 comment | 22 complexity | 3129629c610f15f535d25cc1bd961c5e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hpsf;
  38. import loci.poi.util.HexDump;
  39. /**
  40. * <p>Represents a class ID (16 bytes). Unlike other little-endian
  41. * type the {@link ClassID} is not just 16 bytes stored in the wrong
  42. * order. Instead, it is a double word (4 bytes) followed by two
  43. * words (2 bytes each) followed by 8 bytes.</p>
  44. *
  45. * @author Rainer Klute <a
  46. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  47. * @version $Id: ClassID.java 489730 2006-12-22 19:18:16Z bayard $
  48. * @since 2002-02-09
  49. */
  50. public class ClassID
  51. {
  52. /**
  53. * <p>The bytes making out the class ID in correct order,
  54. * i.e. big-endian.</p>
  55. */
  56. protected byte[] bytes;
  57. /**
  58. * <p>Creates a {@link ClassID} and reads its value from a byte
  59. * array.</p>
  60. *
  61. * @param src The byte array to read from.
  62. * @param offset The offset of the first byte to read.
  63. */
  64. public ClassID(final byte[] src, final int offset)
  65. {
  66. read(src, offset);
  67. }
  68. /**
  69. * <p>Creates a {@link ClassID} and initializes its value with
  70. * 0x00 bytes.</p>
  71. */
  72. public ClassID()
  73. {
  74. bytes = new byte[LENGTH];
  75. for (int i = 0; i < LENGTH; i++)
  76. bytes[i] = 0x00;
  77. }
  78. /** <p>The number of bytes occupied by this object in the byte
  79. * stream.</p> */
  80. public static final int LENGTH = 16;
  81. /**
  82. * @return The number of bytes occupied by this object in the byte
  83. * stream.
  84. */
  85. public int length()
  86. {
  87. return LENGTH;
  88. }
  89. /**
  90. * <p>Gets the bytes making out the class ID. They are returned in
  91. * correct order, i.e. big-endian.</p>
  92. *
  93. * @return the bytes making out the class ID.
  94. */
  95. public byte[] getBytes()
  96. {
  97. return bytes;
  98. }
  99. /**
  100. * <p>Sets the bytes making out the class ID.</p>
  101. *
  102. * @param bytes The bytes making out the class ID in big-endian format. They
  103. * are copied without their order being changed.
  104. */
  105. public void setBytes(final byte[] bytes)
  106. {
  107. for (int i = 0; i < this.bytes.length; i++)
  108. this.bytes[i] = bytes[i];
  109. }
  110. /**
  111. * <p>Reads the class ID's value from a byte array by turning
  112. * little-endian into big-endian.</p>
  113. *
  114. * @param src The byte array to read from
  115. *
  116. * @param offset The offset within the <var>src</var> byte array
  117. *
  118. * @return A byte array containing the class ID.
  119. */
  120. public byte[] read(final byte[] src, final int offset)
  121. {
  122. bytes = new byte[16];
  123. /* Read double word. */
  124. bytes[0] = src[3 + offset];
  125. bytes[1] = src[2 + offset];
  126. bytes[2] = src[1 + offset];
  127. bytes[3] = src[0 + offset];
  128. /* Read first word. */
  129. bytes[4] = src[5 + offset];
  130. bytes[5] = src[4 + offset];
  131. /* Read second word. */
  132. bytes[6] = src[7 + offset];
  133. bytes[7] = src[6 + offset];
  134. /* Read 8 bytes. */
  135. for (int i = 8; i < 16; i++)
  136. bytes[i] = src[i + offset];
  137. return bytes;
  138. }
  139. /**
  140. * <p>Writes the class ID to a byte array in the
  141. * little-endian format.</p>
  142. *
  143. * @param dst The byte array to write to.
  144. *
  145. * @param offset The offset within the <var>dst</var> byte array.
  146. *
  147. * @exception ArrayStoreException if there is not enough room for the class
  148. * ID 16 bytes in the byte array after the <var>offset</var> position.
  149. */
  150. public void write(final byte[] dst, final int offset)
  151. throws ArrayStoreException
  152. {
  153. /* Check array size: */
  154. if (dst.length < 16)
  155. throw new ArrayStoreException
  156. ("Destination byte[] must have room for at least 16 bytes, " +
  157. "but has a length of only " + dst.length + ".");
  158. /* Write double word. */
  159. dst[0 + offset] = bytes[3];
  160. dst[1 + offset] = bytes[2];
  161. dst[2 + offset] = bytes[1];
  162. dst[3 + offset] = bytes[0];
  163. /* Write first word. */
  164. dst[4 + offset] = bytes[5];
  165. dst[5 + offset] = bytes[4];
  166. /* Write second word. */
  167. dst[6 + offset] = bytes[7];
  168. dst[7 + offset] = bytes[6];
  169. /* Write 8 bytes. */
  170. for (int i = 8; i < 16; i++)
  171. dst[i + offset] = bytes[i];
  172. }
  173. /**
  174. * <p>Checks whether this <code>ClassID</code> is equal to another
  175. * object.</p>
  176. *
  177. * @param o the object to compare this <code>PropertySet</code> with
  178. * @return <code>true</code> if the objects are equal, else
  179. * <code>false</code>.</p>
  180. */
  181. public boolean equals(final Object o)
  182. {
  183. if (o == null || !(o instanceof ClassID))
  184. return false;
  185. final ClassID cid = (ClassID) o;
  186. if (bytes.length != cid.bytes.length)
  187. return false;
  188. for (int i = 0; i < bytes.length; i++)
  189. if (bytes[i] != cid.bytes[i])
  190. return false;
  191. return true;
  192. }
  193. /**
  194. * @see Object#hashCode()
  195. */
  196. public int hashCode()
  197. {
  198. return new String(bytes).hashCode();
  199. }
  200. /**
  201. * <p>Returns a human-readable representation of the Class ID in standard
  202. * format <code>"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"</code>.</p>
  203. *
  204. * @return String representation of the Class ID represented by this object.
  205. */
  206. public String toString()
  207. {
  208. StringBuffer sbClassId = new StringBuffer(38);
  209. sbClassId.append('{');
  210. for (int i = 0; i < 16; i++)
  211. {
  212. sbClassId.append(HexDump.toHex(bytes[i]));
  213. if (i == 3 || i == 5 || i == 7 || i == 9)
  214. sbClassId.append('-');
  215. }
  216. sbClassId.append('}');
  217. return sbClassId.toString();
  218. }
  219. }