diff options
author | Marius Volkhart <mariusvolkhart@apache.org> | 2021-10-01 12:07:10 +0000 |
---|---|---|
committer | Marius Volkhart <mariusvolkhart@apache.org> | 2021-10-01 12:07:10 +0000 |
commit | 1273415df302990dc1ef8fb457313e40168d1e0c (patch) | |
tree | 66a46fc47822c1551efc21e35c110184590ba93b | |
parent | 9994a4286df24b9a82254cf580eb8c352cecf9b6 (diff) | |
download | poi-1273415df302990dc1ef8fb457313e40168d1e0c.tar.gz poi-1273415df302990dc1ef8fb457313e40168d1e0c.zip |
Reduce allocations and copying when extracting PNGs created by Mac
This logic is covered by the TestHSSFPictureData#testMacPicture() test.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1893779 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi/src/main/java/org/apache/poi/sl/image/ImageHeaderPNG.java | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/poi/src/main/java/org/apache/poi/sl/image/ImageHeaderPNG.java b/poi/src/main/java/org/apache/poi/sl/image/ImageHeaderPNG.java index 4a6b728fd3..d3ed5a0f0e 100644 --- a/poi/src/main/java/org/apache/poi/sl/image/ImageHeaderPNG.java +++ b/poi/src/main/java/org/apache/poi/sl/image/ImageHeaderPNG.java @@ -18,19 +18,15 @@ package org.apache.poi.sl.image; -import java.io.IOException; -import java.io.InputStream; - -import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream; import org.apache.poi.poifs.filesystem.FileMagic; -import org.apache.poi.util.IOUtils; -import org.apache.poi.util.RecordFormatException; + +import java.util.Arrays; public final class ImageHeaderPNG { private static final int MAGIC_OFFSET = 16; - private byte[] data; + private final byte[] data; /** * @param data The raw image data @@ -46,12 +42,11 @@ public final class ImageHeaderPNG { public byte[] extractPNG() { // //Just cut it off!. - try (InputStream is = new UnsynchronizedByteArrayInputStream(data)) { - if (is.skip(MAGIC_OFFSET) == MAGIC_OFFSET && FileMagic.valueOf(is) == FileMagic.PNG) { - return IOUtils.toByteArray(is); + if (data.length >= MAGIC_OFFSET) { + byte[] newData = Arrays.copyOfRange(data, MAGIC_OFFSET, data.length); + if (FileMagic.valueOf(newData) == FileMagic.PNG) { + return newData; } - } catch (IOException e) { - throw new RecordFormatException("Unable to parse PNG header", e); } return data; |