From ef90a5f2c82653e58e2c011768120a8dbbad1e39 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sun, 8 Mar 2020 23:20:44 +0000 Subject: [PATCH] Get UUID from ClassID git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1874989 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hpsf/ClassID.java | 36 ++++-- .../apache/poi/hpsf/basic/TestClassID.java | 118 ++++++------------ 2 files changed, 65 insertions(+), 89 deletions(-) diff --git a/src/java/org/apache/poi/hpsf/ClassID.java b/src/java/org/apache/poi/hpsf/ClassID.java index a60b50e86c..7644f7fc29 100644 --- a/src/java/org/apache/poi/hpsf/ClassID.java +++ b/src/java/org/apache/poi/hpsf/ClassID.java @@ -17,9 +17,11 @@ package org.apache.poi.hpsf; +import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.Locale; +import java.util.UUID; -import org.apache.commons.codec.binary.Hex; import org.apache.poi.common.Duplicatable; import org.apache.poi.util.LittleEndianInput; import org.apache.poi.util.LittleEndianOutput; @@ -184,6 +186,7 @@ public class ClassID implements Duplicatable { * @param offset The offset within the {@code src} byte array * @return A byte array containing the class ID. */ + @SuppressWarnings("PointlessArithmeticExpression") public byte[] read(final byte[] src, final int offset) { /* Read double word. */ bytes[0] = src[3 + offset]; @@ -215,6 +218,7 @@ public class ClassID implements Duplicatable { * @exception ArrayStoreException if there is not enough room for the class * ID 16 bytes in the byte array after the {@code offset} position. */ + @SuppressWarnings("PointlessArithmeticExpression") public void write(final byte[] dst, final int offset) throws ArrayStoreException { /* Check array size: */ @@ -310,14 +314,32 @@ public class ClassID implements Duplicatable { */ @Override public String toString() { - String hex = Hex.encodeHexString(bytes, false); - return "{" + hex.substring(0,8) + - "-" + hex.substring(8,12) + - "-" + hex.substring(12,16) + - "-" + hex.substring(16,20) + - "-" + hex.substring(20) + "}"; + return "{" + toUUIDString() + "}"; } + /** + * Returns a human-readable representation of the Class ID in UUID + * format {@code "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}. + * + * @return UUID String representation of the Class ID represented by this object. + */ + public String toUUIDString() { + return toUUID().toString().toUpperCase(Locale.ROOT); + } + + /** + * Converts the ClassID to an UUID + * @return the ClassID as UUID + * + * @since POI 4.1.3 + */ + public UUID toUUID() { + final long mostSigBits = ByteBuffer.wrap(bytes, 0, 8).getLong(); + final long leastSigBits = ByteBuffer.wrap(bytes, 8, 8).getLong(); + return new UUID(mostSigBits, leastSigBits); + } + + @Override public ClassID copy() { return new ClassID(this); diff --git a/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java b/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java index 717f74fe6d..4b334f689b 100644 --- a/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java +++ b/src/testcases/org/apache/poi/hpsf/basic/TestClassID.java @@ -17,113 +17,67 @@ package org.apache.poi.hpsf.basic; -import java.util.Locale; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.apache.poi.hpsf.ClassID; -import org.apache.poi.hpsf.DocumentSummaryInformation; -import org.apache.poi.hpsf.PropertySet; -import org.apache.poi.hpsf.SummaryInformation; +import org.apache.poi.hpsf.ClassIDPredefined; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; - /** - *

Tests ClassID structure.

- * - * @author Michael Zalewski (zalewski@optonline.net) + * Tests ClassID structure. */ public final class TestClassID { + private static final byte[] BUF16 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; + /** * Various tests of overridden .equals() */ @Test public void testEquals() { - ClassID clsidTest1 = new ClassID( - new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} - , 0 - ); - ClassID clsidTest2 = new ClassID( - new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} - , 0 - ); - ClassID clsidTest3 = new ClassID( - new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11 } - , 0 - ); + ClassID clsidTest1 = new ClassID(BUF16, 0); + ClassID clsidTest2 = new ClassID(BUF16, 0); + byte[] buf2 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,17}; + ClassID clsidTest3 = new ClassID(buf2, 0); assertEquals(clsidTest1, clsidTest1); assertEquals(clsidTest1, clsidTest2); assertNotEquals(clsidTest1, clsidTest3); assertNotEquals(null, clsidTest1); } - + /** * Try to write to a buffer that is too small. This should * throw an Exception */ - @Test - public void testWriteArrayStoreException() { - ClassID clsidTest = new ClassID( - new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} - , 0 - ); - boolean bExceptionOccurred = false; - try - { - clsidTest.write(new byte[15], 0); - } - catch (Exception e) - { - bExceptionOccurred = true; - } - assertTrue(bExceptionOccurred); - - bExceptionOccurred = false; - try - { - clsidTest.write(new byte[16], 1); - } - catch (Exception e) - { - bExceptionOccurred = true; - } - assertTrue(bExceptionOccurred); - - // These should work without throwing an Exception - bExceptionOccurred = false; - try - { - clsidTest.write(new byte[16], 0); - clsidTest.write(new byte[17], 1); - } - catch (Exception e) - { - bExceptionOccurred = true; - } - assertFalse(bExceptionOccurred); + @Test(expected = ArrayStoreException.class) + public void testWriteArrayStoreException1() { + new ClassID(BUF16, 0).write(new byte[15], 0); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void testWriteArrayStoreException2() { + new ClassID(BUF16, 0).write(new byte[16], 1); + } + + @Test + public void testWriteArrayStoreException3() { + ClassID clsidTest = new ClassID(BUF16, 0); + clsidTest.write(new byte[16], 0); + clsidTest.write(new byte[17], 1); } - /** - *

Tests the {@link PropertySet} methods. The test file has two - * property set: the first one is a {@link SummaryInformation}, - * the second one is a {@link DocumentSummaryInformation}.

- */ @Test public void testClassID() { - ClassID clsidTest = new ClassID( - new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10} - , 0 - ); - assertEquals(clsidTest.toString().toUpperCase(Locale.ROOT), - "{04030201-0605-0807-090A-0B0C0D0E0F10}" - ); + ClassID clsidTest = new ClassID(BUF16, 0); + assertEquals("{04030201-0605-0807-090A-0B0C0D0E0F10}", clsidTest.toString()); + } + + @Test + public void checkUUIDConversion() { + String exp = "EABCECDB-CC1C-4A6F-B4E3-7F888A5ADFC8"; + ClassID clsId = ClassIDPredefined.EXCEL_V14_ODS.getClassID(); + assertEquals(exp, clsId.toUUIDString()); + assertEquals(exp, clsId.toUUID().toString().toUpperCase()); } }