4747import org .apache .tika .config .loader .TikaLoader ;
4848import org .apache .tika .exception .AccessPermissionException ;
4949import org .apache .tika .exception .EncryptedDocumentException ;
50- import org .apache .tika .exception .ZeroByteFileException ;
5150import org .apache .tika .extractor .DocumentSelector ;
5251import org .apache .tika .io .TikaInputStream ;
5352import org .apache .tika .metadata .Font ;
6261import org .apache .tika .metadata .XMPPDF ;
6362import org .apache .tika .mime .MediaType ;
6463import org .apache .tika .parser .AutoDetectParser ;
64+ import org .apache .tika .parser .AutoDetectParserConfig ;
6565import org .apache .tika .parser .CompositeParser ;
66+ import org .apache .tika .parser .MetadataOnlyParse ;
6667import org .apache .tika .parser .ParseContext ;
6768import org .apache .tika .parser .Parser ;
6869import org .apache .tika .parser .PasswordProvider ;
@@ -1358,7 +1359,7 @@ public void testExtractInlineImageMetadata() throws Exception {
13581359 config .setExtractInlineImageMetadataOnly (true );
13591360 context .set (PDFParserConfig .class , config );
13601361 List <Metadata > metadataList = getRecursiveMetadata ("testOCR.pdf" , context );
1361- assertNull (context .get (ZeroByteFileException . IgnoreZeroByteFileException .class ));
1362+ assertNull (context .get (MetadataOnlyParse .class ));
13621363 assertEquals (2 , metadataList .size ());
13631364 assertEquals ("image/png" , metadataList .get (1 ).get (Metadata .CONTENT_TYPE ));
13641365 assertEquals ("/image-0.png" ,
@@ -1368,6 +1369,37 @@ public void testExtractInlineImageMetadata() throws Exception {
13681369 assertEquals ("image-0.png" , metadataList .get (1 ).get (TikaCoreProperties .RESOURCE_NAME_KEY ));
13691370 }
13701371
1372+ @ Test
1373+ public void testExtractInlineImageMetadataThrowOnZeroBytesFalse () throws Exception {
1374+ //TIKA-4749: in metadata-only mode the inline image is registered via a
1375+ //placeholder pseudo-parse. With throwOnZeroBytes=false that placeholder used
1376+ //to be handed to a real parser (image/OCR), recording a spurious embedded
1377+ //exception. The MetadataOnlyParse marker must make it skip the parse instead.
1378+ ParseContext context = new ParseContext ();
1379+ PDFParserConfig config = new PDFParserConfig ();
1380+ config .setExtractInlineImageMetadataOnly (true );
1381+ context .set (PDFParserConfig .class , config );
1382+
1383+ AutoDetectParser p = new AutoDetectParser ();
1384+ AutoDetectParserConfig adpc = new AutoDetectParserConfig ();
1385+ adpc .setThrowOnZeroBytes (false );
1386+ p .setAutoDetectParserConfig (adpc );
1387+
1388+ List <Metadata > metadataList =
1389+ getRecursiveMetadata ("testOCR.pdf" , p , new Metadata (), context , false );
1390+ assertNull (context .get (MetadataOnlyParse .class ));
1391+ assertEquals (2 , metadataList .size ());
1392+ Metadata image = metadataList .get (1 );
1393+ assertEquals ("image/png" , image .get (Metadata .CONTENT_TYPE ));
1394+ assertEquals (261 , (int ) image .getInt (Metadata .IMAGE_LENGTH ));
1395+ assertEquals (934 , (int ) image .getInt (Metadata .IMAGE_WIDTH ));
1396+ //the placeholder must not be dispatched to any content parser. Without the
1397+ //fix it is (EmptyParser here; ImageParser+TesseractOCRParser when tesseract
1398+ //is installed, which is what records the spurious embedded exception).
1399+ assertEquals (0 , image .getValues (TikaCoreProperties .TIKA_PARSED_BY ).length );
1400+ assertNull (image .get (TikaCoreProperties .EMBEDDED_EXCEPTION ));
1401+ }
1402+
13711403 /**
13721404 * Simple class to count end of document events. If functionality is useful,
13731405 * move to org.apache.tika in src/test
0 commit comments