]> source.dussan.org Git - poi.git/commitdiff
Fixup printing record name in case of unknown sid
authorDominik Stadler <centic@apache.org>
Thu, 16 Jul 2015 03:49:44 +0000 (03:49 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 16 Jul 2015 03:49:44 +0000 (03:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691312 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/RecordInputStream.java
src/testcases/org/apache/poi/hssf/record/TestRecordInputStream.java

index 831a3a18781db3d370369328e93e1571e106acac..9de7d68895bf406ceccf74a1f459299f737e01c9 100644 (file)
@@ -52,9 +52,17 @@ public final class RecordInputStream implements LittleEndianInput {
        public static final class LeftoverDataException extends RuntimeException {
                public LeftoverDataException(int sid, int remainingByteCount) {
                        super("Initialisation of record 0x" + Integer.toHexString(sid).toUpperCase()
-                                       + "(" + RecordFactory.getRecordClass(sid).getSimpleName() + ") left " + remainingByteCount 
+                                       + "(" + getRecordName(sid) + ") left " + remainingByteCount 
                                        + " bytes remaining still to be read.");
                }
+
+        private static String getRecordName(int sid) {
+            Class<? extends Record> recordClass = RecordFactory.getRecordClass(sid);
+            if(recordClass == null) {
+                return null;
+            }
+            return recordClass.getSimpleName();
+        }
        }
 
        /** Header {@link LittleEndianInput} facet of the wrapped {@link InputStream} */
index 2b236e1ac7e40eb129366616f096b63796c712c4..518c5f047553795c2a26deaeb141db36d3bf8076 100644 (file)
@@ -94,4 +94,12 @@ public final class TestRecordInputStream extends TestCase {
                String actual = in.readString();
                assertEquals("Multilingual - \u591A\u8A00\u8A9E", actual);
        }
+
+       public void testLeftoverDataException() {
+           // just ensure that the exception is created correctly, even with unknown sids
+           new RecordInputStream.LeftoverDataException(1, 200);
+           new RecordInputStream.LeftoverDataException(0, 200);
+        new RecordInputStream.LeftoverDataException(999999999, 200);
+           new RecordInputStream.LeftoverDataException(HeaderRecord.sid, 200);
+       }
 }