ICNSParser extends AbstractParser { public void parse( InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { byte[] header = new byte[4]; IOUtils.read(stream, header, 0, 4); if (header[0] == (byte) 'i' && header[1] == (byte) 'c' && header[2] == (byte) 'n' && header[3] == (byte) 's') { } else { throw new TikaException("ICNS magic signature invalid"); } IOUtils.read(stream, header, 0, 4); int image_length = java.nio.ByteBuffer.wrap(header).getInt(); image_length -= 8; if (image_length > MAX_IMAGE_LENGTH_BYTES) { throw new TikaMemoryLimitException(image_length, MAX_IMAGE_LENGTH_BYTES); } byte[] full_file = new byte[image_length]; IOUtils.readFully(stream, full_file); ArrayList<ICNSType> icons = new ArrayList<>(); ArrayList<ICNSType> icon_masks = new ArrayList<>(); byte[] tempByteArray = new byte[4]; for (int offset = 0; offset < image_length - 8;) { tempByteArray[0] = full_file[offset]; tempByteArray[1] = full_file[offset + 1]; tempByteArray[2] = full_file[offset + 2]; tempByteArray[3] = full_file[offset + 3]; ICNSType icnstype = findIconType(tempByteArray); if (icnstype == null) { break; } else if (icnstype.hasMask() == true) { icon_masks.add(findIconType(tempByteArray)); } else { icons.add(findIconType(tempByteArray)); } tempByteArray[0] = full_file[offset + 4]; tempByteArray[1] = full_file[offset + 5]; tempByteArray[2] = full_file[offset + 6]; tempByteArray[3] = full_file[offset + 7]; int icon_length = java.nio.ByteBuffer.wrap(tempByteArray).getInt(); if (icon_length <= 0) { break; } offset = offset + icon_length; } String icon_details = "", iconmask_details = "", bitsPerPixel,dimensions; for (ICNSType icon : icons) { bitsPerPixel = (icon.getBitsPerPixel()!=0)?icon.getBitsPerPixel() + " bpp":"JPEG 2000 or PNG format"; dimensions = (!icon.hasRetinaDisplay())?(icon.getHeight() + "x" + icon.getWidth()):(icon.getHeight() + "x" + icon.getWidth() + "@2X"); icon_details = icon_details + ", " + dimensions + " (" + bitsPerPixel + ")"; } for (ICNSType icon : icon_masks) { iconmask_details = iconmask_details + ", " + icon.getHeight() + "x" + icon.getWidth() + " (" + icon.getBitsPerPixel() + " bpp" + ")"; } metadata.set(Metadata.CONTENT_TYPE, ICNS_MIME_TYPE); if (!icon_details.equals("")) { metadata.set("Icon count", String.valueOf(icons.size())); icon_details = icon_details.substring(2); metadata.set("Icon details", icon_details); } if (!iconmask_details.equals("")) { metadata.set("Masked icon count", String.valueOf(icon_masks.size())); iconmask_details = iconmask_details.substring(2); metadata.set("Masked icon details", iconmask_details); } XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); xhtml.endDocument(); }  Set<MediaType> getSupportedTypes(ParseContext context); void parse(
            InputStream stream, ContentHandler handler,
            Metadata metadata, ParseContext context); static final String ICNS_MIME_TYPE; }
@Test public void testICNS_basic() throws Exception { Metadata metadata = new Metadata(); metadata.set(Metadata.CONTENT_TYPE, "image/icns"); metadata.set("Icons count", "1"); metadata.set("Icons details", "512x512 (JPEG 2000 or PNG format)"); try (InputStream stream = getClass().getResourceAsStream("/test-documents/testICNS_basic.icns")) { parser.parse(stream, new DefaultHandler(), metadata, new ParseContext()); } }
@Test public void testICNS() throws Exception { Metadata metadata = new Metadata(); metadata.set(Metadata.CONTENT_TYPE, "image/icns"); metadata.set("Icons count", "2"); metadata.set("Icons details", "16x16 (24 bpp), 32x32 (24 bpp)"); metadata.set("Masked icon count", "2"); metadata.set("Masked icon details", "16x16 (8 bpp), 32x32 (8 bpp)"); try (InputStream stream = getClass().getResourceAsStream("/test-documents/testICNS.icns")) { parser.parse(stream, new DefaultHandler(), metadata, new ParseContext()); } }