public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
                      ParseContext context) throws IOException, SAXException, TikaException {

        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
        Last5 l5 = new Last5();
        int read;

        // Try to get the creation date, which is YYYYMMDDhhmm
        byte[] header = new byte[30];
        IOUtils.readFully(stream, header);
        byte[] date = new byte[12];
        IOUtils.readFully(stream, date);

        String dateStr = new String(date, US_ASCII);
        if (dateStr.startsWith("19") || dateStr.startsWith("20")) {
            String formattedDate = dateStr.substring(0, 4) + "-" + dateStr.substring(4, 6) + "-" +
                    dateStr.substring(6, 8) + "T" + dateStr.substring(8, 10) + ":" +
                    dateStr.substring(10, 12) + ":00";
            metadata.set(TikaCoreProperties.CREATED, formattedDate);
            // TODO Metadata.DATE is used as modified, should it be here?
            metadata.set(TikaCoreProperties.CREATED, formattedDate);
        }
        metadata.set(Metadata.CONTENT_TYPE, PRT_MIME_TYPE);

        // The description, if set, is the next up-to-500 bytes
        byte[] desc = new byte[500];
        IOUtils.readFully(stream, desc);
        String description = extractText(desc, true);
        if (description.length() > 0) {
            metadata.set(TikaCoreProperties.DESCRIPTION, description);
        }

        // Now look for text
        while ((read = stream.read()) > -1) {
            if (read == 0xe0 || read == 0xe3 || read == 0xf0) {
                int nread = stream.read();
                if (nread == 0x3f || nread == 0xbf) {
                    // Looks promising, check back for a suitable value
                    if (read == 0xe3 && nread == 0x3f) {
                        if (l5.is33()) {
                            // Bingo, note text
                            handleNoteText(stream, xhtml);
                        }
                    } else if (l5.is00()) {
                        // Likely view name
                        handleViewName(read, nread, stream, xhtml, l5);
                    }
                }
            } else {
                l5.record(read);
            }
        }
    }
@Test
    public void testPRTParserComplex() throws Exception {
        try (InputStream input = getResourceAsStream("/test-documents/testCADKEY2.prt")) {
            Metadata metadata = new Metadata();
            ContentHandler handler = new BodyContentHandler();
            new PRTParser().parse(input, handler, metadata, new ParseContext());

            assertEquals("application/x-prt", metadata.get(Metadata.CONTENT_TYPE));

            // File has both a date and a description
            assertEquals("1997-04-01T08:59:00", metadata.get(TikaCoreProperties.CREATED));
            assertEquals("TIKA TEST PART DESCRIPTION INFORMATION\r\n",
                    metadata.get(TikaCoreProperties.DESCRIPTION));

            String contents = handler.toString();

            assertContains("ITEM", contents);
            assertContains("REQ.", contents);
            assertContains("DESCRIPTION", contents);
            assertContains("MAT'L", contents);
            assertContains("TOLERANCES UNLESS", contents);
            assertContains("FRACTIONS", contents);
            assertContains("ANGLES", contents);
            assertContains("Acme Corporation", contents);

            assertContains("DATE", contents);
            assertContains("CHANGE", contents);
            assertContains("DRAWN BY", contents);
            assertContains("SCALE", contents);
            assertContains("TIKA TEST DRAWING", contents);
            assertContains("TIKA LETTERS", contents);
            assertContains("5.82", contents);
            assertContains("112" + '\u00b0', contents); // Degrees
            assertContains("TIKA TEST LETTER", contents);
            assertContains("17.11", contents);
            assertContains('\u00d8' + "\ufffd2.000", contents); // Diameter
            assertContains("Diameter", contents);
            assertContains("The Apache Tika toolkit", contents);
        }
    }