You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassID.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 java.nio.ByteBuffer;
  17. import java.util.Arrays;
  18. import java.util.Locale;
  19. import java.util.Map;
  20. import java.util.UUID;
  21. import java.util.function.Supplier;
  22. import org.apache.poi.common.Duplicatable;
  23. import org.apache.poi.common.usermodel.GenericRecord;
  24. import org.apache.poi.util.GenericRecordUtil;
  25. import org.apache.poi.util.LittleEndianInput;
  26. import org.apache.poi.util.LittleEndianOutput;
  27. /**
  28. * Represents a class ID (16 bytes). Unlike other little-endian
  29. * type the {@link ClassID} is not just 16 bytes stored in the wrong
  30. * order. Instead, it is a double word (4 bytes) followed by two
  31. * words (2 bytes each) followed by 8 bytes.<p>
  32. *
  33. * The ClassID (or CLSID) is a UUID - see RFC 4122
  34. */
  35. public class ClassID implements Duplicatable, GenericRecord {
  36. /** The number of bytes occupied by this object in the byte stream. */
  37. public static final int LENGTH = 16;
  38. /**
  39. * The bytes making out the class ID in correct order, i.e. big-endian.
  40. */
  41. private final byte[] bytes = new byte[LENGTH];
  42. /**
  43. * Creates a ClassID and reads its value from a byte array.
  44. *
  45. * @param src The byte array to read from.
  46. * @param offset The offset of the first byte to read.
  47. */
  48. public ClassID(final byte[] src, final int offset) {
  49. read(src, offset);
  50. }
  51. /**
  52. * Creates a ClassID and initializes its value with 0x00 bytes.
  53. */
  54. public ClassID() {
  55. Arrays.fill(bytes, (byte)0);
  56. }
  57. /**
  58. * Clones the given ClassID
  59. *
  60. * @param other The ClassID to use a base for creating this one
  61. */
  62. public ClassID(ClassID other) {
  63. System.arraycopy(other.bytes, 0, bytes, 0, bytes.length);
  64. }
  65. /**
  66. * Creates a ClassID from a human-readable representation of the Class ID in standard
  67. * format {@code "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"}.
  68. *
  69. * @param externalForm representation of the Class ID represented by this object.
  70. */
  71. public ClassID(String externalForm) {
  72. String clsStr = externalForm.replaceAll("[{}-]", "");
  73. for (int i=0; i<clsStr.length(); i+=2) {
  74. bytes[i/2] = (byte)Integer.parseInt(clsStr.substring(i, i+2), 16);
  75. }
  76. }
  77. /**
  78. * Reads the ClassID from the input
  79. * @param lei the input (stream)
  80. */
  81. public ClassID(LittleEndianInput lei) {
  82. byte[] buf = bytes.clone();
  83. lei.readFully(buf);
  84. read(buf, 0);
  85. }
  86. /**
  87. * @return The number of bytes occupied by this object in the byte stream.
  88. */
  89. @SuppressWarnings("java:S1845")
  90. public int length() {
  91. return LENGTH;
  92. }
  93. /**
  94. * Gets the bytes making out the class ID. They are returned in correct order, i.e. big-endian.
  95. *
  96. * @return the bytes making out the class ID.
  97. */
  98. public byte[] getBytes() {
  99. return bytes;
  100. }
  101. /**
  102. * Sets the bytes making out the class ID.
  103. *
  104. * @param bytes The bytes making out the class ID in big-endian format. They
  105. * are copied without their order being changed.
  106. */
  107. public void setBytes(final byte[] bytes) {
  108. System.arraycopy(bytes, 0, this.bytes, 0, LENGTH);
  109. }
  110. /**
  111. * Reads the class ID's value from a byte array by turning little-endian into big-endian.
  112. *
  113. * @param src The byte array to read from
  114. * @param offset The offset within the {@code src} byte array
  115. * @return A byte array containing the class ID.
  116. */
  117. @SuppressWarnings("PointlessArithmeticExpression")
  118. public byte[] read(final byte[] src, final int offset) {
  119. /* Read double word. */
  120. bytes[0] = src[3 + offset];
  121. bytes[1] = src[2 + offset];
  122. bytes[2] = src[1 + offset];
  123. bytes[3] = src[0 + offset];
  124. /* Read first word. */
  125. bytes[4] = src[5 + offset];
  126. bytes[5] = src[4 + offset];
  127. /* Read second word. */
  128. bytes[6] = src[7 + offset];
  129. bytes[7] = src[6 + offset];
  130. /* Read 8 bytes. */
  131. System.arraycopy(src, 8 + offset, bytes, 8, 8);
  132. return bytes;
  133. }
  134. /**
  135. * Writes the class ID to a byte array in the little-endian format.
  136. *
  137. * @param dst The byte array to write to.
  138. *
  139. * @param offset The offset within the {@code dst} byte array.
  140. *
  141. * @throws ArrayStoreException if there is not enough room for the class
  142. * ID 16 bytes in the byte array after the {@code offset} position.
  143. */
  144. @SuppressWarnings("PointlessArithmeticExpression")
  145. public void write(final byte[] dst, final int offset)
  146. throws ArrayStoreException {
  147. /* Check array size: */
  148. if (dst.length < LENGTH) {
  149. throw new ArrayStoreException
  150. ("Destination byte[] must have room for at least 16 bytes, " +
  151. "but has a length of only " + dst.length + ".");
  152. }
  153. /* Write double word. */
  154. dst[0 + offset] = bytes[3];
  155. dst[1 + offset] = bytes[2];
  156. dst[2 + offset] = bytes[1];
  157. dst[3 + offset] = bytes[0];
  158. /* Write first word. */
  159. dst[4 + offset] = bytes[5];
  160. dst[5 + offset] = bytes[4];
  161. /* Write second word. */
  162. dst[6 + offset] = bytes[7];
  163. dst[7 + offset] = bytes[6];
  164. /* Write 8 bytes. */
  165. System.arraycopy(bytes, 8, dst, 8 + offset, 8);
  166. }
  167. /**
  168. * Write the class ID to a LittleEndianOutput (stream)
  169. *
  170. * @param leo the output
  171. */
  172. public void write(LittleEndianOutput leo) {
  173. byte[] buf = bytes.clone();
  174. write(buf, 0);
  175. leo.write(buf);
  176. }
  177. /**
  178. * Checks whether this {@code ClassID} is equal to another object.
  179. *
  180. * @param o the object to compare this {@code ClassID} with
  181. * @return {@code true} if the objects are equal, else {@code false}.
  182. */
  183. @Override
  184. public boolean equals(final Object o) {
  185. return (o instanceof ClassID) && Arrays.equals(bytes, ((ClassID)o).bytes);
  186. }
  187. /**
  188. * Checks whether this {@code ClassID} is equal to another ClassID with inverted endianess,
  189. * because there are apparently not only version 1 GUIDs (aka "network" with big-endian encoding),
  190. * but also version 2 GUIDs (aka "native" with little-endian encoding) out there.
  191. *
  192. * @param o the object to compare this {@code ClassID} with
  193. * @return {@code true} if the objects are equal, else {@code false}.
  194. */
  195. public boolean equalsInverted(ClassID o) {
  196. return
  197. o.bytes[0] == bytes[3] &&
  198. o.bytes[1] == bytes[2] &&
  199. o.bytes[2] == bytes[1] &&
  200. o.bytes[3] == bytes[0] &&
  201. o.bytes[4] == bytes[5] &&
  202. o.bytes[5] == bytes[4] &&
  203. o.bytes[6] == bytes[7] &&
  204. o.bytes[7] == bytes[6] &&
  205. o.bytes[8] == bytes[8] &&
  206. o.bytes[9] == bytes[9] &&
  207. o.bytes[10] == bytes[10] &&
  208. o.bytes[11] == bytes[11] &&
  209. o.bytes[12] == bytes[12] &&
  210. o.bytes[13] == bytes[13] &&
  211. o.bytes[14] == bytes[14] &&
  212. o.bytes[15] == bytes[15]
  213. ;
  214. }
  215. @Override
  216. public int hashCode() {
  217. return toString().hashCode();
  218. }
  219. /**
  220. * Returns a human-readable representation of the Class ID in standard
  221. * format {@code "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"}.
  222. *
  223. * @return String representation of the Class ID represented by this object.
  224. */
  225. @Override
  226. public String toString() {
  227. return "{" + toUUIDString() + "}";
  228. }
  229. /**
  230. * Returns a human-readable representation of the Class ID in UUID
  231. * format {@code "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}.
  232. *
  233. * @return UUID String representation of the Class ID represented by this object.
  234. */
  235. public String toUUIDString() {
  236. return toUUID().toString().toUpperCase(Locale.ROOT);
  237. }
  238. /**
  239. * Converts the ClassID to an UUID
  240. * @return the ClassID as UUID
  241. *
  242. * @since POI 5.0.0
  243. */
  244. public UUID toUUID() {
  245. final long mostSigBits = ByteBuffer.wrap(bytes, 0, 8).getLong();
  246. final long leastSigBits = ByteBuffer.wrap(bytes, 8, 8).getLong();
  247. return new UUID(mostSigBits, leastSigBits);
  248. }
  249. @Override
  250. public ClassID copy() {
  251. return new ClassID(this);
  252. }
  253. @Override
  254. public Map<String, Supplier<?>> getGenericProperties() {
  255. return GenericRecordUtil.getGenericProperties("uuid", this::toString);
  256. }
  257. }