Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ClassID.java 6.5KB

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